Skip to content

StorageAt

Retrieves the storage value of a contract at a specific key and block state.

Method Signature

func (provider *Provider) StorageAt(
	ctx context.Context,
	contractAddress *felt.Felt,
	key string,
	blockID BlockID,
) (string, error)

Parameters

  • ctx - Context for request cancellation and timeout
  • contractAddress - The address of the contract
  • key - The storage key to retrieve
  • blockID - The block identifier (see BlockID helper functions)

Returns

  • string - The storage value at the given key
  • error - 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 block
  • WithBlockTag("pending") - Pending block
  • WithBlockNumber(n) - Specific block number
  • WithBlockHash(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)
	}
 
	// Storage key
	storageKey := "0x0"
 
	// Get storage at key
	storageValue, err := client.StorageAt(ctx, contractAddress, storageKey, rpc.WithBlockTag("latest"))
	if err != nil {
		log.Fatal("Failed to get storage:", err)
	}
 
	fmt.Printf("Storage value at key %s: %s\n", storageKey, storageValue)
}

Expected Output

Storage value at key 0x0: 0x0

Error Handling

storageValue, err := client.StorageAt(ctx, contractAddress, storageKey, rpc.WithBlockTag("latest"))
if err != nil {
	// Handle errors like contract not found, invalid key, etc.
	return err
}

Related Methods

  • StorageProof - Get storage proof for verification
  • ClassAt - Get class definition at a contract address