-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
238 lines (192 loc) · 8.71 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
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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/b-harvest/gravity-dex-firestation/client"
"github.com/b-harvest/gravity-dex-firestation/config"
"github.com/b-harvest/gravity-dex-firestation/tx"
"github.com/b-harvest/gravity-dex-firestation/wallet"
liqtypes "github.com/tendermint/liquidity/x/liquidity/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
// total amount of dollars worth of reserve coins to generate trading volume per hour
remainingAmountPerHour = int64(1_000_000_000)
// sending amount of dollars worth of coins to send for each buy and sell tx
sendAmount = int64(69444)
// sleep 1 seconds for each frequency
frequency = 3600
// number of hours
duration = 10
)
func main() {
cfg, err := config.Read(config.DefaultConfigPath)
if err != nil {
log.Fatalf("failed to read config: %s", err)
}
client, err := client.NewClient(cfg.RPC.Address, cfg.GRPC.Address, cfg.CoinMarketCap)
if err != nil {
log.Fatalf("failed to create new config: %s", err)
}
for i := 0; i < duration; i++ {
log.Printf("🔥 Trading Volume Bot 🔥 %d out of %d duration", i+1, duration)
impactTradingVolume(cfg, client)
}
}
func impactTradingVolume(cfg config.Config, client *client.Client) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
chainID, err := client.RPC.GetNetworkChainID(ctx)
if err != nil {
return fmt.Errorf("failed to get chain id: %s", err)
}
accAddr, privKey, err := wallet.RecoverAccountFromMnemonic(cfg.Wallet.Mnemonic, "")
if err != nil {
return fmt.Errorf("failed to retrieve account and private key from mnemonic: %s", err)
}
account, err := client.GRPC.GetBaseAccountInfo(ctx, accAddr)
if err != nil {
return fmt.Errorf("failed to get account information: %s", err)
}
accSeq := account.GetSequence()
accNum := account.GetAccountNumber()
fees := sdk.NewCoins(sdk.NewCoin(cfg.FireStation.FeeDenom, sdk.NewInt(cfg.FireStation.FeeAmount)))
transaction := tx.NewTransaction(client, chainID, fees)
log.Println("----------------------------------------------------------------")
log.Printf("| ✅ ChainID: %s\n", chainID)
log.Printf("| ✅ Sender: %s\n", accAddr)
log.Printf("| ✅ Fees: %s\n", fees.String())
targetPools, err := client.Market.GetTargetPools(ctx)
if err != nil {
return fmt.Errorf("failed to get target pools: %s", err)
}
var pools liqtypes.Pools
for _, tp := range targetPools {
pool, err := client.GRPC.GetPool(context.Background(), tp)
if err != nil {
return fmt.Errorf("failed to get pool information: %s", err)
}
pools = append(pools, pool)
}
log.Println("----------------------------------------------------------------[Random Pools]")
log.Printf("| pool 1: %s\n", pools[0].String())
log.Printf("| pool 2: %s\n", pools[1].String())
log.Printf("| pool 3: %s\n", pools[2].String())
log.Printf("| pool 4: %s\n", pools[3].String())
log.Printf("| pool 1 ReserveCoinDenoms: %s\n", pools[0].ReserveCoinDenoms)
log.Printf("| pool 2 ReserveCoinDenoms: %s\n", pools[1].ReserveCoinDenoms)
log.Printf("| pool 3 ReserveCoinDenoms: %s\n", pools[2].ReserveCoinDenoms)
log.Printf("| pool 4 ReserveCoinDenoms: %s\n", pools[3].ReserveCoinDenoms)
log.Println("----------------------------------------------------------------")
targetDenoms := []string{
pools[0].ReserveCoinDenoms[0],
pools[0].ReserveCoinDenoms[1],
pools[1].ReserveCoinDenoms[0],
pools[1].ReserveCoinDenoms[1],
pools[2].ReserveCoinDenoms[0],
pools[2].ReserveCoinDenoms[1],
pools[3].ReserveCoinDenoms[0],
pools[3].ReserveCoinDenoms[1],
}
// request global prices only once to prevent from overuse
globalPrices, err := client.Market.GetGlobalPrices(ctx, targetDenoms)
if err != nil {
return fmt.Errorf("failed to get pool prices: %s", err)
}
for i := 0; i < frequency; i++ {
log.Printf("🔥 Trading Volume Bot🔥 %d out of %d frequency", i+1, frequency)
var txBytes [][]byte
for j, p := range pools {
denomX := p.ReserveCoinDenoms[0]
denomY := p.ReserveCoinDenoms[1]
globalPriceX := globalPrices[2*j]
globalPriceY := globalPrices[2*j+1]
reserveAmtX, reserveAmtY, err := client.GRPC.GetPoolReserves(ctx, []string{denomX, denomY})
if err != nil {
return fmt.Errorf("failed to get pool price: %s", err)
}
reservePoolPrice := reserveAmtX.Quo(reserveAmtY)
globalPrice := globalPriceY.Quo(globalPriceX)
priceDiff := globalPrice.Quo(reservePoolPrice).Sub(sdk.NewDec(1))
log.Println("----------------------------------------------------------------")
log.Printf("| denomX: %s globalPriceX: %s\n", denomX, globalPriceX.String())
log.Printf("| denomY: %s globalPriceY: %s\n", denomY, globalPriceY.String())
poolCreator := accAddr
poolId := p.GetPoolId()
swapTypeId := uint32(1)
swapFeeRate := sdk.NewDecWithPrec(3, 3)
// swap denomY for denomX (buy)
orderAmountX := sdk.NewDec(sendAmount / 4).Quo(globalPriceX).Mul(sdk.NewDec(1_000_000))
offerCoinX := sdk.NewCoin(denomX, orderAmountX.RoundInt()) // truncated
demandCoinDenomX := denomY // the other side of pair
orderPriceX := reservePoolPrice.Mul(sdk.MustNewDecFromStr("1.05")) // multiply pool price by 1.2 to buy higher price
// swap denomX for denomY (sell)
orderAmountY := sdk.NewDec(sendAmount / 4).Quo(globalPriceY).Mul(sdk.NewDec(1_000_000))
offerCoinY := sdk.NewCoin(denomY, orderAmountY.RoundInt()) // truncated
demandCoinDenomY := denomX // the other side of pair
orderPriceY := reservePoolPrice.Mul(sdk.MustNewDecFromStr("0.95")) // multiply pool price by 0.8 to sell cheaper price
buyMsg, err := tx.MsgSwap(poolCreator, poolId, swapTypeId, offerCoinX, demandCoinDenomX, orderPriceX, swapFeeRate)
if err != nil {
return fmt.Errorf("failed to create swap message: %s", err)
}
buyMsg2, err := tx.MsgSwap(poolCreator, poolId, swapTypeId, offerCoinX, demandCoinDenomX, orderPriceX, swapFeeRate)
if err != nil {
return fmt.Errorf("failed to create swap message: %s", err)
}
sellMsg, err := tx.MsgSwap(poolCreator, poolId, swapTypeId, offerCoinY, demandCoinDenomY, orderPriceY, swapFeeRate)
if err != nil {
return fmt.Errorf("failed to create swap message: %s", err)
}
sellMsg2, err := tx.MsgSwap(poolCreator, poolId, swapTypeId, offerCoinY, demandCoinDenomY, orderPriceY, swapFeeRate)
if err != nil {
return fmt.Errorf("failed to create swap message: %s", err)
}
txByte, err := transaction.Sign(ctx, accSeq, accNum, privKey, buyMsg, buyMsg2, sellMsg, sellMsg2)
if err != nil {
return fmt.Errorf("failed to sign swap message: %s", err)
}
// increase sequence
accSeq = accSeq + 1
// decrease the remaining target amount of trading volume
remainingAmountPerHour = remainingAmountPerHour - sendAmount
txBytes = append(txBytes, txByte)
log.Println("----------------------------------------------------------------[Common] [", j+1, " out of 4 pools]")
log.Printf("| poolCreator: %s\n", poolCreator)
log.Printf("| poolId: %d\n", poolId)
log.Printf("| swapTypeId: %d\n", swapTypeId)
log.Printf("| swapFeeRate: %s\n", swapFeeRate.String())
log.Printf("| ✨ reservePoolPrice: %s\n", reservePoolPrice.String())
log.Printf("| ✨ globalPrice: %s\n", globalPrice.String())
log.Printf("| ✨ priceDiff : %s\n", priceDiff.String())
log.Printf("| ✨ remainingAmountPerHour: %d\n", remainingAmountPerHour)
log.Println("----------------------------------------------------------------[Swap Msg]")
log.Printf("| ✅ globalPriceX: %s\n", globalPriceX.String())
log.Printf("| ✅ orderAmountX: %s\n", orderAmountX.String())
log.Printf("| ✅ offerCoinX: %s\n", offerCoinX.String())
log.Printf("| ✅ demandCoinDenomX: %s\n", demandCoinDenomX)
log.Printf("| ✅ orderPriceX: %s\n", orderPriceX)
log.Println("----------------------------------------------------------------[Swap Msg]")
log.Printf("| ✅ globalPriceY: %s\n", globalPriceY.String())
log.Printf("| ✅ orderAmountY: %s\n", orderAmountY.String())
log.Printf("| ✅ offerCoinY: %s\n", offerCoinY.String())
log.Printf("| ✅ demandCoinDenomY: %s\n", demandCoinDenomY)
log.Printf("| ✅ orderPriceY: %s\n", orderPriceY)
}
for k, txByte := range txBytes {
resp, err := transaction.BroadcastTx(ctx, txByte)
if err != nil {
return fmt.Errorf("failed to broadcast transaction: %s", err)
}
log.Println("----------------------------------------------------------------[Sending Tx] [", k+1, " out of 4 pools]")
log.Printf("| TxHash: %s\n", resp.GetTxResponse().TxHash)
log.Printf("| Height: %d\n", resp.GetTxResponse().Height)
}
log.Println("----------------------------------------------------------------")
fmt.Println("")
fmt.Println("")
time.Sleep(1 * time.Second)
}
return nil
}