Skip to content

DevNet Methods

Methods available for interacting with Starknet DevNet instances.

NewDevNet

Creates a new DevNet instance connected to a local Starknet development network.

Function Signature

func NewDevNet(baseURL ...string) *DevNet

Parameters

  • baseURL - Optional base URL of the DevNet instance. If not provided, defaults to http://localhost:5050

Returns

  • *DevNet - DevNet instance for interacting with the network

Usage Example

package main
 
import (
	"fmt"
	"log"
 
	"github.com/NethermindEth/starknet.go/devnet"
)
 
func main() {
	// Connect to default DevNet (http://localhost:5050)
	devNet := devnet.NewDevNet()
 
	// Verify connection
	if devNet.IsAlive() {
		fmt.Println("Connected to DevNet on default port")
	}
}

Usage Example with Custom URL

package main
 
import (
	"fmt"
	"log"
 
	"github.com/NethermindEth/starknet.go/devnet"
)
 
func main() {
	// Connect to DevNet on custom port
	devNet := devnet.NewDevNet("http://localhost:5051")
 
	// Verify connection
	if devNet.IsAlive() {
		fmt.Println("Connected to DevNet on port 5051")
	}
 
	// Connect to remote DevNet
	remoteDevNet := devnet.NewDevNet("http://192.168.1.100:5050")
 
	if remoteDevNet.IsAlive() {
		fmt.Println("Connected to remote DevNet")
	}
}

Description

NewDevNet creates a DevNet client instance that communicates with a running starknet-devnet instance. The DevNet instance provides methods to retrieve test accounts, mint tokens, and check network health.

Note that this function only creates the client instance - it does not start a DevNet server. You must have a running DevNet instance before using this client.

Related Methods

  • IsAlive - Check if DevNet is running
  • Accounts - Get test accounts from DevNet

Accounts

Retrieves the list of pre-funded test accounts from DevNet.

Method Signature

func (d *DevNet) Accounts() ([]TestAccount, error)

Parameters

None

Returns

  • []TestAccount - Slice of test accounts with private keys, public keys, and addresses
  • error - Error if request fails or DevNet is not accessible

Usage Example

package main
 
import (
	"fmt"
	"log"
 
	"github.com/NethermindEth/starknet.go/devnet"
)
 
func main() {
	// Connect to DevNet
	devNet := devnet.NewDevNet()
 
	// Get pre-funded accounts
	accounts, err := devNet.Accounts()
	if err != nil {
		log.Fatal("Failed to get accounts:", err)
	}
 
	fmt.Printf("Found %d pre-funded accounts\n", len(accounts))
 
	// Display first account details
	if len(accounts) > 0 {
		fmt.Printf("\nFirst Account:\n")
		fmt.Printf("  Address: %s\n", accounts[0].Address)
		fmt.Printf("  Public Key: %s\n", accounts[0].PublicKey)
		fmt.Printf("  Private Key: %s\n", accounts[0].PrivateKey)
	}
 
	// List all account addresses
	fmt.Println("\nAll Account Addresses:")
	for i, acc := range accounts {
		fmt.Printf("  [%d] %s\n", i, acc.Address)
	}
}

Usage Example with Account Package

package main
 
import (
	"context"
	"fmt"
	"log"
 
	"github.com/NethermindEth/starknet.go/account"
	"github.com/NethermindEth/starknet.go/devnet"
	"github.com/NethermindEth/starknet.go/rpc"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	// Get DevNet accounts
	devNet := devnet.NewDevNet()
	accounts, err := devNet.Accounts()
	if err != nil {
		log.Fatal(err)
	}
 
	// Use first account for testing
	testAccount := accounts[0]
 
	// Create RPC provider
	provider, err := rpc.NewProvider("http://localhost:5050/rpc")
	if err != nil {
		log.Fatal(err)
	}
 
	// Parse account credentials
	privateKey, _ := utils.HexToFelt(testAccount.PrivateKey)
	publicKey, _ := utils.HexToFelt(testAccount.PublicKey)
	address, _ := utils.HexToFelt(testAccount.Address)
 
	// Create keystore
	ks := account.NewMemKeystore()
	ks.Put(publicKey.String(), privateKey)
 
	// Create account instance
	acc, err := account.NewAccount(
		provider,
		address,
		publicKey.String(),
		ks,
		account.CairoV2,
	)
	if err != nil {
		log.Fatal(err)
	}
 
	// Get account nonce
	nonce, err := acc.Nonce(context.Background())
	if err != nil {
		log.Fatal(err)
	}
 
	fmt.Printf("Account %s has nonce: %s\n", acc.Address.String(), nonce.String())
}

