总结
豆包 AI 助手文章总结
首页 > 后端开发 > Golang > 正文

go调用solidity合约新方法

霞舞
发布: 2024-12-23 10:03:55
转载
421人浏览过

上周在群聊吹牛,吹吹我写的golang 调用solana合约的东西。有人说他要学习go调用solidity的方法。我看了看我在登链的无abi调用合约的方法,写的python的,ethers的,solidity的,就是没写golang的,确实可以写写。

那个群里人都说用chatgpt都会用abigen的方式生成go包的方式调用合约方法。
所以我讲种不需要使用abigen生成go包的方法调用合约。

我还是喜欢使用我的晓道fork版,https://github.com/daog1/ethgo
fork自,umbracle/ethgo
好处我不讲,自己读代码。

合约调用最早都是需要abi文件生成的方式,后面还是有了更简单方式,最早我是ethers里面看到叫

a human-readable abi

ethers是这么写的:

// a human-readable abi; for interacting with the contract, we
// must include any fragment we wish to use
const abi = [
    // read-only functions
    "function balanceof(address owner) view returns (uint256)",
    "function decimals() view returns (uint8)",
    "function symbol() view returns (string)",

    // authenticated functions
    "function transfer(address to, uint amount) returns (bool)",

    // events
    "event transfer(address indexed from, address indexed to, uint amount)"
];

// this can be an address or an ens name
const address = "0x70ff5c5b1ad0533eaa5489e0d5ea01485d530674";

// read-only; by connecting to a provider, allows:
// - any constant function
// - querying filters
// - populating unsigned transactions for non-constant methods
// - estimating gas for non-constant (as an anonymous sender)
// - static calling non-constant methods (as anonymous sender)
const erc20 = new ethers.contract(address, abi, provider);

// read-write; by connecting to a signer, allows:
// - everything from read-only (except as signer, not anonymous)
// - sending transactions for non-constant functions
const erc20_rw = new ethers.contract(address, abi, signer);

登录后复制

ethgo 有点fork ethers的意思,所以也支持human-readable abi 的方式。
代码这么写

package main

import (
    "fmt"
    "math/big"

    "github.com/umbracle/ethgo"
    "github.com/umbracle/ethgo/abi"
    "github.com/umbracle/ethgo/contract"
    "github.com/umbracle/ethgo/jsonrpc"
)

func handleErr(err error) {
    if err != nil {
        panic(err)
    }
}

func main() {
    var functions = []string{
        "function totalSupply() view returns (uint256)",  //Human-Readable ABI 
    }

    abiContract, err := abi.NewABIFromList(functions)
    handleErr(err)

    // Matic token
    addr := ethgo.HexToAddress("0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0") 

    client, err := jsonrpc.NewClient("https://eth.llamarpc.com")
    handleErr(err)

    c := contract.NewContract(addr, abiContract, contract.WithJsonRPC(client.Eth()))
    res, err := c.Call("totalSupply", ethgo.Latest)  //call totalSupply
    handleErr(err)

    fmt.Printf("TotalSupply: %s", res["0"].(*big.Int))
}
登录后复制

原版代码在这里:https://github.com/daog1/ethgo/blob/main/examples/contract-call-basic.go/blob/main/examples/contract-call-basic.go
我的那个fork 版,如果你发现什么问题,可以跟我说。
我一般都用human-readable abi 的方式调用,abigen这种太麻烦了,容易搞错。

以上就是go调用solidity合约新方法的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:dev.to网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号