From a74b7b7e259b90b546c21318ca60ef67e394e585 Mon Sep 17 00:00:00 2001 From: sagar sapkota Date: Wed, 18 Sep 2024 18:05:41 +0545 Subject: [PATCH] rate limit default value set --- contracts/asset_manager/src/contract.rs | 8 +- contracts/asset_manager/src/states.rs | 5 +- .../src/tests/asset_manager_test.rs | 2 +- contracts/balanced_doller/src/tests/test.rs | 219 ++++++++++++++++++ 4 files changed, 227 insertions(+), 7 deletions(-) create mode 100644 contracts/balanced_doller/src/tests/test.rs diff --git a/contracts/asset_manager/src/contract.rs b/contracts/asset_manager/src/contract.rs index d83ca1d..7be1a75 100644 --- a/contracts/asset_manager/src/contract.rs +++ b/contracts/asset_manager/src/contract.rs @@ -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, @@ -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); } @@ -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); @@ -131,7 +131,7 @@ impl AssetManager { balance: u128, token: Address, ) -> Result { - 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 { diff --git a/contracts/asset_manager/src/states.rs b/contracts/asset_manager/src/states.rs index 7bf1a60..e6fbd80 100644 --- a/contracts/asset_manager/src/states.rs +++ b/contracts/asset_manager/src/states.rs @@ -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 { +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) { diff --git a/contracts/asset_manager/src/tests/asset_manager_test.rs b/contracts/asset_manager/src/tests/asset_manager_test.rs index afb2654..e4d6fd8 100644 --- a/contracts/asset_manager/src/tests/asset_manager_test.rs +++ b/contracts/asset_manager/src/tests/asset_manager_test.rs @@ -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); diff --git a/contracts/balanced_doller/src/tests/test.rs b/contracts/balanced_doller/src/tests/test.rs new file mode 100644 index 0000000..264425a --- /dev/null +++ b/contracts/balanced_doller/src/tests/test.rs @@ -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); +// } +