Abi file generation needs to install solidity, refer to the previous article
Or compile the smart contract through remix, copy the compiled abi
Download the Ethereum source code and mutate the abigen toolkit
git clone https://github.com/ethereum/go-ethereum.git
go install ./cmd/abigen
You can see the generated abigen tool in the directory corresponding to GOBIN
Generate the corresponding go file according to the abi file through abigen
abigen --abi contract-nft-nbc.abi --pkg nbc --type nbcToken --out nbc.go
–abi indicates the abi file name
–pkg indicates the package to which the generated file belongs
–type indicates the name of the generated data structure, if not filled, it defaults to the package name
–out indicates the generated file name
call contract
The local unit test code is messy. The following code is an example found on the Internet.
Use go to call smart contracts
package main
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"math/big"
"mydefi1/abi/cht"
)
const (
keyStorePath = `your keyStorePath`
fromKeyStore = `your from keyStore`
toAddress = "0x477257735cCEF9C7d42856649c7149865D04eDeb"
chainId = 7851254
chtAddress = "0xb9bD7405797CfFBc7e57309444b4af89c39cA92c" //合约地址
)
func main() {
//服务器地址
conn, err := ethclient.Dial("http://172.16.30.174:8545")
if err != nil {
fmt.Println("Dial err", err)
return
}
defer conn.Close()
//创建合约对象
chtToken, err := cht.NewChttoken(common.HexToAddress(chtAddress), conn)
if err != nil {
fmt.Println("newChttoken error", err)
}
//调用查询方法
//symbol, err := chtToken.Decimals(nil)
//if err != nil {
// fmt.Println("invoke error", err)
//}
//fmt.Println(symbol)
res1, err := chtToken.BalanceOf(&bind.CallOpts{
Pending: false,
From: common.Address{},
BlockNumber: nil,
Context: nil,
}, common.HexToAddress(toAddress))
if err != nil {
fmt.Println("BalanceOf error", err)
}
fmt.Println(res1)
//调用其他方法,需要组装from
//解锁对应账户
fromKey, err := keystore.DecryptKey([]byte(fromKeyStore), "abc123")
if err != nil {
fmt.Println(err)
}
auth, err := bind.NewKeyedTransactorWithChainID(fromKey.PrivateKey, new(big.Int).SetInt64(chainId))
if err != nil {
fmt.Println(err)
}
//fromPrivateKey := fromKey.PrivateKey
//fromPublicKey := fromPrivateKey.PublicKey
//fromAddr := crypto.PubkeyToAddress(fromPublicKey)
tx, err := chtToken.Transfer(&bind.TransactOpts{
From: auth.From,
//Nonce: nil,
Signer: auth.Signer,
//Value: nil,
//GasPrice: nil,
//GasFeeCap: nil,
//GasTipCap: nil,
//GasLimit: 0,
//Context: nil,
//NoSend: false,
}, common.HexToAddress(toAddress), new(big.Int).SetInt64(1e18))
if err != nil {
fmt.Println(err)
}
fmt.Println(tx.Hash())
//等待挖矿完成
receipt, err := bind.WaitMined(context.Background(), conn, tx)
if err != nil {
fmt.Println("WaitMined error", err)
}
fmt.Println(receipt.BlockNumber)
//转账之后查询余额
res2, err := chtToken.BalanceOf(&bind.CallOpts{
Pending: false,
From: common.Address{},
BlockNumber: nil,
Context: nil,
}, common.HexToAddress(toAddress))
if err != nil {
fmt.Println("BalanceOf error", err)
}
fmt.Println(res2)
}