Skip to content

Commit

Permalink
Merge pull request #10 from multiversx/send-tokens
Browse files Browse the repository at this point in the history
Send tokens
  • Loading branch information
dorin-iancu authored Oct 18, 2023
2 parents 2ffad02 + 10eae26 commit ae1b10e
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 35 deletions.
7 changes: 1 addition & 6 deletions common/token-module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ pub trait TokenModule {

#[only_owner]
#[endpoint(addTokenToWhitelist)]
fn add_token_to_whitelist(&self, token_id: TokenIdentifier, ticker: ManagedBuffer) {
self.token_ticker(&token_id).set(&ticker);
fn add_token_to_whitelist(&self, token_id: TokenIdentifier) {
let _ = self.token_whitelist().insert(token_id);
}

#[only_owner]
#[endpoint(removeTokenFromWhitelist)]
fn remove_token_from_whitelist(&self, token_id: TokenIdentifier) {
self.token_ticker(&token_id).clear();
let _ = self.token_whitelist().swap_remove(&token_id);
}

Expand Down Expand Up @@ -45,9 +43,6 @@ pub trait TokenModule {

// storage

#[storage_mapper("tokenTicker")]
fn token_ticker(&self, token_id: &TokenIdentifier) -> SingleValueMapper<ManagedBuffer>;

#[view(getAllKnownTokens)]
#[storage_mapper("tokenWhitelist")]
fn token_whitelist(&self) -> UnorderedSetMapper<TokenIdentifier>;
Expand Down
10 changes: 9 additions & 1 deletion esdt-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ pub trait EsdtSafe:
/// In case of SC gas limits, this value is provided by the user
/// Will be used to compute the fees for the transfer
#[init]
fn init(&self, sovereign_tx_gas_limit: GasLimit) {
fn init(&self, sovereign_tx_gas_limit: GasLimit, multi_transfer_sc_address: ManagedAddress) {
require!(
self.blockchain()
.is_smart_contract(&multi_transfer_sc_address),
"Invalid SC address"
);

self.sovereign_tx_gas_limit().set(sovereign_tx_gas_limit);
self.multi_transfer_sc_address()
.set(multi_transfer_sc_address);

self.max_tx_batch_size().set(DEFAULT_MAX_TX_BATCH_SIZE);
self.max_tx_batch_block_duration()
Expand Down
8 changes: 0 additions & 8 deletions esdt-safe/src/refund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,6 @@ pub trait RefundModule:
});
}

fn is_burn_role_set(&self, payment: &EsdtTokenPayment) -> bool {
if payment.token_nonce == 0 {
self.is_local_role_set(&payment.token_identifier, &EsdtLocalRole::Burn)
} else {
self.is_local_role_set(&payment.token_identifier, &EsdtLocalRole::NftBurn)
}
}

#[storage_mapper("refundAmount")]
fn refund_amount(
&self,
Expand Down
19 changes: 9 additions & 10 deletions esdt-safe/src/set_tx_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub trait SetTxStatusModule:
"Invalid number of statuses provided"
);

let mut sent_tokens = ManagedVec::new();
for (tx, tx_status) in tx_batch.iter().zip(tx_statuses.to_vec().iter()) {
// Since tokens don't exist in the EsdtSafe in the case of a refund transaction
// we have no tokens to burn, nor to refund
Expand All @@ -42,17 +43,8 @@ pub trait SetTxStatusModule:

match tx_status {
TransactionStatus::Executed => {
// local burn role might be removed while tx is executed
// tokens will remain locked forever in that case
// otherwise, the whole batch would fail
for token in &tx.tokens {
if self.is_burn_role_set(&token) {
self.send().esdt_local_burn(
&token.token_identifier,
token.token_nonce,
&token.amount,
)
}
sent_tokens.push(token);
}
}
TransactionStatus::Rejected => {
Expand All @@ -68,6 +60,13 @@ pub trait SetTxStatusModule:
self.set_status_event(batch_id, tx.nonce, tx_status);
}

let multi_transfer_sc_address = self.multi_transfer_sc_address().get();
self.send()
.direct_multi(&multi_transfer_sc_address, &sent_tokens);

self.clear_first_batch(&mut tx_batch);
}

#[storage_mapper("multiTransferScAddress")]
fn multi_transfer_sc_address(&self) -> SingleValueMapper<ManagedAddress>;
}
3 changes: 3 additions & 0 deletions multi-transfer-esdt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ const DEFAULT_MAX_TX_BATCH_BLOCK_DURATION: u64 = u64::MAX;
pub mod bls_signature;
pub mod events;
pub mod refund;
pub mod token_mapping;
pub mod transfer_tokens;

#[multiversx_sc::contract]
pub trait MultiTransferEsdt:
bls_signature::BlsSignatureModule
+ events::EventsModule
+ refund::RefundModule
+ token_mapping::TokenMappingModule
+ transfer_tokens::TransferTokensModule
+ tx_batch_module::TxBatchModule
+ max_bridged_amount_module::MaxBridgedAmountModule
{
/// Needs to be Payable by SC to receive the tokens from EsdtSafe
#[init]
fn init(&self, min_valid_signers: u32, signers: MultiValueEncoded<ManagedAddress>) {
self.max_tx_batch_size().set(DEFAULT_MAX_TX_BATCH_SIZE);
Expand Down
22 changes: 22 additions & 0 deletions multi-transfer-esdt/src/token_mapping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
multiversx_sc::imports!();

#[multiversx_sc::module]
pub trait TokenMappingModule {
#[only_owner]
#[endpoint(setSovToMxTokenId)]
fn set_sov_to_mx_token_id(&self, sov_token_id: TokenIdentifier, mx_token_id: TokenIdentifier) {
require!(
sov_token_id.is_valid_esdt_identifier() && mx_token_id.is_valid_esdt_identifier(),
"Invalid token IDs"
);

self.sovereign_to_multiversx_token_id(&sov_token_id)
.set(mx_token_id);
}

#[storage_mapper("sovToMxTokenId")]
fn sovereign_to_multiversx_token_id(
&self,
sov_token_id: &TokenIdentifier,
) -> SingleValueMapper<TokenIdentifier>;
}
29 changes: 21 additions & 8 deletions multi-transfer-esdt/src/transfer_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub trait TransferTokensModule:
crate::bls_signature::BlsSignatureModule
+ crate::events::EventsModule
+ crate::refund::RefundModule
+ crate::token_mapping::TokenMappingModule
+ tx_batch_module::TxBatchModule
+ max_bridged_amount_module::MaxBridgedAmountModule
{
Expand Down Expand Up @@ -77,23 +78,35 @@ pub trait TransferTokensModule:
payments: PaymentsVec<Self::Api>,
all_token_data: ManagedVec<StolenFromFrameworkEsdtTokenData<Self::Api>>,
) -> PaymentsVec<Self::Api> {
let own_sc_address = self.blockchain().get_sc_address();
let mut output_payments = PaymentsVec::new();
for (payment, token_data) in payments.iter().zip(all_token_data.iter()) {
let token_balance = self.blockchain().get_esdt_balance(
&own_sc_address,
&payment.token_identifier,
payment.token_nonce,
);
if token_balance >= payment.amount {
output_payments.push(payment);

continue;
}

let mx_token_id = self
.sovereign_to_multiversx_token_id(&payment.token_identifier)
.get();

if payment.token_nonce == 0 {
self.send()
.esdt_local_mint(&payment.token_identifier, 0, &payment.amount);
.esdt_local_mint(&mx_token_id, 0, &payment.amount);

output_payments.push(EsdtTokenPayment::new(
payment.token_identifier,
0,
payment.amount,
));
output_payments.push(EsdtTokenPayment::new(mx_token_id, 0, payment.amount));

continue;
}

let token_nonce = self.send().esdt_nft_create(
&payment.token_identifier,
&mx_token_id,
&payment.amount,
&token_data.name,
&token_data.royalties,
Expand All @@ -102,7 +115,7 @@ pub trait TransferTokensModule:
&token_data.uris,
);
output_payments.push(EsdtTokenPayment::new(
payment.token_identifier,
mx_token_id,
token_nonce,
payment.amount,
));
Expand Down
5 changes: 3 additions & 2 deletions multi-transfer-esdt/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 16
// Endpoints: 17
// Async Callback (empty): 1
// Promise callbacks: 1
// Total number of exported functions: 19
// Total number of exported functions: 20

#![no_std]

Expand All @@ -28,6 +28,7 @@ multiversx_sc_wasm_adapter::endpoints! {
addSigners => add_signers
removeSigners => remove_signers
getAndClearFirstRefundBatch => get_and_clear_first_refund_batch
setSovToMxTokenId => set_sov_to_mx_token_id
batchTransferEsdtToken => batch_transfer_esdt_token
setMaxTxBatchSize => set_max_tx_batch_size
setMaxTxBatchBlockDuration => set_max_tx_batch_block_duration
Expand Down

0 comments on commit ae1b10e

Please sign in to comment.