Skip to content

Commit

Permalink
feat: one token whitelist support
Browse files Browse the repository at this point in the history
  • Loading branch information
bucurdavid committed Jun 13, 2024
1 parent 01676a8 commit 82c2d4a
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 63 deletions.
17 changes: 4 additions & 13 deletions src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,12 @@ pub trait AdminModule:
}

#[endpoint(setDepositLimits)]
fn set_deposit_limits(
&self,
token_identifier: TokenIdentifier,
minimum: BigUint,
maximum: BigUint,
) {
fn set_deposit_limits(&self, minimum: BigUint, maximum: BigUint) {
only_privileged!(self, ERR_NOT_PRIVILEGED);
require!(
self.token_whitelist().get() == token_identifier,
ERR_TOKEN_NOT_WHITELISTED
);
require!(minimum <= maximum, ERR_WRONG_VALUES);
self.set_deposit_limits_event(&token_identifier, &minimum, &maximum);
self.minimum_deposit(&token_identifier).set(minimum);
self.maximum_deposit(&token_identifier).set(maximum);
self.set_deposit_limits_event(&minimum, &maximum);
self.minimum_deposit().set(minimum);
self.maximum_deposit().set(maximum);
}

#[endpoint(setFeeCollector)]
Expand Down
7 changes: 1 addition & 6 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ pub trait EventsModule {
fn set_whitelist_state_event(&self, #[indexed] state: &State);

#[event("setDepositLimitsEvent")]
fn set_deposit_limits_event(
&self,
#[indexed] token_identifier: &TokenIdentifier,
#[indexed] minimum: &BigUint,
#[indexed] maximum: &BigUint,
);
fn set_deposit_limits_event(&self, #[indexed] minimum: &BigUint, #[indexed] maximum: &BigUint);

#[event("addTokenToWhitelistEvent")]
fn add_token_to_whitelist_event(&self, #[indexed] token: &TokenIdentifier, decimals: &u32);
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub trait CoreMxBridgeSc:

#[upgrade]
fn upgrade(&self) {
self.set_public_state_inactive();
self.set_relayer_state_inactive();
self.public_state().set(config::State::Inactive);
self.relayer_state().set(config::State::Inactive);
}

#[payable("*")]
Expand Down Expand Up @@ -66,8 +66,8 @@ pub trait CoreMxBridgeSc:
);

require!(
self.minimum_deposit(&deposit.token_identifier).get() <= deposit.amount
&& deposit.amount <= self.maximum_deposit(&deposit.token_identifier).get(),
self.minimum_deposit().get() <= deposit.amount
&& deposit.amount <= self.maximum_deposit().get(),
ERR_PAYMENT_AMOUNT_NOT_IN_ACCEPTED_RANGE
);

Expand Down Expand Up @@ -105,8 +105,8 @@ pub trait CoreMxBridgeSc:
);

require!(
self.minimum_deposit(&deposit.token_identifier).get() <= deposit.amount
&& deposit.amount <= self.maximum_deposit(&deposit.token_identifier).get(),
self.minimum_deposit().get() <= deposit.amount
&& deposit.amount <= self.maximum_deposit().get(),
ERR_PAYMENT_AMOUNT_NOT_IN_ACCEPTED_RANGE
);

Expand Down
4 changes: 2 additions & 2 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ pub trait StorageModule {

#[view(getMinimumDeposit)]
#[storage_mapper("minimum_deposit")]
fn minimum_deposit(&self, token_identifier: &TokenIdentifier) -> SingleValueMapper<BigUint>;
fn minimum_deposit(&self) -> SingleValueMapper<BigUint>;

#[view(getMaximumDeposit)]
#[storage_mapper("maximum_deposit")]
fn maximum_deposit(&self, token_identifier: &TokenIdentifier) -> SingleValueMapper<BigUint>;
fn maximum_deposit(&self) -> SingleValueMapper<BigUint>;

#[view(getFeeCollector)]
#[storage_mapper("fee_collector")]
Expand Down
2 changes: 0 additions & 2 deletions tests/bridge_sc/bridge_sc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ impl ContractState {
pub fn set_deposit_limits(
&mut self,
caller: &str,
token_identifier: &[u8],
min_deposit: &[u8],
max_deposit: &[u8],
expect: Option<TxExpect>,
Expand All @@ -368,7 +367,6 @@ impl ContractState {
ScCallStep::new()
.from(caller)
.call(self.contract.set_deposit_limits(
managed_token_id!(token_identifier),
BigUint::from(managed_buffer!(min_deposit)),
BigUint::from(managed_buffer!(max_deposit)),
))
Expand Down
30 changes: 2 additions & 28 deletions tests/endpoints/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ use core_mx_bridge_sc::{
config::{ProxyTrait as _, State},
storage::ProxyTrait as _,
};
use multiversx_sc::{
imports::SingleValue,
types::{BigUint, MultiValueEncoded},
};
use multiversx_sc::{imports::SingleValue, types::BigUint};
use multiversx_sc_scenario::{
managed_token_id,
scenario_model::{CheckAccount, CheckStateStep, ScQueryStep, TxExpect},
ExpectValue,
};

use crate::bridge_sc::bridge_sc::{
Expand Down Expand Up @@ -85,41 +81,19 @@ fn set_deposit_limits_test() {

state.set_deposit_limits(
ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR,
ITHEUM_TOKEN_IDENTIFIER,
b"10",
b"20",
Some(TxExpect::user_error("str:Token not whitelisted")),
);

state.add_token_to_whitelist(
ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR,
ITHEUM_TOKEN_IDENTIFIER,
None,
);

state.set_deposit_limits(
ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR,
ITHEUM_TOKEN_IDENTIFIER,
b"20",
b"10",
Some(TxExpect::user_error("str:Wrong values")),
);

state.set_deposit_limits(
FIRST_USER_ADDRESS_EXPR,
ITHEUM_TOKEN_IDENTIFIER,
b"10",
b"20",
Some(TxExpect::user_error("str:Not privileged")),
);

state.set_deposit_limits(
ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR,
ITHEUM_TOKEN_IDENTIFIER,
b"0",
b"10",
None,
);
state.set_deposit_limits(ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, b"0", b"10", None);
}

#[test]
Expand Down
10 changes: 4 additions & 6 deletions tests/endpoints/public.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use multiversx_sc_scenario::imports::{
Account, CheckAccount, CheckStateStep, ScCallStep, SetStateStep, TxExpect,
Account, CheckAccount, CheckStateStep, SetStateStep, TxExpect,
};

use crate::bridge_sc::bridge_sc::{
ContractState, ADMIN_BRIDGE_CONTRACT_ADDRESS_EXPR, ANOTHER_TOKEN_IDENTIFIER_EXPR,
BRIDGE_CONTRACT_ADDRESS_EXPR, FIRST_USER_ADDRESS_EXPR, ITHEUM_TOKEN_IDENTIFIER,
ITHEUM_TOKEN_IDENTIFIER_EXPR, OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR,
RELAYER_BRIDGE_CONTRACT_ADDRESS_EXPR, THIRD_USER_ADDRESS_EXPR, WEGLD_TOKEN_IDENTIFIER_EXPR,
BRIDGE_CONTRACT_ADDRESS_EXPR, FIRST_USER_ADDRESS_EXPR, ITHEUM_TOKEN_IDENTIFIER_EXPR,
OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR, RELAYER_BRIDGE_CONTRACT_ADDRESS_EXPR,
THIRD_USER_ADDRESS_EXPR, WEGLD_TOKEN_IDENTIFIER_EXPR,
};

#[test]
Expand Down Expand Up @@ -88,7 +88,6 @@ fn send_to_bridge_test() {
);
state.set_deposit_limits(
OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR,
ITHEUM_TOKEN_IDENTIFIER,
b"0",
b"100000000000000000000", // 100 tokens
None,
Expand Down Expand Up @@ -142,7 +141,6 @@ fn send_to_bridge_require_fee_test() {

state.set_deposit_limits(
OWNER_BRIDGE_CONTRACT_ADDRESS_EXPR,
ITHEUM_TOKEN_IDENTIFIER,
b"0",
b"1000000000000000000000", // 1000 tokens
None,
Expand Down

0 comments on commit 82c2d4a

Please sign in to comment.