description: Tutorial on how to load an initialize a smart contract with Go.
Loading a Smart Contract
These section requires knowledge of how to compile a smart contract’s ABI to a Go contract file. If you haven’t already gone through it, please read the section first.
Once you’ve compiled your smart contract’s ABI to a Go package using the abigen
tool, the next step is to call the “New” method, which is in the format New<ContractName>
, so in our example if you recall it’s going to be NewStore. This initializer method takes in the address of the smart contract and returns a contract instance that you can start interact with it.
address := common.HexToAddress("0x147B8eb97fD247D06C4006D269c90C1908Fb5D54")
instance, err := store.NewStore(address, client)
if err != nil {
log.Fatal(err)
}
_ = instance // we'll be using this in the next section
Full code
Commands
solc --abi Store.sol
solc --bin Store.sol
abigen --bin=Store_sol_Store.bin --abi=Store_sol_Store.abi --pkg=store --out=Store.go
pragma solidity ^0.4.24;
contract Store {
event ItemSet(bytes32 key, bytes32 value);
string public version;
mapping (bytes32 => bytes32) public items;
constructor(string _version) public {
version = _version;
}
function setItem(bytes32 key, bytes32 value) external {
items[key] = value;
emit ItemSet(key, value);
}
}
package main
import (
"fmt"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
store "./contracts" // for demo
)
func main() {
client, err := ethclient.Dial("https://rinkeby.infura.io")
if err != nil {
log.Fatal(err)
}
address := common.HexToAddress("0x147B8eb97fD247D06C4006D269c90C1908Fb5D54")
instance, err := store.NewStore(address, client)
if err != nil {
log.Fatal(err)
}
fmt.Println("contract is loaded")
_ = instance
}
solc version used for these examples
$ solc --version
0.4.24+commit.e67f0147.Emscripten.clang
当前内容版权归 Miguel Mota 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 Miguel Mota .