-
Notifications
You must be signed in to change notification settings - Fork 592
/
main.go
214 lines (183 loc) · 7.28 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
package main
import (
"context"
"fmt"
"log"
"math"
"math/rand"
"os/user"
"strings"
"sync"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ignite/cli/ignite/pkg/cosmosaccount"
"github.com/ignite/cli/ignite/pkg/cosmosclient"
"github.com/osmosis-labs/osmosis/v16/x/concentrated-liquidity/model"
cltypes "github.com/osmosis-labs/osmosis/v16/x/concentrated-liquidity/types"
poolmanagerqueryproto "github.com/osmosis-labs/osmosis/v16/x/poolmanager/client/queryproto"
poolmanagertypes "github.com/osmosis-labs/osmosis/v16/x/poolmanager/types"
)
const (
expectedPoolId uint64 = 1
addressPrefix = "osmo"
localosmosisFromHomePath = "/.osmosisd-local"
consensusFee = "1500uosmo"
denom0 = "uosmo"
denom1 = "uusdc"
tickSpacing int64 = 100
accountNamePrefix = "lo-test"
numPositions = 1_000
minAmountDeposited = int64(1_000_000)
randSeed = 1
maxAmountDeposited = 1_00_000_000
)
var (
defaultAccountName = fmt.Sprintf("%s%d", accountNamePrefix, 1)
defaultMinAmount = sdk.ZeroInt()
accountMutex sync.Mutex
)
func main() {
ctx := context.Background()
clientHome := getClientHomePath()
// Create a Cosmos igniteClient instance
igniteClient, err := cosmosclient.New(
ctx,
cosmosclient.WithAddressPrefix(addressPrefix),
cosmosclient.WithKeyringBackend(cosmosaccount.KeyringTest),
cosmosclient.WithHome(clientHome),
)
if err != nil {
log.Fatal(err)
}
igniteClient.Factory = igniteClient.Factory.WithGas(300000).WithGasAdjustment(1.3).WithFees(consensusFee)
statusResp, err := igniteClient.Status(ctx)
if err != nil {
log.Fatal(err)
}
log.Println("connected to: ", "chain-id", statusResp.NodeInfo.Network, "height", statusResp.SyncInfo.LatestBlockHeight)
// Instantiate a query client
clQueryClient := poolmanagerqueryproto.NewQueryClient(igniteClient.Context())
// Print warnings with common problems
log.Printf("\n\n\nWARNING 1: your localosmosis and client home are assummed to be %s. Run 'osmosisd get-env' and confirm it matches the path you see printed here\n\n\n", clientHome)
log.Printf("\n\n\nWARNING 2: you are attempting to interact with pool id %d.\nConfirm that the pool exists. if this is not the pool you want to interact with, please change the expectedPoolId variable in the code\n\n\n", expectedPoolId)
log.Println("\n\n\nWARNING 3: sometimes the script hangs when just started. In that case, kill it and restart")
// Query pool with id 1 and create new if does not exist.
_, err = clQueryClient.Pool(ctx, &poolmanagerqueryproto.PoolRequest{PoolId: expectedPoolId})
if err != nil {
if !strings.Contains(err.Error(), poolmanagertypes.FailedToFindRouteError{PoolId: expectedPoolId}.Error()) {
log.Fatal(err)
}
createdPoolId := createPool(igniteClient, defaultAccountName)
if createdPoolId != expectedPoolId {
log.Fatalf("created pool id (%d), expected pool id (%d)", createdPoolId, expectedPoolId)
}
}
minTick, maxTick := cltypes.MinTick, cltypes.MaxTick
log.Println(minTick, " ", maxTick)
rand.Seed(randSeed)
for i := 0; i < numPositions; i++ {
var (
// 1 to 9. These are localosmosis keyring test accounts with names such as:
// lo-test1
// lo-test2
// ...
randAccountNum = rand.Intn(8) + 1
accountName = fmt.Sprintf("%s%d", accountNamePrefix, randAccountNum)
// minTick <= lowerTick <= upperTick
lowerTick = roundTickDown(rand.Int63n(maxTick-minTick+1)+minTick, tickSpacing)
// lowerTick <= upperTick <= maxTick
upperTick = roundTickDown(maxTick-rand.Int63n(int64(math.Abs(float64(maxTick-lowerTick)))), tickSpacing)
tokenDesired0 = sdk.NewCoin(denom0, sdk.NewInt(rand.Int63n(maxAmountDeposited)))
tokenDesired1 = sdk.NewCoin(denom1, sdk.NewInt(rand.Int63n(maxAmountDeposited)))
tokensDesired = sdk.NewCoins(tokenDesired0, tokenDesired1)
)
log.Println("creating position: pool id", expectedPoolId, "accountName", accountName, "lowerTick", lowerTick, "upperTick", upperTick, "token0Desired", tokenDesired0, "tokenDesired1", tokenDesired1, "defaultMinAmount", defaultMinAmount)
maxRetries := 100
for j := 0; j < maxRetries; j++ {
amt0, amt1, liquidity := createPosition(igniteClient, expectedPoolId, accountName, lowerTick, upperTick, tokensDesired, defaultMinAmount, defaultMinAmount)
if err == nil {
log.Println("created position: amt0", amt0, "amt1", amt1, "liquidity", liquidity)
break
}
time.Sleep(8 * time.Second)
}
}
}
func createPool(igniteClient cosmosclient.Client, accountName string) uint64 {
msg := &model.MsgCreateConcentratedPool{
Sender: getAccountAddressFromKeyring(igniteClient, accountName),
Denom1: denom0,
Denom0: denom1,
TickSpacing: 1,
SpreadFactor: sdk.ZeroDec(),
}
txResp, err := igniteClient.BroadcastTx(accountName, msg)
if err != nil {
log.Fatal(err)
}
resp := model.MsgCreateConcentratedPoolResponse{}
if err := txResp.Decode(&resp); err != nil {
log.Fatal(err)
}
return resp.PoolID
}
func createPosition(client cosmosclient.Client, poolId uint64, senderKeyringAccountName string, lowerTick int64, upperTick int64, tokensProvided sdk.Coins, tokenMinAmount0, tokenMinAmount1 sdk.Int) (amountCreated0, amountCreated1 sdk.Int, liquidityCreated sdk.Dec) {
accountMutex.Lock() // Lock access to getAccountAddressFromKeyring
senderAddress := getAccountAddressFromKeyring(client, senderKeyringAccountName)
accountMutex.Unlock() // Unlock access to getAccountAddressFromKeyring
msg := &cltypes.MsgCreatePosition{
PoolId: poolId,
Sender: senderAddress,
LowerTick: lowerTick,
UpperTick: upperTick,
TokensProvided: tokensProvided,
TokenMinAmount0: tokenMinAmount0,
TokenMinAmount1: tokenMinAmount1,
}
txResp, err := client.BroadcastTx(senderKeyringAccountName, msg)
if err != nil {
log.Fatal(err)
}
resp := cltypes.MsgCreatePositionResponse{}
if err := txResp.Decode(&resp); err != nil {
log.Fatal(err)
}
return resp.Amount0, resp.Amount1, resp.LiquidityCreated
}
func getAccountAddressFromKeyring(igniteClient cosmosclient.Client, accountName string) string {
account, err := igniteClient.Account(accountName)
if err != nil {
log.Fatal(fmt.Errorf("did not find account with name (%s) in the keyring: %w", accountName, err))
}
address := account.Address(addressPrefix)
if err != nil {
log.Fatal(err)
}
return address
}
func getClientHomePath() string {
currentUser, err := user.Current()
if err != nil {
log.Fatal(err)
return ""
}
return currentUser.HomeDir + localosmosisFromHomePath
}
func roundTickDown(tickIndex int64, tickSpacing int64) int64 {
// Round the tick index down to the nearest tick spacing if the tickIndex is in between authorized tick values
// Note that this is Euclidean modulus.
// The difference from default Go modulus is that Go default results
// in a negative remainder when the dividend is negative.
// Consider example tickIndex = -17, tickSpacing = 10
// tickIndexModulus = tickIndex % tickSpacing = -7
// tickIndexModulus = -7 + 10 = 3
// tickIndex = -17 - 3 = -20
tickIndexModulus := tickIndex % tickSpacing
if tickIndexModulus < 0 {
tickIndexModulus += tickSpacing
}
if tickIndexModulus != 0 {
tickIndex = tickIndex - tickIndexModulus
}
return tickIndex
}