Skip to content

Commit

Permalink
rate limit default value set
Browse files Browse the repository at this point in the history
  • Loading branch information
sagars committed Sep 18, 2024
1 parent cccb4e0 commit a74b7b7
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 7 deletions.
8 changes: 4 additions & 4 deletions contracts/asset_manager/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl AssetManager {
}

pub fn get_rate_limit(env: Env, token_address: Address) -> (u64, u32, u64, u64) {
let data: TokenData = read_token_data(&env, token_address).unwrap();
let data: TokenData = read_token_data(&env, token_address);
(
data.period,
data.percentage,
Expand All @@ -97,7 +97,7 @@ impl AssetManager {

pub fn reset_limit(env: Env, token: Address) {
let balance = Self::get_token_balance(&env, token.clone());
let mut data: TokenData = read_token_data(&env, token.clone()).unwrap();
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);
}

Check warning on line 103 in contracts/asset_manager/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/asset_manager/src/contract.rs#L98-L103

Added lines #L98 - L103 were not covered by tests
Expand All @@ -119,7 +119,7 @@ impl AssetManager {
if balance - amount < limit {
panic_with_error!(&env, ContractError::ExceedsWithdrawLimit);
};
let mut data: TokenData = read_token_data(&env, token.clone()).unwrap();
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 @@ -131,7 +131,7 @@ impl AssetManager {
balance: u128,
token: Address,
) -> Result<u128, ContractError> {
let data: TokenData = read_token_data(&env, token).unwrap();
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
5 changes: 3 additions & 2 deletions contracts/asset_manager/src/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ 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) -> Option<TokenData> {
pub fn read_token_data(env: &Env, token_address: Address) -> TokenData {
let default = TokenData{percentage: 0, period: 0, last_update: 0, current_limit: 0 };
let key = DataKey::TokenData(token_address);
env.storage().persistent().get(&key)
env.storage().persistent().get(&key).unwrap_or(default)
}

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 @@ -288,7 +288,7 @@ fn test_handle_call_message_for_withdraw_to() {
ctx.env.mock_all_auths();

ctx.init_context(&client);
client.configure_rate_limit(&ctx.token, &300, &300);
//client.configure_rate_limit(&ctx.token, &300, &300);

let bnusd_amount = 100000u128;
let token_client = token::Client::new(&ctx.env, &ctx.token);
Expand Down
219 changes: 219 additions & 0 deletions contracts/balanced_doller/src/tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
// #![cfg(test)]
// extern crate std;

// use crate::contract::BalancedDollarClient;


// use soroban_sdk::{
// symbol_short,
// testutils::{Address as _, AuthorizedFunction, AuthorizedInvocation},
// Address, IntoVal, Symbol,
// };

// use super::setup::*;

// #[test]
// fn test() {
// let ctx = TestContext::default();
// let client = BalancedDollarClient::new(&ctx.env, &ctx.registry);
// let e = &ctx.env;
// e.mock_all_auths();
// ctx.init_context(&client);


// let admin2 = Address::generate(&e);
// let user1 = Address::generate(&e);
// let user2 = Address::generate(&e);
// let user3 = Address::generate(&e);

// client.mint(&ctx.admin, &1000);
// assert_eq!(
// e.auths(),
// std::vec![(
// ctx.admin.clone(),
// AuthorizedInvocation {
// function: AuthorizedFunction::Contract((
// client.address.clone(),
// symbol_short!("mint"),
// (&ctx.admin, 1000_i128).into_val(e),
// )),
// sub_invocations: std::vec![]
// }
// )]
// );
// assert_eq!(client.balance(&ctx.admin), 1000);

// client.approve(&ctx.admin, &user3, &500, &200);
// assert_eq!(
// e.auths(),
// std::vec![(
// ctx.admin.clone(),
// AuthorizedInvocation {
// function: AuthorizedFunction::Contract((
// client.address.clone(),
// symbol_short!("approve"),
// (&ctx.admin, &user3, 500_i128, 200_u32).into_val(e),
// )),
// sub_invocations: std::vec![]
// }
// )]
// );
// assert_eq!(client.allowance(&ctx.admin, &user3), 500);

// client.transfer(&ctx.admin, &user2, &600);
// assert_eq!(
// e.auths(),
// std::vec![(
// ctx.admin.clone(),
// AuthorizedInvocation {
// function: AuthorizedFunction::Contract((
// client.address.clone(),
// symbol_short!("transfer"),
// (&ctx.admin, &user2, 600_i128).into_val(e),
// )),
// sub_invocations: std::vec![]
// }
// )]
// );
// assert_eq!(client.balance(&ctx.admin), 400);
// assert_eq!(client.balance(&user2), 600);

// client.transfer_from(&user3, &ctx.admin, &user1, &400);
// assert_eq!(
// e.auths(),
// std::vec![(
// user3.clone(),
// AuthorizedInvocation {
// function: AuthorizedFunction::Contract((
// client.address.clone(),
// Symbol::new(&e, "transfer_from"),
// (&user3, &ctx.admin, &user1, 400_i128).into_val(e),
// )),
// sub_invocations: std::vec![]
// }
// )]
// );
// assert_eq!(client.balance(&user1), 400);
// assert_eq!(client.balance(&user2), 600);

// client.transfer(&user1, &user3, &300);
// assert_eq!(client.balance(&user1), 100);
// assert_eq!(client.balance(&user3), 300);

// client.set_admin(&admin2);
// assert_eq!(
// e.auths(),
// std::vec![(
// ctx.admin.clone(),
// AuthorizedInvocation {
// function: AuthorizedFunction::Contract((
// client.address.clone(),
// symbol_short!("set_admin"),
// (&admin2,).into_val(e),
// )),
// sub_invocations: std::vec![]
// }
// )]
// );

// // Increase to 500
// client.approve(&user2, &user3, &500, &200);
// assert_eq!(client.allowance(&user2, &user3), 500);
// client.approve(&user2, &user3, &0, &200);
// assert_eq!(
// e.auths(),
// std::vec![(
// user2.clone(),
// AuthorizedInvocation {
// function: AuthorizedFunction::Contract((
// client.address.clone(),
// symbol_short!("approve"),
// (&user2, &user3, 0_i128, 200_u32).into_val(e),
// )),
// sub_invocations: std::vec![]
// }
// )]
// );
// assert_eq!(client.allowance(&user2, &user3), 0);
// }

// #[test]
// fn test_burn() {
// let ctx = TestContext::default();
// let client = BalancedDollarClient::new(&ctx.env, &ctx.registry);
// let e = &ctx.env;
// e.mock_all_auths();
// ctx.init_context(&client);


// let user2 = Address::generate(&e);

// client.mint(&ctx.admin, &1000);
// assert_eq!(client.balance(&ctx.admin), 1000);

// client.approve(&ctx.admin, &user2, &500, &200);
// assert_eq!(client.allowance(&ctx.admin, &user2), 500);

// client.burn_from(&user2, &ctx.admin, &500);
// assert_eq!(
// e.auths(),
// std::vec![(
// user2.clone(),
// AuthorizedInvocation {
// function: AuthorizedFunction::Contract((
// client.address.clone(),
// symbol_short!("burn_from"),
// (&user2, &ctx.admin, 500_i128).into_val(e),
// )),
// sub_invocations: std::vec![]
// }
// )]
// );

// assert_eq!(client.allowance(&ctx.admin, &user2), 0);
// assert_eq!(client.balance(&ctx.admin), 500);
// assert_eq!(client.balance(&user2), 0);

// client.burn(&ctx.admin, &500);

// assert_eq!(client.balance(&ctx.admin), 0);
// assert_eq!(client.balance(&user2), 0);
// }

// #[test]
// #[should_panic(expected = "insufficient balance")]
// fn transfer_insufficient_balance() {
// let ctx = TestContext::default();
// let client = BalancedDollarClient::new(&ctx.env, &ctx.registry);
// let e = &ctx.env;
// e.mock_all_auths();
// ctx.init_context(&client);
// e.mock_all_auths();
// let user2 = Address::generate(&e);

// client.mint(&ctx.admin, &1000);
// assert_eq!(client.balance(&ctx.admin), 1000);

// client.transfer(&ctx.admin, &user2, &1001);
// }

// #[test]
// #[should_panic(expected = "insufficient allowance")]
// fn transfer_from_insufficient_allowance() {
// let ctx = TestContext::default();
// let client = BalancedDollarClient::new(&ctx.env, &ctx.registry);
// let e = &ctx.env;
// e.mock_all_auths();
// ctx.init_context(&client);
// let user2 = Address::generate(&e);
// let user3 = Address::generate(&e);

// client.mint(&ctx.admin, &1000);
// assert_eq!(client.balance(&ctx.admin), 1000);

// client.approve(&ctx.admin, &user3, &100, &200);
// assert_eq!(client.allowance(&ctx.admin, &user3), 100);

// client.transfer_from(&user3, &ctx.admin, &user2, &101);
// }

0 comments on commit a74b7b7

Please sign in to comment.