-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (96 loc) · 3.05 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"fmt"
"time"
"cosmossdk.io/math"
"github.com/cometbft/cometbft/rpc/client/http"
sdk "github.com/cosmos/cosmos-sdk/types"
gammtypes "github.com/osmosis-labs/osmosis/v25/x/gamm/types"
)
type SwapEvent struct {
TokensIn sdk.Coin
// TODO: translate the tokens in amount into USDC value
// Exampe API call to get the price:
// curl -X 'GET' \ 'https://sqsprod.osmosis.zone/tokens/prices?base=uosmo&humanDenoms=false' \ -H 'accept: application/json'
// Note: this can be done at the data indexing stage or at the API stage. The choice is yours.
USDCValue math.Int
SenderAddress string
PoolID uint64
Height int64
}
const (
archiveNodeAddress = "https://rpc.archive.osmosis.zone:443"
pricesAPIAddress = "https://sqs.osmosis.zone/tokens/prices?base=uosmo"
startBlock int64 = 17777000
endBlock = 17777050
)
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in main", r)
}
}()
fmt.Println("Hello, Osmosis!")
rpcClient, err := http.New(archiveNodeAddress, "/websocket")
if err != nil {
fmt.Println(err)
panic(err)
}
for i := startBlock; i <= endBlock; i++ {
curHeight := i
results, err := rpcClient.BlockResults(context.Background(), &curHeight)
if err != nil {
fmt.Println(err)
panic(err)
}
swapEvent := SwapEvent{
Height: curHeight,
}
// For every transaction in the block
for _, result := range results.TxsResults {
// Parse events
events := result.GetEvents()
for _, event := range events {
// Find swap event
if event.GetType() == gammtypes.TypeEvtTokenSwapped {
// Process swap event attrbutes to extract relevant data
// into the SwapEvent struct
attributes := event.GetAttributes()
for _, attr := range attributes {
// Parse tokens in
if string(attr.GetKey()) == gammtypes.AttributeKeyTokensIn {
inCoinStr := attr.GetValue()
// Parse the token in amount
inCoin, err := sdk.ParseCoinNormalized(inCoinStr)
if err != nil {
fmt.Println(err)
panic(err)
}
// Update the swap event struct
swapEvent.TokensIn = inCoin
}
// TODO:
// - Parse pool id from swap event
// - Parse sender address from swap event
}
fmt.Println(swapEvent)
}
}
}
// This is done to avoid rate limiting
// Reach out to us if this becomes a problem so that we issue an API key.
time.Sleep(500 * time.Millisecond)
}
// TODO:
// - Complete indexing logic
// * Ensure that indexing can start from a specified height, catch up to the tip and continue.
// according to the chain's progress.
// * Handle de-duplication as needed or overwrite existing data.
// * Choose a suitable storage solution for the data.
// * Convert tokens in amount into USDC value for indexing volume.
// - Expose web API
// * Implement an API to construct a merkle tree of swaps made from the indexed data
// - Bonus
// * Infrastructure / cloud automation and a fully-functional service.
}