go get github.com/ethereum/go-ethereum
用go mod的话 安装包位置:$GOPATH/pkg/mod/.
cd $GOPATH/pkg/mod/github.com/ethereum/go-ethereum@v1.10.17
sudo make && make devtools
appledeMac-mini:~ apple$ abigen --help
abigen [global options] command [command options] [arguments...]
VERSION:
1.10.17-stable
COMMANDS:
help Shows a list of commands or help for one command
GLOBAL OPTIONS:
--abi value Path to the Ethereum contract ABI json to bind, - for STDIN
--bin value Path to the Ethereum contract bytecode (generate deploy method)
--type value Struct name for the binding (default = package name)
--combined-json value Path to the combined-json file generated by compiler
--sol value Path to the Ethereum contract Solidity source to build and bind
--solc value Solidity compiler to use if source builds are requested (default: "solc")
--vy value Path to the Ethereum contract Vyper source to build and bind
--vyper value Vyper compiler to use if source builds are requested (default: "vyper")
--exc value Comma separated types to exclude from binding
--pkg value Package name to generate the binding into
--out value Output file for the generated binding (default = stdout)
--lang value Destination language for the bindings (go, java, objc) (default: "go")
--alias value Comma separated aliases for function and event renaming, e.g. original1=alias1, original2=alias2
--help, -h show help
--version, -v print the version
将.abi转成go代码
abigen -abi ERC20.abi -type ERC20 -pkg main -out ERC20.go
1.abi 要绑定的以太坊合约abi json的路径,-用于STDIN
以太坊合约字节码的路径(生成部署方法)
2. type 绑定的结构名称(默认=包名)
3. pkg要生成绑定到的包名
生成的绑定的输出文件(默认= stdout)
绑定的目标语言(go, java, objc)(默认值:“go”)
4.out 输出文件名
原始合约文件
转化后的go文件
调用合约服务(生成包改成(-pkg ) tools了,可以直接调tools下转化来的合约代码) :
package service
import (
"context"
"firstproject/tools"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
type sGethser struct{}
// 管理职能合约服务
func Gethser() *sGethser {
return &sGethser{}
}
// 创建合约对象
func (s *sGethser) CommonEth(ctx context.Context, ethconnhost, calls string) (*tools.ERC20Caller, error) {
// Dial connects a client to the given URL
// ethconnhost 以太坊地址 calls 智能合约账户地址
conn, err := ethclient.Dial(ethconnhost)
if err != nil {
panic("连接以太坊出错")
}
// 查询等 *** 作可以连接返回后关闭连接
// defer conn.Close()
// 生成合约实例 , NewCallOpts creates a new option set for contract calls.
gethObject, err := tools.NewERC20Caller(common.HexToAddress(calls), conn)
if err != nil {
panic("创建合约对象出错")
}
return gethObject, nil
}
// todo 调用合约对象处理业务逻辑
func (s *sGethser) DoFunc(ctx context.Context) error {
//
gethObject, err := s.CommonEth(ctx, "", "")
if err != nil {
return err
}
// 调用合约方法
gethObject.Name(nil)
return nil
}
.sol–>.abi–>.go
安装 solidity
brew install solidity
if [ == "build" ]; then
rm -rf contracts/
for i in "BasOANN" "BasMarket" "BasMail" "BasView"; do
solc contract_sol/$i.sol --abi -o contracts
done
for i in $(ls ./contracts/); do
# echo ${i%%.*}
abigen -abi contracts/${i%%.*}.abi -type ${i%%.*} -pkg contracts -out contracts/${i%%.*}.go
done
fi
#if [ == "build_go" ]; then
# for i in $(ls ./contracts/); do
# echo $i
# abigen -abi contracts/$i.abi -type $i -pkg contracts -out contracts/$i.go
# done
#fi
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)