Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs): move l1asset smart contract example from wiki to wasp #3465

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/evm/evmtest/wiki_how_tos/L1Assets.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"components":[{"internalType":"bytes","name":"data","type":"bytes"}],"internalType":"struct L1Address","name":"to","type":"tuple"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]
1 change: 1 addition & 0 deletions packages/evm/evmtest/wiki_how_tos/L1Assets.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6080604052348015600e575f80fd5b506110228061001c5f395ff3fe608060405234801561000f575f80fd5b5060043610610029575f3560e01c80639505f6dc1461002d575b5f80fd5b6100476004803603810190610042919061047b565b610049565b005b5f73107400000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff1663a714b58d336040518263ffffffff1660e01b81526004016100979190610501565b5f60405180830381865afa1580156100b1573d5f803e3d5ffd5b505050506040513d5f823e3d601f19601f820116820180604052508101906100d99190610932565b905073107400000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff1663423fa15133836040518363ffffffff1660e01b815260040161012a929190610c21565b5f604051808303815f87803b158015610141575f80fd5b505af1158015610153573d5f803e3d5ffd5b5050505061015f6101ef565b61016761023d565b73107400000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff1663b7a53f5385855f86866040518663ffffffff1660e01b81526004016101bc959493929190610f7f565b5f604051808303815f87803b1580156101d3575f80fd5b505af11580156101e5573d5f803e3d5ffd5b5050505050505050565b6040518060a001604052805f63ffffffff1681526020015f63ffffffff16815260200161021a61025f565b8152602001610227610272565b81526020015f67ffffffffffffffff1681525090565b60405180604001604052805f60070b815260200161025961029c565b81525090565b6040518060200160405280606081525090565b60405180606001604052805f67ffffffffffffffff16815260200160608152602001606081525090565b60405180604001604052805f60070b81526020016102b86102be565b81525090565b6040518060200160405280606081525090565b5f604051905090565b5f80fd5b5f80fd5b5f80fd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b61032c826102e6565b810181811067ffffffffffffffff8211171561034b5761034a6102f6565b5b80604052505050565b5f61035d6102d1565b90506103698282610323565b919050565b5f80fd5b5f80fd5b5f80fd5b5f67ffffffffffffffff821115610394576103936102f6565b5b61039d826102e6565b9050602081019050919050565b828183375f83830152505050565b5f6103ca6103c58461037a565b610354565b9050828152602081018484840111156103e6576103e5610376565b5b6103f18482856103aa565b509392505050565b5f82601f83011261040d5761040c610372565b5b813561041d8482602086016103b8565b91505092915050565b5f6020828403121561043b5761043a6102e2565b5b6104456020610354565b90505f82013567ffffffffffffffff8111156104645761046361036e565b5b610470848285016103f9565b5f8301525092915050565b5f602082840312156104905761048f6102da565b5b5f82013567ffffffffffffffff8111156104ad576104ac6102de565b5b6104b984828501610426565b91505092915050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6104eb826104c2565b9050919050565b6104fb816104e1565b82525050565b5f6020820190506105145f8301846104f2565b92915050565b5f67ffffffffffffffff82169050919050565b6105368161051a565b8114610540575f80fd5b50565b5f815190506105518161052d565b92915050565b5f67ffffffffffffffff821115610571576105706102f6565b5b602082029050602081019050919050565b5f80fd5b8281835e5f83830152505050565b5f6105a66105a18461037a565b610354565b9050828152602081018484840111156105c2576105c1610376565b5b6105cd848285610586565b509392505050565b5f82601f8301126105e9576105e8610372565b5b81516105f9848260208601610594565b91505092915050565b5f60208284031215610617576106166102e2565b5b6106216020610354565b90505f82015167ffffffffffffffff8111156106405761063f61036e565b5b61064c848285016105d5565b5f8301525092915050565b5f819050919050565b61066981610657565b8114610673575f80fd5b50565b5f8151905061068481610660565b92915050565b5f6040828403121561069f5761069e6102e2565b5b6106a96040610354565b90505f82015167ffffffffffffffff8111156106c8576106c761036e565b5b6106d484828501610602565b5f8301525060206106e784828501610676565b60208301525092915050565b5f61070561070084610557565b610354565b9050808382526020820190506020840283018581111561072857610727610582565b5b835b8181101561076f57805167ffffffffffffffff81111561074d5761074c610372565b5b80860161075a898261068a565b8552602085019450505060208101905061072a565b5050509392505050565b5f82601f83011261078d5761078c610372565b5b815161079d8482602086016106f3565b91505092915050565b5f67ffffffffffffffff8211156107c0576107bf6102f6565b5b602082029050602081019050919050565b5f819050919050565b6107e3816107d1565b81146107ed575f80fd5b50565b5f815190506107fe816107da565b92915050565b5f610816610811846107a6565b610354565b9050808382526020820190506020840283018581111561083957610838610582565b5b835b81811015610862578061084e88826107f0565b84526020840193505060208101905061083b565b5050509392505050565b5f82601f8301126108805761087f610372565b5b8151610890848260208601610804565b91505092915050565b5f606082840312156108ae576108ad6102e2565b5b6108b86060610354565b90505f6108c784828501610543565b5f83015250602082015167ffffffffffffffff8111156108ea576108e961036e565b5b6108f684828501610779565b602083015250604082015167ffffffffffffffff81111561091a5761091961036e565b5b6109268482850161086c565b60408301525092915050565b5f60208284031215610947576109466102da565b5b5f82015167ffffffffffffffff811115610964576109636102de565b5b61097084828501610899565b91505092915050565b6109828161051a565b82525050565b5f81519050919050565b5f82825260208201905092915050565b5f819050602082019050919050565b5f81519050919050565b5f82825260208201905092915050565b5f6109d5826109b1565b6109df81856109bb565b93506109ef818560208601610586565b6109f8816102e6565b840191505092915050565b5f602083015f8301518482035f860152610a1d82826109cb565b9150508091505092915050565b610a3381610657565b82525050565b5f604083015f8301518482035f860152610a538282610a03565b9150506020830151610a686020860182610a2a565b508091505092915050565b5f610a7e8383610a39565b905092915050565b5f602082019050919050565b5f610a9c82610988565b610aa68185610992565b935083602082028501610ab8856109a2565b805f5b85811015610af35784840389528151610ad48582610a73565b9450610adf83610a86565b925060208a01995050600181019050610abb565b50829750879550505050505092915050565b5f81519050919050565b5f82825260208201905092915050565b5f819050602082019050919050565b5f610b38826107d1565b9050919050565b610b4881610b2e565b82525050565b5f610b598383610b3f565b60208301905092915050565b5f602082019050919050565b5f610b7b82610b05565b610b858185610b0f565b9350610b9083610b1f565b805f5b83811015610bc0578151610ba78882610b4e565b9750610bb283610b65565b925050600181019050610b93565b5085935050505092915050565b5f606083015f830151610be25f860182610979565b5060208301518482036020860152610bfa8282610a92565b91505060408301518482036040860152610c148282610b71565b9150508091505092915050565b5f604082019050610c345f8301856104f2565b8181036020830152610c468184610bcd565b90509392505050565b5f602083015f8301518482035f860152610c6982826109cb565b9150508091505092915050565b5f8115159050919050565b610c8a81610c76565b82525050565b5f63ffffffff82169050919050565b5f819050919050565b5f610cc2610cbd610cb884610c90565b610c9f565b610c90565b9050919050565b610cd281610ca8565b82525050565b5f81519050919050565b5f82825260208201905092915050565b5f819050602082019050919050565b5f604083015f8301518482035f860152610d1b82826109cb565b91505060208301518482036020860152610d3582826109cb565b9150508091505092915050565b5f610d4d8383610d01565b905092915050565b5f602082019050919050565b5f610d6b82610cd8565b610d758185610ce2565b935083602082028501610d8785610cf2565b805f5b85811015610dc25784840389528151610da38582610d42565b9450610dae83610d55565b925060208a01995050600181019050610d8a565b50829750879550505050505092915050565b5f602083015f8301518482035f860152610dee8282610d61565b9150508091505092915050565b5f606083015f830151610e105f860182610979565b5060208301518482036020860152610e288282610a92565b91505060408301518482036040860152610e428282610b71565b9150508091505092915050565b5f60a083015f830151610e645f860182610cc9565b506020830151610e776020860182610cc9565b5060408301518482036040860152610e8f8282610dd4565b91505060608301518482036060860152610ea98282610dfb565b9150506080830151610ebe6080860182610979565b508091505092915050565b5f8160070b9050919050565b610ede81610ec9565b82525050565b5f602083015f8301518482035f860152610efe82826109cb565b9150508091505092915050565b5f604083015f830151610f205f860182610ed5565b5060208301518482036020860152610f388282610ee4565b9150508091505092915050565b5f604083015f830151610f5a5f860182610ed5565b5060208301518482036020860152610f728282610f0b565b9150508091505092915050565b5f60a0820190508181035f830152610f978188610c4f565b90508181036020830152610fab8187610bcd565b9050610fba6040830186610c81565b8181036060830152610fcc8185610e4f565b90508181036080830152610fe08184610f45565b9050969550505050505056fea2646970667358221220bc9fb66c19a0073be20bc438e3354360e47b6904ffba39e99e0e5e38acf3c32464736f6c63430008190033
16 changes: 16 additions & 0 deletions packages/evm/evmtest/wiki_how_tos/L1Assets.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@iscmagic/ISC.sol";

