Skip to content

Commit 6d65c1d

Browse files
authored
Tests: Avoid hardcoded fn selectors (#23)
1 parent 80e380c commit 6d65c1d

16 files changed

Lines changed: 845 additions & 189 deletions

File tree

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
[
2+
{
3+
"constant": true,
4+
"inputs": [],
5+
"name": "name",
6+
"outputs": [
7+
{
8+
"name": "",
9+
"type": "string"
10+
}
11+
],
12+
"payable": false,
13+
"stateMutability": "view",
14+
"type": "function"
15+
},
16+
{
17+
"constant": false,
18+
"inputs": [
19+
{
20+
"name": "_spender",
21+
"type": "address"
22+
},
23+
{
24+
"name": "_value",
25+
"type": "uint256"
26+
}
27+
],
28+
"name": "approve",
29+
"outputs": [
30+
{
31+
"name": "",
32+
"type": "bool"
33+
}
34+
],
35+
"payable": false,
36+
"stateMutability": "nonpayable",
37+
"type": "function"
38+
},
39+
{
40+
"constant": true,
41+
"inputs": [],
42+
"name": "totalSupply",
43+
"outputs": [
44+
{
45+
"name": "",
46+
"type": "uint256"
47+
}
48+
],
49+
"payable": false,
50+
"stateMutability": "view",
51+
"type": "function"
52+
},
53+
{
54+
"constant": false,
55+
"inputs": [
56+
{
57+
"name": "_from",
58+
"type": "address"
59+
},
60+
{
61+
"name": "_to",
62+
"type": "address"
63+
},
64+
{
65+
"name": "_value",
66+
"type": "uint256"
67+
}
68+
],
69+
"name": "transferFrom",
70+
"outputs": [
71+
{
72+
"name": "",
73+
"type": "bool"
74+
}
75+
],
76+
"payable": false,
77+
"stateMutability": "nonpayable",
78+
"type": "function"
79+
},
80+
{
81+
"constant": true,
82+
"inputs": [],
83+
"name": "decimals",
84+
"outputs": [
85+
{
86+
"name": "",
87+
"type": "uint8"
88+
}
89+
],
90+
"payable": false,
91+
"stateMutability": "view",
92+
"type": "function"
93+
},
94+
{
95+
"constant": true,
96+
"inputs": [
97+
{
98+
"name": "_owner",
99+
"type": "address"
100+
}
101+
],
102+
"name": "balanceOf",
103+
"outputs": [
104+
{
105+
"name": "balance",
106+
"type": "uint256"
107+
}
108+
],
109+
"payable": false,
110+
"stateMutability": "view",
111+
"type": "function"
112+
},
113+
{
114+
"constant": true,
115+
"inputs": [],
116+
"name": "symbol",
117+
"outputs": [
118+
{
119+
"name": "",
120+
"type": "string"
121+
}
122+
],
123+
"payable": false,
124+
"stateMutability": "view",
125+
"type": "function"
126+
},
127+
{
128+
"constant": false,
129+
"inputs": [
130+
{
131+
"name": "_to",
132+
"type": "address"
133+
},
134+
{
135+
"name": "_value",
136+
"type": "uint256"
137+
}
138+
],
139+
"name": "transfer",
140+
"outputs": [
141+
{
142+
"name": "",
143+
"type": "bool"
144+
}
145+
],
146+
"payable": false,
147+
"stateMutability": "nonpayable",
148+
"type": "function"
149+
},
150+
{
151+
"constant": true,
152+
"inputs": [
153+
{
154+
"name": "_owner",
155+
"type": "address"
156+
},
157+
{
158+
"name": "_spender",
159+
"type": "address"
160+
}
161+
],
162+
"name": "allowance",
163+
"outputs": [
164+
{
165+
"name": "",
166+
"type": "uint256"
167+
}
168+
],
169+
"payable": false,
170+
"stateMutability": "view",
171+
"type": "function"
172+
},
173+
{
174+
"payable": true,
175+
"stateMutability": "payable",
176+
"type": "fallback"
177+
},
178+
{
179+
"anonymous": false,
180+
"inputs": [
181+
{
182+
"indexed": true,
183+
"name": "owner",
184+
"type": "address"
185+
},
186+
{
187+
"indexed": true,
188+
"name": "spender",
189+
"type": "address"
190+
},
191+
{
192+
"indexed": false,
193+
"name": "value",
194+
"type": "uint256"
195+
}
196+
],
197+
"name": "Approval",
198+
"type": "event"
199+
},
200+
{
201+
"anonymous": false,
202+
"inputs": [
203+
{
204+
"indexed": true,
205+
"name": "from",
206+
"type": "address"
207+
},
208+
{
209+
"indexed": true,
210+
"name": "to",
211+
"type": "address"
212+
},
213+
{
214+
"indexed": false,
215+
"name": "value",
216+
"type": "uint256"
217+
}
218+
],
219+
"name": "Transfer",
220+
"type": "event"
221+
}
222+
]

examples/02-simple-transfer-with-inputs/tests/task.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { fp, OpType, randomEvmAddress } from '@mimicprotocol/sdk'
22
import { Context, ContractCallMock, runTask, Transfer } from '@mimicprotocol/test-ts'
33
import { expect } from 'chai'
4+
import { Interface } from 'ethers'
5+
6+
import ERC20Abi from '../abis/ERC20.json'
7+
8+
const ERC20Interface = new Interface(ERC20Abi)
49

510
describe('Task', () => {
611
const taskDir = './build'
@@ -21,7 +26,11 @@ describe('Task', () => {
2126

2227
const calls: ContractCallMock[] = [
2328
{
24-
request: { to: inputs.token, chainId: inputs.chainId, fnSelector: '0x313ce567' }, // decimals
29+
request: {
30+
to: inputs.token,
31+
chainId: inputs.chainId,
32+
fnSelector: ERC20Interface.getFunction('decimals')!.selector,
33+
},
2534
response: { value: '6', abiType: 'uint8' },
2635
},
2736
]

examples/03-transfer-balance-threshold/tests/task.spec.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { fp, OpType, randomEvmAddress } from '@mimicprotocol/sdk'
22
import { Context, ContractCallMock, runTask, Transfer } from '@mimicprotocol/test-ts'
33
import { expect } from 'chai'
4+
import { Interface } from 'ethers'
5+
6+
import ERC20Abi from '../abis/ERC20.json'
7+
8+
const ERC20Interface = new Interface(ERC20Abi)
49

510
describe('Task', () => {
611
const taskDir = './build'
@@ -25,29 +30,18 @@ describe('Task', () => {
2530
request: {
2631
to: inputs.token,
2732
chainId: inputs.chainId,
28-
fnSelector: '0x70a08231', // `balanceOf`
29-
params: [
30-
{
31-
value: inputs.recipient,
32-
abiType: 'address',
33-
},
34-
],
35-
},
36-
response: {
37-
value: balance,
38-
abiType: 'uint256',
33+
fnSelector: ERC20Interface.getFunction('balanceOf')!.selector,
34+
params: [{ value: inputs.recipient, abiType: 'address' }],
3935
},
36+
response: { value: balance, abiType: 'uint256' },
4037
},
4138
{
4239
request: {
4340
to: inputs.token,
4441
chainId: inputs.chainId,
45-
fnSelector: '0x313ce567', // `decimals`
46-
},
47-
response: {
48-
value: '6',
49-
abiType: 'uint8',
42+
fnSelector: ERC20Interface.getFunction('decimals')!.selector,
5043
},
44+
response: { value: '6', abiType: 'uint8' },
5145
},
5246
]
5347

examples/04-transfer-balance-threshold-with-oracles/tests/task.spec.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { fp, OpType, randomEvmAddress } from '@mimicprotocol/sdk'
22
import { Context, ContractCallMock, GetPriceMock, runTask, Transfer } from '@mimicprotocol/test-ts'
33
import { expect } from 'chai'
4+
import { Interface } from 'ethers'
5+
6+
import ERC20Abi from '../abis/ERC20.json'
7+
8+
const ERC20Interface = new Interface(ERC20Abi)
49

510
describe('Task', () => {
611
const taskDir = './build'
@@ -22,10 +27,7 @@ describe('Task', () => {
2227

2328
const prices: GetPriceMock[] = [
2429
{
25-
request: {
26-
token: inputs.token,
27-
chainId: inputs.chainId,
28-
},
30+
request: { token: inputs.token, chainId: inputs.chainId },
2931
response: ['1000000000000000000'], // 1 token = 1 USD
3032
},
3133
]
@@ -35,29 +37,18 @@ describe('Task', () => {
3537
request: {
3638
to: inputs.token,
3739
chainId: inputs.chainId,
38-
fnSelector: '0x70a08231', // `balanceOf`,
39-
params: [
40-
{
41-
value: inputs.recipient,
42-
abiType: 'address',
43-
},
44-
],
45-
},
46-
response: {
47-
value: balance,
48-
abiType: 'uint256',
40+
fnSelector: ERC20Interface.getFunction('balanceOf')!.selector,
41+
params: [{ value: inputs.recipient, abiType: 'address' }],
4942
},
43+
response: { value: balance, abiType: 'uint256' },
5044
},
5145
{
5246
request: {
5347
to: inputs.token,
5448
chainId: inputs.chainId,
55-
fnSelector: '0x313ce567', // `decimals`
56-
},
57-
response: {
58-
value: '6',
59-
abiType: 'uint8',
49+
fnSelector: ERC20Interface.getFunction('decimals')!.selector,
6050
},
51+
response: { value: '6', abiType: 'uint8' },
6152
},
6253
]
6354

0 commit comments

Comments
 (0)