-
Notifications
You must be signed in to change notification settings - Fork 8
/
contract.go
51 lines (42 loc) · 1.12 KB
/
contract.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package ethereum
import (
"fmt"
"math/big"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/contract"
)
// Contract exposes a contract
type Contract struct {
*contract.Contract
client *Client
}
type TxnOpts struct {
Value uint64
GasPrice uint64
GasLimit uint64
Nonce uint64
}
// Call executes a call on the contract
func (c *Contract) Call(method string, args ...interface{}) (map[string]interface{}, error) {
return c.Contract.Call(method, ethgo.Latest, args...)
}
// Txn executes a transactions on the contract and waits for it to be mined
// TODO maybe use promise
func (c *Contract) Txn(method string, opts TxnOpts, args ...interface{}) (string, error) {
txn, err := c.Contract.Txn(method, args...)
if err != nil {
return "", fmt.Errorf("failed to create contract transaction: %w", err)
}
txo := contract.TxnOpts{
Value: big.NewInt(int64(opts.Value)),
GasPrice: opts.GasPrice,
GasLimit: opts.GasLimit,
Nonce: opts.Nonce,
}
txn.WithOpts(&txo)
err = txn.Do()
if err != nil {
return "", fmt.Errorf("failed to send contract transaction: %w", err)
}
return txn.Hash().String(), nil
}