go calls new method of solidity contract
I was bragging in the group chat last week, bragging about the golang I wrote to call the solana contract. Some people say that they want to learn how go calls solidity. I looked at the methods of calling contracts without ABI that I posted on the link. They were written in python, ethers, and solidity, but not in golang. They can indeed be written.
People in that group said that using chatgpt will use abigen to generate go packages to call contract methods.
So I will talk about a method that does not require using abigen to generate a go package to call the contract.
I still like to use my fork version of Xiaodao, https://github.com/daog1/ethgo
Fork from,umbracle/ethgo
I won’t talk about the benefits, just read the code yourself.
At first, contract calls required the generation of abi files. Later, there was a simpler method. The earliest I saw was called
in ethers.A Human-Readable ABI
ethers is written like this:
// 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 is a bit like forking ethers, so it also supports Human-Readable ABI.
The code is written like this
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)) }
The original code is here: https://github.com/daog1/ethgo/blob/main/examples/contract-call-basic.go
If you find any problems with my fork version, you can tell me.
I usually use Human-Readable ABI to call, abigen is too troublesome and easy to make mistakes.
The above is the detailed content of go calls new method of solidity contract. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.

Golang is suitable for rapid development and concurrent scenarios, and C is suitable for scenarios where extreme performance and low-level control are required. 1) Golang improves performance through garbage collection and concurrency mechanisms, and is suitable for high-concurrency Web service development. 2) C achieves the ultimate performance through manual memory management and compiler optimization, and is suitable for embedded system development.

The performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.

C is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.
