Skip to content

Call

Calls a contract function on Starknet without creating a transaction.

Method Signature

func (provider *Provider) Call(
	ctx context.Context,
	request FunctionCall,
	blockID BlockID,
) ([]*felt.Felt, error)

Parameters

  • ctx - Context for request cancellation and timeout
  • request - The function call request containing:
    • ContractAddress - The address of the contract
    • EntryPointSelector - The selector of the function to call
    • Calldata - The input data for the function call
  • blockID - The block identifier (see BlockID helper functions)

Returns

  • []*felt.Felt - Array of felt values returned by the function
  • error - Error if the request fails

BlockID Parameter

The blockID parameter specifies which block state to use for the call. 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"
	"encoding/json"
	"fmt"
	"log"
	"os"
 
	"github.com/NethermindEth/juno/core/felt"
	"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 symbol selector (symbol())
	symbolSelector := utils.GetSelectorFromNameFelt("symbol")
 
	// Create function call request
	request := rpc.FunctionCall{
		ContractAddress:    contractAddress,
		EntryPointSelector: symbolSelector,
		Calldata:           []*felt.Felt{},
	}
 
	// Call the function
	result, err := client.Call(ctx, request, rpc.WithBlockTag("latest"))
	if err != nil {
		log.Fatal("Call failed:", err)
	}
 
	// Print result
	resultJSON, _ := json.MarshalIndent(result, "", "  ")
	fmt.Printf("Symbol result:\n%s\n", resultJSON)
}

Expected Output

Symbol result:
[
  "0x455448"
]

Error Handling

result, err := client.Call(ctx, request, rpc.WithBlockTag("latest"))
if err != nil {
	// Handle errors like contract not found, invalid selector, etc.
	return err
}

Related Methods