diff --git a/documentation/tutorial-examples/solidity/GetBalance.sol b/documentation/tutorial-examples/solidity/GetBalance.sol new file mode 100644 index 0000000000..1fc859a664 --- /dev/null +++ b/documentation/tutorial-examples/solidity/GetBalance.sol @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: MIT + +pragma solidity 0.8.0; + +import "./node_modules/@iota/iscmagic/ISC.sol"; + +contract GetBalance { + event GotAgentID(bytes agentID); + event GotBaseBalance(uint64 baseBalance); + event GotNativeTokenBalance(uint256 nativeTokenBalance); + event GotNFTIDs(uint256 nftBalance); + + function getBalance(bytes memory nativeTokenID) public { + ISCAgentID memory agentID = ISC.sandbox.getSenderAccount(); + emit GotAgentID(agentID.data); + + uint64 baseBalance = ISC.accounts.getL2BalanceBaseTokens(agentID); + emit GotBaseBalance(baseBalance); + + NativeTokenID memory id = NativeTokenID({ data: nativeTokenID}); + uint256 nativeTokens = ISC.accounts.getL2BalanceNativeTokens(id, agentID); + emit GotNativeTokenBalance(nativeTokens); + + uint256 nfts = ISC.accounts.getL2NFTAmount(agentID); + emit GotNativeTokenBalance(nfts); + + } +} \ No newline at end of file diff --git a/documentation/tutorial-examples/solidity/NativeTokenBalance.sol b/documentation/tutorial-examples/solidity/NativeTokenBalance.sol deleted file mode 100644 index f27fac62a7..0000000000 --- a/documentation/tutorial-examples/solidity/NativeTokenBalance.sol +++ /dev/null @@ -1,35 +0,0 @@ -// 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_balance_test.go b/documentation/tutorial-examples/solidity/get_balance_test.go new file mode 100644 index 0000000000..75c8698af4 --- /dev/null +++ b/documentation/tutorial-examples/solidity/get_balance_test.go @@ -0,0 +1,62 @@ +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 GetBalance.sol -o . +var ( + //go:embed GetBalance.abi + GetBalanceContractABI string + //go:embed GetBalance.bin + GetBalanceContractBytecodeHex string + GetBalanceContractBytecode = common.FromHex(strings.TrimSpace(GetBalanceContractBytecodeHex)) +) + +func TestGetBalance(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, GetBalanceContractABI, GetBalanceContractBytecode, &big.Int{}) + + serial, token_id, err := chain.NewNativeTokenParams(1000000000).CreateFoundry() + + t.Log("Contract address: ", serial) + t.Log("Contract address: ", token_id) + t.Log("Contract address: ", err) + + callArgs, err := abi.Pack("getBalance", []byte(token_id.ToHex())) + callMsg := ethereum.CallMsg{ + To: &contract_addr, + Data: callArgs, + } + + t.Log("Err: ", err) + + result, err := chain.EVM().CallContract(callMsg, nil) + + t.Log("Result: ", result) + + t.Log("contract err: ", err) + +} diff --git a/documentation/tutorial-examples/solidity/simple_test.go b/documentation/tutorial-examples/solidity/simple_test.go deleted file mode 100644 index aa70311602..0000000000 --- a/documentation/tutorial-examples/solidity/simple_test.go +++ /dev/null @@ -1,52 +0,0 @@ -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) - -}