https://github.com/boolw/go-web3
Ethereum Golang jsonrpc API contract ABI encode decode
https://github.com/boolw/go-web3
ethereum ethereum-contract jsonrpc
Last synced: 5 months ago
JSON representation
Ethereum Golang jsonrpc API contract ABI encode decode
- Host: GitHub
- URL: https://github.com/boolw/go-web3
- Owner: boolw
- License: mpl-2.0
- Created: 2020-12-28T04:14:29.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-07-30T07:23:25.000Z (almost 2 years ago)
- Last Synced: 2024-07-30T10:34:54.711Z (almost 2 years ago)
- Topics: ethereum, ethereum-contract, jsonrpc
- Language: Go
- Homepage:
- Size: 159 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go-Web3
fork: https://github.com/umbracle/go-web3
commit: e3fe470083504f038262bcd073fa49e4908320dc
# Contract parse tool
Demo: https://abi.yiew.cn
## JsonRPC
```
package main
import (
"fmt"
web3 "github.com/boolw/go-web3"
"github.com/boolw/go-web3/jsonrpc"
)
func main() {
client, err := jsonrpc.NewClient("https://mainnet.infura.io")
if err != nil {
panic(err)
}
number, err := client.Eth().BlockNumber()
if err != nil {
panic(err)
}
header, err := client.Eth().GetBlockByNumber(web3.BlockNumber(number), true)
if err != nil {
panic(err)
}
fmt.Println(header)
}
```
## ABI
The ABI codifier uses randomized tests with e2e integration tests with a real Geth client to ensure that the codification is correct and provides the same results as the AbiEncoder from Solidity.
To use the library import:
```
"github.com/boolw/go-web3/abi"
```
Declare basic objects:
```
typ, err := abi.NewType("uint256")
```
or
```
typ = abi.MustNewType("uint256")
```
and use it to encode/decode the data:
```
num := big.NewInt(1)
encoded, err := typ.Encode(num)
if err != nil {
panic(err)
}
decoded, err := typ.Decode(num) // decoded as interface
if err != nil {
panic(err)
}
num2 := decoded.(*big.Int)
fmt.Println(num.Cmp(num2) == 0) // num == num2
```
You can also codify structs as Solidity tuples:
```
import (
"fmt"
web3 "github.com/boolw/go-web3"
"github.com/boolw/go-web3/abi"
"math/big"
)
func main() {
typ := abi.MustNewType("tuple(address a, uint256 b)")
type Obj struct {
A web3.Address
B *big.Int
}
obj := &Obj{
A: web3.Address{0x1},
B: big.NewInt(1),
}
// Encode
encoded, err := typ.Encode(obj)
if err != nil {
panic(err)
}
// Decode output into a map
res, err := typ.Decode(encoded)
if err != nil {
panic(err)
}
// Decode into a struct
var obj2 Obj
if err := typ.DecodeStruct(encoded, &obj2); err != nil {
panic(err)
}
fmt.Println(res)
fmt.Println(obj)
}
```
## ENS
Resolve names on the Ethereum Name Service registrar.
```
import (
"fmt"
web3 "github.com/boolw/go-web3"
"github.com/boolw/go-web3/jsonrpc"
"github.com/boolw/go-web3/contract/builtin/ens"
)
var mainnetAddress = web3.HexToAddress("0x314159265dD8dbb310642f98f50C066173C1259b")
func main() {
client, err := jsonrpc.NewClient("https://mainnet.infura.io")
if err != nil {
panic(err)
}
resolver := ens.NewENSResolver(mainnetAddress, client)
addr, err := resolver.Resolve("ens_address")
if err != nil {
panic(err)
}
fmt.Println(addr)
}
```
## Tracker
Complete example of the tracker [here](./tracker/README.md)