Skip to content

Commit

Permalink
support vmb tx
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoGuo621 committed Jul 17, 2023
1 parent cb79198 commit 8be952a
Show file tree
Hide file tree
Showing 10 changed files with 463 additions and 22 deletions.
134 changes: 112 additions & 22 deletions app/app_parallel_test.go

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app

import (
"time"

"github.com/spf13/viper"

"github.com/okex/exchain/libs/cosmos-sdk/codec"
Expand Down Expand Up @@ -79,11 +81,13 @@ func SetupWithGenesisAccounts(isCheckTx bool, genAccs []authexported.GenesisAcco
}

// Initialize the chain
testTime, _ := time.Parse("2006-01-02 15:04:05", "2017-04-11 13:33:37")
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
ChainId: setupOption.chainId,
Time: testTime,
},
)

Expand Down
Binary file added app/testdata/cw20.wasm
Binary file not shown.
40 changes: 40 additions & 0 deletions app/testdata/freecall.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.18;
// pragma solidity >=0.7.0 <0.9.0;


contract FreeCall {
address public constant moduleAddress =
address(0x1033796B018B2bf0Fc9CB88c0793b2F275eDB624);

event __OKCCallToWasm(string wasmAddr, uint256 value, string data);

function callByWasm(string memory callerWasmAddr,string memory data) public payable returns (string memory response) {
string memory temp1 = strConcat("callByWasm return: ",callerWasmAddr);
string memory temp2 = strConcat(temp1," ---data: ");
string memory temp3 = strConcat(temp2,data);
return temp3;
}


function callToWasm(string memory wasmAddr, uint256 value, string memory data) public returns (bool success){
emit __OKCCallToWasm(wasmAddr,value,data);
return true;
}


function strConcat(string memory _a, string memory _b) internal returns (string memory){
bytes memory _ba = bytes(_a);
bytes memory _bb = bytes(_b);
string memory ret = new string(_ba.length + _bb.length);
bytes memory bret = bytes(ret);
uint k = 0;
for (uint i = 0; i < _ba.length; i++) {
bret[k++] = _ba[i];
}
for (uint i = 0; i < _bb.length; i++) {
bret[k++] = _bb[i];
}
return string(ret);
}
}
Binary file added app/testdata/freecall.wasm
Binary file not shown.
73 changes: 73 additions & 0 deletions app/testdata/precompile.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract ContractA {

address precomplieContarct = 0x0000000000000000000000000000000000000100;
uint256 public number;
event pushLog(string data);

function callWasm(string memory wasmAddr, string memory msgData,bool requireASuccess) public payable returns (bytes memory response){
number = number + 1;
(bool success, bytes memory data) = precomplieContarct.call{value: msg.value} (
abi.encodeWithSignature("callToWasm(string,string)", wasmAddr,msgData)
);
if (requireASuccess) {
require(success);
string memory res = abi.decode(data,(string));
emit pushLog(res);
}
number = number + 1;
return data;
}

function queryWasm(string memory msgData,bool requireASuccess) public payable returns (bytes memory response){
number = number + 1;
(bool success, bytes memory data) = precomplieContarct.call{value: msg.value} (
abi.encodeWithSignature("queryToWasm(string)",msgData)
);
if (requireASuccess) {
require(success);
string memory res = abi.decode(data,(string));
emit pushLog(res);
}
number = number + 1;
return data;
}

function callToWasm(string memory wasmAddr, string memory data) public payable returns (string memory response) {
return "";
}

function queryToWasm(string memory data) public view returns (string memory response) {
return "";
}
}

contract ContractB {
uint256 public number;

function callWasm(address contractA ,string memory wasmAddr, string memory msgData, bool requireASuccess,bool requireBSuccess) public payable returns (bytes memory response){
number = number + 1;
(bool success, bytes memory data) = contractA.call{value: msg.value} (
abi.encodeWithSignature("callWasm(string,string,bool)", wasmAddr,msgData,requireASuccess)
);
number = number + 1;
if (requireBSuccess) {
require(success);
}
return data;
}

function queryWasm(address contractA , string memory msgData, bool requireASuccess,bool requireBSuccess) public payable returns (bytes memory response){
number = number + 1;
(bool success, bytes memory data) = contractA.call{value: msg.value} (
abi.encodeWithSignature("queryWasm(string,bool)",msgData,requireASuccess)
);
number = number + 1;
if (requireBSuccess) {
require(success);
}
return data;
}
}
Binary file added app/testdata/precompile.wasm
Binary file not shown.
234 changes: 234 additions & 0 deletions app/testdata/testerc20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.7;