contract L1Assets {
function withdraw(L1Address memory to) public {
ISCAssets memory allowance = ISC.sandbox.getAllowanceFrom(msg.sender);
ISC.sandbox.takeAllowedFunds(msg.sender, allowance);

ISCSendMetadata memory metadata;
ISCSendOptions memory options;
ISC.sandbox.send(to, allowance, false, metadata, options);
}
}
65 changes: 65 additions & 0 deletions packages/evm/evmtest/wiki_how_tos/L1_assets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package wiki_how_tos_test

import (
_ "embed"
"math/big"
"strings"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/solo"
"github.com/iotaledger/wasp/packages/vm/core/evm/evmtest"
)

//go:generate sh -c "solc --abi --bin --overwrite @iscmagic=`realpath ../../../vm/core/evm/iscmagic` L1Assets.sol -o ."
var (
//go:embed L1Assets.abi
L1AssetsContractABI string
//go:embed L1Assets.bin
L1AssetsContractBytecodeHex string
L1AssetsContractBytecode = common.FromHex(strings.TrimSpace(L1AssetsContractBytecodeHex))
)

func TestWithdraw(t *testing.T) {
env := evmtest.InitEVMWithSolo(t, solo.New(t), true)
privateKey, deployer := env.Chain.NewEthereumAccountWithL2Funds()

instance := env.DeployContract(privateKey, L1AssetsContractABI, L1AssetsContractBytecode)

// Define a mock L1 address for withdrawal
recipientAddress := common.HexToAddress("0x1234567890Abcdef1234567890Abcdef12345678")

// Mock the allowance that would be returned by ISC.sandbox.getAllowanceFrom
mockAllowance := isc.NewEmptyAssets()
mockAllowance.AddBaseTokens(1000)

Ginowine marked this conversation as resolved.
Show resolved Hide resolved
// Call the withdraw function and expect an event (assuming "Withdrawn" event)
var withdrawnEvent struct {
To common.Address
Amount *big.Int
}
result, err := instance.CallFnExpectEvent(nil, "Withdrawn", &withdrawnEvent, "withdraw", recipientAddress)

Check failure on line 45 in packages/evm/evmtest/wiki_how_tos/L1_assets_test.go

View workflow job for this annotation

GitHub Actions / Test

assignment mismatch: 2 variables but instance.CallFnExpectEvent returns 1 value

Check failure on line 45 in packages/evm/evmtest/wiki_how_tos/L1_assets_test.go

View workflow job for this annotation

GitHub Actions / Lint

assignment mismatch: 2 variables but instance.CallFnExpectEvent returns 1 value
Ginowine marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)

