Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mintBurn or locking functionality #154

Merged
merged 33 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
743facd
add mintBurn or locking functionality
dragos-rebegea Oct 25, 2023
fc41c0d
fix proxytrait and always wrap tokens
dragos-rebegea Oct 26, 2023
1414d36
Merge branch 'feat/tx-with-SC-call' into mintBurn-or-locking
CostinCarabas Oct 27, 2023
dc4f12d
Fix compiling issues
CostinCarabas Oct 27, 2023
f85a3db
Fix clippy
CostinCarabas Oct 27, 2023
c6079b6
Fix dependencies
CostinCarabas Oct 27, 2023
ea878b3
Add blackbox test for Multi Transfer SC
CostinCarabas Oct 30, 2023
f90ac87
Add check storage for test
CostinCarabas Oct 30, 2023
23260b3
Fix compilation issues
CostinCarabas Nov 9, 2023
13c9034
add accumulated burned tokens
dragos-rebegea Nov 27, 2023
3228628
Framework upgrade 0.45.1
CostinCarabas Dec 5, 2023
da8fcec
Fixed after reviews
CostinCarabas Dec 20, 2023
3557b6e
Merge remote-tracking branch 'origin/feat/tx-with-SC-call' into mintB…
dragos-rebegea Dec 27, 2023
6c43b9d
Fixes after review
CostinCarabas Dec 27, 2023
b249789
Merge branch 'feat/tx-with-SC-call' into mintBurn-or-locking
CostinCarabas Dec 27, 2023
3743ad0
Merge remote-tracking branch 'origin/mintBurn-or-locking' into mintBu…
CostinCarabas Dec 27, 2023
a93762f
Comiling fixes
CostinCarabas Dec 27, 2023
d1767c5
Multi transfer: Batch Transfer blackbox test
CostinCarabas Dec 29, 2023
1185594
Fix go tests - replace mandos -> scenarios
CostinCarabas Dec 29, 2023
37772a6
Fix mandos tests
CostinCarabas Dec 29, 2023
a40c1c0
Update price-aggregator to 0.45.2
CostinCarabas Jan 3, 2024
b9958ac
price-aggregator scenarios:Submit correctly prices
CostinCarabas Jan 4, 2024
455aaeb
Fix tests
CostinCarabas Jan 5, 2024
40d7189
Update Rust toolchain
CostinCarabas Jan 5, 2024
b7717ae
Rebranding elrond -> MultiversX
CostinCarabas Jan 5, 2024
d6d2ff2
Framework upgrade 0.46
CostinCarabas Jan 5, 2024
fb20be9
esdt-safe: tests: ix roles
CostinCarabas Jan 5, 2024
ba6292b
Bridge-proxy: Add option for args + tests
CostinCarabas Jan 9, 2024
8c54750
Bridge-proxy: transform args to ManagedVec
CostinCarabas Jan 10, 2024
a6c8ae1
Update common/token-module/src/lib.rs
CostinCarabas Jan 12, 2024
58546e3
Multi-transfer: Add new test
CostinCarabas Jan 12, 2024
40dc6ea
Framework upgrade 0.46.1
CostinCarabas Jan 19, 2024
12ea6db
Test fix
CostinCarabas Jan 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions common/token-module/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub trait TokenModule: fee_estimator_module::FeeEstimatorModule {
&self,
token_id: TokenIdentifier,
ticker: ManagedBuffer,
mint_burn_allowed: bool,
opt_default_price_per_gas_unit: OptionalValue<BigUint>,
) {
self.token_ticker(&token_id).set(&ticker);
Expand All @@ -76,6 +77,7 @@ pub trait TokenModule: fee_estimator_module::FeeEstimatorModule {
.set(&default_price_per_gas_unit);
}

self.whitelisted_token_mint_burn(&token_id).set(mint_burn_allowed);
CostinCarabas marked this conversation as resolved.
Show resolved Hide resolved
let _ = self.token_whitelist().insert(token_id);
}

Expand All @@ -88,8 +90,32 @@ pub trait TokenModule: fee_estimator_module::FeeEstimatorModule {
let _ = self.token_whitelist().swap_remove(&token_id);
}

