diff --git a/documentation/tutorial-examples/solidity/.gitignore b/documentation/tutorial-examples/solidity/.gitignore new file mode 100644 index 0000000000..2acdfb89a1 --- /dev/null +++ b/documentation/tutorial-examples/solidity/.gitignore @@ -0,0 +1,4 @@ +*.bin +*.abi +node_modules +package-lock.json \ No newline at end of file diff --git a/documentation/tutorial-examples/solidity/GetAllowance.sol b/documentation/tutorial-examples/solidity/GetAllowance.sol new file mode 100644 index 0000000000..54cc497ab1 --- /dev/null +++ b/documentation/tutorial-examples/solidity/GetAllowance.sol @@ -0,0 +1,17 @@ + +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.0; + +import "./node_modules/@iota/iscmagic/ISC.sol"; + +contract GetAllowance { + event AllowanceFrom(ISCAssets assets); + event AllowanceTo(ISCAssets assets); + event Allowance(ISCAssets assets); + + function getAllowanceFrom(address _address) public { + ISCAssets memory assets = ISC.sandbox.getAllowanceFrom(_address); + emit AllowanceFrom(assets); + } +} \ No newline at end of file diff --git a/documentation/tutorial-examples/solidity/NativeTokenBalance.sol b/documentation/tutorial-examples/solidity/NativeTokenBalance.sol new file mode 100644 index 0000000000..f27fac62a7 --- /dev/null +++ b/documentation/tutorial-examples/solidity/NativeTokenBalance.sol @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.0; + +import "./node_modules/@iota/iscmagic/ISC.sol"; + +contract NativeTokenBalance { + event NativeTokenBalance(uint balance); + + function getNativeTokenBalance(bytes memory nativeTokenID) public { + ISCAgentID memory agentID = ISC.sandbox.getSenderAccount(); + + ISCDict memory params = ISCDict(new ISCDictItem[](2)); + params.items[0] = ISCDictItem("a", agentID.data); + params.items[1] = ISCDictItem("N", nativeTokenID); + + ISCDict memory result = ISC.sandbox.callView( + ISC.util.hn("accounts"), + ISC.util.hn("balanceNativeToken"), + params + ); + + emit NativeTokenBalance(bytesToUint(result.items[0].value)); + } + + function bytesToUint(bytes memory b) internal pure virtual returns (uint256) { + require(b.length <= 32, "Bytes length exceeds 32."); + return abi.decode(abi.encodePacked(new bytes(32 - b.length), b), (uint256)); + } + + + function simpleFunction() public pure returns (uint) { + return 42; + } +} \ No newline at end of file diff --git a/documentation/tutorial-examples/solidity/get_allowance_test.go b/documentation/tutorial-examples/solidity/get_allowance_test.go new file mode 100644 index 0000000000..37c253219d --- /dev/null +++ b/documentation/tutorial-examples/solidity/get_allowance_test.go @@ -0,0 +1,52 @@ +package solidity_test + +import ( + _ "embed" + "math/big" + "strings" + "testing" + + "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/common" + "github.com/iotaledger/wasp/packages/solo" +) + +// compile the solidity contract + +//go:generate solc --abi --bin --overwrite GetAllowance.sol -o . +var ( + //go:embed GetAllowance.abi + GetAllowanceContarctABI string + //go:embed GetAllowance.bin + GetAllowanceContractBytecodeHex string + GetAllowanceeContractBytecode = common.FromHex(strings.TrimSpace(GetAllowanceContractBytecodeHex)) +) + +func TestGetAllowance(t *testing.T) { + env := solo.New(t) + chain := env.NewChain() + + chainID, chainOwnerID, _ := chain.GetInfo() + + t.Log("chain", chainID.String()) + t.Log("chain owner ID: ", chainOwnerID.String()) + + private_key, user_address := chain.NewEthereumAccountWithL2Funds() + t.Log("Private key of the userWallet is: ", private_key) + t.Log("Address of the userWallet is: ", user_address) + + contract_addr, abi := chain.DeployEVMContract(private_key, GetAllowanceContarctABI, GetAllowanceeContractBytecode, &big.Int{}) + + t.Log("contract_addr: ", contract_addr, abi) + + callArgs, _ := abi.Pack("getAllowanceFrom", user_address) + callMsg := ethereum.CallMsg{ + To: &contract_addr, + Data: callArgs, + } + + result, _ := chain.EVM().CallContract(callMsg, nil) + + t.Log("result: ", result) + +} diff --git a/documentation/tutorial-examples/solidity/package.json b/documentation/tutorial-examples/solidity/package.json new file mode 100644 index 0000000000..d3e3b3192d --- /dev/null +++ b/documentation/tutorial-examples/solidity/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "@iota/iscmagic": "^1.3.0-rc.1" + } +} diff --git a/documentation/tutorial-examples/solidity/simple_test.go b/documentation/tutorial-examples/solidity/simple_test.go new file mode 100644 index 0000000000..aa70311602 --- /dev/null +++ b/documentation/tutorial-examples/solidity/simple_test.go @@ -0,0 +1,52 @@ +package solidity_test + +import ( + _ "embed" + "math/big" + "strings" + "testing" + + "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/common" + "github.com/iotaledger/wasp/packages/solo" + // import the solidity package +) + +// compile the solidity contract + +//go:generate solc --abi --bin --overwrite NativeTokenBalance.sol -o . +var ( + //go:embed NativeTokenBalance.abi + NativeTokenBalanceContractABI string + //go:embed NativeTokenBalance.bin + nativeTokenBalanceContractBytecodeHex string + NativeTokenBalanceContractBytecode = common.FromHex(strings.TrimSpace(nativeTokenBalanceContractBytecodeHex)) +) + +func TestNativeTokenBalance(t *testing.T) { + env := solo.New(t) + chain := env.NewChain() + + chainID, chainOwnerID, _ := chain.GetInfo() + + t.Log("chainID: ", chainID.String()) + t.Log("chain owner ID: ", chainOwnerID.String()) + + private_key, user_address := chain.NewEthereumAccountWithL2Funds() + + t.Log("Address of the userWallet is: ", private_key) + t.Log("Address of the userWallet1 is: ", user_address) + + contract_addr, abi := chain.DeployEVMContract(private_key, NativeTokenBalanceContractABI, NativeTokenBalanceContractBytecode, &big.Int{}) + + callArgs, _ := abi.Pack("simpleFunction") + callMsg := ethereum.CallMsg{ + To: &contract_addr, + Data: callArgs, + } + + result, _ := chain.EVM().CallContract(callMsg, nil) + + t.Log("result: ", result) + +}