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

Add contract and solo tests #3445

Closed
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
4 changes: 4 additions & 0 deletions documentation/tutorial-examples/solidity/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.bin
*.abi
node_modules
package-lock.json
17 changes: 17 additions & 0 deletions documentation/tutorial-examples/solidity/GetAllowance.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
35 changes: 35 additions & 0 deletions documentation/tutorial-examples/solidity/NativeTokenBalance.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}
52 changes: 52 additions & 0 deletions documentation/tutorial-examples/solidity/get_allowance_test.go
Original file line number Diff line number Diff line change
@@ -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)

}
5 changes: 5 additions & 0 deletions documentation/tutorial-examples/solidity/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@iota/iscmagic": "^1.3.0-rc.1"
}
}
52 changes: 52 additions & 0 deletions documentation/tutorial-examples/solidity/simple_test.go
Original file line number Diff line number Diff line change
@@ -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)

}