Skip to content

Commit

Permalink
Contract revamp (#167)
Browse files Browse the repository at this point in the history
* Update go.mod

* Abigen improv

* Improve contract

* Update docs

* Update changelog

* Update changelog 2

* Update README
  • Loading branch information
ferranbt authored Mar 10, 2022
1 parent 5019505 commit 2e51d36
Show file tree
Hide file tree
Showing 39 changed files with 655 additions and 610 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

# 0.1.1 (Not released)

- Introduce `NodeProvider` and update `Contract` and `abigen` format. [[GH-167](https://github.com/umbracle/ethgo/issues/167)]

# 0.1.0 (5 March, 2022)

- Initial public release.
216 changes: 6 additions & 210 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,218 +1,14 @@

# Eth-Go

## JsonRPC
Ethgo is a lightweight SDK in Go to interact with Ethereum compatible blockchains.

```golang
package main
- Website: https://ethgo.dev

import (
"fmt"

"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/jsonrpc"
)
Ethgo provides the next key features:

func main() {
client, err := jsonrpc.NewClient("https://mainnet.infura.io")
if err != nil {
panic(err)
}
- **Simple**: Light and with a small number of direct dependencies.

number, err := client.Eth().BlockNumber()
if err != nil {
panic(err)
}
- **Ethereum ecosystem**: Native integration with other tools from the ecosystem like `ens` and `etherscan`.

header, err := client.Eth().GetBlockByNumber(ethgo.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/umbracle/ethgo/abi"
```

Declare basic objects:

```golang
typ, err := abi.NewType("uint256")
```

or

```golang
typ = abi.MustNewType("uint256")
```

and use it to encode/decode the data:

```golang
num := big.NewInt(1)

encoded, err := typ.Encode(num)
if err != nil {
panic(err)
}

decoded, err := typ.Decode(encoded) // 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:

```golang
import (
"fmt"

"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/abi"
"math/big"
)

func main() {
typ := abi.MustNewType("tuple(address a, uint256 b)")

type Obj struct {
A ethgo.Address
B *big.Int
}
obj := &Obj{
A: ethgo.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)
}
```

## Wallet

As for now the library only provides primitive abstractions to send signed abstractions. The intended goal is to abstract the next steps inside the contract package.

```golang
// Generate a random wallet
key, _ := wallet.GenerateKey()

to := ethgo.Address{0x1}
transferVal := big.NewInt(1000)

// Create the transaction
txn := &ethgo.Transaction{
To: &to,
Value: transferVal,
Gas: 100000,
}

// Create the signer object and sign
signer := wallet.NewEIP155Signer(chainID)
txn, _ = signer.SignTx(txn, key)

// Send the signed transaction
data := txn.MarshalRLP()
hash, _ := c.Eth().SendRawTransaction(data)
```

## ENS

Resolve names on the Ethereum Name Service registrar.

```golang
import (
"fmt"

"github.com/umbracle/ethgo/jsonrpc"
"github.com/umbracle/ethgo/ens"
)

func main() {
client, err := jsonrpc.NewClient("https://mainnet.infura.io")
if err != nil {
panic(err)
}

ens, err := ens.NewENS(ens.WithClient(client))
if err != nil {
panic(err)
}
addr, err := ens.Resolve("ens_address")
if err != nil {
panic(err)
}
fmt.Println(addr)
}
```

## Block tracker

```golang
import (
"fmt"

"github.com/umbracle/ethgo/jsonrpc"
"github.com/umbracle/ethgo/blocktracker"
)

func main() {
client, err := jsonrpc.NewClient("https://mainnet.infura.io")
if err != nil {
panic(err)
}

tracker = blocktracker.NewBlockTracker(client, WithBlockMaxBacklog(1000))
if err := tracker.Init(); err != nil {
panic(err)
}
go tracker.Start()

sub := tracker.Subscribe()
go func() {
for {
select {
case evnt := <-sub:
fmt.Println(evnt)
case <-ctx.Done():
return
}
}
}
}
```

## Tracker

Complete example of the tracker [here](./tracker/README.md)
- **Command-line-interface**: Ethgo is both a Golang SDK library and a CLI.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 14 additions & 18 deletions contract/builtin/ens/ens.go → builtin/ens/ens.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package ens

import (
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/contract"
"github.com/umbracle/ethgo/jsonrpc"
)

type ENSResolver struct {
e *ENS
provider *jsonrpc.Client
provider *jsonrpc.Eth
}

func NewENSResolver(addr ethgo.Address, provider *jsonrpc.Client) *ENSResolver {
return &ENSResolver{NewENS(addr, provider), provider}
return &ENSResolver{NewENS(addr, contract.WithJsonRPC(provider.Eth())), provider.Eth()}
}

func (e *ENSResolver) Resolve(addr string, block ...ethgo.BlockNumber) (res ethgo.Address, err error) {
Expand All @@ -21,7 +22,7 @@ func (e *ENSResolver) Resolve(addr string, block ...ethgo.BlockNumber) (res ethg
return
}

resolver := NewResolver(resolverAddr, e.provider)
resolver := NewResolver(resolverAddr, contract.WithJsonRPC(e.provider))
res, err = resolver.Addr(addrHash, block...)
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
)

var (
url = "https://mainnet.infura.io"
mainnetAddr = ethgo.HexToAddress("0x314159265dD8dbb310642f98f50C066173C1259b")
)

Expand Down
Loading

1 comment on commit 2e51d36

@vercel
Copy link

@vercel vercel bot commented on 2e51d36 Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.