Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Oct 18, 2023
1 parent 876e876 commit c8615f2
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 116 deletions.
9 changes: 0 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions common/token-module/Cargo.toml

This file was deleted.

49 changes: 0 additions & 49 deletions common/token-module/src/lib.rs

This file was deleted.

3 changes: 0 additions & 3 deletions esdt-safe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ path = "src/lib.rs"
[dependencies.transaction]
path = "../common/transaction"

[dependencies.token-module]
path = "../common/token-module"

[dependencies.tx-batch-module]
path = "../common/tx-batch-module"

Expand Down
1 change: 0 additions & 1 deletion esdt-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub trait EsdtSafe:
+ from_sovereign::refund::RefundModule
+ from_sovereign::token_mapping::TokenMappingModule
+ from_sovereign::transfer_tokens::TransferTokensModule
+ token_module::TokenModule
+ tx_batch_module::TxBatchModule
+ max_bridged_amount_module::MaxBridgedAmountModule
+ multiversx_sc_modules::pause::PauseModule
Expand Down
18 changes: 12 additions & 6 deletions esdt-safe/src/to_sovereign/create_tx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use transaction::{GasLimit, StolenFromFrameworkEsdtTokenData, Transaction, TransferData};

use crate::to_sovereign::events::DepositEvent;

multiversx_sc::imports!();

