-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
314 lines (267 loc) · 8.63 KB
/
cmd.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"context"
"encoding/csv"
"fmt"
"os"
"strconv"
"time"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
liquiditytypes "github.com/gravity-devs/liquidity/x/liquidity/types"
"github.com/schollz/progressbar/v3"
"github.com/spf13/cobra"
)
func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "gravity-dex-stats",
Short: "Gravity DEX statistics extractor",
}
cmd.AddCommand(
SummaryCmd(),
ReadGenesisCmd(),
SearchBlockCmd(),
)
return cmd
}
type PoolSummary struct {
ID uint64
ReserveCoins [2]sdk.Coin
Swaps [2]SwapSummary
}
type SwapSummary struct {
OfferCoin sdk.Coin
OfferCoinFee sdk.Coin
DemandCoin sdk.Coin
DemandCoinFee sdk.Coin
}
func SummaryCmd() *cobra.Command {
var beginHeight, endHeight int64
var outFileName string
cmd := &cobra.Command{
Use: "summary",
Short: "Display short summary",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
cfg, err := ReadClientConfig("config.toml")
if err != nil {
return fmt.Errorf("read client config: %w", err)
}
c, err := NewClient(cfg)
if err != nil {
return fmt.Errorf("new client: %w", err)
}
defer c.Close()
ctx := context.Background()
if endHeight == 0 {
h, err := c.LatestBlockHeight(ctx)
if err != nil {
return fmt.Errorf("get latest block height: %w", err)
}
endHeight = h
}
if beginHeight > endHeight {
return fmt.Errorf("begin height must be less or equal than end height")
}
fmt.Println("loading liquidity pools")
pools, err := c.Pools(ctx, WithBlockHeight(endHeight))
if err != nil {
return fmt.Errorf("get pools: %w", err)
}
summaries := make(map[uint64]*PoolSummary)
denomSet := make(map[string]struct{})
swapRequesters := make(map[string]struct{})
bar := progressbar.Default(int64(len(pools)))
for _, pool := range pools {
summaries[pool.Id] = &PoolSummary{ID: pool.Id}
for i, denom := range pool.ReserveCoinDenoms {
balance, err := c.Balance(ctx, pool.ReserveAccountAddress, denom, WithBlockHeight(endHeight))
if err != nil {
return fmt.Errorf("get balance: %w", err)
}
summaries[pool.Id].ReserveCoins[i] = balance
summaries[pool.Id].Swaps[i].OfferCoin = sdk.NewCoin(denom, sdk.ZeroInt())
summaries[pool.Id].Swaps[i].OfferCoinFee = sdk.NewCoin(denom, sdk.ZeroInt())
summaries[pool.Id].Swaps[i].DemandCoin = sdk.NewCoin(pool.ReserveCoinDenoms[1-i], sdk.ZeroInt())
summaries[pool.Id].Swaps[i].DemandCoinFee = sdk.NewCoin(pool.ReserveCoinDenoms[1-i], sdk.ZeroInt())
denomSet[denom] = struct{}{}
}
_ = bar.Add(1)
}
fmt.Println("loading events")
heights, err := c.SearchBlockHeights(
ctx,
fmt.Sprintf(`swap_transacted.pool_id EXISTS AND block.height >= %d AND block.height <= %d`, beginHeight, endHeight),
)
if err != nil {
return fmt.Errorf("search block heights: %w", err)
}
if len(heights) == 0 {
fmt.Println("no swap events found")
return nil
}
bar = progressbar.Default(int64(len(heights)))
for _, height := range heights {
events, err := c.EndBlockEvents(ctx, height)
if err != nil {
return fmt.Errorf("get end block events: %w", err)
}
for _, event := range events {
if event.Type == liquiditytypes.EventTypeSwapTransacted {
ste, err := NewSwapTransactedEvent(event)
if err != nil {
return fmt.Errorf("new swap_transacted event: %w", err)
}
if ste.Success {
ps, ok := summaries[ste.PoolID]
if !ok {
return fmt.Errorf("pool id not found: %d", ste.PoolID)
}
var i int
if ste.ExchangedOfferCoin.Denom == ps.Swaps[0].OfferCoin.Denom {
i = 0
} else {
i = 1
}
ps.Swaps[i].OfferCoin = ps.Swaps[i].OfferCoin.Add(ste.ExchangedOfferCoin)
ps.Swaps[i].OfferCoinFee = ps.Swaps[i].OfferCoinFee.Add(ste.ExchangedOfferCoinFee)
ps.Swaps[i].DemandCoin = ps.Swaps[i].DemandCoin.Add(ste.ExchangedDemandCoin)
ps.Swaps[i].DemandCoinFee = ps.Swaps[i].DemandCoinFee.Add(ste.ExchangedDemandCoinFee)
swapRequesters[ste.SwapRequesterAddress] = struct{}{}
}
}
}
_ = bar.Add(1)
}
fmt.Printf("Gravity DEX Summary (block height: %d)\n", endHeight)
fmt.Printf("* %d kind(s) of token\n", len(denomSet))
fmt.Printf("* %d swap trader(s)\n", len(swapRequesters))
outFile, err := os.Create(outFileName)
if err != nil {
return fmt.Errorf("create output file: %w", err)
}
defer outFile.Close()
csvWriter := csv.NewWriter(outFile)
records := [][]string{{
"id", "x_denom", "y_denom", "x", "y",
"offer_x", "offer_x_fee", "demand_y", "demand_y_fee",
"offer_y", "offer_y_fee", "demand_x", "demand_x_fee",
}}
for _, pool := range pools {
ps := summaries[pool.Id]
records = append(records, []string{
strconv.FormatUint(ps.ID, 10),
ps.ReserveCoins[0].Denom,
ps.ReserveCoins[1].Denom,
ps.ReserveCoins[0].Amount.String(),
ps.ReserveCoins[1].Amount.String(),
ps.Swaps[0].OfferCoin.Amount.String(),
ps.Swaps[0].OfferCoinFee.Amount.String(),
ps.Swaps[0].DemandCoin.Amount.String(),
ps.Swaps[0].DemandCoinFee.Amount.String(),
ps.Swaps[1].OfferCoin.Amount.String(),
ps.Swaps[1].OfferCoinFee.Amount.String(),
ps.Swaps[1].DemandCoin.Amount.String(),
ps.Swaps[1].DemandCoinFee.Amount.String(),
})
}
if err := csvWriter.WriteAll(records); err != nil {
return fmt.Errorf("write output: %w", err)
}
return nil
},
}
cmd.Flags().Int64VarP(&beginHeight, "begin", "b", 1, "Begin block height")
cmd.Flags().Int64VarP(&endHeight, "end", "e", 0, "End block height")
cmd.Flags().StringVarP(&outFileName, "out", "o", "pools.csv", "Output file name")
return cmd
}
func ReadGenesisCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "read-genesis [file]",
Short: "Read genesis file and extract summary",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
cfg, err := ReadClientConfig("config.toml")
if err != nil {
return fmt.Errorf("read client config: %w", err)
}
c, err := NewClient(cfg)
if err != nil {
return fmt.Errorf("new client: %w", err)
}
defer c.Close()
ctx := context.Background()
pools, err := c.Pools(ctx)
if err != nil {
return fmt.Errorf("get pools: %w", err)
}
poolCoinDenomSet := make(map[string]uint64) // (pool coin denom) => (pool id)
for _, pool := range pools {
poolCoinDenomSet[pool.PoolCoinDenom] = pool.Id
}
appState, _, err := genutiltypes.GenesisStateFromGenFile(args[0])
if err != nil {
return fmt.Errorf("genesis state from file: %w", err)
}
genState := banktypes.GetGenesisStateFromAppState(codec.NewProtoCodec(codectypes.NewInterfaceRegistry()), appState)
numPoolInvestors := make(map[uint64]int) // (pool id) => (num pool investors)
totalPoolInvestors := make(map[string]struct{})
for _, balance := range genState.Balances {
for poolCoinDenom, poolID := range poolCoinDenomSet {
if balance.Coins.AmountOf(poolCoinDenom).IsPositive() {
numPoolInvestors[poolID]++
totalPoolInvestors[balance.Address] = struct{}{}
}
}
}
fmt.Println("number of pool investors")
fmt.Println("========================")
for _, pool := range pools {
fmt.Printf("pool %d: %d\n", pool.Id, numPoolInvestors[pool.Id])
}
fmt.Printf("total: %d\n", len(totalPoolInvestors))
return nil
},
}
return cmd
}
func SearchBlockCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "search-block [time]",
Short: "Search block heights for specific time",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
t, err := time.Parse(time.RFC3339, args[0])
if err != nil {
return fmt.Errorf("parse time: %w", err)
}
cmd.SilenceUsage = true
cfg, err := ReadClientConfig("config.toml")
if err != nil {
return fmt.Errorf("read client config: %w", err)
}
c, err := NewClient(cfg)
if err != nil {
return fmt.Errorf("new client: %w", err)
}
defer c.Close()
ctx := context.Background()
h, err := c.SearchBlockHeightByTime(ctx, t)
if err != nil {
return fmt.Errorf("search block height by time: %w", err)
}
t, err = c.BlockTime(ctx, h)
if err != nil {
return fmt.Errorf("get block time: %w", err)
}
fmt.Printf("Nearest block height is %d, time is %s\n", h, t.Format(time.RFC3339))
return nil
},
}
return cmd
}