|
| 1 | +import { Simulator } from "../../src"; |
| 2 | +import { swapSimulationResultsExample } from "./data/simulation-example"; |
| 3 | + |
| 4 | +describe("Simulator", () => { |
| 5 | + it("should process trace data for ERC20, Native, ERC1155 and ERC721", async () => { |
| 6 | + const debugTrace = async () => { |
| 7 | + return [swapSimulationResultsExample.result as any]; |
| 8 | + }; |
| 9 | + const simulator = new Simulator([{ chainId: 1, debugTrace }]); |
| 10 | + |
| 11 | + const result = await simulator.simulate({ |
| 12 | + from: "0x377e19e0D6525f5fABd565bc47dc4e5FC8Bafb01", |
| 13 | + to: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 14 | + calldata: "0x", |
| 15 | + gas: 1, |
| 16 | + value: "0x82F79CD9000", |
| 17 | + chainId: 1, |
| 18 | + }); |
| 19 | + |
| 20 | + expect(result).toEqual({ |
| 21 | + transfers: [ |
| 22 | + { |
| 23 | + address: "0x0000000000000000000000000000000000000000", |
| 24 | + type: "NATIVE", |
| 25 | + from: "0x377e19e0d6525f5fabd565bc47dc4e5fc8bafb01", |
| 26 | + to: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 27 | + amounts: ["9000000000000"], |
| 28 | + id: "", |
| 29 | + }, |
| 30 | + { |
| 31 | + address: "0x0000000000000000000000000000000000000000", |
| 32 | + type: "NATIVE", |
| 33 | + from: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 34 | + to: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", |
| 35 | + amounts: ["9000000000000"], |
| 36 | + id: "", |
| 37 | + }, |
| 38 | + { |
| 39 | + address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", |
| 40 | + type: "ERC20", |
| 41 | + from: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 42 | + to: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f", |
| 43 | + amounts: ["9000000000000"], |
| 44 | + }, |
| 45 | + { |
| 46 | + address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", |
| 47 | + type: "ERC721", |
| 48 | + from: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 49 | + to: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f", |
| 50 | + id: "9000000000000", |
| 51 | + }, |
| 52 | + { |
| 53 | + address: "0x6b175474e89094c44da98b954eedeac495271d0f", |
| 54 | + type: "ERC1155", |
| 55 | + from: "0x377e19e0d6525f5fabd565bc47dc4e5fc8bafb01", |
| 56 | + to: "0x377e19e0d6525f5fabd565bc47dc4e5fc8bafb01", |
| 57 | + operator: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f", |
| 58 | + amounts: ["16404456013677256"], |
| 59 | + id: "16404456013677256", |
| 60 | + }, |
| 61 | + { |
| 62 | + address: "0x6b175474e89094c44da98b954eedeac495271d0f", |
| 63 | + type: "ERC1155", |
| 64 | + from: "0x377e19e0d6525f5fabd565bc47dc4e5fc8bafb01", |
| 65 | + to: "0x377e19e0d6525f5fabd565bc47dc4e5fc8bafb01", |
| 66 | + operator: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f", |
| 67 | + amounts: ["1"], |
| 68 | + id: "1", |
| 69 | + }, |
| 70 | + ], |
| 71 | + approvals: [ |
| 72 | + { |
| 73 | + address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", |
| 74 | + type: "ERC721", |
| 75 | + owner: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 76 | + operator: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 77 | + approveForAllStatus: true, |
| 78 | + }, |
| 79 | + { |
| 80 | + address: "0x6b175474e89094c44da98b954eedeac495271d0f", |
| 81 | + type: "ERC20", |
| 82 | + owner: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f", |
| 83 | + operator: "0x377e19e0d6525f5fabd565bc47dc4e5fc8bafb01", |
| 84 | + amounts: ["16404456013677256"], |
| 85 | + }, |
| 86 | + { |
| 87 | + address: "0x6b175474e89094c44da98b954eedeac495271d0f", |
| 88 | + type: "ERC721", |
| 89 | + owner: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f", |
| 90 | + operator: "0x377e19e0d6525f5fabd565bc47dc4e5fc8bafb01", |
| 91 | + id: "16404456013677256", |
| 92 | + }, |
| 93 | + ], |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should throw if there's a simulation error", async () => { |
| 98 | + const debugTrace = async () => { |
| 99 | + return [{ error: {}, output: "0x0000" } as any]; |
| 100 | + }; |
| 101 | + const simulator = new Simulator([{ chainId: 1, debugTrace }]); |
| 102 | + |
| 103 | + await expect( |
| 104 | + simulator.simulate({ |
| 105 | + from: "0x377e19e0D6525f5fABd565bc47dc4e5FC8Bafb01", |
| 106 | + to: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 107 | + calldata: "0x", |
| 108 | + gas: 1, |
| 109 | + value: "0x82F79CD9000", |
| 110 | + chainId: 1, |
| 111 | + }) |
| 112 | + ).rejects.toThrow("Transaction will fail with error: '0x0000'"); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should throw if chain is not supported", async () => { |
| 116 | + const debugTrace = async () => { |
| 117 | + return [{ error: {} } as any]; |
| 118 | + }; |
| 119 | + const simulator = new Simulator([{ chainId: 1, debugTrace }]); |
| 120 | + |
| 121 | + await expect( |
| 122 | + simulator.simulate({ |
| 123 | + from: "0x377e19e0D6525f5fABd565bc47dc4e5FC8Bafb01", |
| 124 | + to: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 125 | + calldata: "0x", |
| 126 | + gas: 1, |
| 127 | + value: "0x82F79CD9000", |
| 128 | + chainId: 2, |
| 129 | + }) |
| 130 | + ).rejects.toThrow("unsupported chain_id: 2"); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should throw if 'chainId' is undefined", async () => { |
| 134 | + const debugTrace = async () => { |
| 135 | + return [{} as any]; |
| 136 | + }; |
| 137 | + const simulator = new Simulator([{ chainId: 1, debugTrace }]); |
| 138 | + |
| 139 | + await expect( |
| 140 | + simulator.simulate({ |
| 141 | + from: "0x377e19e0D6525f5fABd565bc47dc4e5FC8Bafb01", |
| 142 | + to: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 143 | + calldata: "0x", |
| 144 | + gas: 1, |
| 145 | + value: "0x82F79CD9000", |
| 146 | + chainId: undefined as any, |
| 147 | + }) |
| 148 | + ).rejects.toThrow("undefined chainId param"); |
| 149 | + }); |
| 150 | + |
| 151 | + it("should throw if 'from' is undefined", async () => { |
| 152 | + const debugTrace = async () => { |
| 153 | + return [{} as any]; |
| 154 | + }; |
| 155 | + const simulator = new Simulator([{ chainId: 1, debugTrace }]); |
| 156 | + |
| 157 | + await expect( |
| 158 | + simulator.simulate({ |
| 159 | + from: "", |
| 160 | + to: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 161 | + calldata: "0x", |
| 162 | + gas: 1, |
| 163 | + value: "0x82F79CD9000", |
| 164 | + chainId: 1, |
| 165 | + }) |
| 166 | + ).rejects.toThrow("undefined from param"); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should throw if 'to' is undefined", async () => { |
| 170 | + const debugTrace = async () => { |
| 171 | + return [{} as any]; |
| 172 | + }; |
| 173 | + const simulator = new Simulator([{ chainId: 1, debugTrace }]); |
| 174 | + |
| 175 | + await expect( |
| 176 | + simulator.simulate({ |
| 177 | + to: "", |
| 178 | + from: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 179 | + calldata: "0x", |
| 180 | + gas: 1, |
| 181 | + value: "0x82F79CD9000", |
| 182 | + chainId: 1, |
| 183 | + }) |
| 184 | + ).rejects.toThrow("undefined to param"); |
| 185 | + }); |
| 186 | + |
| 187 | + it("should throw if 'calldata' is undefined", async () => { |
| 188 | + const debugTrace = async () => { |
| 189 | + return [{} as any]; |
| 190 | + }; |
| 191 | + const simulator = new Simulator([{ chainId: 1, debugTrace }]); |
| 192 | + |
| 193 | + await expect( |
| 194 | + simulator.simulate({ |
| 195 | + to: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 196 | + from: "0x1111111254eeb25477b68fb85ed929f73a960582", |
| 197 | + calldata: "", |
| 198 | + gas: 1, |
| 199 | + value: "0x82F79CD9000", |
| 200 | + chainId: 1, |
| 201 | + }) |
| 202 | + ).rejects.toThrow("undefined calldata param"); |
| 203 | + }); |
| 204 | +}); |
0 commit comments