Skip to content

Commit

Permalink
whitelist authorization, u128 casting to i127 and verify_withdraw met…
Browse files Browse the repository at this point in the history
…hod private -> fixes
  • Loading branch information
sagars committed Sep 30, 2024
1 parent a1d1e04 commit 4eaa04a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 61 deletions.
33 changes: 22 additions & 11 deletions contracts/asset_manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ pub struct AssetManager;

#[contractimpl]
impl AssetManager {
pub fn initialize(env: Env, registry: Address, admin: Address, config: ConfigData) {
pub fn initialize(env: Env, registry: Address, admin: Address, config: ConfigData) -> Result<(), ContractError> {
if has_registry(&env.clone()) {
panic_with_error!(&env, ContractError::ContractAlreadyInitialized)
return Err(ContractError::ContractAlreadyInitialized)
}
write_registry(&env, &registry);
write_administrator(&env, &admin);
Self::configure(env, config);
Ok(())
}

pub fn get_config(env: Env) -> ConfigData {
Expand Down Expand Up @@ -94,14 +95,17 @@ impl AssetManager {
Ok(())
}

pub fn get_rate_limit(env: Env, token_address: Address) -> (u64, u32, u64, u64) {
pub fn get_rate_limit(env: Env, token_address: Address) -> Result<(u64, u32, u64, u64), ContractError> {
let data: TokenData = read_token_data(&env, token_address);
(
if data.period<=0 {
return Err(ContractError::TokenDoesNotExists);
}
Ok((
data.period,
data.percentage,
data.last_update,
data.current_limit,
)
))
}

pub fn reset_limit(env: Env, token: Address) {
Expand All @@ -121,11 +125,11 @@ impl AssetManager {
return token_client.balance(&env.current_contract_address()) as u128;
}

pub fn verify_withdraw(env: Env, token: Address, amount: u128) -> Result<bool, ContractError> {
fn verify_withdraw(env: Env, token: Address, amount: u128) -> Result<bool, ContractError> {
let balance = Self::get_token_balance(&env, token.clone());
let limit = Self::calculate_limit(&env, balance, token.clone())?;
if balance - amount < limit {
panic_with_error!(&env, ContractError::ExceedsWithdrawLimit);
return Err(ContractError::ExceedsWithdrawLimit);
};
let mut data: TokenData = read_token_data(&env, token.clone());
data.current_limit = limit as u64;
Expand Down Expand Up @@ -204,7 +208,7 @@ impl AssetManager {
token.clone(),
current_address.clone(),
amount,
);
)?;

let xcall_message: Deposit = Deposit::new(
token.to_string(),
Expand Down Expand Up @@ -313,14 +317,19 @@ impl AssetManager {

let verified = Self::verify_withdraw(e.clone(), token.clone(), amount)?;
if verified {
Self::transfer_token_to(e, from, token, to, amount);
Self::transfer_token_to(e, from, token, to, amount)?;
}
Ok(())
}

fn transfer_token_to(e: &Env, from: Address, token: Address, to: Address, amount: u128) {
fn transfer_token_to(e: &Env, from: Address, token: Address, to: Address, amount: u128) -> Result<(), ContractError> {
let token_client = token::Client::new(e, &token);
token_client.transfer(&from, &to, &(amount as i128));
if amount <= i128::MAX as u128 {
token_client.transfer(&from, &to, &(amount as i128));
} else {
return Err(ContractError::InvalidAmount)
}
Ok(())
}

pub fn balance_of(e: Env, token: Address) -> i128 {
Expand Down Expand Up @@ -351,4 +360,6 @@ impl AssetManager {
pub fn extend_ttl(e: Env) {
extent_ttl(&e);
}


}
2 changes: 2 additions & 0 deletions contracts/asset_manager/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ pub enum ContractError {
AdminRequired = 11,
TokenExists = 12,
InvalidAddress = 13,
TokenDoesNotExists = 14,
InvalidAmount = 15
}
94 changes: 47 additions & 47 deletions contracts/asset_manager/src/tests/asset_manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ fn test_configure_rate_limit_panic() {
client.configure_rate_limit(&ctx.token, period, percentage);

let limit = client.get_withdraw_limit(&ctx.token);
let verified = client.verify_withdraw(&ctx.token, &limit);
assert_eq!(verified, true);
// let verified = client.verify_withdraw(&ctx.token, &limit);
// assert_eq!(verified, true);
}

#[test]
Expand Down Expand Up @@ -87,8 +87,8 @@ fn test_configure_rate_limit() {
let token_data = client.get_rate_limit(&ctx.token);
assert_eq!(token_data.3, 0);
let limit = client.get_withdraw_limit(&ctx.token);
let verified = client.verify_withdraw(&ctx.token, &limit);
assert_eq!(verified, true);
// let verified = client.verify_withdraw(&ctx.token, &limit);
// assert_eq!(verified, true);
}

#[test]
Expand Down Expand Up @@ -159,51 +159,51 @@ fn test_veryfy_rate_limit() {

let limit = client.get_withdraw_limit(&ctx.token);
assert_eq!(limit, 3000);
let verified = client.verify_withdraw(&ctx.token, &(amount - 3000 - 1));
assert_eq!(verified, true);
// let verified = client.verify_withdraw(&ctx.token, &(amount - 3000 - 1));
// assert_eq!(verified, true);
}

#[test]
#[should_panic(expected = "HostError: Error(Contract, #5)")]
fn test_veryfy_rate_limit_panic_exceeds_withdraw_limit() {
let ctx = TestContext::default();
let client = AssetManagerClient::new(&ctx.env, &ctx.registry);
ctx.init_context(&client);
let period = &300;
let percentage = &300;
client.configure_rate_limit(&ctx.token, period, percentage);

let token_client = token::Client::new(&ctx.env, &ctx.token);
let stellar_asset_client: token::StellarAssetClient =
token::StellarAssetClient::new(&ctx.env, &ctx.token);
let amount_i128: i128 = 100000i128;
let amount = &(amount_i128 as u128);
let mint_amount = &(amount_i128 + amount_i128);

stellar_asset_client.mint(&ctx.depositor, mint_amount);

ctx.mint_native_token(&ctx.depositor, 500u128);
assert_eq!(ctx.get_native_token_balance(&ctx.depositor), 500u128);

token_client.approve(
&ctx.depositor,
&ctx.registry,
&(amount_i128 + amount_i128),
&1312000,
);
client.deposit(
&ctx.depositor,
&ctx.token,
&amount,
&Option::Some(String::from_str(&ctx.env, "")),
&Option::Some(Bytes::from_array(&ctx.env, &[0u8; 32])),
);

let limit = client.get_withdraw_limit(&ctx.token);
assert_eq!(limit, 3000);
let verified = client.verify_withdraw(&ctx.token, &(amount - 3000 + 1));
assert_eq!(verified, true);
}
// #[test]
// #[should_panic(expected = "HostError: Error(Contract, #5)")]
// fn test_veryfy_rate_limit_panic_exceeds_withdraw_limit() {
// let ctx = TestContext::default();
// let client = AssetManagerClient::new(&ctx.env, &ctx.registry);
// ctx.init_context(&client);
// let period = &300;
// let percentage = &300;
// client.configure_rate_limit(&ctx.token, period, percentage);

// let token_client = token::Client::new(&ctx.env, &ctx.token);
// let stellar_asset_client: token::StellarAssetClient =
// token::StellarAssetClient::new(&ctx.env, &ctx.token);
// let amount_i128: i128 = 100000i128;
// let amount = &(amount_i128 as u128);
// let mint_amount = &(amount_i128 + amount_i128);

// stellar_asset_client.mint(&ctx.depositor, mint_amount);

// ctx.mint_native_token(&ctx.depositor, 500u128);
// assert_eq!(ctx.get_native_token_balance(&ctx.depositor), 500u128);

// token_client.approve(
// &ctx.depositor,
// &ctx.registry,
// &(amount_i128 + amount_i128),
// &1312000,
// );
// client.deposit(
// &ctx.depositor,
// &ctx.token,
// &amount,
// &Option::Some(String::from_str(&ctx.env, "")),
// &Option::Some(Bytes::from_array(&ctx.env, &[0u8; 32])),
// );

// let limit = client.get_withdraw_limit(&ctx.token);
// assert_eq!(limit, 3000);
// // let verified = client.verify_withdraw(&ctx.token, &(amount - 3000 + 1));
// // assert_eq!(verified, true);
// }

#[test]
fn test_deposit_with_to_and_without_data() {
Expand Down
18 changes: 15 additions & 3 deletions contracts/balanced_doller/src/balanced_dollar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ pub fn _cross_transfer(
to: String,
data: Bytes,
) -> Result<(), ContractError> {
_burn(&e, from.clone(), amount as i128);
if amount <= i128::MAX as u128 {
_burn(&e, from.clone(), amount as i128);
}else{
return Err(ContractError::InvalidAmount);
}
let xcall_message = CrossTransfer::new(from.clone().to_string(), to, amount, data);
let rollback = CrossTransferRevert::new(from.clone(), amount);
let config = get_config(&e);
Expand Down Expand Up @@ -87,13 +91,21 @@ pub fn _handle_call_message(
}
let message = CrossTransfer::decode(&e, data);
let to_network_address: Address = get_address(message.to, &e)?;
_mint(&e, to_network_address, message.amount as i128);
if message.amount <= i128::MAX as u128 {
_mint(&e, to_network_address, message.amount as i128);
}else{
return Err(ContractError::InvalidAmount);
}
} else if method == String::from_str(&e, &CROSS_TRANSFER_REVERT) {
if config.xcall_network_address != from {
return Err(ContractError::OnlyCallService);
}
let message = CrossTransferRevert::decode(&e, data);
_mint(&e, message.to, message.amount as i128);
if message.amount <= i128::MAX as u128 {
_mint(&e, message.to, message.amount as i128);
}else{
return Err(ContractError::InvalidAmount);
}
} else {
return Err(ContractError::UnknownMessageType);
}
Expand Down
1 change: 1 addition & 0 deletions contracts/balanced_doller/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pub enum ContractError {
InvalidAddress = 8,
InvalidNetworkAddressLength = 9,
InvalidNetworkAddress = 10,
InvalidAmount = 11
}
4 changes: 4 additions & 0 deletions contracts/xcall_manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ impl XcallManager {
}

pub fn white_list_actions(e: Env, action: Bytes) {
let admin = read_administrator(&e);
admin.require_auth();
let actions = WhiteListActions::new(DataKey::WhiteListedActions);
actions.add(&e, action);
}

pub fn remove_action(e: Env, action: Bytes) -> Result<bool, ContractError> {
let admin = read_administrator(&e);
admin.require_auth();
let actions = WhiteListActions::new(DataKey::WhiteListedActions);
if !actions.contains(&e, action.clone()) {
return Err(ContractError::NotWhiteListed);
Expand Down

0 comments on commit 4eaa04a

Please sign in to comment.