/
payments.go
81 lines (62 loc) · 2.28 KB
/
payments.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
79
80
81
package atlas
import (
"math/big"
"context"
"log"
"strings"
binding "github.com/ChorusOne/Icarus/binding/mainnet"
. "github.com/ChorusOne/Icarus/common"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
// Atlas Functions relevant to validator and group epoch payments (aka commissions, in cUSD)
//EpochPaymentEvent is a struct to represent data for the event
//ValidatorEpochPaymentDistributed(address,unit256,address,uint256)
//emitted by Validators.sol
type EpochPaymentEvent struct {
Validator common.Address
ValidatorPayment *big.Int
Group common.Address
GroupPayment *big.Int
BlockNumber *big.Int
}
//GetEpochPaymentEvents fetches data for all
//ValidatorEpochPaymentDistributed events emitted by the Validators contract
// between (and including) the two specified block numbers.
func GetEpochPaymentEvents(fromBlock *big.Int, toBlock *big.Int) []*EpochPaymentEvent {
var logEpochPaymentSig = []byte("ValidatorEpochPaymentDistributed(address,uint256,address,uint256)")
var logEpochPaymentSigHash = crypto.Keccak256Hash(logEpochPaymentSig)
var TopicsFilter = [][]common.Hash{{logEpochPaymentSigHash}}
contractAddress := common.HexToAddress(WrapperContractDeploymentAddress[NetActive][Validators])
query := ethereum.FilterQuery{
FromBlock: fromBlock,
ToBlock: toBlock,
Topics: TopicsFilter,
Addresses: []common.Address{
contractAddress,
},
}
logs, err := atlasEthClient.FilterLogs(context.Background(), query)
if err != nil {
log.Fatal(err)
}
paymentsDistributed := make([]*EpochPaymentEvent, 0, len(logs))
contractAbi, err := abi.JSON(strings.NewReader(string(binding.ValidatorsABI)))
if err != nil {
log.Fatal(err)
}
for _, vLog := range logs {
var epochPayment EpochPaymentEvent
err := contractAbi.Unpack(&epochPayment, "ValidatorEpochPaymentDistributed", vLog.Data)
if err != nil {
log.Fatal(err)
}
epochPayment.Validator = common.HexToAddress(vLog.Topics[1].Hex())
epochPayment.Group = common.HexToAddress(vLog.Topics[2].Hex())
epochPayment.BlockNumber = new(big.Int).SetUint64(vLog.BlockNumber)
paymentsDistributed = append(paymentsDistributed, &epochPayment)
}
return paymentsDistributed
}