-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patheth_deploy_contract_and_interact_test.js
More file actions
334 lines (300 loc) · 13.3 KB
/
Copy patheth_deploy_contract_and_interact_test.js
File metadata and controls
334 lines (300 loc) · 13.3 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
const { assert } = require('chai')
const conf = require('./config')
const helpers = require('./helpers')
const web3 = conf.web3
it('deploy contract and interact', async () => {
let deployed = await helpers.deployContract("storage")
let contractAddress = deployed.receipt.contractAddress
// make sure deploy results are correct
assert.equal(deployed.receipt.status, conf.successStatus)
assert.isString(deployed.receipt.transactionHash)
assert.isString(contractAddress)
assert.equal(deployed.receipt.from, conf.eoa.address)
assert.isUndefined(deployed.receipt.to)
let rcp = await web3.eth.getTransactionReceipt(deployed.receipt.transactionHash)
assert.equal(rcp.contractAddress, contractAddress)
assert.equal(rcp.status, conf.successStatus)
assert.isUndefined(rcp.to)
assert.equal(rcp.gasUsed, 1200498n)
assert.equal(rcp.gasUsed, rcp.cumulativeGasUsed)
// check if latest block contains the deploy results
let latestHeight = await web3.eth.getBlockNumber()
let deployTx = await web3.eth.getTransactionFromBlock(latestHeight, 0)
assert.equal(deployTx.hash, deployed.receipt.transactionHash)
assert.isUndefined(deployTx.to)
// check that getCode supports specific block heights
let code = await web3.eth.getCode(contractAddress, latestHeight - 1n)
assert.equal(code, "0x") // empty at previous height
code = await web3.eth.getCode(contractAddress)
// deploy data has more than just the contract
// since it contains the initialization code,
// but subset of the data is the contract code
assert.isTrue(deployTx.data.includes(code.replace("0x", "")))
let deployReceipt = await web3.eth.getTransactionReceipt(deployed.receipt.transactionHash)
assert.deepEqual(deployReceipt, deployed.receipt)
// get the default deployed value on contract
const initValue = 1337
let callRetrieve = deployed.contract.methods.retrieve().encodeABI()
result = await web3.eth.call({ to: contractAddress, data: callRetrieve }, "latest")
assert.equal(result, initValue)
// set the value on the contract, to its current value
let updateData = deployed.contract.methods.store(initValue).encodeABI()
// store a value in the contract
let res = await helpers.signAndSend({
from: conf.eoa.address,
to: contractAddress,
data: updateData,
value: '0',
gasPrice: conf.minGasPrice,
})
assert.equal(res.receipt.status, conf.successStatus)
// check the new value on contract
result = await web3.eth.call({ to: contractAddress, data: callRetrieve }, "latest")
assert.equal(result, initValue)
// update the value on the contract
newValue = 100
updateData = deployed.contract.methods.store(newValue).encodeABI()
// store a value in the contract
res = await helpers.signAndSend({
from: conf.eoa.address,
to: contractAddress,
data: updateData,
value: '0',
gasPrice: conf.minGasPrice,
})
assert.equal(res.receipt.status, conf.successStatus)
// check the new value on contract
result = await web3.eth.call({ to: contractAddress, data: callRetrieve }, "latest")
assert.equal(result, newValue)
// make sure receipts and txs are indexed
latestHeight = await web3.eth.getBlockNumber()
let updateTx = await web3.eth.getTransactionFromBlock(latestHeight, 0)
let updateRcp = await web3.eth.getTransactionReceipt(updateTx.hash)
assert.equal(updateRcp.status, conf.successStatus)
assert.equal(updateTx.data, updateData)
// check that call can handle specific block heights
result = await web3.eth.call({ to: contractAddress, data: callRetrieve }, latestHeight - 1n)
assert.equal(result, initValue)
// submit a transaction that emits logs
res = await helpers.signAndSend({
from: conf.eoa.address,
to: contractAddress,
data: deployed.contract.methods.sum(100, 200).encodeABI(),
gas: 1_000_000,
gasPrice: conf.minGasPrice
})
assert.equal(res.receipt.status, conf.successStatus)
// assert that logsBloom from transaction receipt and block match
latestHeight = await web3.eth.getBlockNumber()
let block = await web3.eth.getBlock(latestHeight)
assert.equal(block.logsBloom, res.receipt.logsBloom)
// check that revert reason for custom error is correctly returned for signed transaction
try {
let callCustomError = deployed.contract.methods.customError().encodeABI()
result = await helpers.signAndSend({
from: conf.eoa.address,
to: contractAddress,
data: callCustomError,
gas: 1_000_000,
gasPrice: conf.minGasPrice
})
} catch (error) {
assert.equal(error.reason, 'execution reverted')
assert.equal(error.signature, '0x9195785a')
assert.equal(
error.data,
'00000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001056616c756520697320746f6f206c6f7700000000000000000000000000000000'
)
}
// check that revert reason for custom error is correctly returned for contract call
// and it is properly ABI decoded.
try {
result = await deployed.contract.methods.customError().call({ from: conf.eoa.address })
} catch (err) {
let error = err.innerError
assert.equal(
error.data,
'0x9195785a00000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001056616c756520697320746f6f206c6f7700000000000000000000000000000000'
)
assert.equal(error.errorName, 'MyCustomError')
assert.equal(error.errorSignature, 'MyCustomError(uint256,string)')
assert.equal(error.errorArgs.value, 5n)
assert.equal(error.errorArgs.message, 'Value is too low')
}
// check that assertion error is correctly returned for signed transaction
try {
let callAssertError = deployed.contract.methods.assertError().encodeABI()
result = await helpers.signAndSend({
from: conf.eoa.address,
to: contractAddress,
data: callAssertError,
gas: 1_000_000,
gasPrice: conf.minGasPrice
})
} catch (error) {
assert.equal(error.reason, 'execution reverted: Assert Error Message')
assert.equal(error.signature, '0x08c379a0')
assert.equal(
error.data,
'00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000014417373657274204572726f72204d657373616765000000000000000000000000'
)
}
// check that assertion error is correctly returned for contract call
// and it is properly ABI decoded.
try {
result = await deployed.contract.methods.assertError().call({ from: conf.eoa.address })
} catch (err) {
let error = err.innerError
assert.equal(
error.message,
'execution reverted: Assert Error Message'
)
assert.equal(
error.data,
'0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000014417373657274204572726f72204d657373616765000000000000000000000000'
)
}
// check that revert reason for custom error is correctly returned for gas estimation
try {
let callCustomError = deployed.contract.methods.customError().encodeABI()
result = await web3.eth.estimateGas({
from: conf.eoa.address,
to: contractAddress,
data: callCustomError,
gas: 1_000_000,
gasPrice: conf.minGasPrice
})
} catch (error) {
assert.equal(error.innerError.message, 'execution reverted')
assert.equal(
error.innerError.data,
'0x9195785a00000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000001056616c756520697320746f6f206c6f7700000000000000000000000000000000'
)
}
// check that assertion error is correctly returned for gas estimation
try {
let callAssertError = deployed.contract.methods.assertError().encodeABI()
result = await web3.eth.estimateGas({
from: conf.eoa.address,
to: contractAddress,
data: callAssertError,
gas: 1_000_000,
gasPrice: conf.minGasPrice
})
} catch (error) {
assert.equal(error.innerError.message, 'execution reverted: Assert Error Message')
assert.equal(
error.innerError.data,
'0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000014417373657274204572726f72204d657373616765000000000000000000000000'
)
}
// check that gas estimation reports proper error code and data for empty reverts
try {
let callStoreButRevert = deployed.contract.methods.storeButRevert(150n).encodeABI()
result = await web3.eth.estimateGas({
from: conf.eoa.address,
to: contractAddress,
data: callStoreButRevert,
gas: 1_000_000,
gasPrice: conf.minGasPrice
})
assert.fail('expected eth_estimateGas to revert with empty revert data')
} catch (error) {
assert.equal(error.innerError.code, 3)
assert.equal(error.innerError.data, '0x')
assert.equal(error.innerError.message, 'execution reverted')
}
// check that contract call reports proper error code and data for empty reverts
try {
let callStoreButRevert = deployed.contract.methods.storeButRevert(150n).encodeABI()
result = await web3.eth.call({
from: conf.eoa.address,
to: contractAddress,
data: callStoreButRevert,
gas: 1_000_000,
gasPrice: conf.minGasPrice
})
assert.fail('expected eth_call to revert with empty revert data')
} catch (error) {
assert.equal(error.innerError.code, 3)
assert.equal(error.innerError.data, '0x')
assert.equal(error.innerError.message, 'execution reverted')
}
// check that block height is properly handled by gas estimation endpoint
let gasEstimate = await web3.eth.estimateGas(
{
from: conf.eoa.address,
to: contractAddress,
data: deployed.contract.methods.sum(100, 200).encodeABI(),
gas: 1_000_000,
gasPrice: 0
},
'0x1' // give a block height at which the contract did not exist
)
assert.equal(gasEstimate, 22026n)
gasEstimate = await web3.eth.estimateGas(
{
from: conf.eoa.address,
to: contractAddress,
data: deployed.contract.methods.sum(100, 200).encodeABI(),
gas: 1_000_000,
gasPrice: 0
},
'latest' // give a block height at which the contract did exist
)
assert.equal(gasEstimate, 25050n)
// check that `eth_call` can handle state overrides
let stateOverrides = {
[contractAddress]: {
stateDiff: {
'0x0000000000000000000000000000000000000000000000000000000000000000': '0x00000000000000000000000000000000000000000000000000000000000003e8'
}
}
}
let response = await helpers.callRPCMethod(
'eth_call',
[{ to: contractAddress, data: callRetrieve }, 'latest', stateOverrides]
)
assert.equal(response.status, 200)
assert.isDefined(response.body)
result = response.body.result
assert.equal(
result,
'0x00000000000000000000000000000000000000000000000000000000000003e8'
)
// check that `eth_estimateGas` can handle state overrides
stateOverrides = {
[contractAddress]: {
stateDiff: {
'0x0000000000000000000000000000000000000000000000000000000000000000': '0x00000000000000000000000000000000000000000000000000000000000003e8'
}
}
}
updateData = deployed.contract.methods.store(100n).encodeABI()
response = await helpers.callRPCMethod(
'eth_estimateGas',
[{ to: contractAddress, data: updateData }, 'latest', stateOverrides]
)
assert.equal(response.status, 200)
assert.isDefined(response.body)
result = response.body.result
assert.equal(result, '0x6969')
stateOverrides = {
[contractAddress]: {
stateDiff: {
'0x0000000000000000000000000000000000000000000000000000000000000000': '0x0000000000000000000000000000000000000000000000000000000000000000'
}
}
}
updateData = deployed.contract.methods.store(100n).encodeABI()
response = await helpers.callRPCMethod(
'eth_estimateGas',
[{ to: contractAddress, data: updateData }, 'latest', stateOverrides]
)
assert.equal(response.status, 200)
assert.isDefined(response.body)
// setting a storage slot from a zero-value, to a non-zero value has an
// increase of about 20,000 gas. Which is quite different to `0x72c3`.
result = response.body.result
assert.equal(result, '0xac56')
})