contract Exchange is ERC20 {
address public constant moduleAddress =
address(0xc63cf6c8E1f3DF41085E9d8Af49584dae1432b4f);

string public wasmContractAddress = "ex14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s6fqu27";

event __OKCSendToWasm(string wasmAddr, string recipient, uint256 amount);

function initialize(string memory denom_, uint8 decimals_) public {
__ERC20_init(denom_, denom_, decimals_);
}

function native_denom() public view returns (string memory) {
return symbol();
}

function updatewasmContractAddress(string memory addr) public {
wasmContractAddress = addr;
}

function mint(address recipient,uint256 amount) public {
_mint(recipient, amount);
}


function mintERC20(string calldata caller, address recipient,uint256 amount) public returns (bool) {
require(msg.sender == moduleAddress);
require(keccak256(abi.encodePacked(caller)) == keccak256(abi.encodePacked(wasmContractAddress)));
_mint(recipient, amount);
return true;
}


// send an "amount" of the contract token to recipient on wasm
function send_to_wasm(string memory recipient,string memory wasmContract , uint256 amount) public {
_burn(msg.sender, amount);
emit __OKCSendToWasm(wasmContract,recipient, amount);
}
}

contract ERC20 {
bool private initialized;

string private _name;
string private _symbol;
uint8 private _decimals;
uint256 private _totalSupply;

mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;

event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);

function __ERC20_init(
string memory name_,
string memory symbol_,
uint8 decimals_
) internal {
require(!initialized, "ERC20: already initialized;");
initialized = true;

_name = name_;
_symbol = symbol_;
_decimals = decimals_;
}

function name() public view virtual returns (string memory) {
return _name;
}

function symbol() public view virtual returns (string memory) {
return _symbol;
}

function decimals() public view virtual returns (uint8) {
return _decimals;
}

function totalSupply() public view virtual returns (uint256) {
return _totalSupply;
}

function balanceOf(address account) public view virtual returns (uint256) {
return _balances[account];
}

function transfer(address to, uint256 amount)
public
virtual
returns (bool)
{
address owner = msg.sender;
_transfer(owner, to, amount);
return true;
}

function allowance(address owner, address spender)
public
view
virtual
returns (uint256)
{
return _allowances[owner][spender];
}

function approve(address spender, uint256 amount)
public
virtual
returns (bool)
{
address owner = msg.sender;
_approve(owner, spender, amount);
return true;
}

function transferFrom(
address from,
address to,
uint256 amount
) public virtual returns (bool) {
address spender = msg.sender;
_spendAllowance(from, spender, amount);
_transfer(from, to, amount);
return true;
}

function increaseAllowance(address spender, uint256 addedValue)
public
virtual
returns (bool)
{
address owner = msg.sender;
_approve(owner, spender, _allowances[owner][spender] + addedValue);
return true;
}

function decreaseAllowance(address spender, uint256 subtractedValue)
public
virtual
returns (bool)
{
address owner = msg.sender;
uint256 currentAllowance = _allowances[owner][spender];
require(
currentAllowance >= subtractedValue,
"ERC20: decreased allowance below zero"
);
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}

return true;
}

function _transfer(
address from,
address to,
uint256 amount
) internal virtual {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");

uint256 fromBalance = _balances[from];
require(
fromBalance >= amount,
"ERC20: transfer amount exceeds balance"
);
unchecked {
_balances[from] = fromBalance - amount;
}
_balances[to] += amount;

emit Transfer(from, to, amount);
}

function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");

_totalSupply += amount;
_balances[account] += amount;
emit Transfer(address(0), account, amount);
}

function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");

uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
}
_totalSupply -= amount;

emit Transfer(account, address(0), amount);
}

function _approve(
address owner,
address spender,
uint256 amount
) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");

_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}

function _spendAllowance(
address owner,
address spender,
uint256 amount
) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(
currentAllowance >= amount,
"ERC20: insufficient allowance"
);
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
}

0 comments on commit 8be952a

Please sign in to comment.