Skip to content

Commit

Permalink
fix: throw error for non-existing token from read_token_data (#18)
Browse files Browse the repository at this point in the history
fix: logic update for read_token_data

Co-authored-by: sagar sapkota <[email protected]>
  • Loading branch information
TRSSMG and sagars authored Nov 6, 2024
1 parent 5b1a288 commit a07bcd7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
15 changes: 7 additions & 8 deletions contracts/asset_manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ impl AssetManager {
}

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);
}
let data: TokenData = read_token_data(&env, token_address)?;

Ok((
data.period,
data.percentage,
Expand All @@ -108,13 +106,14 @@ impl AssetManager {
))
}

pub fn reset_limit(env: Env, token: Address) {
pub fn reset_limit(env: Env, token: Address) -> Result<bool, ContractError> {
let admin = read_administrator(&env);
admin.require_auth();
let balance = Self::get_token_balance(&env, token.clone());
let mut data: TokenData = read_token_data(&env, token.clone());
let mut data: TokenData = read_token_data(&env, token.clone())?;
data.current_limit = (balance * data.percentage as u128 / POINTS) as u64;
write_token_data(&env, token, data);
Ok(true)
}

pub fn get_withdraw_limit(env: Env, token: Address) -> Result<u128, ContractError> {
Expand All @@ -133,7 +132,7 @@ impl AssetManager {
if balance - amount < limit {
return Err(ContractError::ExceedsWithdrawLimit);
};
let mut data: TokenData = read_token_data(&env, token.clone());
let mut data: TokenData = read_token_data(&env, token.clone())?;
data.current_limit = limit as u64;
data.last_update = env.ledger().timestamp();
write_token_data(&env, token, data);
Expand All @@ -145,7 +144,7 @@ impl AssetManager {
balance: u128,
token: Address,
) -> Result<u128, ContractError> {
let data: TokenData = read_token_data(&env, token);
let data: TokenData = read_token_data(&env, token)?;
let period: u128 = data.period as u128;
let percentage: u128 = data.percentage as u128;
if period == 0 {
Expand Down
12 changes: 8 additions & 4 deletions contracts/asset_manager/src/states.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use soroban_sdk::{Address, Env, Vec};

use crate::storage_types::{DataKey, TokenData};
use crate::{errors::ContractError, storage_types::{DataKey, TokenData}};

pub(crate) const DAY_IN_LEDGERS: u32 = 17280;
pub(crate) const INSTANCE_BUMP_AMOUNT: u32 = 30 * DAY_IN_LEDGERS;
Expand Down Expand Up @@ -41,10 +41,14 @@ pub fn write_token_data(env: &Env, token_address: Address, data: TokenData) {
env.storage().persistent().set(&key, &data);
}

pub fn read_token_data(env: &Env, token_address: Address) -> TokenData {
let default = TokenData{percentage: 0, period: 0, last_update: 0, current_limit: 0 };
pub fn read_token_data(env: &Env, token_address: Address) -> Result<TokenData, ContractError> {
let key = DataKey::TokenData(token_address);
env.storage().persistent().get(&key).unwrap_or(default)
let token_data: TokenData = env
.storage()
.persistent()
.get(&key)
.ok_or(ContractError::TokenDoesNotExists)?;
Ok(token_data)
}

pub fn write_tokens(e: &Env, token: Address) {
Expand Down
2 changes: 1 addition & 1 deletion contracts/asset_manager/src/tests/asset_manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn test_handle_call_message_for_withdraw_to() {
let stellar_asset_client: token::StellarAssetClient =
token::StellarAssetClient::new(&ctx.env, &ctx.token);
stellar_asset_client.mint(&ctx.registry, &((bnusd_amount * 2) as i128));

client.configure_rate_limit(&ctx.token, &300, &300);
let data = WithdrawTo::new(
ctx.token.to_string(),
ctx.withdrawer.to_string(),
Expand Down

0 comments on commit a07bcd7

Please sign in to comment.