Description

Accounts retrieves pre-funded test accounts from the DevNet instance. These accounts are automatically created when DevNet starts and are ready to use for testing without additional funding.

Each account includes:

  • Private key for signing transactions
  • Public key derived from the private key
  • Deployed account contract address

DevNet typically provides 10 pre-funded accounts by default, though this may vary based on DevNet configuration.

Error Handling

accounts, err := devNet.Accounts()
if err != nil {
	// Handle errors like:
	// - DevNet not running
	// - Network connectivity issues
	// - Invalid response format
	return err
}

Related Types

Related Methods

  • NewDevNet - Create DevNet instance
  • Mint - Mint tokens to accounts

FeeToken

Retrieves information about the fee token used by the DevNet network.

Method Signature

func (d *DevNet) FeeToken() (*FeeToken, error)

Parameters

None

Returns

  • *FeeToken - Fee token information including symbol and address
  • error - Error if request fails

Usage Example

package main
 
import (
	"fmt"
	"log"
 
	"github.com/NethermindEth/starknet.go/devnet"
)
 
func main() {
	// Connect to DevNet
	devNet := devnet.NewDevNet()
 
	// Get fee token information
	feeToken, err := devNet.FeeToken()
	if err != nil {
		log.Fatal("Failed to get fee token:", err)
	}
 
	fmt.Printf("Fee Token Symbol: %s\n", feeToken.Symbol)
	fmt.Printf("Fee Token Address: %s\n", feeToken.Address.String())
}

Usage Example with Minting

package main
 
import (
	"fmt"
	"log"
	"math/big"
 
	"github.com/NethermindEth/starknet.go/devnet"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	devNet := devnet.NewDevNet()
 
	// Get fee token details
	feeToken, err := devNet.FeeToken()
	if err != nil {
		log.Fatal(err)
	}
 
	fmt.Printf("DevNet uses %s token at %s\n",
		feeToken.Symbol,
		feeToken.Address.String())
 
	// Mint fee tokens to an address
	recipientAddress, _ := utils.HexToFelt("0x1234...")
	amount := big.NewInt(1000000000000000000) // 1 token
 
	mintResp, err := devNet.Mint(recipientAddress, amount)
	if err != nil {
		log.Fatal(err)
	}
 
	fmt.Printf("Minted %s %s to %s\n",
		mintResp.NewBalance,
		mintResp.Unit,
		recipientAddress.String())
}

Description

FeeToken retrieves metadata about the token used for transaction fees on the DevNet network. This is typically ETH or STRK, depending on the DevNet configuration.

The returned information includes:

  • Symbol: Token symbol (e.g., "ETH", "STRK")
  • Address: Contract address of the token

This method is useful when you need to interact with the fee token contract directly, such as for checking balances or approving transfers.

Error Handling

feeToken, err := devNet.FeeToken()
if err != nil {
	// Handle errors like:
	// - DevNet not running
	// - Network connectivity issues
	// - API endpoint not available
	return err
}

Related Types

Related Methods

  • Mint - Mint fee tokens to addresses
  • IsAlive - Check DevNet availability

IsAlive

Checks if the DevNet instance is running and accessible.

Method Signature

func (d *DevNet) IsAlive() bool

Parameters

None

Returns

  • bool - true if DevNet is running and responding, false otherwise

Usage Example

package main
 
import (
	"fmt"
	"log"
	"time"
 
	"github.com/NethermindEth/starknet.go/devnet"
)
 
func main() {
	devNet := devnet.NewDevNet()
 
	// Check if DevNet is running
	if devNet.IsAlive() {
		fmt.Println("DevNet is running")
	} else {
		log.Fatal("DevNet is not running. Start it with: starknet-devnet")
	}
}

Usage Example with Connection Retry

package main
 
import (
	"fmt"
	"log"
	"time"
 
	"github.com/NethermindEth/starknet.go/devnet"
)
 
