Skip to content

Commit

Permalink
setup test
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Feb 28, 2024
1 parent 66f54af commit 5252631
Show file tree
Hide file tree
Showing 8 changed files with 383 additions and 428 deletions.
4 changes: 2 additions & 2 deletions common/token-module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ pub trait TokenModule: fee_estimator_module::FeeEstimatorModule {

#[only_owner]
#[endpoint(setAccumulatedBurnedTokens)]
fn set_accumulated_burned_tokens(&self, token_id: &TokenIdentifier, value: BigUint) {
self.accumulated_burned_tokens(token_id).set_if_empty(value);
fn set_accumulated_burned_tokens(&self, token_id: TokenIdentifier, value: BigUint) {
self.accumulated_burned_tokens(&token_id).set_if_empty(value);
}

// storage
Expand Down
2 changes: 2 additions & 0 deletions common/tx-batch-module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use tx_batch_mapper::TxBatchMapper;
pub mod batch_status;
pub mod tx_batch_mapper;

pub const FIRST_BATCH_ID: u64 = 1;

#[multiversx_sc::module]
pub trait TxBatchModule {
// endpoints - owner-only
Expand Down
36 changes: 9 additions & 27 deletions esdt-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use core::convert::TryFrom;
use eth_address::*;
use fee_estimator_module::GWEI_STRING;
use transaction::{transaction_status::TransactionStatus, Transaction};
use tx_batch_module::FIRST_BATCH_ID;

const DEFAULT_MAX_TX_BATCH_SIZE: usize = 10;
const DEFAULT_MAX_TX_BATCH_BLOCK_DURATION: u64 = 100; // ~10 minutes
Expand All @@ -29,17 +30,16 @@ pub trait EsdtSafe:
#[init]
fn init(&self, fee_estimator_contract_address: ManagedAddress, eth_tx_gas_limit: BigUint) {
self.fee_estimator_contract_address()
.set(&fee_estimator_contract_address);
self.eth_tx_gas_limit().set(&eth_tx_gas_limit);
.set(fee_estimator_contract_address);
self.eth_tx_gas_limit().set(eth_tx_gas_limit);

self.max_tx_batch_size()
.set_if_empty(DEFAULT_MAX_TX_BATCH_SIZE);
self.max_tx_batch_size().set(DEFAULT_MAX_TX_BATCH_SIZE);
self.max_tx_batch_block_duration()
.set_if_empty(DEFAULT_MAX_TX_BATCH_BLOCK_DURATION);
.set(DEFAULT_MAX_TX_BATCH_BLOCK_DURATION);

// batch ID 0 is considered invalid
self.first_batch_id().set_if_empty(1);
self.last_batch_id().set_if_empty(1);
self.first_batch_id().set(FIRST_BATCH_ID);
self.last_batch_id().set(FIRST_BATCH_ID);

// set ticker for "GWEI"
let gwei_token_id = TokenIdentifier::from(GWEI_STRING);
Expand All @@ -50,25 +50,7 @@ pub trait EsdtSafe:
}

#[upgrade]
fn upgrade(&self, fee_estimator_contract_address: ManagedAddress, eth_tx_gas_limit: BigUint) {
self.fee_estimator_contract_address()
.set(&fee_estimator_contract_address);
self.eth_tx_gas_limit().set(&eth_tx_gas_limit);

self.max_tx_batch_size()
.set_if_empty(DEFAULT_MAX_TX_BATCH_SIZE);
self.max_tx_batch_block_duration()
.set_if_empty(DEFAULT_MAX_TX_BATCH_BLOCK_DURATION);

// batch ID 0 is considered invalid
self.first_batch_id().set_if_empty(1);
self.last_batch_id().set_if_empty(1);

// set ticker for "GWEI"
let gwei_token_id = TokenIdentifier::from(GWEI_STRING);
self.token_ticker(&gwei_token_id)
.set(gwei_token_id.as_managed_buffer());

fn upgrade(&self) {
self.set_paused(true);
}

Expand Down Expand Up @@ -116,7 +98,7 @@ pub trait EsdtSafe:
}
}
TransactionStatus::Rejected => {
let addr = ManagedAddress::try_from(tx.from).unwrap();
let addr = unsafe { ManagedAddress::try_from(tx.from).unwrap_unchecked() };
self.mark_refund(&addr, &tx.token_identifier, &tx.amount);
}
_ => {
Expand Down
50 changes: 26 additions & 24 deletions multi-transfer-esdt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ multiversx_sc::imports!();

use token_module::ProxyTrait as OtherProxyTrait;
use transaction::{
EthTransaction, EthTransactionPayment, PaymentsVec, Transaction, TxBatchSplitInFields,
call_data::CallData, EthTransaction, EthTransactionPayment, PaymentsVec, Transaction,
TxBatchSplitInFields,
};
use tx_batch_module::FIRST_BATCH_ID;

const DEFAULT_MAX_TX_BATCH_SIZE: usize = 10;
const DEFAULT_MAX_TX_BATCH_BLOCK_DURATION: u64 = u64::MAX;
Expand All @@ -18,25 +20,16 @@ pub trait MultiTransferEsdt:
{
#[init]
fn init(&self) {
self.max_tx_batch_size()
.set_if_empty(DEFAULT_MAX_TX_BATCH_SIZE);
self.max_tx_batch_size().set(DEFAULT_MAX_TX_BATCH_SIZE);
self.max_tx_batch_block_duration()
.set_if_empty(DEFAULT_MAX_TX_BATCH_BLOCK_DURATION);
.set(DEFAULT_MAX_TX_BATCH_BLOCK_DURATION);
// batch ID 0 is considered invalid
self.first_batch_id().set_if_empty(1);
self.last_batch_id().set_if_empty(1);
self.first_batch_id().set(FIRST_BATCH_ID);
self.last_batch_id().set(FIRST_BATCH_ID);
}

#[upgrade]
fn upgrade(&self) {
self.max_tx_batch_size()
.set_if_empty(DEFAULT_MAX_TX_BATCH_SIZE);
self.max_tx_batch_block_duration()
.set_if_empty(DEFAULT_MAX_TX_BATCH_BLOCK_DURATION);
// batch ID 0 is considered invalid
self.first_batch_id().set_if_empty(1);
self.last_batch_id().set_if_empty(1);
}
fn upgrade(&self) {}

#[only_owner]
#[endpoint(batchTransferEsdtToken)]
Expand Down Expand Up @@ -68,14 +61,7 @@ pub trait MultiTransferEsdt:
} else if is_dest_sc {
match &eth_tx.call_data {
Some(call_data) => {
#[allow(clippy::if_same_then_else)]
if call_data.gas_limit < MIN_GAS_LIMIT_FOR_SC_CALL
|| call_data.gas_limit > MAX_GAS_LIMIT_FOR_SC_CALL
{
must_refund = true;
} else if call_data.endpoint.len() > u8::MAX as usize {
must_refund = true;
} else if call_data.args.len() > u8::MAX as usize {
if self.must_refund_tx_with_call_data(&call_data) {

Check warning on line 64 in multi-transfer-esdt/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] multi-transfer-esdt/src/lib.rs#L64

warning: this expression creates a reference which is immediately dereferenced by the compiler --> multi-transfer-esdt/src/lib.rs:64:63 | 64 | if self.must_refund_tx_with_call_data(&call_data) { | ^^^^^^^^^^ help: change this to: `call_data` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Raw output
multi-transfer-esdt/src/lib.rs:64:63:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
  --> multi-transfer-esdt/src/lib.rs:64:63
   |
64 |                         if self.must_refund_tx_with_call_data(&call_data) {
   |                                                               ^^^^^^^^^^ help: change this to: `call_data`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
   = note: `#[warn(clippy::needless_borrow)]` on by default


__END__

Check warning on line 64 in multi-transfer-esdt/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] multi-transfer-esdt/src/lib.rs#L64

warning: this expression creates a reference which is immediately dereferenced by the compiler --> multi-transfer-esdt/src/lib.rs:64:63 | 64 | if self.must_refund_tx_with_call_data(&call_data) { | ^^^^^^^^^^ help: change this to: `call_data` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Raw output
multi-transfer-esdt/src/lib.rs:64:63:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
  --> multi-transfer-esdt/src/lib.rs:64:63
   |
64 |                         if self.must_refund_tx_with_call_data(&call_data) {
   |                                                               ^^^^^^^^^^ help: change this to: `call_data`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
   = note: `#[warn(clippy::needless_borrow)]` on by default


__END__
must_refund = true;
}
}
Expand Down Expand Up @@ -107,7 +93,7 @@ pub trait MultiTransferEsdt:
// emit event before the actual transfer so we don't have to save the tx_nonces as well
self.transfer_performed_event(batch_id, eth_tx.tx_nonce);

valid_tx_list.push(eth_tx.clone());
valid_tx_list.push(eth_tx);
valid_payments_list.push(minted_token);
}

Expand Down Expand Up @@ -191,8 +177,23 @@ pub trait MultiTransferEsdt:

self.add_multiple_tx_to_batch(&refund_tx_list);
}

// private

fn must_refund_tx_with_call_data(&self, call_data: &CallData<Self::Api>) -> bool {
if call_data.gas_limit < MIN_GAS_LIMIT_FOR_SC_CALL
|| call_data.gas_limit > MAX_GAS_LIMIT_FOR_SC_CALL
{
return true;
}

if call_data.endpoint.len() > u8::MAX as usize {
return true;
}

call_data.args.len() > u8::MAX as usize
}

fn convert_to_refund_tx(&self, eth_tx: EthTransaction<Self::Api>) -> Transaction<Self::Api> {
Transaction {
block_nonce: self.blockchain().get_block_nonce(),
Expand Down Expand Up @@ -285,6 +286,7 @@ pub trait MultiTransferEsdt:
}

// storage

#[view(getWrappingContractAddress)]
#[storage_mapper("wrappingContractAddress")]
fn wrapping_contract_address(&self) -> SingleValueMapper<ManagedAddress>;
Expand Down
Loading

0 comments on commit 5252631

Please sign in to comment.