Skip to content

abort bridgeTransfer if dust exist #74

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

Merged
merged 5 commits into from
Mar 26, 2025
Merged
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
6 changes: 3 additions & 3 deletions antelope_contracts/contracts/erc20/include/erc20/erc20.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ class [[eosio::contract]] erc20 : public contract {
struct [[eosio::table("tokens")]] token_t {
uint64_t id = 0;
eosio::name token_contract;
bytes address; // <-- proxy contract addr
bytes address; // <-- portal contract addr
eosio::asset ingress_fee;
eosio::asset balance; // total amount in EVM side, only valid for native->evm tokens
eosio::asset fee_balance;
uint8_t erc20_precision = 0;
eosio::binary_extension<bool> from_evm_to_native{false};
eosio::binary_extension<bytes> original_erc20_token_address; // set if token is originated from EVM

uint64_t primary_key() const {
return id;
Expand All @@ -126,7 +127,7 @@ class [[eosio::contract]] erc20 : public contract {
return false;
}

EOSLIB_SERIALIZE(token_t, (id)(token_contract)(address)(ingress_fee)(balance)(fee_balance)(erc20_precision)(from_evm_to_native));
EOSLIB_SERIALIZE(token_t, (id)(token_contract)(address)(ingress_fee)(balance)(fee_balance)(erc20_precision)(from_evm_to_native)(original_erc20_token_address));
};
typedef eosio::multi_index<"tokens"_n, token_t,
indexed_by<"by.symbol"_n, const_mem_fun<token_t, uint128_t, &token_t::by_contract_symbol> >,
Expand Down Expand Up @@ -168,7 +169,6 @@ class [[eosio::contract]] erc20 : public contract {

uint64_t get_next_nonce();
void handle_erc20_transfer(const token_t &token, eosio::asset quantity, const std::string &memo);
void handle_call_upgrade(const bytes& proxy_address);

private:
void regtokenwithcodebytes(eosio::name token_contract, const bytes& address_bytes, std::string evm_token_name, std::string evm_token_symbol, const eosio::asset& ingress_fee, const eosio::asset &egress_fee, uint8_t erc20_precision);
Expand Down
77 changes: 40 additions & 37 deletions antelope_contracts/contracts/erc20/src/erc20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ void erc20::regevm2nat(std::string erc20_token_address,
v.fee_balance = v.balance;
v.erc20_precision = erc20_precision;
v.from_evm_to_native = true;
v.original_erc20_token_address = *erc20_address_bytes;
});
}

Expand Down Expand Up @@ -409,11 +410,9 @@ void erc20::onbridgemsg(const bridge_message_t &message) {

intx::uint256 value = read_uint256(msg, 4 + 32);

if (itr->is_evm_to_native() == false) {
intx::uint256 mult = intx::exp(10_u256, intx::uint256(itr->erc20_precision - itr->ingress_fee.symbol.precision()));
check(value % mult == 0_u256, "bridge amount can not have dust");
value /= mult;
}
intx::uint256 mult = intx::exp(10_u256, intx::uint256(itr->erc20_precision - itr->ingress_fee.symbol.precision()));
check(value % mult == 0_u256, "bridge amount can not have dust");
value /= mult;

uint64_t dest_amount = (uint64_t)value;
check(intx::uint256(dest_amount) == value && dest_amount < (1ull<<62)-1, "bridge amount value overflow");
Expand Down Expand Up @@ -489,9 +488,7 @@ void erc20::handle_erc20_transfer(const token_t &token, eosio::asset quantity, c

intx::uint256 value((uint64_t)quantity.amount);

if (token.is_evm_to_native() == false) {
value *= intx::exp(10_u256, intx::uint256(token.erc20_precision - quantity.symbol.precision()));
}
value *= intx::exp(10_u256, intx::uint256(token.erc20_precision - quantity.symbol.precision()));

uint8_t value_buffer[32] = {};
intx::be::store(value_buffer, value);
Expand Down Expand Up @@ -660,39 +657,45 @@ void erc20::callupgrade(eosio::name token_contract, eosio::symbol token_symbol){
auto token_table_iter = index_symbol.find(token_symbol_key(token_contract, token_symbol.code()));
eosio::check(token_table_iter != index_symbol.end(), "token not registered");

handle_call_upgrade(token_table_iter->address);
}

void erc20::handle_call_upgrade(const bytes& proxy_address) {
config_t config = get_config();
impl_contract_table_t contract_table(_self, _self.value);
eosio::check(contract_table.begin() != contract_table.end(), "no implementaion contract available");
auto contract_itr = contract_table.end();
--contract_itr;

auto pack_uint32 = [&](bytes &ds, uint32_t val) {
uint8_t val_[32] = {};
val_[28] = (uint8_t)(val >> 24);
val_[29] = (uint8_t)(val >> 16);
val_[30] = (uint8_t)(val >> 8);
val_[31] = (uint8_t)val;
ds.insert(ds.end(), val_, val_ + sizeof(val_));
};
auto handle_call_upgrade = [&](const bytes& proxy_address, auto &contract_table) {
config_t config = get_config();

bytes call_data;
// sha(upgradeTo(address)) == 3659cfe6
uint8_t func_[4] = {0x36,0x59,0xcf,0xe6};
call_data.insert(call_data.end(), func_, func_ + sizeof(func_));
eosio::check(contract_table.begin() != contract_table.end(), "no implementaion contract available");
auto contract_itr = contract_table.end();
--contract_itr;

auto pack_uint32 = [&](bytes &ds, uint32_t val) {
uint8_t val_[32] = {};
val_[28] = (uint8_t)(val >> 24);
val_[29] = (uint8_t)(val >> 16);
val_[30] = (uint8_t)(val >> 8);
val_[31] = (uint8_t)val;
ds.insert(ds.end(), val_, val_ + sizeof(val_));
};

bytes call_data;
// sha(upgradeTo(address)) == 3659cfe6
uint8_t func_[4] = {0x36,0x59,0xcf,0xe6};
call_data.insert(call_data.end(), func_, func_ + sizeof(func_));


call_data.insert(call_data.end(), 32 - kAddressLength, 0); // padding for address offset 0
call_data.insert(call_data.end(), contract_itr->address.begin(), contract_itr->address.end());

call_data.insert(call_data.end(), 32 - kAddressLength, 0); // padding for address offset 0
call_data.insert(call_data.end(), contract_itr->address.begin(), contract_itr->address.end());

bytes value_zero;
value_zero.resize(32, 0);
bytes value_zero;
value_zero.resize(32, 0);

evm_runtime::call_action call_act(config.evm_account, {{receiver_account(), "active"_n}});
call_act.send(receiver_account(), proxy_address, value_zero, call_data, config.evm_gaslimit);
};

evm_runtime::call_action call_act(config.evm_account, {{receiver_account(), "active"_n}});
call_act.send(receiver_account(), proxy_address, value_zero, call_data, config.evm_gaslimit);
if (token_table_iter->is_evm_to_native()) {
evm2native_impl_table_t contract_table(_self, _self.value);
handle_call_upgrade(token_table_iter->address, contract_table);
} else {
impl_contract_table_t contract_table(_self, _self.value);
handle_call_upgrade(token_table_iter->address, contract_table);
}
}

} // namespace erc20
6 changes: 5 additions & 1 deletion antelope_contracts/tests/erc20/different_gas_token_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,13 @@ try {
evm_address = gold_evm_acc->address_0x();
bal = balanceOf(evm2.address_0x().c_str());
BOOST_REQUIRE(bal == 534'000'000'000'000'000);

BOOST_REQUIRE(7000 == get_balance("alice"_n, gold_token_account_name, symbol::from_string("4,GOLD")).get_amount());

// EVM -> native (with dust) should not work
evm_address = proxy_address;
BOOST_REQUIRE_EXCEPTION(bridgeTransferERC20(evm2, addr_alice, (uint64_t)100'000'000'000'000'999, "hello world", fee),
eosio_assert_message_exception, eosio_assert_message_is("bridge amount can not have dust"));

// native -> EVM, 0.2 GOLD (0.1 ingress fee)
transfer_token(gold_token_account_name, "alice"_n, erc20_account, make_asset(2000, symbol::from_string("4,GOLD")), evm2.address_0x().c_str());

Expand Down
6 changes: 5 additions & 1 deletion antelope_contracts/tests/erc20/integrated_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,9 +936,13 @@ try {
evm_address = gold_evm_acc->address_0x();
bal = balanceOf(evm2.address_0x().c_str());
BOOST_REQUIRE(bal == 534'000'000'000'000'000);

BOOST_REQUIRE(7000 == get_balance("alice"_n, gold_token_account_name, symbol::from_string("4,GOLD")).get_amount());

// EVM -> native (with dust) should not work
evm_address = proxy_address;
BOOST_REQUIRE_EXCEPTION(bridgeTransferERC20(evm2, addr_alice, (uint64_t)100'000'000'000'000'999, "hello world", fee),
eosio_assert_message_exception, eosio_assert_message_is("bridge amount can not have dust"));

// native -> EVM, 0.2 GOLD (0.1 ingress fee)
transfer_token(gold_token_account_name, "alice"_n, erc20_account, make_asset(2000, symbol::from_string("4,GOLD")), evm2.address_0x().c_str());

Expand Down
14 changes: 5 additions & 9 deletions solidity_contracts/erc20/evm2native.sol
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,6 @@ contract Evm2Native is Initializable, UUPSUpgradeable {
bool public suspended;
uint8 public evm_precision;
uint8 public host_precision;
uint256 public mult_factor;
uint256 public egressFee;
address public evm_address;
IERC20 public token;
Expand All @@ -952,7 +951,6 @@ contract Evm2Native is Initializable, UUPSUpgradeable {
egressFee = _egressFee;
require(evm_precision >= host_precision, "invalid host_precision");
require(evm_precision == _evm_precision, "invalid evm precision check");
mult_factor = 10**(evm_precision - host_precision);
}

function _addressToName(address input) internal pure returns (string memory) {
Expand Down Expand Up @@ -987,9 +985,9 @@ contract Evm2Native is Initializable, UUPSUpgradeable {
if (msg.sender != owner) { revert(); }
}

function transfer(address to, uint256 host_amount) external returns (bool) {
function transfer(address to, uint256 amount) external returns (bool) {
require(msg.sender == owner, "can only called from owner"); // eosio.erc2o
bool success = token.transfer(to, host_amount * mult_factor);
bool success = token.transfer(to, amount);
if (!success) { revert(); }
return success;
}
Expand All @@ -1002,17 +1000,15 @@ contract Evm2Native is Initializable, UUPSUpgradeable {
require(suspended == false, "bridge suspended");
require(msg.value == egressFee, "incorrect egress bridge fee");
require(_isReservedAddress(to), "to address must be reserved address");
require(amount >= mult_factor, "amount too small");
require(amount > 0, "amount too small");

uint256 host_amount = amount / mult_factor;
uint256 bill_amount = host_amount * mult_factor;
if (!token.transferFrom(msg.sender, address(this), bill_amount)) {
if (!token.transferFrom(msg.sender, address(this), amount)) {
revert();
}

// Call bridgeMessage of EVM Runtime
// sha("bridgeTransferV0(address,uint256,string)") = 0x653332e5
bytes memory receiver_msg = abi.encodeWithSignature("bridgeTransferV0(address,uint256,string)", to, host_amount, memo);
bytes memory receiver_msg = abi.encodeWithSignature("bridgeTransferV0(address,uint256,string)", to, amount, memo);
(bool success, ) = evm_address.call{value: msg.value}(abi.encodeWithSignature("bridgeMsgV0(string,bool,bytes)", linkedOwnerName, true, receiver_msg ));
if(!success) { revert(); }
return success;
Expand Down