func main() {
	devNet := devnet.NewDevNet()
 
	// Wait for DevNet to be ready
	fmt.Println("Waiting for DevNet to be ready...")
	maxAttempts := 10
	for i := 0; i < maxAttempts; i++ {
		if devNet.IsAlive() {
			fmt.Println("DevNet is ready!")
			break
		}
 
		if i == maxAttempts-1 {
			log.Fatal("DevNet did not start in time")
		}
 
		fmt.Printf("Attempt %d/%d failed, retrying...\n", i+1, maxAttempts)
		time.Sleep(2 * time.Second)
	}
 
	// Proceed with testing
	accounts, err := devNet.Accounts()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Found %d accounts\n", len(accounts))
}

Usage Example with Health Check

package main
 
import (
	"fmt"
	"time"
 
	"github.com/NethermindEth/starknet.go/devnet"
)
 
func healthCheck(devNet *devnet.DevNet) {
	ticker := time.NewTicker(10 * time.Second)
	defer ticker.Stop()
 
	for range ticker.C {
		if devNet.IsAlive() {
			fmt.Println("DevNet health check: OK")
		} else {
			fmt.Println("DevNet health check: FAILED")
		}
	}
}
 
func main() {
	devNet := devnet.NewDevNet()
 
	// Initial check
	if !devNet.IsAlive() {
		fmt.Println("Warning: DevNet is not running")
		return
	}
 
	// Start health monitoring in background
	go healthCheck(devNet)
 
	// Continue with testing
	// ...
}

Description

IsAlive performs a simple health check to verify that the DevNet instance is running and accessible. It sends a request to the DevNet's health endpoint and returns true if the server responds successfully.

This method is useful for:

  • Verifying DevNet is running before starting tests
  • Implementing retry logic for flaky connections
  • Health monitoring in test infrastructure
  • Graceful degradation in development tools

Unlike other methods, IsAlive does not return an error - it simply returns false if the check fails for any reason.

Use Cases

// 1. Pre-test validation
if !devNet.IsAlive() {
	t.Skip("DevNet is not running, skipping integration tests")
}
 
// 2. Setup verification
devNet := devnet.NewDevNet()
if !devNet.IsAlive() {
	return fmt.Errorf("DevNet is not available at %s", baseURL)
}
 
// 3. Connection testing
func TestDevNetConnection(t *testing.T) {
	devNet := devnet.NewDevNet()
	if !devNet.IsAlive() {
		t.Fatal("Cannot connect to DevNet")
	}
}

Related Methods


Mint

Mints tokens to a specified address on the DevNet network.

Method Signature

func (d *DevNet) Mint(address *felt.Felt, amount *big.Int) (*MintResponse, error)

Parameters

  • address - Target address to receive tokens
  • amount - Amount of tokens to mint in wei (smallest unit)

Returns

  • *MintResponse - Mint transaction details including new balance, unit, and transaction hash
  • error - Error if minting fails

Usage Example

package main
 
import (
	"fmt"
	"log"
	"math/big"
 
	"github.com/NethermindEth/starknet.go/devnet"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	devNet := devnet.NewDevNet()
 
	// Address to receive tokens
	recipientAddress, err := utils.HexToFelt("0x1234...")
	if err != nil {
		log.Fatal(err)
	}
 
	// Mint 1 token (1 * 10^18 wei)
	amount := big.NewInt(1000000000000000000)
 
	// Execute mint
	mintResp, err := devNet.Mint(recipientAddress, amount)
	if err != nil {
		log.Fatal("Failed to mint tokens:", err)
	}
 
	fmt.Printf("Successfully minted tokens!\n")
	fmt.Printf("New Balance: %s %s\n", mintResp.NewBalance, mintResp.Unit)
	fmt.Printf("Transaction Hash: %s\n", mintResp.TransactionHash)
}

Usage Example with Multiple Addresses

package main
 
