|
| 1 | +package fuzzing |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 7 | + "github.com/sei-protocol/sei-chain/x/dex/types" |
| 8 | +) |
| 9 | + |
| 10 | +const BaselinePrice = 1234.56 |
| 11 | + |
| 12 | +var ValidAccountCorpus = []string{ |
| 13 | + "sei1h9yjz89tl0dl6zu65dpxcqnxfhq60wxx8s5kag", |
| 14 | + "sei1c2q6xm0x684rshrnlg898zm3vpwz92pcfhgmws", |
| 15 | + "sei1ewxvf5a9wq9zk5nurtl6m9yfxpnhyp7s7uk5sl", |
| 16 | + "sei1lllgxa294pshcsrsrteh7sj6ey0zqgty30sl8a", |
| 17 | + "sei1vhn2p3xavts9swus27zz3n56tz98g3f6unavs2", |
| 18 | + "sei1jpkqjfydghgrc23chmnj52xln0muz09j5huhkt", |
| 19 | + "sei1k98zjg7scsmk6d4ye8hhrv3an6ppykvt660736", |
| 20 | + "sei1wxpqjzdmtjm6gwg6555n0a0aqglrvnp3pqh9hs", |
| 21 | + "sei1yuyyr3xg7jhk7pjkrp4j6h88t7gv35e29pfvmf", |
| 22 | + "sei1vjgdad5v2euf98nj3pwg5d8agflr384k0eks43", |
| 23 | +} |
| 24 | + |
| 25 | +var AccountCorpus = append([]string{ |
| 26 | + "invalid", |
| 27 | +}, ValidAccountCorpus...) |
| 28 | + |
| 29 | +var ContractCorpus = []string{ |
| 30 | + "invalid", |
| 31 | + "sei14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sh9m79m", |
| 32 | + "sei1nc5tatafv6eyq7llkr2gv50ff9e22mnf70qgjlv737ktmt4eswrqms7u8a", |
| 33 | +} |
| 34 | + |
| 35 | +var ( |
| 36 | + MicroTick = sdk.MustNewDecFromStr("0.000001") |
| 37 | + MilliTick = sdk.MustNewDecFromStr("0.001") |
| 38 | + WholeTick = sdk.OneDec() |
| 39 | + PairCorpus = []types.Pair{ |
| 40 | + {}, |
| 41 | + {PriceDenom: "SEI"}, |
| 42 | + {AssetDenom: "ATOM"}, |
| 43 | + { |
| 44 | + PriceDenom: "SEI", |
| 45 | + AssetDenom: "ATOM", |
| 46 | + }, |
| 47 | + { |
| 48 | + PriceDenom: "SEI", |
| 49 | + AssetDenom: "ATOM", |
| 50 | + Ticksize: &MicroTick, |
| 51 | + }, |
| 52 | + { |
| 53 | + PriceDenom: "SEI", |
| 54 | + AssetDenom: "ATOM", |
| 55 | + Ticksize: &MilliTick, |
| 56 | + }, |
| 57 | + { |
| 58 | + PriceDenom: "SEI", |
| 59 | + AssetDenom: "ATOM", |
| 60 | + Ticksize: &WholeTick, |
| 61 | + }, |
| 62 | + { |
| 63 | + PriceDenom: "USDC", |
| 64 | + AssetDenom: "ATOM", |
| 65 | + }, |
| 66 | + { |
| 67 | + PriceDenom: "USDC", |
| 68 | + AssetDenom: "ATOM", |
| 69 | + Ticksize: &MicroTick, |
| 70 | + }, |
| 71 | + { |
| 72 | + PriceDenom: "USDC", |
| 73 | + AssetDenom: "ATOM", |
| 74 | + Ticksize: &MilliTick, |
| 75 | + }, |
| 76 | + { |
| 77 | + PriceDenom: "USDC", |
| 78 | + AssetDenom: "ATOM", |
| 79 | + Ticksize: &WholeTick, |
| 80 | + }, |
| 81 | + } |
| 82 | +) |
| 83 | + |
| 84 | +func GetAccount(i int) string { |
| 85 | + ui := uint64(i) % uint64(len(AccountCorpus)) |
| 86 | + return AccountCorpus[int(ui)] |
| 87 | +} |
| 88 | + |
| 89 | +func GetValidAccount(i int) string { |
| 90 | + ui := uint64(i) % uint64(len(ValidAccountCorpus)) |
| 91 | + return ValidAccountCorpus[int(ui)] |
| 92 | +} |
| 93 | + |
| 94 | +func GetContract(i int) string { |
| 95 | + ui := uint64(i) % uint64(len(ContractCorpus)) |
| 96 | + return ContractCorpus[int(ui)] |
| 97 | +} |
| 98 | + |
| 99 | +func GetPair(i int) types.Pair { |
| 100 | + ui := uint64(i) % uint64(len(PairCorpus)) |
| 101 | + return PairCorpus[int(ui)] |
| 102 | +} |
| 103 | + |
| 104 | +func GetPlacedOrders(direction types.PositionDirection, orderType types.OrderType, pair types.Pair, prices []byte, quantities []byte) []*types.Order { |
| 105 | + // take the shorter slice's length |
| 106 | + if len(prices) < len(quantities) { |
| 107 | + quantities = quantities[:len(prices)] |
| 108 | + } else { |
| 109 | + prices = prices[:len(quantities)] |
| 110 | + } |
| 111 | + res := []*types.Order{} |
| 112 | + for i, price := range prices { |
| 113 | + var priceDec sdk.Dec |
| 114 | + if direction == types.PositionDirection_LONG { |
| 115 | + priceDec = sdk.MustNewDecFromStr(fmt.Sprintf("%f", BaselinePrice+float64(price))) |
| 116 | + } else { |
| 117 | + priceDec = sdk.MustNewDecFromStr(fmt.Sprintf("%f", BaselinePrice-float64(price))) |
| 118 | + } |
| 119 | + quantity := sdk.NewDec(int64(quantities[i])) |
| 120 | + res = append(res, &types.Order{ |
| 121 | + Id: uint64(i), |
| 122 | + Status: types.OrderStatus_PLACED, |
| 123 | + Price: priceDec, |
| 124 | + Quantity: quantity, |
| 125 | + PositionDirection: direction, |
| 126 | + OrderType: orderType, |
| 127 | + PriceDenom: pair.PriceDenom, |
| 128 | + AssetDenom: pair.AssetDenom, |
| 129 | + }) |
| 130 | + } |
| 131 | + return res |
| 132 | +} |
| 133 | + |
| 134 | +func GetOrderBookEntries(buy bool, priceDenom string, assetDenom string, entryWeights []byte, allAccountIndices []byte, allWeights []byte) []types.OrderBook { |
| 135 | + res := []types.OrderBook{} |
| 136 | + totalPriceWeights := uint64(0) |
| 137 | + for _, entryWeight := range entryWeights { |
| 138 | + totalPriceWeights += uint64(entryWeight) |
| 139 | + } |
| 140 | + sliceStartAccnt, sliceStartWeights := 0, 0 |
| 141 | + cumWeights := uint64(0) |
| 142 | + for i, entryWeight := range entryWeights { |
| 143 | + var price sdk.Dec |
| 144 | + if buy { |
| 145 | + price = sdk.MustNewDecFromStr(fmt.Sprintf("%f", BaselinePrice-float64(i))) |
| 146 | + } else { |
| 147 | + price = sdk.MustNewDecFromStr(fmt.Sprintf("%f", BaselinePrice+float64(i))) |
| 148 | + } |
| 149 | + cumWeights += uint64(entryWeight) |
| 150 | + nextSliceStartAccnt := int(cumWeights * uint64(len(allAccountIndices)) / totalPriceWeights) |
| 151 | + nextSliceStartWeights := int(cumWeights * uint64(len(allWeights)) / totalPriceWeights) |
| 152 | + entry := types.OrderEntry{ |
| 153 | + Price: price, |
| 154 | + Quantity: sdk.NewDec(int64(uint64((entryWeight)))), |
| 155 | + PriceDenom: priceDenom, |
| 156 | + AssetDenom: assetDenom, |
| 157 | + Allocations: GetAllocations( |
| 158 | + int64(uint64((entryWeight))), |
| 159 | + allAccountIndices[sliceStartAccnt:nextSliceStartAccnt], |
| 160 | + allWeights[sliceStartWeights:nextSliceStartWeights], |
| 161 | + ), |
| 162 | + } |
| 163 | + if buy { |
| 164 | + res = append(res, &types.LongBook{ |
| 165 | + Price: price, |
| 166 | + Entry: &entry, |
| 167 | + }) |
| 168 | + } else { |
| 169 | + res = append(res, &types.ShortBook{ |
| 170 | + Price: price, |
| 171 | + Entry: &entry, |
| 172 | + }) |
| 173 | + } |
| 174 | + sliceStartAccnt, sliceStartWeights = nextSliceStartAccnt, nextSliceStartWeights |
| 175 | + } |
| 176 | + return res |
| 177 | +} |
| 178 | + |
| 179 | +func GetAllocations(totalQuantity int64, accountIndices []byte, weights []byte) []*types.Allocation { |
| 180 | + // take the shorter slice's length |
| 181 | + if len(accountIndices) < len(weights) { |
| 182 | + weights = weights[:len(accountIndices)] |
| 183 | + } else { |
| 184 | + accountIndices = accountIndices[:len(weights)] |
| 185 | + } |
| 186 | + // dedupe and aggregate |
| 187 | + aggregatedAccountsToWeights := map[string]uint64{} |
| 188 | + totalWeight := uint64(0) |
| 189 | + for i, accountIdx := range accountIndices { |
| 190 | + account := GetValidAccount(int(accountIdx)) |
| 191 | + weight := uint64(weights[i]) |
| 192 | + if old, ok := aggregatedAccountsToWeights[account]; !ok { |
| 193 | + aggregatedAccountsToWeights[account] = weight |
| 194 | + } else { |
| 195 | + aggregatedAccountsToWeights[account] = old + weight |
| 196 | + } |
| 197 | + totalWeight += weight |
| 198 | + } |
| 199 | + |
| 200 | + quantityDec := sdk.NewDec(totalQuantity) |
| 201 | + totalWeightDec := sdk.NewDec(int64(totalWeight)) |
| 202 | + res := []*types.Allocation{} |
| 203 | + for account, weight := range aggregatedAccountsToWeights { |
| 204 | + res = append(res, &types.Allocation{ |
| 205 | + Account: account, |
| 206 | + Quantity: quantityDec.Mul(sdk.NewDec(int64(weight))).Quo(totalWeightDec), |
| 207 | + }) |
| 208 | + } |
| 209 | + return res |
| 210 | +} |
0 commit comments