const MAX_USER_TX_GAS_LIMIT: GasLimit = 300_000_000;
Expand All @@ -8,15 +10,14 @@ const MAX_TRANSFERS_PER_TX: usize = 10;
#[multiversx_sc::module]
pub trait CreateTxModule:
super::events::EventsModule
+ token_module::TokenModule
+ tx_batch_module::TxBatchModule
+ max_bridged_amount_module::MaxBridgedAmountModule
+ multiversx_sc_modules::pause::PauseModule
{
/// Create an Elrond -> Sovereign transaction.
#[payable("*")]
#[endpoint(createTransaction)]
fn create_transaction(
#[endpoint]
fn deposit(
&self,
to: ManagedAddress,
opt_transfer_data: OptionalValue<TransferData<Self::Api>>,
Expand All @@ -37,7 +38,6 @@ pub trait CreateTxModule:
let own_sc_address = self.blockchain().get_sc_address();
let mut all_token_data = ManagedVec::new();
for payment in &payments {
self.require_token_in_whitelist(&payment.token_identifier);
self.require_below_max_amount(&payment.token_identifier, &payment.amount);

if payment.token_nonce > 0 {
Expand All @@ -55,6 +55,13 @@ pub trait CreateTxModule:
let caller = self.blockchain().get_caller();
let block_nonce = self.blockchain().get_block_nonce();
let tx_nonce = self.get_and_save_next_tx_id();

self.deposit_event(
&to,
&payments,
DepositEvent::from(tx_nonce, &opt_transfer_data),
);

let tx = Transaction {
block_nonce,
nonce: tx_nonce,
Expand All @@ -66,7 +73,6 @@ pub trait CreateTxModule:
is_refund_tx: false,
};

let batch_id = self.add_to_batch(tx);
self.create_transaction_event(batch_id, tx_nonce);
let _ = self.add_to_batch(tx);
}
}
41 changes: 38 additions & 3 deletions esdt-safe/src/to_sovereign/events.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
use transaction::{transaction_status::TransactionStatus, BatchId, TxId};
use transaction::{
transaction_status::TransactionStatus, BatchId, GasLimit, PaymentsVec, TransferData, TxId,
};

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct DepositEvent<M: ManagedTypeApi> {
pub tx_nonce: TxId,
pub opt_function: Option<ManagedBuffer<M>>,
pub opt_arguments: Option<ManagedVec<M, ManagedBuffer<M>>>,
pub opt_gas_limit: Option<GasLimit>,
}

impl<M: ManagedTypeApi> DepositEvent<M> {
pub fn from(tx_nonce: TxId, opt_transfer_data: &OptionalValue<TransferData<M>>) -> Self {
match opt_transfer_data {
OptionalValue::Some(transfer_data) => DepositEvent {
tx_nonce,
opt_function: Some(transfer_data.function.clone()),
opt_arguments: Some(transfer_data.args.clone()),
opt_gas_limit: Some(transfer_data.gas_limit),
},
OptionalValue::None => DepositEvent {
tx_nonce,
opt_function: None,
opt_arguments: None,
opt_gas_limit: None,
},
}
}
}

#[multiversx_sc::module]
pub trait EventsModule {
#[event("createTransactionEvent")]
fn create_transaction_event(&self, #[indexed] batch_id: BatchId, #[indexed] tx_id: TxId);
#[event("deposit")]
fn deposit_event(
&self,
#[indexed] dest_address: &ManagedAddress,
#[indexed] tokens: &PaymentsVec<Self::Api>,
event_data: DepositEvent<Self::Api>,
);

#[event("setStatusEvent")]
fn set_status_event(
Expand Down
26 changes: 0 additions & 26 deletions esdt-safe/src/to_sovereign/refund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub struct NonceAmountPair<M: ManagedTypeApi> {
#[multiversx_sc::module]
pub trait RefundModule:
super::events::EventsModule
+ token_module::TokenModule
+ tx_batch_module::TxBatchModule
+ max_bridged_amount_module::MaxBridgedAmountModule
{
Expand All @@ -37,31 +36,6 @@ pub trait RefundModule:
output_payments
}

/// Query function that lists all refund amounts for a user.
/// Useful for knowing which token IDs to pass to the claimRefund endpoint.
#[view(getRefundAmounts)]
fn get_refund_amounts(
&self,
address: ManagedAddress,
) -> MultiValueEncoded<MultiValue3<TokenIdentifier, u64, BigUint>> {
let mut refund_amounts = MultiValueEncoded::new();
for token_id in self.token_whitelist().iter() {
let nonce_amount_pairs = self.refund_amount(&address, &token_id).get();
for nonce_amount_pair in &nonce_amount_pairs {
refund_amounts.push(
(
token_id.clone(),
nonce_amount_pair.nonce,
nonce_amount_pair.amount,
)
.into(),
);
}
}

refund_amounts
}

fn mark_refund(&self, to: &ManagedAddress, token: &EsdtTokenPayment) {
self.refund_amount(to, &token.token_identifier)
.update(|refund| {
Expand Down
1 change: 0 additions & 1 deletion esdt-safe/src/to_sovereign/set_tx_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub trait SetTxStatusModule:
bls_signature::BlsSignatureModule
+ super::events::EventsModule
+ super::refund::RefundModule
+ token_module::TokenModule
+ tx_batch_module::TxBatchModule
+ max_bridged_amount_module::MaxBridgedAmountModule
{
Expand Down
5 changes: 1 addition & 4 deletions esdt-safe/tests/bridge_setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use esdt_safe::EsdtSafe;
use multiversx_sc::types::{Address, MultiValueEncoded};
use multiversx_sc_modules::pause::PauseModule;
use multiversx_sc_scenario::{
managed_token_id, rust_biguint,
rust_biguint,
testing_framework::{BlockchainStateWrapper, ContractObjWrapper},
DebugApi,
};
use token_module::TokenModule;
use tx_batch_module::TxBatchModule;

multiversx_sc::derive_imports!();
Expand Down Expand Up @@ -60,8 +59,6 @@ where
sc.init(0, MultiValueEncoded::new());
sc.set_max_tx_batch_size(1);
sc.set_paused(false);
sc.add_token_to_whitelist(managed_token_id!(FUNGIBLE_TOKEN_ID));
sc.add_token_to_whitelist(managed_token_id!(NFT_TOKEN_ID));
})
.assert_ok();

Expand Down
6 changes: 3 additions & 3 deletions esdt-safe/tests/bridge_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn transfer_two_tokens_to_sov_ok() {
&bridge_setup.bridge_wrapper,
&transfers,
|sc| {
sc.create_transaction(managed_address!(&dest), OptionalValue::None);
sc.deposit(managed_address!(&dest), OptionalValue::None);
},
)
.assert_ok();
Expand Down Expand Up @@ -107,7 +107,7 @@ fn refund_failed_tx_to_sov() {
&bridge_setup.bridge_wrapper,
&transfers,
|sc| {
sc.create_transaction(managed_address!(&dest), OptionalValue::None);
sc.deposit(managed_address!(&dest), OptionalValue::None);
},
)
.assert_ok();
Expand Down Expand Up @@ -185,7 +185,7 @@ fn transfer_token_to_and_from_sov_ok() {
&bridge_setup.bridge_wrapper,
&transfers,
|sc| {
sc.create_transaction(managed_address!(&dest), OptionalValue::None);
sc.deposit(managed_address!(&dest), OptionalValue::None);
},
)
.assert_ok();
Expand Down

0 comments on commit c8615f2

Please sign in to comment.