-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (67 loc) · 1.93 KB
/
main.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 main
import (
"context"
"fmt"
"math"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/playground/go-redemptions/conditional_tokens"
"github.com/shopspring/decimal"
)
const (
WS_URL = "wss://..." // TODO: Complete this with a WS RPC
)
var REDEEMER_ADDRESS = common.HexToAddress("0x5f211A24da4c005d9438A1eA269673b85eD0b376")
var HASHED_SIGNATURES = [...]common.Hash{crypto.Keccak256Hash([]byte("PayoutRedemption(address,address,bytes32,bytes32,uint256[],uint256)"))}
var CONTRACT_ADDRESS = []common.Address{common.HexToAddress("0x4D97DCd97eC945f40cF65F87097ACe5EA0476045")}
var FQ = ethereum.FilterQuery{
Addresses: CONTRACT_ADDRESS[:],
Topics: [][]common.Hash{HASHED_SIGNATURES[:]},
}
func main() {
ctx := context.Background()
client, err := ethclient.DialContext(ctx, WS_URL)
if err != nil {
panic(err)
}
logs := make(chan types.Log)
subs, err := client.SubscribeFilterLogs(ctx, FQ, logs)
if err != nil {
panic(err)
}
for {
select {
case err, ok := <-subs.Err():
if ok && err != nil {
panic(err)
}
case log, ok := <-logs:
if !ok {
panic("logs channel closed")
}
redemption, err := getContract().ParsePayoutRedemption(log)
if err != nil {
panic(err)
}
if redemption.Redeemer.String() == REDEEMER_ADDRESS.String() {
fmt.Printf("redemption: %s\n", normalizeAmounts(redemption.Payout).String())
}
}
}
}
// gets a conditional token instance
func getContract() *conditional_tokens.ConditionalTokensFilterer {
c, err := conditional_tokens.NewConditionalTokensFilterer(CONTRACT_ADDRESS[0], nil)
if err != nil {
panic(err)
}
return c
}
// removes the 6 usdc 0s
func normalizeAmounts(amount *big.Int) decimal.Decimal {
return decimal.NewFromBigInt(amount, 0).Div(decimal.NewFromFloat(math.Pow(10, 6)))
}