繁体   English   中英

如何使用geth提供的go API调用智能合约?

[英]How to use the go API provided by geth to call smart contracts?

    msg := ethereum.CallMsg{
        To:        &contractAddr,
        Gas:       uint64(21000),
        GasTipCap: tipCap,
        GasFeeCap: feeCap,
        Data:
    }
    client.CallContract(context.Background(), msg, nil)

我想用这个API来调用智能合约,但是不知道“数据”字段要填什么。 有人可以提供一个例子供我参考吗? 非常感谢!

这里ethereum.CallMsg是这个结构

type CallMsg struct {
    From      common.Address  // the sender of the 'transaction'
    To        *common.Address // the destination contract (nil for contract creation)
    Gas       uint64          // if 0, the call executes with near-infinite gas
    GasPrice  *big.Int        // wei <-> gas exchange ratio
    GasFeeCap *big.Int        // EIP-1559 fee cap per gas.
    GasTipCap *big.Int        // EIP-1559 tip per gas.
    Value     *big.Int        // amount of wei sent along with the call
    Data      []byte          // input data, usually an ABI-encoded contract method invocation

    AccessList types.AccessList // EIP-2930 access list.
}

这里数据的类型是[]byte

但我不知道在“数据”字段中填写什么

在 Data 字段中,您需要提供 ABI 编码的合约方法调用

要创建 ABI 编码的合约方法,您需要导入"github.com/ethereum/go-ethereum/accounts/abi"并使用abi.Pack方法

供您参考,您可以查看测试用例abi_test.go

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM