Skip to content

Commit fbdc512

Browse files
authored
Merge pull request #25 from trustwallet/feat/simulator-test-coverage
test(simulator): increase test coverage
2 parents bfe35cb + 55a0b36 commit fbdc512

File tree

3 files changed

+452
-0
lines changed

3 files changed

+452
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
export const swapSimulationResultsExample = {
2+
jsonrpc: "2.0",
3+
id: 1,
4+
result: {
5+
from: "0x377e19e0d6525f5fabd565bc47dc4e5fc8bafb01",
6+
gas: "0x2fa9924",
7+
gasUsed: "0x1bb0e",
8+
to: "0x1111111254eeb25477b68fb85ed929f73a960582",
9+
input:
10+
"0x0502b1c500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000082f79cd90000000000000000000000000000000000000000000000000000030696974acbc900000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000180000000000000003b6d0340c3d03e4f041fd4cd388c549ee2a29a9e5075882fe26b9977",
11+
output: "0x000000000000000000000000000000000000000000000000003a47c40d6dc2c8",
12+
calls: [
13+
{
14+
from: "0x1111111254eeb25477b68fb85ed929f73a960582",
15+
gas: "0x2ee87ed",
16+
gasUsed: "0x1ada",
17+
to: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
18+
input: "0xd0e30db0",
19+
logs: [
20+
{
21+
address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
22+
topics: [
23+
"0x17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c31",
24+
"0x0000000000000000000000001111111254eeb25477b68fb85ed929f73a960582",
25+
"0x0000000000000000000000001111111254eeb25477b68fb85ed929f73a960582",
26+
],
27+
data: "0x0000000000000000000000000000000000000000000000000000000000000001",
28+
},
29+
],
30+
value: "0x82f79cd9000",
31+
type: "CALL",
32+
},
33+
{
34+
from: "0x1111111254eeb25477b68fb85ed929f73a960582",
35+
gas: "0x2ee6c93",
36+
gasUsed: "0x1f7e",
37+
to: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
38+
input:
39+
"0xa9059cbb000000000000000000000000c3d03e4f041fd4cd388c549ee2a29a9e5075882f0000000000000000000000000000000000000000000000000000082f79cd9000",
40+
output: "0x0000000000000000000000000000000000000000000000000000000000000001",
41+
logs: [
42+
{
43+
address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
44+
topics: [
45+
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
46+
"0x0000000000000000000000001111111254eeb25477b68fb85ed929f73a960582",
47+
"0x000000000000000000000000c3d03e4f041fd4cd388c549ee2a29a9e5075882f",
48+
],
49+
data: "0x0000000000000000000000000000000000000000000000000000082f79cd9000",
50+
},
51+
{
52+
address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
53+
topics: [
54+
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
55+
"0x0000000000000000000000001111111254eeb25477b68fb85ed929f73a960582",
56+
"0x000000000000000000000000c3d03e4f041fd4cd388c549ee2a29a9e5075882f",
57+
"0x0000000000000000000000000000000000000000000000000000082f79cd9000",
58+
],
59+
data: "0x0000000000000000000000000000000000000000000000000000082f79cd9000",
60+
},
61+
],
62+
value: "0x0",
63+
type: "CALL",
64+
},
65+
{
66+
from: "0x1111111254eeb25477b68fb85ed929f73a960582",
67+
gas: "0x2ee42c8",
68+
gasUsed: "0x9d5",
69+
to: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f",
70+
input: "0x0902f1ac",
71+
output:
72+
"0x000000000000000000000000000000000000000000033ecd2787c3cba6b25c6800000000000000000000000000000000000000000000007455f078bccb9d31ef00000000000000000000000000000000000000000000000000000000645ccdeb",
73+
type: "STATICCALL",
74+
},
75+
{
76+
from: "0x1111111254eeb25477b68fb85ed929f73a960582",
77+
gas: "0x2ee37a6",
78+
gasUsed: "0xffe2",
79+
to: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f",
80+
input:
81+
"0x022c0d9f000000000000000000000000000000000000000000000000003a47c40d6dc2c80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000377e19e0d6525f5fabd565bc47dc4e5fc8bafb0100000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000",
82+
calls: [
83+
{
84+
from: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f",
85+
gas: "0x2e24aef",
86+
gasUsed: "0x75de",
87+
to: "0x6b175474e89094c44da98b954eedeac495271d0f",
88+
input:
89+
"0xa9059cbb000000000000000000000000377e19e0d6525f5fabd565bc47dc4e5fc8bafb01000000000000000000000000000000000000000000000000003a47c40d6dc2c8",
90+
output: "0x0000000000000000000000000000000000000000000000000000000000000001",
91+
logs: [
92+
{
93+
address: "0x6b175474e89094c44da98b954eedeac495271d0f",
94+
topics: [
95+
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
96+
"0x000000000000000000000000c3d03e4f041fd4cd388c549ee2a29a9e5075882f",
97+
"0x000000000000000000000000377e19e0d6525f5fabd565bc47dc4e5fc8bafb01",
98+
],
99+
data: "0x000000000000000000000000000000000000000000000000003a47c40d6dc2c8",
100+
},
101+
{
102+
address: "0x6b175474e89094c44da98b954eedeac495271d0f",
103+
topics: [
104+
"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
105+
"0x000000000000000000000000c3d03e4f041fd4cd388c549ee2a29a9e5075882f",
106+
"0x000000000000000000000000377e19e0d6525f5fabd565bc47dc4e5fc8bafb01",
107+
"0x000000000000000000000000000000000000000000000000003a47c40d6dc2c8",
108+
],
109+
data: "0x000000000000000000000000000000000000000000000000003a47c40d6dc2c8",
110+
},
111+
{
112+
address: "0x6b175474e89094c44da98b954eedeac495271d0f",
113+
topics: [
114+
"0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62",
115+
"0x000000000000000000000000c3d03e4f041fd4cd388c549ee2a29a9e5075882f",
116+
"0x000000000000000000000000377e19e0d6525f5fabd565bc47dc4e5fc8bafb01",
117+
"0x000000000000000000000000377e19e0d6525f5fabd565bc47dc4e5fc8bafb01",
118+
],
119+
data: "0x000000000000000000000000000000000000000000000000003a47c40d6dc2c8000000000000000000000000000000000000000000000000003a47c40d6dc2c8",
120+
},
121+
{
122+
address: "0x6b175474e89094c44da98b954eedeac495271d0f",
123+
topics: [
124+
"0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb",
125+
"0x000000000000000000000000c3d03e4f041fd4cd388c549ee2a29a9e5075882f",
126+
"0x000000000000000000000000377e19e0d6525f5fabd565bc47dc4e5fc8bafb01",
127+
"0x000000000000000000000000377e19e0d6525f5fabd565bc47dc4e5fc8bafb01",
128+
],
129+
data: "0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
130+
},
131+
],
132+
value: "0x0",
133+
type: "CALL",
134+
},
135+
{
136+
from: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f",
137+
gas: "0x2e1d469",
138+
gasUsed: "0x25a",
139+
to: "0x6b175474e89094c44da98b954eedeac495271d0f",
140+
input:
141+
"0x70a08231000000000000000000000000c3d03e4f041fd4cd388c549ee2a29a9e5075882f",
142+
output: "0x000000000000000000000000000000000000000000033ecd274d7c07994499a0",
143+
type: "STATICCALL",
144+
},
145+
{
146+
from: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f",
147+
gas: "0x2e1d071",
148+
gasUsed: "0x216",
149+
to: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
150+
input:
151+
"0x70a08231000000000000000000000000c3d03e4f041fd4cd388c549ee2a29a9e5075882f",
152+
output: "0x00000000000000000000000000000000000000000000007455f080ec456ac1ef",
153+
type: "STATICCALL",
154+
},
155+
],
156+
logs: [
157+
{
158+
address: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f",
159+
topics: [
160+
"0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1",
161+
],
162+
data: "0x000000000000000000000000000000000000000000033ecd274d7c07994499a000000000000000000000000000000000000000000000007455f080ec456ac1ef",
163+
},
164+
{
165+
address: "0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f",
166+
topics: [
167+
"0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822",
168+
"0x0000000000000000000000001111111254eeb25477b68fb85ed929f73a960582",
169+
"0x000000000000000000000000377e19e0d6525f5fabd565bc47dc4e5fc8bafb01",
170+
],
171+
data: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000082f79cd9000000000000000000000000000000000000000000000000000003a47c40d6dc2c80000000000000000000000000000000000000000000000000000000000000000",
172+
},
173+
],
174+
value: "0x0",
175+
type: "CALL",
176+
},
177+
],
178+
value: "0x82f79cd9000",
179+
type: "CALL",
180+
},
181+
};

test/simulator/index.test.ts

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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

Comments
 (0)