Skip to content

Internal Transactions

This guide demonstrates how to work with internal transactions in Starknet using Starknet.go.

Prerequisites

  • Go 1.18 or higher
  • Starknet.go installed
  • A Starknet node URL
  • Deployed contracts
  • Account with sufficient funds

Code Example

main.go
// Internal package to set up basic configurations for the examples contained in the 'examples' folder.
package setup
 
import (
	"errors"
	"fmt"
	"os"
	"strconv"
 
	"github.com/NethermindEth/juno/core/felt"
	"github.com/joho/godotenv"
)
 
// Loads environment variables contained in the ".env" file in the root of "examples" folder.
func init() {
	err := godotenv.Load("../.env")
	if err != nil {
		panic(errors.Join(errors.New("error loading '.env' file"), err))
	}
}
 
// Validates whether the RPC_PROVIDER_URL variable has been set in the '.env' file and returns it; panics otherwise.
func GetRpcProviderUrl() string {
	return getEnv("RPC_PROVIDER_URL")
}
 
// Validates whether the WS_PROVIDER_URL variable has been set in the '.env' file and returns it; panics otherwise.
func GetWsProviderUrl() string {
	return getEnv("WS_PROVIDER_URL")
}
 
// Validates whether the PRIVATE_KEY variable has been set in the '.env' file and returns it; panics otherwise.
func GetPrivateKey() string {
	return getEnv("PRIVATE_KEY")
}
 
// Validates whether the PUBLIC_KEY variable has been set in the '.env' file and returns it; panics otherwise.
func GetPublicKey() string {
	return getEnv("PUBLIC_KEY")
}
 
// Validates whether the ACCOUNT_ADDRESS variable has been set in the '.env' file and returns it; panics otherwise.
func GetAccountAddress() string {
	return getEnv("ACCOUNT_ADDRESS")
}
 
// Validates whether the ACCOUNT_CAIRO_VERSION variable has been set in the '.env' file and returns it; panics otherwise.
func GetAccountCairoVersion() int {
	num, err := strconv.Atoi(getEnv("ACCOUNT_CAIRO_VERSION"))
	if err != nil {
		panic("Invalid ACCOUNT_CAIRO_VERSION number set in the '.env' file")
	}
 
	return num
}
 
// Loads an env variable by name and returns it; panics otherwise.
func getEnv(envName string) string {
	env := os.Getenv(envName)
	if env == "" {
		panic(fmt.Sprintf("%s variable not set in the '.env' file", envName))
	}
	return env
}
 
// PadZerosInFelt it's a helper function that pads zeros to the left of a hex felt value to make sure it is 64 characters long.
func PadZerosInFelt(hexFelt *felt.Felt) string {
	length := 66
	hexStr := hexFelt.String()
 
	// Check if the hex value is already of the desired length
	if len(hexStr) >= length {
		return hexStr
	}
 
	// Extract the hex value without the "0x" prefix
	hexValue := hexStr[2:]
	// Pad zeros after the "0x" prefix
	paddedHexValue := fmt.Sprintf("%0*s", length-2, hexValue)
	// Add back the "0x" prefix to the padded hex value
	paddedHexStr := "0x" + paddedHexValue
 
	return paddedHexStr
}

Explanation

  1. Initialize the Starknet client
  2. Create an account instance
  3. Define the internal transaction parameters
  4. Execute the internal transaction
  5. The transaction hash is returned upon successful execution

Best Practices

  • Verify contract addresses before execution
  • Use proper error handling
  • Consider gas estimation
  • Test on testnet first
  • Keep track of transaction hashes

Common Issues

  • Invalid contract address
  • Insufficient funds
  • Invalid entry point
  • Invalid calldata format
  • Network congestion