Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 52 additions & 20 deletions cli/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
58 changes: 58 additions & 0 deletions contracts/Test.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
4 changes: 4 additions & 0 deletions tools/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
'*': {
'*': ['*'],
Expand Down
2 changes: 1 addition & 1 deletion tools/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})