Skip to content

Commit

Permalink
Merge pull request #63 from pnetwork-association/chore/finalize-adapt…
Browse files Browse the repository at this point in the history
…er-tests

Finalize adapter tests
  • Loading branch information
gitmp01 authored Nov 10, 2024
2 parents 70efd6e + fe6a69e commit 2b387df
Show file tree
Hide file tree
Showing 19 changed files with 1,212 additions and 909 deletions.
48 changes: 27 additions & 21 deletions cpp/contracts/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ asset adapter::calculate_fees(const asset& quantity) {
auto registry_data = _registry.get();

check(
quantity.symbol == registry_data.token_symbol ||
quantity.symbol == registry_data.token_symbol ||
quantity.symbol == registry_data.xerc20_symbol,
"invalid quantity given for calculating the fees"
);
Expand All @@ -27,6 +27,7 @@ void adapter::check_symbol_is_valid(const name& account, const symbol& sym) {
stats _stats(account, sym.code().raw());
auto itr = _stats.find(sym.code().raw());
check(itr != _stats.end(), "symbol not found");
check(sym == itr->supply.symbol, "invalid symbol");
}

void adapter::create(
Expand All @@ -39,12 +40,12 @@ void adapter::create(
) {
require_auth(get_self());
registry_adapter _registry(get_self(), get_self().value);
check(!_registry.exists(), "adapter already initialized");
check(!_registry.exists(), "contract already initialized");

auto _token_bytes = token_bytes.extract_as_byte_array();
check(is_account(xerc20), "xERC20 account does not exist");
check(min_fee.symbol == xerc20_symbol, "invalid minimum fee symbol");
check(is_account(xerc20), "invalid account");
check_symbol_is_valid(xerc20, xerc20_symbol);
check(min_fee.symbol == xerc20_symbol, "invalid minimum fee symbol");

// Checks done only for the local token
if (token != name(0)) {
Expand Down Expand Up @@ -84,7 +85,6 @@ void adapter::setfeemanagr(const name& fee_manager) {

void adapter::extract_memo_args(
const name& self,
const name& userdata_owner,
const string& memo,
string& out_sender,
string& out_dest_chainid,
Expand All @@ -100,39 +100,45 @@ void adapter::extract_memo_args(
out_sender = parts[0];
out_dest_chainid = parts[1].substr(2);
out_recipient = parts[2];
uint64_t userdata_id = stoull(parts[3]);
uint8_t has_userdata = stoi(parts[3]);

check(out_sender.length() > 0, "invalid sender address");
check(out_recipient.length() > 0, "invalid destination address");
check(out_dest_chainid.length() == 64, "chain id must be a 32 bytes hex-string");

auto sender_account = name(out_sender);
check(is_account(sender_account), "invalid sender account");

if (userdata_id > 0) {
user_data table(userdata_owner, userdata_owner.value);
auto row = table.find(userdata_id);
if (has_userdata > 0) {
user_data table(self, sender_account.value);

check(row != table.end(), "userdata record not found");
check(table.begin() != table.end(), "userdata record not found");

out_data = row->payload;
auto row = *(table.begin());
out_data = row.payload;
table.erase(table.begin());
}
}

void adapter::adduserdata(const name& caller, bytes payload) {
require_auth(caller);
check(payload.size() > 0, "invalid payload");

user_data table(caller, caller.value);
user_data table(get_self(), caller.value);

// NOTE: we use a single record table here
// instead of a singleton because we can
// scope it by account value.
// Singletons doesn't support scoping and they
// are global througout the contract
if (table.begin() == table.end()) {
table.emplace(caller, [&](auto& r) {
// NOTE: an id == 0 means no userdata, check extract_memo_args
// for details
r.id = 1;
r.payload = payload;
});
} else {
table.emplace(caller, [&](auto& r) {
r.id = table.end()->id + 1;
table.modify(table.begin(), caller, [&](auto& r) {
r.id = 1;
r.payload = payload;
});
}
Expand Down Expand Up @@ -238,14 +244,13 @@ void adapter::settle(const name& caller, const operation& operation, const metad
}
}

void adapter::swap(const uint64_t& nonce, const bytes& event_bytes) {
void adapter::swap(const bytes& event_bytes) {
require_auth(get_self());

// IMPORTANT: this is for the tests, vert doesn't correctly
// deserialize the event_bytes arg, so we'll get it from
// the bc.console
// NOTE: performance are not affected by this
print("adapter_swap_event_bytes:");
printhex(event_bytes.data(), event_bytes.size());
}

Expand Down Expand Up @@ -276,7 +281,7 @@ void adapter::token_transfer_from_user(

void adapter::xerc20_transfer_from_any(
const name& self,
const name& caller,
const name& from,
const name& token,
const name& xerc20,
const asset& quantity,
Expand Down Expand Up @@ -305,7 +310,7 @@ void adapter::xerc20_transfer_from_any(
string recipient;
bytes userdata;

extract_memo_args(self, caller, memo, sender, dest_chainid, recipient, userdata);
extract_memo_args(self, memo, sender, dest_chainid, recipient, userdata);

auto recipient_bytes = to_bytes(recipient);

Expand All @@ -322,7 +327,7 @@ void adapter::xerc20_transfer_from_any(
);

action_swap _swap{self, {self, "active"_n}};
_swap.send(storage.nonce, event_bytes);
_swap.send(event_bytes);

storage.nonce++;
_storage.set(storage, self);
Expand Down Expand Up @@ -351,6 +356,7 @@ void adapter::ontransfer(const name& from, const name& to, const asset& quantity
if (is_token_transfer) check(quantity.symbol == token_symbol, "invalid token quantity symbol");
if (is_xerc20_transfer) check(quantity.symbol == xerc20_symbol, "invalid xerc20 quantity symbol");


if (is_token_transfer) {
lockbox_singleton _lockbox(xerc20, xerc20.value);
check(_lockbox.exists(), "lockbox is not set for the underlying token");
Expand Down
9 changes: 6 additions & 3 deletions cpp/contracts/adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace eosio {

ACTION setorigin(bytes chain_id, bytes emitter, bytes topic_zero);

ACTION swap(const uint64_t& nonce, const bytes& event_bytes);
ACTION swap(const bytes& event_bytes);

ACTION settle(const name& caller, const operation& operation, const metadata& metadata);

Expand Down Expand Up @@ -101,31 +101,34 @@ namespace eosio {
};

void check_symbol_is_valid(const name& account, const symbol& sym);

void extract_memo_args(
const name& self,
const name& userdata_owner,
const string& memo,
string& out_sender,
string& out_dest_chainid,
string& out_recipient,
bytes& out_dat
);

void token_transfer_from_lockbox(
const name& self,
const name& token,
const asset& quantity,
const string& memo
);

void token_transfer_from_user(
const name& self,
const name& token,
const name& lockbox,
const asset& quantity,
const string& memo
);

void xerc20_transfer_from_any(
const name& self,
const name& caller,
const name& from,
const name& token,
const name& xerc20,
const asset& quantity,
Expand Down
2 changes: 1 addition & 1 deletion cpp/contracts/xerc20.token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void xtoken::burn( const name& caller, const asset& quantity, const string& memo
const auto& st = *existing;

lockbox_singleton _lockbox( get_self(), get_self().value );
auto lockbox = _lockbox.get();
auto lockbox = _lockbox.get_or_default(name(0));
bridges bridgestable( get_self(), get_self().value );
auto idx = bridgestable.get_index<name("bysymbol")>();
auto itr = idx.lower_bound( quantity.symbol.code().raw() );
Expand Down
Loading

0 comments on commit 2b387df

Please sign in to comment.