-
Notifications
You must be signed in to change notification settings - Fork 829
/
Copy pathtypes.go
210 lines (183 loc) · 6.57 KB
/
types.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
package main
import (
"fmt"
"math/rand"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/sei-protocol/sei-chain/utils"
)
const (
Bank string = "bank"
EVM string = "evm"
ERC20 string = "erc20"
ERC721 string = "erc721"
CollectRewards string = "collect_rewards"
DistributeRewards string = "distribute_rewards"
FailureBankMalformed string = "failure_bank_malformed"
FailureBankInvalid string = "failure_bank_invalid"
FailureDexMalformed string = "failure_dex_malformed"
FailureDexInvalid string = "failure_dex_invalid"
Dex string = "dex"
Staking string = "staking"
Tokenfactory string = "tokenfactory"
Limit string = "limit"
Market string = "market"
WasmMintNft string = "wasm_mint_nft"
UNIV2 string = "univ2"
Vortex string = "vortex"
WasmInstantiate string = "wasm_instantiate"
)
type EVMAddresses struct {
ERC20 common.Address
ERC721 common.Address
UniV2Swapper common.Address
}
type Config struct {
ChainID string `json:"chain_id"`
GrpcEndpoints string `json:"grpc_endpoints"`
EvmRpcEndpoints string `json:"evm_rpc_endpoints"`
BlockchainEndpoint string `json:"blockchain_endpoint"`
NodeURI string `json:"node_uri"`
TargetTps uint64 `json:"target_tps"`
MaxAccounts uint64 `json:"max_accounts"`
MsgsPerTx uint64 `json:"msgs_per_tx"`
MessageType string `json:"message_type"`
PriceDistr NumericDistribution `json:"price_distribution"`
QuantityDistr NumericDistribution `json:"quantity_distribution"`
MsgTypeDistr MsgTypeDistribution `json:"message_type_distribution"`
WasmMsgTypes WasmMessageTypes `json:"wasm_msg_types"`
ContractDistr ContractDistributions `json:"contract_distribution"`
PerMessageConfigs MessageConfigs `json:"message_configs"`
MetricsPort uint64 `json:"metrics_port"`
TLS bool `json:"tls"`
// These are dynamically set at startup
EVMAddresses *EVMAddresses
}
func (c *Config) EVMRpcEndpoint() string {
endpoints := strings.Split(c.EvmRpcEndpoints, ",")
return endpoints[0]
}
func (c *Config) ContainsAnyMessageTypes(types ...string) bool {
mTypes := strings.Split(c.MessageType, ",")
for _, t := range types {
for _, mt := range mTypes {
if mt == t {
return true
}
}
}
return false
}
type EncodingConfig struct {
InterfaceRegistry types.InterfaceRegistry
// NOTE: this field will be renamed to Codec
Marshaler codec.Codec
TxConfig client.TxConfig
Amino *codec.LegacyAmino
}
// MessageConfig is the configuration for a message
// Specify the gas and fee for the message type
type MessageTypeConfig struct {
Gas uint64 `json:"gas"`
Fee uint64 `json:"fee"`
}
type MessageConfigs map[string]MessageTypeConfig
type NumericDistribution struct {
Min sdk.Dec `json:"min"`
Max sdk.Dec `json:"max"`
NumDistinct int64 `json:"number_of_distinct_values"`
}
func (d *NumericDistribution) Sample() sdk.Dec {
steps := sdk.NewDec(rand.Int63n(d.NumDistinct))
return d.Min.Add(d.Max.Sub(d.Min).QuoInt64(d.NumDistinct).Mul(steps))
}
// Invalid numeric distribution sample
func (d *NumericDistribution) InvalidSample() sdk.Dec {
steps := sdk.NewDec(rand.Int63n(d.NumDistinct))
if rand.Float64() < 0.5 {
return d.Min.Add(d.Max.Sub(d.Min).QuoInt64(d.NumDistinct).Mul(steps))
}
return d.Max.Add(d.Max.Sub(d.Min).QuoInt64(d.NumDistinct).Mul(steps))
}
type DexMsgTypeDistribution struct {
LimitOrderPct sdk.Dec `json:"limit_order_percentage"`
MarketOrderPct sdk.Dec `json:"market_order_percentage"`
}
type StakingMsgTypeDistribution struct {
DelegatePct sdk.Dec `json:"delegate_percentage"`
UndelegatePct sdk.Dec `json:"undelegate_percentage"`
BeginRedelegatePct sdk.Dec `json:"begin_redelegate_percentage"`
}
type MsgTypeDistribution struct {
Dex DexMsgTypeDistribution `json:"dex"`
Staking StakingMsgTypeDistribution `json:"staking"`
}
// Struct containing contract address
// For a specific wasm message type.
// TODO: Abstract interface for any wasm type + execute msg
type WasmMessageTypes struct {
MintNftType WasmMintNftType `json:"wasm_mint_nft"`
Vortex VortexContract `json:"vortex"`
Instantiate WasmInstantiateType `json:"instantiate"`
}
type WasmMintNftType struct {
ContractAddr string `json:"contract_address"`
}
func (d *MsgTypeDistribution) SampleDexMsgs() string {
if !d.Dex.LimitOrderPct.Add(d.Dex.MarketOrderPct).Equal(sdk.OneDec()) {
panic("Distribution percentages must add up to 1")
}
randNum := sdk.MustNewDecFromStr(fmt.Sprintf("%f", rand.Float64()))
if randNum.LT(d.Dex.LimitOrderPct) {
return Limit
}
return Market
}
func (d *MsgTypeDistribution) SampleStakingMsgs() string {
if !d.Staking.DelegatePct.Add(d.Staking.UndelegatePct).Add(d.Staking.BeginRedelegatePct).Equal(sdk.OneDec()) {
panic("Distribution percentages must add up to 1")
}
randNum := sdk.MustNewDecFromStr(fmt.Sprintf("%f", rand.Float64()))
if randNum.LT(d.Staking.DelegatePct) {
return "delegate"
} else if randNum.LT(d.Staking.DelegatePct.Add(d.Staking.UndelegatePct)) {
return "undelegate"
}
return "begin_redelegate"
}
type ContractDistributions []ContractDistribution
func (d *ContractDistributions) Sample() string {
if !utils.Reduce(*d, func(i ContractDistribution, o sdk.Dec) sdk.Dec { return o.Add(i.Percentage) }, sdk.ZeroDec()).Equal(sdk.OneDec()) {
panic("Distribution percentages must add up to 1")
}
randNum := sdk.MustNewDecFromStr(fmt.Sprintf("%f", rand.Float64()))
cumPct := sdk.ZeroDec()
for _, dist := range *d {
cumPct = cumPct.Add(dist.Percentage)
if randNum.LTE(cumPct) {
return dist.ContractAddr
}
}
panic("this should never be triggered")
}
type ContractDistribution struct {
ContractAddr string `json:"contract_address"`
Percentage sdk.Dec `json:"percentage"`
}
type VortexContract struct {
ContractAddr string `json:"contract_address"`
NumOrdersPerTx int64 `json:"num_orders_per_tx"`
}
type WasmInstantiateType struct {
CodeID uint64 `json:"code_id"`
Payload string `json:"payload"`
}
type SignedTx struct {
TxBytes []byte
EvmTx *ethtypes.Transaction
}