#[endpoint(getToken)]
CostinCarabas marked this conversation as resolved.
Show resolved Hide resolved
fn get_token(&self, token_id: &TokenIdentifier, amount: &BigUint) {
let caller = self.blockchain().get_caller();
require!(caller == self.multi_transfer_contract_address().get(), "Only MultiTransfer can get tokens");
if self.whitelisted_token_mint_burn(token_id).get() {
self.mint_esdt_token(token_id, amount);
}

self.send().direct_esdt(
&self.blockchain().get_caller(),
CostinCarabas marked this conversation as resolved.
Show resolved Hide resolved
token_id,
0,
amount,
);
}

// private

fn burn_esdt_token(&self, token_id: &TokenIdentifier, amount: &BigUint) {
CostinCarabas marked this conversation as resolved.
Show resolved Hide resolved
self.send().esdt_local_burn(token_id, 0, amount);
}

fn mint_esdt_token(&self, token_id: &TokenIdentifier, amount: &BigUint) {
self.send().esdt_local_mint(token_id, 0, amount);
}

fn require_token_in_whitelist(&self, token_id: &TokenIdentifier) {
require!(
self.token_whitelist().contains(token_id),
Expand All @@ -110,12 +136,35 @@ pub trait TokenModule: fee_estimator_module::FeeEstimatorModule {
roles.has_role(role)
}

#[only_owner]
#[endpoint(setMultiTransferContractAddress)]
fn set_multi_transfer_contract_address(&self, opt_new_address: OptionalValue<ManagedAddress>) {
CostinCarabas marked this conversation as resolved.
Show resolved Hide resolved
match opt_new_address {
OptionalValue::Some(sc_addr) => {
require!(
self.blockchain().is_smart_contract(&sc_addr),
"Invalid multi transfer contract address"
);

self.multi_transfer_contract_address().set(&sc_addr);
}
OptionalValue::None => self.multi_transfer_contract_address().clear(),
}
}

// storage

#[view(getAllKnownTokens)]
#[storage_mapper("tokenWhitelist")]
fn token_whitelist(&self) -> UnorderedSetMapper<TokenIdentifier>;

#[storage_mapper("whitelistedTokenMintBurn")]
fn whitelisted_token_mint_burn(&self, token: &TokenIdentifier) -> SingleValueMapper<bool>;

#[view(getMultiTransferContractAddress)]
#[storage_mapper("multiTransferContractAddress")]
fn multi_transfer_contract_address(&self) -> SingleValueMapper<ManagedAddress>;

#[view(getAccumulatedTransactionFees)]
#[storage_mapper("accumulatedTransactionFees")]
fn accumulated_transaction_fees(
Expand Down
8 changes: 3 additions & 5 deletions esdt-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ pub trait EsdtSafe:
// 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.is_local_role_set(&tx.token_identifier, &EsdtLocalRole::Burn) {
if self.whitelisted_token_mint_burn(&tx.token_identifier).get()
&& self.is_local_role_set(&tx.token_identifier, &EsdtLocalRole::Burn)
{
self.burn_esdt_token(&tx.token_identifier, &tx.amount);
}
}
Expand Down Expand Up @@ -243,10 +245,6 @@ pub trait EsdtSafe:

// private

fn burn_esdt_token(&self, token_id: &TokenIdentifier, amount: &BigUint) {
self.send().esdt_local_burn(token_id, 0, amount);
}

fn mark_refund(&self, to: &ManagedAddress, token_id: &TokenIdentifier, amount: &BigUint) {
self.refund_amount(to, token_id)
.update(|refund| *refund += amount);
Expand Down
7 changes: 5 additions & 2 deletions esdt-safe/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 31
// Endpoints: 34
// Async Callback (empty): 1
// Total number of exported functions: 33
// Total number of exported functions: 36

#![no_std]
#![feature(lang_items)]
Expand Down Expand Up @@ -35,7 +35,10 @@ multiversx_sc_wasm_adapter::endpoints! {
distributeFees => distribute_fees
addTokenToWhitelist => add_token_to_whitelist
removeTokenFromWhitelist => remove_token_from_whitelist
getToken => get_token
setMultiTransferContractAddress => set_multi_transfer_contract_address
getAllKnownTokens => token_whitelist
getMultiTransferContractAddress => multi_transfer_contract_address
getAccumulatedTransactionFees => accumulated_transaction_fees
setMaxTxBatchSize => set_max_tx_batch_size
setMaxTxBatchBlockDuration => set_max_tx_batch_block_duration
Expand Down
6 changes: 6 additions & 0 deletions multi-transfer-esdt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ path = "../bridged-tokens-wrapper"
[dependencies.bridge-proxy]
path = "../bridge-proxy"

[dependencies.esdt-safe]
path = "../esdt-safe"

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

[dependencies.multiversx-sc]
version = "0.42.0"
[dev-dependencies.multiversx-sc-scenario]
Expand Down
68 changes: 50 additions & 18 deletions multi-transfer-esdt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ multiversx_sc::imports!();
use transaction::{
EthTransaction, EthTransactionPayment, PaymentsVec, Transaction, TxBatchSplitInFields,
};
use token_module::ProxyTrait as OtherProxyTrait;

const DEFAULT_MAX_TX_BATCH_SIZE: usize = 10;
const DEFAULT_MAX_TX_BATCH_BLOCK_DURATION: u64 = u64::MAX;
Expand Down Expand Up @@ -33,7 +34,7 @@ pub trait MultiTransferEsdt:
transfers: MultiValueEncoded<EthTransaction<Self::Api>>,
) {
let mut valid_payments_list = ManagedVec::new();
let mut valid_dest_addresses_list = ManagedVec::new();
let mut valid_tx_list = ManagedVec::new();
let mut refund_tx_list = ManagedVec::new();

let own_sc_address = self.blockchain().get_sc_address();
Expand Down Expand Up @@ -66,26 +67,21 @@ pub trait MultiTransferEsdt:
continue;
}

self.send()
.esdt_local_mint(&eth_tx.token_id, 0, &eth_tx.amount);
let _: IgnoreValue = self
.get_esdt_safe_contract_proxy_instance()
.get_token(&eth_tx.token_id, &eth_tx.amount)
.with_esdt_transfer((eth_tx.token_id.clone(), 0, eth_tx.amount.clone()))
.execute_on_dest_context();

// 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);

if self.blockchain().is_smart_contract(&eth_tx.to.clone()) {
let _: IgnoreValue = self
.get_bridge_proxy_contract_proxy_instance()
.deposit(&eth_tx)
.with_esdt_transfer((eth_tx.token_id.clone(), 0, eth_tx.amount.clone()))
.execute_on_dest_context();
} else {
valid_dest_addresses_list.push(eth_tx.to);
valid_payments_list.push(EsdtTokenPayment::new(eth_tx.token_id, 0, eth_tx.amount));
}
valid_tx_list.push(eth_tx.clone());
valid_payments_list.push(EsdtTokenPayment::new(eth_tx.token_id, 0, eth_tx.amount));
CostinCarabas marked this conversation as resolved.
Show resolved Hide resolved
}

let payments_after_wrapping = self.wrap_tokens(valid_payments_list);
self.distribute_payments(valid_dest_addresses_list, payments_after_wrapping);
self.distribute_payments(valid_tx_list, payments_after_wrapping);

self.add_multiple_tx_to_batch(&refund_tx_list);
}
Expand Down Expand Up @@ -136,6 +132,22 @@ pub trait MultiTransferEsdt:
}
}

