-
Notifications
You must be signed in to change notification settings - Fork 2
/
liquidity.go
189 lines (151 loc) · 5.85 KB
/
liquidity.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
package tx
import (
"context"
"fmt"
"math/rand"
"github.com/b-harvest/modules-test-tool/client"
liquiditytypes "github.com/gravity-devs/liquidity/x/liquidity/types"
sdkclienttx "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
)
// Transaction is an object that has common fields when signing transaction.
type Transaction struct {
Client *client.Client `json:"client"`
ChainID string `json:"chain_id"`
GasLimit uint64 `json:"gas_limit"`
Fees sdktypes.Coins `json:"fees"`
Memo string `json:"memo"`
}
// NewTransaction returns new Transaction object.
func NewTransaction(client *client.Client, chainID string, gasLimit uint64, fees sdktypes.Coins, memo string) *Transaction {
return &Transaction{
Client: client,
ChainID: chainID,
GasLimit: gasLimit,
Fees: fees,
Memo: memo,
}
}
// MsgCreatePool creates create pool message and returns MsgCreatePool transaction message.
func MsgCreatePool(poolCreator string, poolTypeId uint32, depositCoins sdktypes.Coins) (sdktypes.Msg, error) {
accAddr, err := sdktypes.AccAddressFromBech32(poolCreator)
if err != nil {
return &liquiditytypes.MsgCreatePool{}, err
}
msg := liquiditytypes.NewMsgCreatePool(accAddr, poolTypeId, depositCoins)
if err := msg.ValidateBasic(); err != nil {
return &liquiditytypes.MsgCreatePool{}, err
}
return msg, nil
}
// MsgDeposit creates deposit message and returns MsgDeposit transaction message.
func MsgDeposit(poolCreator string, poolId uint64, depositCoins sdktypes.Coins) (sdktypes.Msg, error) {
accAddr, err := sdktypes.AccAddressFromBech32(poolCreator)
if err != nil {
return &liquiditytypes.MsgDepositWithinBatch{}, err
}
msg := liquiditytypes.NewMsgDepositWithinBatch(accAddr, poolId, depositCoins)
if err := msg.ValidateBasic(); err != nil {
return &liquiditytypes.MsgDepositWithinBatch{}, err
}
return msg, nil
}
// MsgWithdraw creates withdraw message and returns MsgWithdraw transaction message.
func MsgWithdraw(poolCreator string, poolId uint64, poolCoin sdktypes.Coin) (sdktypes.Msg, error) {
accAddr, err := sdktypes.AccAddressFromBech32(poolCreator)
if err != nil {
return &liquiditytypes.MsgWithdrawWithinBatch{}, err
}
msg := liquiditytypes.NewMsgWithdrawWithinBatch(accAddr, poolId, poolCoin)
if err := msg.ValidateBasic(); err != nil {
return &liquiditytypes.MsgWithdrawWithinBatch{}, err
}
return msg, nil
}
// MsgSwap creates swap message and returns MsgWithdraw MsgSwap message.
func MsgSwap(poolCreator string, poolId uint64, swapTypeId uint32, offerCoin sdktypes.Coin,
demandCoinDenom string, orderPrice sdktypes.Dec, swapFeeRate sdktypes.Dec) (sdktypes.Msg, error) {
accAddr, err := sdktypes.AccAddressFromBech32(poolCreator)
if err != nil {
return &liquiditytypes.MsgSwapWithinBatch{}, err
}
msg := liquiditytypes.NewMsgSwapWithinBatch(accAddr, poolId, swapTypeId, offerCoin, demandCoinDenom, orderPrice, swapFeeRate)
if err := msg.ValidateBasic(); err != nil {
return &liquiditytypes.MsgSwapWithinBatch{}, err
}
return msg, nil
}
// CreateSwapBot creates a bot that makes multiple swaps which increases and decreases
func (t *Transaction) CreateSwapBot(ctx context.Context, poolCreator string,
poolId uint64, offerCoin sdktypes.Coin, demandCoinDenom string, msgNum int) ([]sdktypes.Msg, error) {
pool, err := t.Client.GRPC.GetPool(ctx, poolId)
if err != nil {
return []sdktypes.Msg{}, err
}
reserveCoins := sdktypes.NewCoins()
for _, denom := range pool.ReserveCoinDenoms {
coin, err := t.Client.GRPC.GetBalance(ctx, pool.GetReserveAccount().String(), denom)
if err != nil {
return []sdktypes.Msg{}, err
}
reserveCoins = reserveCoins.Add(*coin)
}
orderPrice := reserveCoins.AmountOf(pool.ReserveCoinDenoms[0]).ToDec().Quo(reserveCoins.AmountOf(pool.ReserveCoinDenoms[1]).ToDec())
var msgs []sdktypes.Msg
// randomize order price
for i := 0; i < msgNum; i++ {
random := sdktypes.NewDec(int64(rand.Intn(2)))
orderPricePercentage := orderPrice.Mul(random.Quo(sdktypes.NewDec(100)))
orderPrice = orderPrice.Sub(orderPricePercentage)
msg, err := MsgSwap(poolCreator, poolId, uint32(1), offerCoin, demandCoinDenom, orderPrice, sdktypes.NewDecWithPrec(3, 3))
if err != nil {
return []sdktypes.Msg{}, err
}
msgs = append(msgs, msg)
}
return msgs, nil
}
// Sign signs message(s) with the account's private key and braodacasts the message(s).
func (t *Transaction) Sign(ctx context.Context, accSeq uint64, accNum uint64, privKey *secp256k1.PrivKey, msgs ...sdktypes.Msg) ([]byte, error) {
txBuilder := t.Client.CliCtx.TxConfig.NewTxBuilder()
if err := txBuilder.SetMsgs(msgs...); err != nil {
return nil, err
}
txBuilder.SetGasLimit(t.GasLimit)
txBuilder.SetFeeAmount(t.Fees)
txBuilder.SetMemo(t.Memo)
signMode := t.Client.CliCtx.TxConfig.SignModeHandler().DefaultMode()
sigV2 := signing.SignatureV2{
PubKey: privKey.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: signMode,
Signature: nil,
},
Sequence: accSeq,
}
err := txBuilder.SetSignatures(sigV2)
if err != nil {
return nil, fmt.Errorf("failed to set signatures: %s", err)
}
signerData := authsigning.SignerData{
ChainID: t.ChainID,
AccountNumber: accNum,
Sequence: accSeq,
}
sigV2, err = sdkclienttx.SignWithPrivKey(signMode, signerData, txBuilder, privKey, t.Client.CliCtx.TxConfig, accSeq)
if err != nil {
return nil, fmt.Errorf("failed to sign with private key: %s", err)
}
err = txBuilder.SetSignatures(sigV2)
if err != nil {
return nil, fmt.Errorf("failed to set signatures: %s", err)
}
txByte, err := t.Client.CliCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
if err != nil {
return nil, fmt.Errorf("failed to encode tx and get raw tx data: %s", err)
}
return txByte, nil
}