Logo

dev-resources.site

for different kinds of informations.

go调用solidity合约新方法

Published at
12/21/2024
Categories
solidity
go
Author
xiaodao
Categories
2 categories in total
solidity
open
go
open
Author
7 person written this
xiaodao
open
go调用solidity合约新方法

上周在群聊吹牛,吹吹我写的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);

Enter fullscreen mode Exit fullscreen mode

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))
}
Enter fullscreen mode Exit fullscreen mode

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

solidity Article's
30 articles in total
Favicon
Have You Fallen for a Phishing Scam? Let’s Talk About It 👀
Favicon
Solidity
Favicon
Why Every Crypto Developer Should Understand Tokenomics 🚀
Favicon
How we used the ERC-2535 Diamonds at Proof of Peacemaking Protocol
Favicon
🔐 Solidity Limitations, Solutions, Best Practices and Gas Optimization 🚀
Favicon
go调用solidity合约新方法
Favicon
⚖️ The Importance of Using ReentrancyGuard in Solidity Smart Contract
Favicon
Formal Verification: An Example
Favicon
OverFlow and UnderFlow causes in Solidity
Favicon
When to Use ERC-721 vs ERC-1155: Choosing the Right NFT Standard
Favicon
Solidity Pattern - Proxy Delegate and Decorator Patterns
Favicon
Solidity Patterns - CEI
Favicon
Foundry vs Hardhat (A story version)
Favicon
Energy NFT Marketplace
Favicon
OverFlow and UnderFlow causes in Solidity
Favicon
🚀 Getting Started with kritisi CLI: An AI-Driven Security Tool for Solidity
Favicon
Formal Verification: The Foundation of Ethereum Smart Contracts
Favicon
The Danger of Randomness in Smart Contracts and its solution
Favicon
What is Reentrancy?
Favicon
Understanding approve and depositCollateral: The Core of ERC-20 Token Transfers in Solidity
Favicon
Ethereum Transaction Calls and State Changes
Favicon
Creating a Toy Solidity compiler and running it in a Toy EVM
Favicon
Send Tokens in Bulk with Low Fees and Fast Delivery: The Ultimate Airdrop Tool for 2024
Favicon
🛡️ Why Using OpenZeppelin in Smart Contracts Is Essential
Favicon
The delegatecall Function in Solidity
Favicon
The delegatecall Function in Solidity
Favicon
A Walkthrough of Solidity Custom Errors
Favicon
How to write dynamic staking smart contract step by step in practice
Favicon
Mainnet Forking in Foundry
Favicon
Every Blockchain Developer Must Know About This Scam!

Featured ones: