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

fix: audit fix issues: 1,4,5 and suggestion(2) #5

Merged
merged 4 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 0 additions & 21 deletions Cargo.lock

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

9 changes: 0 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,9 @@ path = "src/lib.rs"
[dependencies.multiversx-sc]
version = "0.50.3"

[dependencies.multiversx-sc-modules]
version = "0.50.3"

[dev-dependencies]
multiversx-wegld-swap-sc = "0.50.3"


[dev-dependencies.multiversx-sc-scenario]
version = "0.50.3"



[workspace]
members = [
".",
Expand Down
52 changes: 45 additions & 7 deletions interaction/devnet.snippets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ upgrade(){



addTokenToWhitelist(){
# $1 = token
# $2 = decimals

token="0x$(echo -n ${1} | xxd -p -u | tr -d '\n')"

mxpy --verbose contract call ${ADDRESS} \
--recall-nonce \
--pem=${WALLET} \
--gas-limit=6000000 \
--function "addTokenToWhitelist" \
--arguments $token $2 \
--proxy ${PROXY} \
--chain ${CHAIN_ID} \
--send || return

}



setAdministrator(){
# $1 = address

Expand Down Expand Up @@ -88,8 +108,6 @@ setFeeCollector(){
setFeeValue(){
# $1 = value



mxpy --verbose contract call ${ADDRESS} \
--recall-nonce \
--pem=${WALLET} \
Expand All @@ -102,17 +120,37 @@ setFeeValue(){

}

setWegldContractAddress(){
# $1 = address

address="0x$(mxpy wallet bech32 --decode ${1})"
setDepositLimits(){
# $1 = min
# $2 = max



mxpy --verbose contract call ${ADDRESS} \
--recall-nonce \
--pem=${WALLET} \
--gas-limit=6000000 \
--function "setWegldContractAddress" \
--arguments $address \
--function "setDepositLimits" \
--arguments $1 $2 \
--proxy ${PROXY} \
--chain ${CHAIN_ID} \
--send || return


}

setWegldTokenIdentifier(){
# $1 = token

token="0x$(echo -n ${1} | xxd -p -u | tr -d '\n')"

mxpy --verbose contract call ${ADDRESS} \
--recall-nonce \
--pem=${WALLET} \
--gas-limit=6000000 \
--function "setWegldTokenIdentifier" \
--arguments $token \
--proxy ${PROXY} \
--chain ${CHAIN_ID} \
--send || return
Expand Down
77 changes: 36 additions & 41 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.tokens_whitelist().contains(&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 All @@ -119,55 +110,58 @@ pub trait AdminModule:
#[endpoint(setFeeValue)]
fn set_fee_value(&self, fee_value: BigUint) {
only_privileged!(self, ERR_NOT_PRIVILEGED);
self.set_fee_value_event(&fee_value);
self.fee_value().set(fee_value);
}

#[endpoint(setWegldContractAddress)]
fn set_wegld_contract_address(&self, wegld_contract_address: ManagedAddress) {
#[endpoint(setWegldTokenIdentifier)]
fn set_wegld_contract_address(&self, wegld_token_identifier: TokenIdentifier) {
only_privileged!(self, ERR_NOT_PRIVILEGED);
self.set_wegld_contract_address_event(&wegld_contract_address);
self.wegld_contract_address().set(wegld_contract_address);
self.set_wegld_token_identifier(&wegld_token_identifier);
self.wegld_token_identifier().set(wegld_token_identifier);
}

#[endpoint(addTokensToWhitelist)]
fn add_tokens_to_whitelist(
&self,
tokens: MultiValueEncoded<MultiValue2<TokenIdentifier, u32>>,
) {
#[endpoint(addTokenToWhitelist)]
fn add_tokens_to_whitelist(&self, token: TokenIdentifier, decimals: u32) {
only_privileged!(self, ERR_NOT_PRIVILEGED);

for token in tokens.into_iter() {
let (token_identifier, token_decimals) = token.into_tuple();
self.token_decimals(&token_identifier).set(token_decimals);
require!(
self.tokens_whitelist().insert(token_identifier),
ERR_TOKEN_ALREADY_IN_WHITELIST
);
self.add_token_to_whitelist_event(&token, &decimals);
if !self.token_whitelist().is_empty() {
self.token_decimals(&token).set(decimals);
self.token_whitelist().update(|whitelist_token| {
require!(*whitelist_token != token, ERR_TOKEN_ALREADY_IN_WHITELIST);
*whitelist_token = token
});
} else {
self.token_decimals(&token).set(decimals);
self.token_whitelist().set(token);
}
}

#[endpoint(removeTokensFromWhitelist)]
fn remove_tokens_from_whitelist(&self, tokens: MultiValueEncoded<TokenIdentifier>) {
#[endpoint(removeTokenFromWhitelist)]
fn remove_token_from_whitelist(&self, token: TokenIdentifier) {
only_privileged!(self, ERR_NOT_PRIVILEGED);
self.remove_tokens_from_whitelist_event(&tokens.to_vec());
for token in tokens.into_iter() {
self.token_decimals(&token).clear();
require!(
self.tokens_whitelist().swap_remove(&token),
ERR_TOKEN_NOT_WHITELISTED
);
}

self.remove_token_from_whitelist_event(&token);
require!(
self.token_whitelist().get() == token,
ERR_TOKEN_NOT_WHITELISTED
);

self.token_decimals(&token).clear();
self.token_whitelist().clear();
}

#[endpoint(addToWhitelist)]
fn add_to_whitelist(&self, addresses: MultiValueEncoded<ManagedAddress>) {
only_privileged!(self, ERR_NOT_PRIVILEGED);
for address in addresses.into_iter() {
require!(
self.whitelist().contains(&address) == false,
!self.whitelist().contains(&address),
ERR_ADDRESS_ALREADY_WHITELISTED
);
self.whitelist().add(&address);
self.add_to_whitelist_event(&address);
}
}

Expand All @@ -180,6 +174,7 @@ pub trait AdminModule:
ERR_ADDRESS_NOT_WHITELISTED
);
self.whitelist().remove(&address);
self.remove_from_whitelist_event(&address);
}
}

Expand All @@ -198,7 +193,7 @@ pub trait AdminModule:
let payment = self.call_value().single_esdt();

require!(
self.tokens_whitelist().contains(&payment.token_identifier),
self.token_whitelist().get() == payment.token_identifier,
ERR_TOKEN_NOT_WHITELISTED
);

Expand Down
8 changes: 4 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ pub trait ConfigModule: storage::StorageModule + events::EventsModule {
is_ready = false;
}

if self.wegld_contract_address().is_empty() {
if self.wegld_token_identifier().is_empty() {
is_ready = false;
}
}

if self.relayer().is_empty() {
is_ready = false;
}
if self.tokens_whitelist().is_empty() {
if self.token_whitelist().is_empty() {
is_ready = false;
}
is_ready
Expand All @@ -103,8 +103,8 @@ pub trait ConfigModule: storage::StorageModule + events::EventsModule {
fn administrator(&self) -> SingleValueMapper<ManagedAddress>;

#[view(getTokensWhitelist)]
#[storage_mapper("tokens_whitelist")]
fn tokens_whitelist(&self) -> UnorderedSetMapper<TokenIdentifier>;
#[storage_mapper("token_whitelist")]
fn token_whitelist(&self) -> SingleValueMapper<TokenIdentifier>;

#[storage_mapper("whitelist")]
fn whitelist(&self) -> WhitelistMapper<ManagedAddress>;
Expand Down
31 changes: 15 additions & 16 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use multiversx_sc::types::ManagedVec;

use crate::config::State;

#[multiversx_sc::module]
Expand All @@ -14,6 +12,12 @@ pub trait EventsModule {
#[indexed] destination_signature: &ManagedBuffer,
);

#[event("addToWhitelist")]
fn add_to_whitelist_event(&self, #[indexed] address: &ManagedAddress);

#[event("removeFromWhitelist")]
fn remove_from_whitelist_event(&self, #[indexed] address: &ManagedAddress);

#[event("sendFromLiquidityEvent")]
fn send_from_liquidity_event(
&self,
Expand All @@ -29,12 +33,6 @@ pub trait EventsModule {
#[event("setAdministratorEvent")]
fn set_administrator_event(&self, #[indexed] administrator: &ManagedAddress);

#[event("addTokensToWhitelistEvent")]
fn add_tokens_to_whitelist_event(&self, #[indexed] tokens: &ManagedVec<TokenIdentifier>);

#[event("removeTokensFromWhitelistEvent")]
fn remove_tokens_from_whitelist_event(&self, #[indexed] tokens: &ManagedVec<TokenIdentifier>);

#[event("setRelayerEvent")]
fn set_relayer_event(&self, #[indexed] relayer: &ManagedAddress);

Expand All @@ -58,21 +56,22 @@ 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);

#[event("removeTokenFromWhitelistEvent")]
fn remove_token_from_whitelist_event(&self, #[indexed] token: &TokenIdentifier);

#[event("setFeeCollectorEvent")]
fn set_fee_collector_event(&self, #[indexed] fee_collector: &ManagedAddress);

#[event("setFeeValueEvent")]
fn set_fee_value_event(&self, #[indexed] fee_value: &BigUint);

#[event("setWegldContractAddressEvent")]
fn set_wegld_contract_address_event(&self, #[indexed] wegld_contract_address: &ManagedAddress);
#[event("setWegldTokenIdentifier")]
fn set_wegld_token_identifier(&self, #[indexed] wegld_token_identifier: &TokenIdentifier);

#[event("setRelayerStateEvent")]
fn set_relayer_state_event(&self, #[indexed] state: &State);
Expand Down
Loading
Loading