Swan Chain
  • BULDERS
    • Getting Started
    • DApp Developer
      • Deploying Your First Smart Contract with Remix
      • Interacting with Smart Contract on Swan Chain Using Go
    • App Developer
      • Building Docker Images and Deployment file with LDL
        • Lagrange Definition Language(LDL)
      • Deploying with Swan SDK
      • Store and Retrieve a File with Swan Storage
        • 1. Set up the python-MCS-SDK
        • 2.Create and Manage Buckets
        • 3.Upload Files and Folders
        • 4.Retrieve and Download Files
        • 5.Delete Files and Buckets
    • Node Operator
      • Swan Node Snapshots
    • Market Provider
      • Storage Market
      • AI/ML Orchestrator
        • Decentralized AI Marketplace
        • Connect to Orchestrator
      • Web3 ZK Computing Market
        • ZK Auction Engine
        • Sequencer
        • Contribute zk-UBI-task
          • How to Contribute
        • Example
      • Customized Market Provider
    • Computing Provider
      • Fog Computing Provider(FCP)
        • FCP Setup
        • Migrating FCP to Swan Mainnet
        • FCP Funding Operations Guide
        • FCP FAQ
      • Edge Computing Provider (ECP)
        • ECP Setup
        • ECP Funding Operations Guide
        • ECP FAQ
      • FAQ
    • Storage Provider
      • Storage Auction System
    • Developer Tools
      • Swan SDK
        • Python Swan SDK
          • Special Case: Create ssh login instance
          • SWAN Orchestrator SDK - Function and Parameter Reference
        • Go Swan SDK
          • A Sample Tutorial
      • Swan Console
        • Getting Started
        • Blockchain GPU Task Example
        • Custom Blockchain GPU Task Pools
      • Lagrange
      • Swan IPFS Storage
      • Nebula Block Cloud
      • Ecosystem Projects
    • Mission 3.0
      • Get Started
        • For Users
        • For Space Holders
  • NETWORK REFERENCE
    • Network Info
      • Set Up Your Wallet
      • Bridge Token
    • Contract Addresses
    • Fees
  • Core Concepts
    • Introduction to Swan Chain
    • Consensus Layer
    • Peer-to-peer (P2P) Network
    • Payment Channels
    • Service Discovery
    • Market Provider
      • Storage Market
      • AI Computing Marketplace
        • Orchestrator
        • Auction Engine
        • Bidding Task State Machine
      • ZK Proof Marketplace
        • ZK Task
        • ZK Pool
    • Storage Layer
    • Computing Layer
      • Computing Provider Protocol
      • Computing Provider Account
      • Layer3 Computing Protocol
      • Reputation System
      • Dynamic Pricing
    • CDN Layer
    • Tokenomics
      • UBI Allocation Curve
      • Computing Provider Income
      • Computing Provider Collateral
        • Collateral Requirement and Computing Unit
        • DePIN Oracle
      • Governance
        • Treasure DAO
    • Glossary
  • Swan Chain Campaign
    • Swan CP UBI
    • Swan Chain Mainnet
      • Network Information
      • Swan Token
      • Swan Chain Mission
        • Social Mission
        • Onchain Mission
        • Mission: Celestial Bloom
      • Swan Provider Campaign
        • Fog Computing Provider (FCP)
        • Edge Computing Provider (ECP)
        • Market Provider (MP)
      • GALXE Campaign
      • Free Tier and Special Credit Programs
    • Atom Accelerator Race
      • Before You Get Started
        • Set Up MetaMask
        • Claim SepoliaETH
        • Claim testSWAN
      • CP Acceleration Program
      • Builder Acceleration Program
      • On-chain Interaction Race
      • Community Engagement Challenge
      • FAQ
    • Swan Saturn Testnet
      • Before You Get Started
        • Set Up MetaMask
        • Claim Faucet Tokens
        • Bridge Tokens
      • Transaction Drive Program
      • Community and Educational Events
      • Partnership and Integration Program
      • KOL Program
      • Computing Provider Program
        • FAQ
      • Developer Grant Program
      • FAQ
    • Swan Jupiter Testnet
      • How to Participate
      • Before You Get Started
      • Network Early Adopter
      • FAQ
  • RESOURCE
    • Links
    • Brand Kit
Powered by GitBook
On this page
  1. BULDERS
  2. DApp Developer

Interacting with Smart Contract on Swan Chain Using Go

Introduction to Swan Chain and Connecting to RPC

  • Exploring Swan Chain: An Overview of Its Blockchain Features

  • Setting up the Go Development Environment for Swan Chain

  • Connecting to a Swan Chain RPC Using Go

  • Fetching Basic Blockchain Data from Swan Chain

1.Setting up the Go Development Environment for Swan Chain

package main
// Shared RPC URL
const rpcURL = "https://mainnet-rpc.swanchain.org" // Replace with your testnet's RPC URL

2.Connecting to a Swan Chain RPC Using Go

func TestConnectToTestnet(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
	defer cancel()

	// Assuming rpcURL is defined as a constant or variable that contains your Ethereum testnet RPC URL
	client, err := ethclient.DialContext(ctx, rpcURL)
	if err != nil {
		t.Fatalf("Failed to connect to the testnet: %v", err)
	}
	defer client.Close()

	// Fetching the network ID
	networkID, err := client.NetworkID(ctx)
	if err != nil {
		t.Fatalf("Failed to get network ID: %v", err)
	}

	// Fetching the latest block number
	blockNumber, err := client.BlockNumber(ctx)
	if err != nil {
		t.Fatalf("Failed to get the latest block number: %v", err)
	}

	t.Logf("Network ID: %v", networkID)
	t.Logf("Latest block number: %d", blockNumber)
}

3.Managing Wallets and Checking Balances

  • Creating and Managing Swan Chain Wallets with Go

  • Understanding and Checking Wallet Balances on Swan Chain

  • Handling Swan Chain's Native Cryptocurrency Units

// TestGetAccountBalance tests fetching the balance for a specific account
func TestGetAccountBalance(t *testing.T) {
   accountAddress := "0xA41c36BCd65bDbFB62FE93E3b7a28d290E63C1F7" // Replace with the account address

   ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
   defer cancel()

   client, err := ethclient.DialContext(ctx, rpcURL)
   if err != nil {
      t.Fatalf("Failed to connect to the mainnet: %v", err)
   }
   defer client.Close()

   address := common.HexToAddress(accountAddress)
   balanceWei, err := client.BalanceAt(ctx, address, nil) // Balance in Wei
   if err != nil {
      t.Fatalf("Failed to get the account balance: %v", err)
   }

   // Convert balance from Wei (big.Int) to Ether (float64)
   balanceEther := new(big.Float).Quo(new(big.Float).SetInt(balanceWei), big.NewFloat(math.Pow10(18)))

   t.Logf("Balance of account [%s]: %f Ether", accountAddress, balanceEther)

}

Output:

=== RUN TestGetAccountBalance ethclient_test.go:64: Balance of account [0xA41c36BCd65bDbFB62FE93E3b7a28d290E63C1F7]: 0.045930 Ether

--- PASS: TestGetAccountBalance (0.21s)

4.Writing and Reading from a Smart Contract

  • Setting up Go for Smart Contract Interaction

  • Writing Data to a Smart Contract on Swan Chain

  • Reading and Interpreting Data from a Smart Contract

Source code can be found here:

PreviousDeploying Your First Smart Contract with RemixNextApp Developer

Last updated 9 months ago

GitHub - swanchain/ether-testGitHub
Logo