import (
	"fmt"
	"log"
	"math/big"
 
	"github.com/NethermindEth/juno/core/felt"
	"github.com/NethermindEth/starknet.go/devnet"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	devNet := devnet.NewDevNet()
 
	// Multiple addresses to fund
	addresses := []string{
		"0x1234...",
		"0x5678...",
		"0xabcd...",
	}
 
	// Mint 10 tokens to each address
	amount := new(big.Int).Mul(
		big.NewInt(10),
		big.NewInt(1000000000000000000), // 10 * 10^18
	)
 
	for i, addrStr := range addresses {
		address, err := utils.HexToFelt(addrStr)
		if err != nil {
			log.Printf("Invalid address %s: %v", addrStr, err)
			continue
		}
 
		mintResp, err := devNet.Mint(address, amount)
		if err != nil {
			log.Printf("Failed to mint to %s: %v", addrStr, err)
			continue
		}
 
		fmt.Printf("[%d] Minted to %s\n", i+1, addrStr)
		fmt.Printf("    Balance: %s %s\n", mintResp.NewBalance, mintResp.Unit)
		fmt.Printf("    TX: %s\n", mintResp.TransactionHash)
	}
}

Usage Example with Custom Amounts

package main
 
import (
	"fmt"
	"log"
	"math/big"
 
	"github.com/NethermindEth/starknet.go/devnet"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	devNet := devnet.NewDevNet()
 
	address, _ := utils.HexToFelt("0x1234...")
 
	// Different ways to specify amounts
 
	// 1. Exact wei amount
	oneWei := big.NewInt(1)
	devNet.Mint(address, oneWei)
 
	// 2. Using token units (1 token = 10^18 wei)
	oneToken := new(big.Int).Exp(
		big.NewInt(10),
		big.NewInt(18),
		nil,
	)
	devNet.Mint(address, oneToken)
 
	// 3. Multiple tokens
	tenTokens := new(big.Int).Mul(
		big.NewInt(10),
		oneToken,
	)
	devNet.Mint(address, tenTokens)
 
	// 4. String parsing
	amountStr := "1000000000000000000" // 1 token
	amount := new(big.Int)
	amount.SetString(amountStr, 10)
	mintResp, err := devNet.Mint(address, amount)
	if err != nil {
		log.Fatal(err)
	}
 
	fmt.Printf("Final balance: %s %s\n",
		mintResp.NewBalance,
		mintResp.Unit)
}

Usage Example for Testing

package main
 
import (
	"context"
	"math/big"
	"testing"
 
	"github.com/NethermindEth/starknet.go/account"
	"github.com/NethermindEth/starknet.go/devnet"
	"github.com/NethermindEth/starknet.go/rpc"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func TestContractDeployment(t *testing.T) {
	// Setup DevNet
	devNet := devnet.NewDevNet()
	if !devNet.IsAlive() {
		t.Skip("DevNet not running")
	}
 
	// Get test account
	accounts, err := devNet.Accounts()
	if err != nil {
		t.Fatal(err)
	}
	testAccount := accounts[0]
 
	// Mint additional tokens for testing
	address, _ := utils.HexToFelt(testAccount.Address)
	amount := big.NewInt(1000000000000000000) // 1 token
 
	mintResp, err := devNet.Mint(address, amount)
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("Minted %s %s to test account", mintResp.NewBalance, mintResp.Unit)
 
	// Continue with contract deployment test
	// ...
}

Description

Mint creates new tokens and sends them to the specified address on the DevNet network. This is only available on DevNet and is essential for:

  • Funding test accounts for contract deployments
  • Providing gas for transaction testing
  • Setting up specific balance scenarios
  • Resetting account balances during test runs

The amount parameter should be specified in wei (the smallest unit). For example:

  • 1 wei = 1
  • 1 token = 1,000,000,000,000,000,000 wei (10^18)

The minting operation creates a transaction on DevNet, which is included in the response.

Error Handling

mintResp, err := devNet.Mint(address, amount)
if err != nil {
	// Handle errors like:
	// - DevNet not running
	// - Invalid address format
	// - Network connectivity issues
	// - Minting disabled on DevNet
	return err
}

Related Types

Related Methods


Summary

The DevNet package provides essential methods for local Starknet development:

  • NewDevNet: Initialize connection to DevNet instance
  • Accounts: Retrieve pre-funded test accounts
  • FeeToken: Get fee token metadata
  • IsAlive: Health check for DevNet availability
  • Mint: Create tokens for testing

These methods form the foundation for local testing workflows, enabling rapid development and automated testing without requiring testnet access or gas fees.

Related Documentation