Skip to content

Commit

Permalink
Merge pull request #714 from eosnetworkfoundation/elmato/add-tests-fo…
Browse files Browse the repository at this point in the history
…r-different-token

Add basic test for non-EOS gas token
  • Loading branch information
elmato committed May 16, 2024
2 parents 150166f + 9c66828 commit 43b4ea5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ include_directories(
)

add_eosio_test_executable( unit_test
${CMAKE_SOURCE_DIR}/different_gas_token_tests.cpp
${CMAKE_SOURCE_DIR}/version_tests.cpp
${CMAKE_SOURCE_DIR}/account_id_tests.cpp
${CMAKE_SOURCE_DIR}/basic_evm_tester.cpp
Expand Down
14 changes: 10 additions & 4 deletions tests/basic_evm_tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ basic_evm_tester::basic_evm_tester(std::string native_symbol_str) :

asset basic_evm_tester::make_asset(int64_t amount) const { return asset(amount, native_symbol); }

transaction_trace_ptr basic_evm_tester::transfer_token(name from, name to, asset quantity, std::string memo)
transaction_trace_ptr basic_evm_tester::transfer_token(name from, name to, asset quantity, std::string memo, name acct)
{
return push_action(
token_account_name, "transfer"_n, from, mvo()("from", from)("to", to)("quantity", quantity)("memo", memo));
acct, "transfer"_n, from, mvo()("from", from)("to", to)("quantity", quantity)("memo", memo));
}

action basic_evm_tester::get_action( account_name code, action_name acttype, vector<permission_level> auths,
Expand Down Expand Up @@ -281,7 +281,8 @@ void basic_evm_tester::init(const uint64_t chainid,
const uint64_t gas_price,
const uint32_t miner_cut,
const std::optional<asset> ingress_bridge_fee,
const bool also_prepare_self_balance)
const bool also_prepare_self_balance,
const std::optional<name> token_contract)
{
mvo fee_params;
fee_params("gas_price", gas_price)("miner_cut", miner_cut);
Expand All @@ -292,7 +293,12 @@ void basic_evm_tester::init(const uint64_t chainid,
fee_params("ingress_bridge_fee", "0.0000 EOS");
}

push_action(evm_account_name, "init"_n, evm_account_name, mvo()("chainid", chainid)("fee_params", fee_params));
if(!token_contract.has_value()) {
push_action(evm_account_name, "init"_n, evm_account_name, mvo()("chainid", chainid)("fee_params", fee_params));
} else {
push_action(evm_account_name, "init"_n, evm_account_name, mvo()("chainid", chainid)("fee_params", fee_params)("token_contract", token_contract));
}


if (also_prepare_self_balance) {
prepare_self_balance();
Expand Down
5 changes: 3 additions & 2 deletions tests/basic_evm_tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ class basic_evm_tester : public evm_validating_tester

asset make_asset(int64_t amount) const;

transaction_trace_ptr transfer_token(name from, name to, asset quantity, std::string memo = "");
transaction_trace_ptr transfer_token(name from, name to, asset quantity, std::string memo = "", name acct=token_account_name);

action get_action( account_name code, action_name acttype, vector<permission_level> auths,
const bytes& data )const;
Expand All @@ -415,7 +415,8 @@ class basic_evm_tester : public evm_validating_tester
const uint64_t gas_price = suggested_gas_price,
const uint32_t miner_cut = suggested_miner_cut,
const std::optional<asset> ingress_bridge_fee = std::nullopt,
const bool also_prepare_self_balance = true);
const bool also_prepare_self_balance = true,
const std::optional<name> token_contract = std::nullopt);

template <typename... T>
void start_block(T... t) {
Expand Down
87 changes: 87 additions & 0 deletions tests/different_gas_token_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "basic_evm_tester.hpp"

using namespace eosio::testing;
using namespace evm_test;

#include <eosevm/block_mapping.hpp>

struct different_gas_token_tester : basic_evm_tester
{
static constexpr name miner_account_name = "theminer"_n;
static constexpr name gas_token_account_name = "gastoken"_n;

symbol gas_symbol = symbol::from_string("6,GAS");
extended_asset gas_token{asset(0, gas_symbol), gas_token_account_name};

different_gas_token_tester()
{
create_accounts({miner_account_name});

create_accounts({gas_token_account_name});
set_code(gas_token_account_name, testing::contracts::eosio_token_wasm());
set_abi(gas_token_account_name, testing::contracts::eosio_token_abi().data());
produce_block();

push_action(gas_token_account_name, "create"_n, gas_token_account_name,
mvo()("issuer", gas_token_account_name)("maximum_supply", asset(10'000'000'000'0000, gas_symbol)));
push_action(gas_token_account_name, "issue"_n, gas_token_account_name,
mvo()("to", gas_token_account_name)("quantity", asset(1'000'000'000'0000, gas_symbol))("memo", ""));
produce_block();
}

transaction_trace_ptr transfer_gas_token(name from, name to, asset quantity, std::string memo="") {
return transfer_token(from, to, quantity, memo, gas_token_account_name);
}

asset gas_balance(name acct) {
return get_currency_balance(gas_token_account_name, gas_symbol, acct);
}

};

BOOST_AUTO_TEST_SUITE(different_gas_token_tests)

BOOST_FIXTURE_TEST_CASE(basic_test, different_gas_token_tester)
try {

// Init with GAS token
init(
evm_chain_id,
suggested_gas_price,
suggested_miner_cut,
asset::from_string("0.000001 GAS"),
false,
gas_token_account_name
);

// Prepare self balance
transfer_gas_token(gas_token_account_name, evm_account_name, asset::from_string("1.000000 GAS"), evm_account_name.to_string());

open(miner_account_name);

auto cfg = get_config();
BOOST_REQUIRE(cfg.token_contract.has_value() && cfg.token_contract == gas_token_account_name );

// Fund evm1 address with 10.000000 GAS
evm_eoa evm1;
transfer_gas_token(gas_token_account_name, evm_account_name, asset::from_string("10.000000 GAS"), evm1.address_0x());

BOOST_REQUIRE(evm_balance(evm1) == intx::uint256(balance_and_dust{asset::from_string("9.999999 GAS"), 0}));
BOOST_REQUIRE(vault_balance(evm_account_name) == (balance_and_dust{asset::from_string("1.000001 GAS"), 0}));
BOOST_REQUIRE(vault_balance(miner_account_name) == (balance_and_dust{asset::from_string("0.000000 GAS"), 0ULL}));

// Transfer 1 Wei to evm2
evm_eoa evm2;
auto tx = generate_tx(evm2.address, 1);
evm1.sign(tx);
pushtx(tx, miner_account_name);

BOOST_REQUIRE(evm_balance(evm1) == intx::uint256(balance_and_dust{asset::from_string("9.996848 GAS"), 999999999999}));
BOOST_REQUIRE(evm_balance(evm2) == intx::uint256(balance_and_dust{asset::from_string("0.000000 GAS"), 1}));
BOOST_REQUIRE(vault_balance(evm_account_name) == (balance_and_dust{asset::from_string("1.002836 GAS"), 0}));
BOOST_REQUIRE(vault_balance(miner_account_name) == (balance_and_dust{asset::from_string("0.000315 GAS"), 0}));

}
FC_LOG_AND_RETHROW()

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 43b4ea5

Please sign in to comment.