Nonce
Retrieves the nonce for a contract at a specific block state.
Method Signature
func (provider *Provider) Nonce(
ctx context.Context,
blockID BlockID,
contractAddress *felt.Felt,
) (*felt.Felt, error)Parameters
ctx- Context for request cancellation and timeoutblockID- The block identifier (see BlockID helper functions)contractAddress- The address of the contract
Returns
*felt.Felt- The contract's nonce at the requested stateerror- Error if the request fails
BlockID Parameter
The blockID parameter specifies which block state to query. See BlockID helper functions for available options:
WithBlockTag("latest")- Latest blockWithBlockTag("pending")- Pending blockWithBlockNumber(n)- Specific block numberWithBlockHash(hash)- Specific block hash
Usage Example
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
)
func main() {
// Create RPC client
// Get RPC URL from environment variable
rpcURL := os.Getenv("STARKNET_RPC_URL")
if rpcURL == "" {
log.Fatal("STARKNET_RPC_URL not set in environment")
}
ctx := context.Background()
client, err := rpc.NewProvider(ctx, rpcURL)
if err != nil {
log.Fatal("Failed to create client:", err)
}
// ETH contract address on Sepolia
contractAddress, err := utils.HexToFelt("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7")
if err != nil {
log.Fatal("Failed to parse contract address:", err)
}
// Get nonce
nonce, err := client.Nonce(ctx, rpc.WithBlockTag("latest"), contractAddress)
if err != nil {
log.Fatal("Failed to get nonce:", err)
}
fmt.Printf("Nonce: %s\n", nonce)
}Expected Output
Nonce: 0x0Error Handling
nonce, err := client.Nonce(ctx, rpc.WithBlockTag("latest"), contractAddress)
if err != nil {
// Handle errors like contract not found, invalid block ID, etc.
return err
}Related Methods
- ClassHashAt - Get class hash at a specific contract address
- EstimateFee - Estimate transaction fees

