diff --git a/packages/evm/evmtest/wiki_how_tos/TakeAllowance.abi b/packages/evm/evmtest/wiki_how_tos/TakeAllowance.abi new file mode 100644 index 0000000000..2b72a42523 --- /dev/null +++ b/packages/evm/evmtest/wiki_how_tos/TakeAllowance.abi @@ -0,0 +1,38 @@ +[ + { + "inputs": [ + { + "internalType": "address", + "name": "_address", + "type": "address" + }, + { + "internalType": "bytes32", + "name": "_allowanceNFTID", + "type": "bytes32" + } + ], + "name": "allow", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_address", + "type": "address" + }, + { + "internalType": "bytes32", + "name": "_allowanceNFTID", + "type": "bytes32" + } + ], + "name": "takeAllowedFunds", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } +] \ No newline at end of file diff --git a/packages/evm/evmtest/wiki_how_tos/TakeAllowance.bin b/packages/evm/evmtest/wiki_how_tos/TakeAllowance.bin new file mode 100644 index 0000000000..43441be276 --- /dev/null +++ b/packages/evm/evmtest/wiki_how_tos/TakeAllowance.bin @@ -0,0 +1 @@ +6080604052348015600e575f80fd5b506107188061001c5f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c806374e47c2614610038578063aab19db414610054575b5f80fd5b610052600480360381019061004d919061032b565b610070565b005b61006e6004803603810190610069919061032b565b610170565b005b5f600167ffffffffffffffff81111561008c5761008b610369565b5b6040519080825280602002602001820160405280156100ba5781602001602082028036833780820191505090505b50905081815f815181106100d1576100d0610396565b5b6020026020010181815250506100e5610270565b81816040018190525073107400000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff1663423fa15185836040518363ffffffff1660e01b815260040161013d9291906106b4565b5f604051808303815f87803b158015610154575f80fd5b505af1158015610166573d5f803e3d5ffd5b5050505050505050565b5f600167ffffffffffffffff81111561018c5761018b610369565b5b6040519080825280602002602001820160405280156101ba5781602001602082028036833780820191505090505b50905081815f815181106101d1576101d0610396565b5b6020026020010181815250506101e5610270565b81816040018190525073107400000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff1663765f4f9e85836040518363ffffffff1660e01b815260040161023d9291906106b4565b5f604051808303815f87803b158015610254575f80fd5b505af1158015610266573d5f803e3d5ffd5b5050505050505050565b60405180606001604052805f67ffffffffffffffff16815260200160608152602001606081525090565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6102c78261029e565b9050919050565b6102d7816102bd565b81146102e1575f80fd5b50565b5f813590506102f2816102ce565b92915050565b5f819050919050565b61030a816102f8565b8114610314575f80fd5b50565b5f8135905061032581610301565b92915050565b5f80604083850312156103415761034061029a565b5b5f61034e858286016102e4565b925050602061035f85828601610317565b9150509250929050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b7f4e487b71000000000000000000000000000000000000000000000000000000005f52603260045260245ffd5b6103cc816102bd565b82525050565b5f67ffffffffffffffff82169050919050565b6103ee816103d2565b82525050565b5f81519050919050565b5f82825260208201905092915050565b5f819050602082019050919050565b5f81519050919050565b5f82825260208201905092915050565b8281835e5f83830152505050565b5f601f19601f8301169050919050565b5f61045f8261041d565b6104698185610427565b9350610479818560208601610437565b61048281610445565b840191505092915050565b5f602083015f8301518482035f8601526104a78282610455565b9150508091505092915050565b5f819050919050565b6104c6816104b4565b82525050565b5f604083015f8301518482035f8601526104e6828261048d565b91505060208301516104fb60208601826104bd565b508091505092915050565b5f61051183836104cc565b905092915050565b5f602082019050919050565b5f61052f826103f4565b61053981856103fe565b93508360208202850161054b8561040e565b805f5b8581101561058657848403895281516105678582610506565b945061057283610519565b925060208a0199505060018101905061054e565b50829750879550505050505092915050565b5f81519050919050565b5f82825260208201905092915050565b5f819050602082019050919050565b5f6105cb826102f8565b9050919050565b6105db816105c1565b82525050565b5f6105ec83836105d2565b60208301905092915050565b5f602082019050919050565b5f61060e82610598565b61061881856105a2565b9350610623836105b2565b805f5b8381101561065357815161063a88826105e1565b9750610645836105f8565b925050600181019050610626565b5085935050505092915050565b5f606083015f8301516106755f8601826103e5565b506020830151848203602086015261068d8282610525565b915050604083015184820360408601526106a78282610604565b9150508091505092915050565b5f6040820190506106c75f8301856103c3565b81810360208301526106d98184610660565b9050939250505056fea2646970667358221220669483c9a406db2f4f2440e5ee0c6ac097afff492422bb754e3cb38dd399a06264736f6c634300081a0033 \ No newline at end of file diff --git a/packages/evm/evmtest/wiki_how_tos/TakeAllowance.sol b/packages/evm/evmtest/wiki_how_tos/TakeAllowance.sol new file mode 100644 index 0000000000..6099077bb7 --- /dev/null +++ b/packages/evm/evmtest/wiki_how_tos/TakeAllowance.sol @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.0; + +import "@iscmagic/ISC.sol"; + +contract Allowance { + function allow(address _address, bytes32 _allowanceNFTID) public { + NFTID[] memory nftIDs = new NFTID[](1); + nftIDs[0] = NFTID.wrap(_allowanceNFTID); + ISCAssets memory assets; + assets.nfts = nftIDs; + ISC.sandbox.allow(_address, assets); + } + + function takeAllowedFunds( + address _address, + bytes32 _allowanceNFTID + ) public { + NFTID[] memory nftIDs = new NFTID[](1); + nftIDs[0] = NFTID.wrap(_allowanceNFTID); + ISCAssets memory assets; + assets.nfts = nftIDs; + ISC.sandbox.takeAllowedFunds(_address, assets); + } +} diff --git a/packages/evm/evmtest/wiki_how_tos/how_tos_test.go b/packages/evm/evmtest/wiki_how_tos/how_tos_test.go index 2d0186b217..52ec7b5719 100644 --- a/packages/evm/evmtest/wiki_how_tos/how_tos_test.go +++ b/packages/evm/evmtest/wiki_how_tos/how_tos_test.go @@ -16,13 +16,19 @@ import ( "github.com/iotaledger/wasp/packages/vm/core/evm/evmtest" ) -//go:generate sh -c "solc --abi --bin --overwrite @iscmagic=`realpath ../../../vm/core/evm/iscmagic` GetBalance.sol -o ." +//go:generate sh -c "solc --abi --bin --overwrite @iscmagic=`realpath ../../../vm/core/evm/iscmagic` GetBalance.sol TakeAllowance.sol Allowance.sol -o ." var ( //go:embed GetBalance.abi GetBalanceContractABI string //go:embed GetBalance.bin GetBalanceContractBytecodeHex string GetBalanceContractBytecode = common.FromHex(strings.TrimSpace(GetBalanceContractBytecodeHex)) + + //go:embed TakeAllowance.abi + TakeAllowanceContractABI string + //go:embed TakeAllowance.bin + TakeAllowanceContractBytecodeHex string + TakeAllowanceContractBytecode = common.FromHex(strings.TrimSpace(TakeAllowanceContractBytecodeHex)) ) func TestBaseBalance(t *testing.T) { @@ -112,3 +118,34 @@ func TestAgentID(t *testing.T) { instance.CallFnExpectEvent(nil, "GotAgentID", &agentID, "getAgentID") assert.Equal(t, senderAgentID.Bytes(), agentID) } + +func TestTakeAllowance(t *testing.T) { + env := evmtest.InitEVMWithSolo(t, solo.New(t), true) + privateKey, deployer := env.Chain.NewEthereumAccountWithL2Funds() + + instance := env.DeployContract(privateKey, TakeAllowanceContractABI, TakeAllowanceContractBytecode) + + senderAgentID := isc.NewEthereumAddressAgentID(env.Chain.ChainID, deployer) + + // mint an NFToken to the contract deployer + mockMetaData := []byte("sesa") + nfti, info, err := env.Chain.Env.MintNFTL1(env.Chain.OriginatorPrivateKey, env.Chain.OriginatorAddress, mockMetaData) + require.NoError(t, err) + env.Chain.MustDepositNFT(nfti, env.Chain.OriginatorAgentID, env.Chain.OriginatorPrivateKey) + + transfer := isc.NewEmptyAssets() + transfer.AddNFTs(info.NFTID) + + // send the NFT to the contract deployer + err = env.Chain.SendFromL2ToL2Account(transfer, senderAgentID, env.Chain.OriginatorPrivateKey) + require.NoError(t, err) + + // allowing funds + instance.CallFn(nil, "allow", deployer, nfti.ID) + assert.Nil(t, err) + + // taking allowed funds + result, err := instance.CallFn(nil, "takeAllowedFunds", deployer, nfti.ID) + // assert.Nil(t, err) + t.Log("Take Allowed funds :", result, err) +}