// Verify the event
assert.Equal(t, recipientAddress, withdrawnEvent.To, "Recipient address should match")
assert.Equal(t, mockAllowance.BaseTokens(), withdrawnEvent.Amount.Uint64(), "Withdrawn amount should match the allowance")

Check failure on line 50 in packages/evm/evmtest/wiki_how_tos/L1_assets_test.go

View workflow job for this annotation

GitHub Actions / Test

invalid operation: cannot call non-function mockAllowance.BaseTokens (variable of type uint64)

Check failure on line 50 in packages/evm/evmtest/wiki_how_tos/L1_assets_test.go

View workflow job for this annotation

GitHub Actions / Lint

invalid operation: cannot call non-function mockAllowance.BaseTokens (variable of type uint64)

// Get the transaction hash from the result
txHash := result.GetTxHash()
Ginowine marked this conversation as resolved.
Show resolved Hide resolved
require.NotNil(t, txHash)

// Wait for the transaction to be confirmed
receipt, err := env.Chain.EVM().TransactionReceipt(txHash)

Check failure on line 57 in packages/evm/evmtest/wiki_how_tos/L1_assets_test.go

View workflow job for this annotation

GitHub Actions / Test

assignment mismatch: 2 variables but env.Chain.EVM().TransactionReceipt returns 1 value

Check failure on line 57 in packages/evm/evmtest/wiki_how_tos/L1_assets_test.go

View workflow job for this annotation

GitHub Actions / Lint

assignment mismatch: 2 variables but env.Chain.EVM().TransactionReceipt returns 1 value (typecheck)
require.NoError(t, err)
assert.Equal(t, receipt.Status, uint64(1), "Transaction should be successful")

Ginowine marked this conversation as resolved.
Show resolved Hide resolved
// Additional check: Ensure deployer still has funds
balance, err := env.Chain.EVM().Balance(deployer, nil)
require.NoError(t, err)
assert.True(t, balance.Sign() > 0, "Deployer should still have a positive balance")
}
Loading