#[only_owner]
#[endpoint(setEsdtSafeContractAddress)]
fn set_esdt_safe_contract_address(&self, opt_new_address: OptionalValue<ManagedAddress>) {
match opt_new_address {
OptionalValue::Some(sc_addr) => {
require!(
self.blockchain().is_smart_contract(&sc_addr),
"Invalid esdt safe contract address"
);

self.esdt_safe_contract_address().set(&sc_addr);
}
OptionalValue::None => self.esdt_safe_contract_address().clear(),
}
}

#[endpoint(getFailedTxFromBridgeProxy)]
fn get_failed_tx_from_bridge_proxy(&self) {
let mut refund_tx_list = ManagedVec::new();
Expand Down Expand Up @@ -203,12 +215,21 @@ pub trait MultiTransferEsdt:

fn distribute_payments(
&self,
dest_addresses: ManagedVec<ManagedAddress>,
transfers: ManagedVec<EthTransaction<Self::Api>>,
payments: PaymentsVec<Self::Api>,
) {
for (dest, p) in dest_addresses.iter().zip(payments.iter()) {
self.send()
.direct_esdt(&dest, &p.token_identifier, 0, &p.amount);
for (eth_tx, p) in transfers.iter().zip(payments.iter()) {
if self.blockchain().is_smart_contract(&eth_tx.to.clone()) {
CostinCarabas marked this conversation as resolved.
Show resolved Hide resolved
let _: IgnoreValue = self
.get_bridge_proxy_contract_proxy_instance()
.deposit(&eth_tx)
.with_esdt_transfer((eth_tx.token_id.clone(), 0, eth_tx.amount.clone()))
.execute_on_dest_context();
} else {
self.send()
.direct_esdt(&eth_tx.to, &p.token_identifier, 0, &p.amount);
}

}
}

Expand All @@ -231,6 +252,13 @@ pub trait MultiTransferEsdt:
self.bridge_proxy(self.bridge_proxy_contract_address().get())
}

#[proxy]
fn esdt_safe(&self, sc_address: ManagedAddress) -> esdt_safe::Proxy<Self::Api>;

fn get_esdt_safe_contract_proxy_instance(&self) -> esdt_safe::Proxy<Self::Api> {
self.esdt_safe(self.esdt_safe_contract_address().get())
}

// storage
#[view(getWrappingContractAddress)]
#[storage_mapper("wrappingContractAddress")]
Expand All @@ -240,6 +268,10 @@ pub trait MultiTransferEsdt:
#[storage_mapper("bridgeProxyContractAddress")]
fn bridge_proxy_contract_address(&self) -> SingleValueMapper<ManagedAddress>;

#[view(getEsdtSafeContractAddress)]
#[storage_mapper("esdtSafeContractAddress")]
fn esdt_safe_contract_address(&self) -> SingleValueMapper<ManagedAddress>;

// events

#[event("transferPerformedEvent")]
Expand Down
31 changes: 31 additions & 0 deletions multi-transfer-esdt/wasm/Cargo.lock

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

6 changes: 4 additions & 2 deletions multi-transfer-esdt/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 17
// Endpoints: 19
// Async Callback (empty): 1
// Total number of exported functions: 19
// Total number of exported functions: 21

#![no_std]
#![feature(lang_items)]
Expand All @@ -23,9 +23,11 @@ multiversx_sc_wasm_adapter::endpoints! {
getAndClearFirstRefundBatch => get_and_clear_first_refund_batch
setWrappingContractAddress => set_wrapping_contract_address
setBridgeProxyContractAddress => set_bridge_proxy_contract_address
setEsdtSafeContractAddress => set_esdt_safe_contract_address
getFailedTxFromBridgeProxy => get_failed_tx_from_bridge_proxy
getWrappingContractAddress => wrapping_contract_address
getBridgeProxyContractAddress => bridge_proxy_contract_address
getEsdtSafeContractAddress => esdt_safe_contract_address
setMaxTxBatchSize => set_max_tx_batch_size
setMaxTxBatchBlockDuration => set_max_tx_batch_block_duration
getCurrentTxBatch => get_current_tx_batch
Expand Down
21 changes: 20 additions & 1 deletion multisig/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,30 @@ pub trait SetupModule:
&self,
token_id: TokenIdentifier,
ticker: ManagedBuffer,
mint_burn_allowed: bool,
opt_default_price_per_gas_unit: OptionalValue<BigUint>,
) {
let _: IgnoreValue = self
.get_esdt_safe_proxy_instance()
.add_token_to_whitelist(token_id, ticker, opt_default_price_per_gas_unit)
.add_token_to_whitelist(token_id, ticker, mint_burn_allowed, opt_default_price_per_gas_unit)
.execute_on_dest_context();
}

#[only_owner]
#[endpoint(setMultiTransferOnEsdtSafe)]
fn set_multi_transfer_on_esdt_safe(&self) {
let _: IgnoreValue = self
.get_esdt_safe_proxy_instance()
.set_multi_transfer_contract_address(self.multi_transfer_esdt_address().get())
.execute_on_dest_context();
}

#[only_owner]
#[endpoint(setEsdtSafeOnMultiTransfer)]
fn set_esdt_safe_on_multi_transfer(&self) {
let _: IgnoreValue = self
.get_multi_transfer_esdt_proxy_instance()
.set_esdt_safe_contract_address(self.esdt_safe_address().get())
.execute_on_dest_context();
}

Expand Down
2 changes: 2 additions & 0 deletions multisig/wasm/Cargo.lock

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

Loading