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

eosio.erc2o contract implementation #5

Merged
merged 14 commits into from
Sep 5, 2023
2 changes: 1 addition & 1 deletion antelope_contracts/contracts/erc20/include/erc20/erc20.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class [[eosio::contract]] erc20 : public contract {
eosio::asset ingress_fee;
eosio::asset balance; // <-- total amount in EVM side
eosio::asset fee_balance;
int erc20_precision = 0;
int8_t erc20_precision = 0;

uint64_t primary_key() const {
return id;
Expand Down
19 changes: 10 additions & 9 deletions antelope_contracts/contracts/erc20/src/erc20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ uint64_t erc20::get_next_nonce() {
auto itr = table.find(erc2o_account.value);
uint64_t next_nonce = (itr == table.end() ? 0 : itr->next_nonce);

assertnonce_action act(evm_account, {{erc2o_account, "active"_n}});
assertnonce_action act(evm_account, std::vector<eosio::permission_level>{});
act.send(erc2o_account, next_nonce);
return next_nonce;
}
Expand Down Expand Up @@ -206,18 +206,19 @@ void erc20::onbridgemsg(const bridge_message_t &message) {
check(msg.data.size() >= 4 + 32 /*to*/ + 32 /*amount*/ + 32 /*memo offset*/ + 32 /*memo len*/,
"not enough data in bridge_message_v0 of application type 0x653332e5");

evmc::address dest_addr;
memcpy(dest_addr.bytes, (void *)&(msg.data[4 + 32 - kAddressLength]), kAddressLength);
std::optional<uint64_t> dest_acc = silkworm::extract_reserved_address(dest_addr);
check(!!dest_acc, "destination address in bridge_message_v0 must be reserved address");

auto read_uint256 = [&](const auto &msg, size_t offset) -> intx::uint256 {
uint8_t buffer[32]={};
check(msg.data.size() >= offset + 32, "not enough data in bridge_message_v0 of application type 0x653332e5");
memcpy(buffer, (void *)&(msg.data[offset]), 32);
return intx::be::load<intx::uint256>(buffer);
};

check(read_uint256(msg, 4) <= 0xffffFFFFffffFFFFffffFFFFffffFFFFffffFFFF_u256, "invalid destination address");
evmc::address dest_addr;
memcpy(dest_addr.bytes, (void *)&(msg.data[4 + 32 - kAddressLength]), kAddressLength);
std::optional<uint64_t> dest_acc = silkworm::extract_reserved_address(dest_addr);
check(!!dest_acc, "destination address in bridge_message_v0 must be reserved address");

intx::uint256 value = read_uint256(msg, 4 + 32);
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");
Expand Down Expand Up @@ -271,14 +272,14 @@ void erc20::transfer(eosio::name from, eosio::name to, eosio::asset quantity,
eosio::check(quantity.amount > itr->ingress_fee.amount, "deposit amount must be greater than ingress fee");

uint64_t ingress_fee = itr->ingress_fee.amount;
arhag marked this conversation as resolved.
Show resolved Hide resolved
quantity.amount -= ingress_fee;
quantity -= itr->ingress_fee;
eosio::check(quantity.amount > 0 && quantity.amount < (1ll<<62)-1, "deposit amount overflow");

if (memo.size() == 42 && memo[0] == '0' && memo[1] == 'x') {
handle_erc20_transfer(*itr, quantity, memo);
token_table.modify(*itr, _self, [&](auto &v) {
v.balance.amount += quantity.amount;
v.fee_balance.amount += ingress_fee;
v.balance += quantity;
v.fee_balance += v.ingress_fee;
});
} else
eosio::check(false, "memo must be 0x EVM address");
Expand Down
Loading