Skip to content

Commit

Permalink
fix backword compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dragos-rebegea committed Mar 6, 2024
1 parent 270642e commit f0e5f39
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 50 deletions.
104 changes: 68 additions & 36 deletions common/token-module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ pub trait TokenModule: fee_estimator_module::FeeEstimatorModule {
self.default_price_per_gas_unit(&token_id)
.set(&default_price_per_gas_unit);
}

if !mint_burn_allowed {
require!(is_native_token, "Only native tokens can be stored!");
}
self.mint_burn_allowed(&token_id).set(mint_burn_allowed);
let _ = self.token_whitelist().insert(token_id.clone());
if is_native_token {
Expand All @@ -96,52 +98,62 @@ pub trait TokenModule: fee_estimator_module::FeeEstimatorModule {
self.token_native().swap_remove(&token_id);
}

#[endpoint(mintToken)]
fn mint_token(&self, token_id: &TokenIdentifier, amount: &BigUint) -> EsdtTokenPayment {
#[endpoint(getTokens)]
fn get_tokens(&self, token_id: &TokenIdentifier, amount: &BigUint) -> bool {
let caller = self.blockchain().get_caller();
require!(
caller == self.multi_transfer_contract_address().get(),
"Only MultiTransfer can get tokens"
);

if self.mint_burn_allowed(token_id).get() {
if !self
.blockchain()
.get_esdt_local_roles(token_id)
.has_role(&EsdtLocalRole::Mint)
{
return EsdtTokenPayment::new(token_id.clone(), 0, BigUint::zero());
}

if !self.token_native().contains(token_id) {
let accumulated_burned_tokens_mapper = self.accumulated_burned_tokens(token_id);
accumulated_burned_tokens_mapper.update(|burned| {
require!(*burned >= *amount, "Not enough accumulated burned tokens!");
*burned -= amount;
if !self.mint_burn_allowed(token_id).get() {
let total_balances_mapper = self.total_balances(token_id);
if &total_balances_mapper.get() >= amount {
total_balances_mapper.update(|total| {
*total -= amount;
});
self.send().direct_esdt(&caller, token_id, 0, amount);
return true;
} else {
return false;
}
self.mint_esdt_token(token_id, amount);
}

let current_balance =
self.blockchain()
.get_esdt_balance(&self.blockchain().get_sc_address(), token_id, 0);
if &current_balance >= amount {
self.send().direct_esdt(&caller, token_id, 0, amount);
} else {
return EsdtTokenPayment::new(token_id.clone(), 0, BigUint::zero());
let burn_balances_mapper = self.burn_balances(token_id);
let mint_balances_mapper = self.mint_balances(token_id);
if self.token_native().contains(token_id) {
require!(burn_balances_mapper.get() >= &mint_balances_mapper.get() + amount, "Not enough burned tokens!");
}

let mint_executed = self.internal_mint(token_id, amount);
if !mint_executed {
return false;
}
EsdtTokenPayment::new(token_id.clone(), 0, amount.clone())
mint_balances_mapper.update(|minted| {
*minted += amount;
});

true
}

// private

fn burn_esdt_token(&self, token_id: &TokenIdentifier, amount: &BigUint) {
self.send().esdt_local_burn(token_id, 0, amount);
fn internal_mint(&self, token_id: &TokenIdentifier, amount: &BigUint) -> bool {
if !self.is_local_role_set(token_id, &EsdtLocalRole::Mint)
{
return false;
}
self.send().esdt_local_mint(token_id, 0, amount);
return true;
}

fn mint_esdt_token(&self, token_id: &TokenIdentifier, amount: &BigUint) {
self.send().esdt_local_mint(token_id, 0, amount);
fn internal_burn(&self, token_id: &TokenIdentifier, amount: &BigUint) -> bool {
if !self.is_local_role_set(token_id, &EsdtLocalRole::Burn)
{
return false;
}
self.send().esdt_local_burn(token_id, 0, amount);
return true;
}

fn require_token_in_whitelist(&self, token_id: &TokenIdentifier) {
Expand Down Expand Up @@ -176,9 +188,21 @@ 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);
#[endpoint(setTotalBalances)]
fn set_total_balances(&self, token_id: &TokenIdentifier, value: BigUint) {
self.total_balances(token_id).set_if_empty(value);
}

#[only_owner]
#[endpoint(setMintBalances)]
fn set_mint_balances(&self, token_id: &TokenIdentifier, value: BigUint) {
self.mint_balances(token_id).set_if_empty(value);
}

#[only_owner]
#[endpoint(setBurnBalances)]
fn set_burn_balances(&self, token_id: &TokenIdentifier, value: BigUint) {
self.burn_balances(token_id).set_if_empty(value);
}

// storage
Expand Down Expand Up @@ -206,7 +230,15 @@ pub trait TokenModule: fee_estimator_module::FeeEstimatorModule {
token_id: &TokenIdentifier,
) -> SingleValueMapper<BigUint>;

#[view(getAccumulatedBurnedTokens)]
#[storage_mapper("accumulatedBurnedTokens")]
fn accumulated_burned_tokens(&self, token_id: &TokenIdentifier) -> SingleValueMapper<BigUint>;
#[view(getTotalBalances)]
#[storage_mapper("totalBalances")]
fn total_balances(&self, token_id: &TokenIdentifier) -> SingleValueMapper<BigUint>;

#[view(getMintBalances)]
#[storage_mapper("mintBalances")]
fn mint_balances(&self, token_id: &TokenIdentifier) -> SingleValueMapper<BigUint>;

#[view(getBurnBalances)]
#[storage_mapper("burnBalances")]
fn burn_balances(&self, token_id: &TokenIdentifier) -> SingleValueMapper<BigUint>;
}
28 changes: 18 additions & 10 deletions esdt-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,6 @@ pub trait EsdtSafe:

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
if self.mint_burn_allowed(&tx.token_identifier).get()
&& self.is_local_role_set(&tx.token_identifier, &EsdtLocalRole::Burn)
{
self.burn_esdt_token(&tx.token_identifier, &tx.amount);
self.accumulated_burned_tokens(&tx.token_identifier)
.update(|burned| *burned += &tx.amount);
}
}
TransactionStatus::Rejected => {
let addr = ManagedAddress::try_from(tx.from).unwrap();
Expand Down Expand Up @@ -231,6 +221,22 @@ pub trait EsdtSafe:
};

let batch_id = self.add_to_batch(tx);
if !self.mint_burn_allowed(&payment_token).get() {
self.total_balances(&payment_token).update(|total| {
*total += &actual_bridged_amount;
});
} else {
let burn_balances_mapper = self.burn_balances(&payment_token);
let mint_balances_mapper = self.mint_balances(&payment_token);
if self.token_native().contains(&payment_token) {
require!(mint_balances_mapper.get() >= &burn_balances_mapper.get() + &actual_bridged_amount, "Not enough minted tokens!");
}
let burn_executed = self.internal_burn(&payment_token, &actual_bridged_amount);
require!(burn_executed, "Cannot do the burn action!");
burn_balances_mapper.update(|burned| {
*burned += &actual_bridged_amount;
});
}
self.create_transaction_event(batch_id, tx_nonce, payment_token, actual_bridged_amount);
}

Expand All @@ -244,6 +250,8 @@ pub trait EsdtSafe:
require!(refund_amount > 0, "Nothing to refund");

self.refund_amount(&caller, &token_id).clear();
let mint_executed = self.internal_mint(&token_id, &refund_amount);
require!(mint_executed, "Cannot do the mint action!");
self.send()
.direct_esdt(&caller, &token_id, 0, &refund_amount);

Expand Down
8 changes: 4 additions & 4 deletions multi-transfer-esdt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ pub trait MultiTransferEsdt:
continue;
}

let minted_token: EsdtTokenPayment = self
let is_success: bool = self
.get_esdt_safe_contract_proxy_instance()
.mint_token(&eth_tx.token_id, &eth_tx.amount)
.get_tokens(&eth_tx.token_id, &eth_tx.amount)
.execute_on_dest_context();

if minted_token.amount == BigUint::zero() {
if !is_success {
let refund_tx = self.convert_to_refund_tx(eth_tx);
refund_tx_list.push(refund_tx);

Expand All @@ -98,7 +98,7 @@ pub trait MultiTransferEsdt:
self.transfer_performed_event(batch_id, eth_tx.tx_nonce);

valid_tx_list.push(eth_tx.clone());
valid_payments_list.push(minted_token);
valid_payments_list.push(EsdtTokenPayment::new(eth_tx.token_id, 0, eth_tx.amount));
}

let payments_after_wrapping = self.wrap_tokens(valid_payments_list);
Expand Down

0 comments on commit f0e5f39

Please sign in to comment.