Skip to content

Commit be5cdbd

Browse files
committed
Gas limit tests
1 parent fc5c394 commit be5cdbd

File tree

4 files changed

+115
-21
lines changed

4 files changed

+115
-21
lines changed

cli/playground.ts

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,59 @@
22

33
import { env } from '../tools/lib/index.ts'
44
import { abis } from '../codegen/abis.ts'
5-
import { Storage } from '../codegen/addresses.ts'
5+
import { Test } from '../codegen/addresses.ts'
6+
import { encodeFunctionData } from 'viem'
67

7-
{
8-
const { request } = await env.wallet.simulateContract({
9-
address: Storage,
10-
abi: abis.Storage,
11-
functionName: 'store',
12-
args: [42n],
13-
})
8+
switch (Deno.env.get('ACTION')) {
9+
case 'execute': {
10+
const { request } = await env.wallet.simulateContract({
11+
address: Test,
12+
abi: abis.Test,
13+
functionName: 'tryCatchNewContract',
14+
args: ['0x0000000000000000000000000000000000000000'],
15+
})
1416

15-
const result = await env.wallet.writeContract(request)
16-
console.log('store tx', result)
17-
}
17+
const hash = await env.wallet.writeContract(request)
18+
const receipt = await env.wallet.waitForTransactionReceipt({
19+
hash,
20+
})
21+
22+
const res = await env.debugClient.traceTransaction(
23+
receipt.transactionHash,
24+
'callTracer',
25+
{},
26+
)
27+
console.log(JSON.stringify(res))
28+
break
29+
}
30+
31+
case 'estimate': {
32+
const res = await env.wallet.estimateContractGas({
33+
address: Test,
34+
abi: abis.Test,
35+
functionName: 'tryCatchNewContract',
36+
args: ['0x0000000000000000000000000000000000000000'],
37+
})
38+
39+
console.log(res)
40+
break
41+
}
1842

19-
{
20-
const result = await env.wallet.readContract(
21-
{
22-
address: Storage,
23-
abi: abis.Storage,
24-
functionName: 'retrieve',
25-
},
26-
)
27-
console.log('retrieve:', result)
43+
default: {
44+
const res = await env.debugClient.traceCall(
45+
{
46+
account: env.wallet.account,
47+
gas: 28663198355n, // * 140n / 100n,
48+
to: Test,
49+
data: encodeFunctionData({
50+
abi: abis.Test,
51+
functionName: 'tryCatchNewContract',
52+
args: ['0x0000000000000000000000000000000000000000'],
53+
}),
54+
},
55+
'callTracer',
56+
{},
57+
)
58+
console.log(JSON.stringify(res))
59+
}
2860
}

contracts/Test.sol

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.7.0;
3+
4+
// External contract used for try / catch examples
5+
contract Foo {
6+
address public owner;
7+
8+
constructor(address _owner) {
9+
require(_owner != address(0), "invalid address");
10+
assert(_owner != 0x0000000000000000000000000000000000000001);
11+
owner = _owner;
12+
}
13+
14+
function myFunc(uint x) public pure returns (string memory) {
15+
require(x != 0, "require failed");
16+
return "my func was called";
17+
}
18+
}
19+
20+
contract Test {
21+
event Log(string message);
22+
event LogBytes(bytes data);
23+
24+
Foo public foo;
25+
26+
constructor() {
27+
// This Foo contract is used for example of try catch with external call
28+
foo = new Foo(msg.sender);
29+
}
30+
31+
// Example of try / catch with external call
32+
// tryCatchExternalCall(0) => Log("external call failed")
33+
// tryCatchExternalCall(1) => Log("my func was called")
34+
function tryCatchExternalCall(uint _i) public {
35+
try foo.myFunc(_i) returns (string memory result) {
36+
emit Log(result);
37+
} catch {
38+
emit Log("external call failed");
39+
}
40+
}
41+
42+
// Example of try / catch with contract creation
43+
// tryCatchNewContract(0x0000000000000000000000000000000000000000) => Log("invalid address")
44+
// tryCatchNewContract(0x0000000000000000000000000000000000000001) => LogBytes("")
45+
// tryCatchNewContract(0x0000000000000000000000000000000000000002) => Log("Foo created")
46+
function tryCatchNewContract(address _owner) public {
47+
try new Foo(_owner) returns (Foo foo) {
48+
// you can use variable foo here
49+
emit Log("Foo created");
50+
} catch Error(string memory reason) {
51+
// catch failing revert() and require()
52+
emit Log(reason);
53+
} catch (bytes memory reason) {
54+
// catch failing assert()
55+
emit LogBytes(reason);
56+
}
57+
}
58+
}

tools/build.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ function evmCompile(file: Deno.DirEntry, sources: CompileInput) {
129129
language: 'Solidity',
130130
sources,
131131
settings: {
132+
optimizer: {
133+
enabled: true,
134+
},
135+
viaIR: true, // <-- THIS activates the Y+ pipeline
132136
outputSelection: {
133137
'*': {
134138
'*': ['*'],

tools/deploy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { deploy } from './lib/index.ts'
2323
// })
2424

2525
await deploy({
26-
name: 'Storage',
26+
name: 'Test',
2727
args: [],
2828
// bytecodeType: 'polkavm', // Specify `pvm` for PVM bytecode deployment
2929
})

0 commit comments

Comments
 (0)