diff --git a/cli/playground.ts b/cli/playground.ts index 0b1837a..e24ddec 100755 --- a/cli/playground.ts +++ b/cli/playground.ts @@ -2,27 +2,59 @@ import { env } from '../tools/lib/index.ts' import { abis } from '../codegen/abis.ts' -import { Storage } from '../codegen/addresses.ts' +import { Test } from '../codegen/addresses.ts' +import { encodeFunctionData } from 'viem' -{ - const { request } = await env.wallet.simulateContract({ - address: Storage, - abi: abis.Storage, - functionName: 'store', - args: [42n], - }) +switch (Deno.env.get('ACTION')) { + case 'execute': { + const { request } = await env.wallet.simulateContract({ + address: Test, + abi: abis.Test, + functionName: 'tryCatchNewContract', + args: ['0x0000000000000000000000000000000000000000'], + }) - const result = await env.wallet.writeContract(request) - console.log('store tx', result) -} + const hash = await env.wallet.writeContract(request) + const receipt = await env.wallet.waitForTransactionReceipt({ + hash, + }) + + const res = await env.debugClient.traceTransaction( + receipt.transactionHash, + 'callTracer', + {}, + ) + console.log(JSON.stringify(res)) + break + } + + case 'estimate': { + const res = await env.wallet.estimateContractGas({ + address: Test, + abi: abis.Test, + functionName: 'tryCatchNewContract', + args: ['0x0000000000000000000000000000000000000000'], + }) + + console.log(res) + break + } -{ - const result = await env.wallet.readContract( - { - address: Storage, - abi: abis.Storage, - functionName: 'retrieve', - }, - ) - console.log('retrieve:', result) + default: { + const res = await env.debugClient.traceCall( + { + account: env.wallet.account, + gas: 28663198355n, // * 140n / 100n, + to: Test, + data: encodeFunctionData({ + abi: abis.Test, + functionName: 'tryCatchNewContract', + args: ['0x0000000000000000000000000000000000000000'], + }), + }, + 'callTracer', + {}, + ) + console.log(JSON.stringify(res)) + } } diff --git a/contracts/Test.sol b/contracts/Test.sol new file mode 100644 index 0000000..355b14b --- /dev/null +++ b/contracts/Test.sol @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.7.0; + +// External contract used for try / catch examples +contract Foo { + address public owner; + + constructor(address _owner) { + require(_owner != address(0), "invalid address"); + assert(_owner != 0x0000000000000000000000000000000000000001); + owner = _owner; + } + + function myFunc(uint x) public pure returns (string memory) { + require(x != 0, "require failed"); + return "my func was called"; + } +} + +contract Test { + event Log(string message); + event LogBytes(bytes data); + + Foo public foo; + + constructor() { + // This Foo contract is used for example of try catch with external call + foo = new Foo(msg.sender); + } + + // Example of try / catch with external call + // tryCatchExternalCall(0) => Log("external call failed") + // tryCatchExternalCall(1) => Log("my func was called") + function tryCatchExternalCall(uint _i) public { + try foo.myFunc(_i) returns (string memory result) { + emit Log(result); + } catch { + emit Log("external call failed"); + } + } + + // Example of try / catch with contract creation + // tryCatchNewContract(0x0000000000000000000000000000000000000000) => Log("invalid address") + // tryCatchNewContract(0x0000000000000000000000000000000000000001) => LogBytes("") + // tryCatchNewContract(0x0000000000000000000000000000000000000002) => Log("Foo created") + function tryCatchNewContract(address _owner) public { + try new Foo(_owner) returns (Foo foo) { + // you can use variable foo here + emit Log("Foo created"); + } catch Error(string memory reason) { + // catch failing revert() and require() + emit Log(reason); + } catch (bytes memory reason) { + // catch failing assert() + emit LogBytes(reason); + } + } +} diff --git a/tools/build.ts b/tools/build.ts index 23eb99e..b3aac8d 100644 --- a/tools/build.ts +++ b/tools/build.ts @@ -129,6 +129,10 @@ function evmCompile(file: Deno.DirEntry, sources: CompileInput) { language: 'Solidity', sources, settings: { + optimizer: { + enabled: true, + }, + viaIR: true, // <-- THIS activates the Y+ pipeline outputSelection: { '*': { '*': ['*'], diff --git a/tools/deploy.ts b/tools/deploy.ts index a20d9f1..a699db0 100644 --- a/tools/deploy.ts +++ b/tools/deploy.ts @@ -23,7 +23,7 @@ import { deploy } from './lib/index.ts' // }) await deploy({ - name: 'Storage', + name: 'Test', args: [], // bytecodeType: 'polkavm', // Specify `pvm` for PVM bytecode deployment })