-
Notifications
You must be signed in to change notification settings - Fork 249
/
contract.go
78 lines (66 loc) · 2.49 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package incentivisation
import (
"context"
"errors"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/status-im/status-go/mailserver/registry"
"math/big"
"time"
)
type Contract interface {
Vote(opts *bind.TransactOpts, joinNodes []gethcommon.Address, removeNodes []gethcommon.Address) (*types.Transaction, error)
GetCurrentSession(opts *bind.CallOpts) (*big.Int, error)
Registered(opts *bind.CallOpts, publicKey []byte) (bool, error)
RegisterNode(opts *bind.TransactOpts, publicKey []byte, ip uint32, port uint16) (*types.Transaction, error)
ActiveNodeCount(opts *bind.CallOpts) (*big.Int, error)
InactiveNodeCount(opts *bind.CallOpts) (*big.Int, error)
GetNode(opts *bind.CallOpts, index *big.Int) ([]byte, uint32, uint16, uint32, uint32, error)
GetInactiveNode(opts *bind.CallOpts, index *big.Int) ([]byte, uint32, uint16, uint32, uint32, error)
VoteSync(opts *bind.TransactOpts, joinNodes []gethcommon.Address, removeNodes []gethcommon.Address) (*types.Transaction, error)
}
type ContractImpl struct {
registry.NodesV2
client *ethclient.Client
}
// VoteSync votes on the contract and wait until the transaction has been accepted, returns an error otherwise
func (c *ContractImpl) VoteSync(opts *bind.TransactOpts, joinNodes []gethcommon.Address, removeNodes []gethcommon.Address) (*types.Transaction, error) {
tx, err := c.Vote(opts, joinNodes, removeNodes)
if err != nil {
return nil, err
}
for {
receipt, _ := c.client.TransactionReceipt(context.TODO(), tx.Hash())
if receipt != nil {
if receipt.Status == 0 {
return nil, errors.New("Invalid receipt")
}
break
}
time.Sleep(500 * time.Millisecond)
}
return tx, nil
}
// NewContract creates a new instance of Contract, bound to a specific deployed contract.
func NewContract(address gethcommon.Address, backend bind.ContractBackend, client *ethclient.Client) (Contract, error) {
contract := &ContractImpl{}
contract.client = client
caller, err := registry.NewNodesV2Caller(address, backend)
if err != nil {
return nil, err
}
contract.NodesV2Caller = *caller
transactor, err := registry.NewNodesV2Transactor(address, backend)
if err != nil {
return nil, err
}
contract.NodesV2Transactor = *transactor
filterer, err := registry.NewNodesV2Filterer(address, backend)
if err != nil {
return nil, err
}
contract.NodesV2Filterer = *filterer
return contract, nil
}