Deploying Contracts

We can copy the generated Store.sol file to the Golang project, and realize the interaction with the Store.sol contract by calling the functions in Store.go. Here is a complete example of deploying a contract:

package main
import (
	store "...The path of the store.go file..."
	"context"
	"fmt"
	"log"
	"math/big"
	"github.com/PhoenixGlobal/Phoenix-Chain-SDK/ethereum/accounts/abi/bind"
	"github.com/PhoenixGlobal/Phoenix-Chain-SDK/ethereum/ethclient"
	"github.com/PhoenixGlobal/Phoenix-Chain-SDK/libs/common"
	"github.com/PhoenixGlobal/Phoenix-Chain-SDK/libs/crypto"
)
func main() {
	client, err := ethclient.Dial("https://dataseed1.phoenix.global/rpc")
	if err != nil {
		log.Fatal(err)
	}
	privateKey, err := crypto.HexToECDSA("your account private key")
	if err != nil {
		log.Fatal(err)
	}
	auth := bind.NewKeyedTransactor(privateKey)
	fromAddress := common.HexToAddress("your account address")
	nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
	if err != nil {
		log.Fatal(err)
	}
	auth.Nonce = big.NewInt(int64(nonce))
	auth.Value = big.NewInt(0)     // in wei
	auth.GasLimit = uint64(300000) // in units
	auth.GasPrice = big.NewInt(200000000000)
	version := "1.0.0"
	contractAddress, tx, contractInstance, err := store.DeployStore(auth, client, version)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(contractAddress.Hex()) // print contract address, example: 0x9aC8B849A8b6Fc14F8dEcfa6A22dB41671B38eFB
	fmt.Println(tx.Hash().Hex())       // print transaction has
	_ = contractInstance // new contract instance
}

Last updated