From 818f85f3f0e8185ebea234ee6152428ffc70cf3f Mon Sep 17 00:00:00 2001 From: HaiTranQuang Date: Tue, 18 Apr 2023 14:49:28 +0700 Subject: [PATCH 1/2] add lp_token_decimals to LPTokenInfo struct --- contracts/halo-factory/src/contract.rs | 1 + contracts/halo-factory/src/tests/test.rs | 14 ++++- contracts/halo-pair/src/contract.rs | 2 +- contracts/halo-pair/src/tests/test.rs | 7 ++- .../halo-router/src/tests/integration_test.rs | 63 +++++++++++++++++-- packages/haloswap/src/asset.rs | 1 + 6 files changed, 79 insertions(+), 9 deletions(-) diff --git a/contracts/halo-factory/src/contract.rs b/contracts/halo-factory/src/contract.rs index 85cf1d3..cdf6290 100644 --- a/contracts/halo-factory/src/contract.rs +++ b/contracts/halo-factory/src/contract.rs @@ -186,6 +186,7 @@ pub fn execute_create_pair( lp_token_info: LPTokenInfo { lp_token_name: lp_token_info.lp_token_name, lp_token_symbol: lp_token_info.lp_token_symbol, + lp_token_decimals: lp_token_info.lp_token_decimals, }, })?, }), diff --git a/contracts/halo-factory/src/tests/test.rs b/contracts/halo-factory/src/tests/test.rs index db7be72..7eb76eb 100644 --- a/contracts/halo-factory/src/tests/test.rs +++ b/contracts/halo-factory/src/tests/test.rs @@ -159,6 +159,7 @@ fn create_pair() { lp_token_info: LPTokenInfo { lp_token_name: "uusd_mAAPL_LP".to_string(), lp_token_symbol: "uusd_mAAPL_LP".to_string(), + lp_token_decimals: None, }, }; @@ -191,7 +192,8 @@ fn create_pair() { commission_rate: Decimal256::from_str("0.003").unwrap(), lp_token_info: LPTokenInfo { lp_token_name: "uusd_mAAPL_LP".to_string(), - lp_token_symbol: "uusd_mAAPL_LP".to_string() + lp_token_symbol: "uusd_mAAPL_LP".to_string(), + lp_token_decimals: None, }, }) .unwrap(), @@ -239,6 +241,7 @@ fn create_pair() { lp_token_info: LPTokenInfo { lp_token_name: "uusd_mAAPL_LP".to_string(), lp_token_symbol: "uusd_mAAPL_LP".to_string(), + lp_token_decimals: None, }, }; @@ -283,6 +286,7 @@ fn create_pair_native_token_and_ibc_token() { lp_token_info: LPTokenInfo { lp_token_name: "uusd_ibc/HASH_LP".to_string(), lp_token_symbol: "uusd_ibc/HASH_LP".to_string(), + lp_token_decimals: None, }, }; @@ -312,7 +316,8 @@ fn create_pair_native_token_and_ibc_token() { commission_rate: Decimal256::from_str("0.003").unwrap(), lp_token_info: LPTokenInfo { lp_token_name: "uusd_ibc/HASH_LP".to_string(), - lp_token_symbol: "uusd_ibc/HASH_LP".to_string() + lp_token_symbol: "uusd_ibc/HASH_LP".to_string(), + lp_token_decimals: None, }, }) .unwrap(), @@ -367,6 +372,7 @@ fn fail_to_create_same_pair() { lp_token_info: LPTokenInfo { lp_token_name: "uusd_uusd_LP".to_string(), lp_token_symbol: "uusd_uusd_LP".to_string(), + lp_token_decimals: None, }, }; @@ -402,6 +408,7 @@ fn fail_to_create_pair_with_unactive_denoms() { lp_token_info: LPTokenInfo { lp_token_name: "uusd_uxxx_LP".to_string(), lp_token_symbol: "uusd_uxxx_LP".to_string(), + lp_token_decimals: None, }, }; @@ -437,6 +444,7 @@ fn fail_to_create_pair_with_invalid_denom() { lp_token_info: LPTokenInfo { lp_token_name: "uluna_xxx_LP".to_string(), lp_token_symbol: "uluna_xxx_LP".to_string(), + lp_token_decimals: None, }, }; @@ -482,6 +490,7 @@ fn fail_to_create_pair_with_unknown_token() { lp_token_info: LPTokenInfo { lp_token_name: "uluna_xxx_LP".to_string(), lp_token_symbol: "uluna_xxx_LP".to_string(), + lp_token_decimals: None, }, }; @@ -527,6 +536,7 @@ fn fail_to_create_pair_with_unknown_ibc_token() { lp_token_info: LPTokenInfo { lp_token_name: "uluna_ibc/HA_LP".to_string(), lp_token_symbol: "uluna_ibc/HA_LP".to_string(), + lp_token_decimals: None, }, }; diff --git a/contracts/halo-pair/src/contract.rs b/contracts/halo-pair/src/contract.rs index 6479bf6..2ef8f58 100644 --- a/contracts/halo-pair/src/contract.rs +++ b/contracts/halo-pair/src/contract.rs @@ -58,7 +58,7 @@ pub fn instantiate( msg: to_binary(&TokenInstantiateMsg { name: msg.lp_token_info.clone().lp_token_name, symbol: msg.lp_token_info.lp_token_symbol, - decimals: 6, + decimals: msg.lp_token_info.lp_token_decimals.unwrap_or(6), initial_balances: vec![], mint: Some(MinterResponse { minter: env.contract.address.to_string(), diff --git a/contracts/halo-pair/src/tests/test.rs b/contracts/halo-pair/src/tests/test.rs index 2e37237..394cc76 100644 --- a/contracts/halo-pair/src/tests/test.rs +++ b/contracts/halo-pair/src/tests/test.rs @@ -42,6 +42,7 @@ fn proper_initialization() { lp_token_info: LPTokenInfo { lp_token_name: "uusd_asset0000_lp".to_string(), lp_token_symbol: "uusd_asset0000_lp".to_string(), + lp_token_decimals: Some(18), }, }; @@ -57,7 +58,7 @@ fn proper_initialization() { msg: to_binary(&TokenInstantiateMsg { name: "uusd_asset0000_lp".to_string(), symbol: "uusd_asset0000_lp".to_string(), - decimals: 6, + decimals: 18, initial_balances: vec![], mint: Some(MinterResponse { minter: MOCK_CONTRACT_ADDR.to_string(), @@ -249,6 +250,7 @@ fn provide_liquidity() { lp_token_info: LPTokenInfo { lp_token_name: "uusd_asset0000_LP".to_string(), lp_token_symbol: "uusd_asset0000_LP".to_string(), + lp_token_decimals: None, }, }; @@ -739,6 +741,7 @@ fn withdraw_liquidity() { lp_token_info: LPTokenInfo { lp_token_name: "uusd_asset0000_LP".to_string(), lp_token_symbol: "uusd_asset0000_LP".to_string(), + lp_token_decimals: None, }, }; @@ -1066,6 +1069,7 @@ fn try_token_to_native() { lp_token_info: LPTokenInfo { lp_token_name: "uusd_asset0000_LP".to_string(), lp_token_symbol: "uusd_asset0000_LP".to_string(), + lp_token_decimals: None, }, }; @@ -1508,6 +1512,7 @@ fn test_query_pool() { lp_token_info: LPTokenInfo { lp_token_name: "uusd_asset0000_LP".to_string(), lp_token_symbol: "uusd_asset0000_LP".to_string(), + lp_token_decimals: None, }, }; diff --git a/contracts/halo-router/src/tests/integration_test.rs b/contracts/halo-router/src/tests/integration_test.rs index 2f43cc4..f8293ed 100644 --- a/contracts/halo-router/src/tests/integration_test.rs +++ b/contracts/halo-router/src/tests/integration_test.rs @@ -12,7 +12,6 @@ mod tests { use haloswap::factory::{ ExecuteMsg as FactoryExecuteMsg, NativeTokenDecimalsResponse, QueryMsg as FactoryQueryMsg, }; - use haloswap::pair::Cw20HookMsg; // Mock information for CW20 token contract const MOCK_1000_HALO_TOKEN_AMOUNT: u128 = 1_000_000_000; @@ -33,7 +32,10 @@ mod tests { use cw_multi_test::Executor; use haloswap::{ asset::{Asset, LPTokenInfo, LP_TOKEN_RESERVED_AMOUNT}, - pair::{ExecuteMsg, PoolResponse, QueryMsg}, + pair::{ + ExecuteMsg, PoolResponse, QueryMsg as RouterQueryMsg, ReverseSimulationResponse, + SimulationResponse, + }, router::{ExecuteMsg as RouterExecuteMsg, SwapOperation}, }; @@ -174,6 +176,7 @@ mod tests { lp_token_info: LPTokenInfo { lp_token_name: "aura-HALO".to_string(), lp_token_symbol: "aura-HALO".to_string(), + lp_token_decimals: None, }, }; @@ -306,7 +309,7 @@ mod tests { // Query Pool let response: PoolResponse = app .wrap() - .query_wasm_smart("contract3".to_string(), &QueryMsg::Pool {}) + .query_wasm_smart("contract3".to_string(), &RouterQueryMsg::Pool {}) .unwrap(); // Assert Pool: total_share amount @@ -370,6 +373,56 @@ mod tests { } ); + // Query SimulateSwapOperations for native token token to cw20 + let msg = RouterQueryMsg::Simulation { + offer_asset: Asset { + info: AssetInfo::NativeToken { + denom: NATIVE_DENOM_2.to_string(), + }, + amount: Uint128::from(1000u128), + }, + }; + + let response: SimulationResponse = app + .wrap() + .query_wasm_smart("contract3".to_string(), &msg) + .unwrap(); + + // Assert SimulateSwapOperations for native token token to cw20 + assert_eq!( + response, + SimulationResponse { + return_amount: Uint128::from(485u128), + spread_amount: Uint128::from(1u128), + commission_amount: Uint128::from(14u128), + } + ); + + // Query ReverseSimulation for cw20 token to native token + let msg = RouterQueryMsg::ReverseSimulation { + ask_asset: Asset { + info: AssetInfo::Token { + contract_addr: cw20_token_contract.clone(), + }, + amount: Uint128::from(485u128), + }, + }; + + let response: ReverseSimulationResponse = app + .wrap() + .query_wasm_smart("contract3".to_string(), &msg) + .unwrap(); + + // Assert ReverseSimulation for cw20 token to native token + assert_eq!( + response, + ReverseSimulationResponse { + offer_amount: Uint128::from(998u128), + spread_amount: Uint128::from(0u128), + commission_amount: Uint128::from(14u128), + } + ); + // Swap native token to cw20 token let msg = RouterExecuteMsg::ExecuteSwapOperations { operations: vec![SwapOperation::HaloSwap { @@ -399,7 +452,7 @@ mod tests { // Query Pool let response: PoolResponse = app .wrap() - .query_wasm_smart("contract3".to_string(), &QueryMsg::Pool {}) + .query_wasm_smart("contract3".to_string(), &RouterQueryMsg::Pool {}) .unwrap(); // Assert Pool: total_share amount @@ -484,7 +537,7 @@ mod tests { // Query Pool let response: PoolResponse = app .wrap() - .query_wasm_smart("contract3".to_string(), &QueryMsg::Pool {}) + .query_wasm_smart("contract3".to_string(), &RouterQueryMsg::Pool {}) .unwrap(); // Assert Pool: total_share amount diff --git a/packages/haloswap/src/asset.rs b/packages/haloswap/src/asset.rs index d07e2b7..78a3267 100644 --- a/packages/haloswap/src/asset.rs +++ b/packages/haloswap/src/asset.rs @@ -320,4 +320,5 @@ impl PairInfoRaw { pub struct LPTokenInfo { pub lp_token_name: String, pub lp_token_symbol: String, + pub lp_token_decimals: Option, } From 00d6144bd828fcb94f4c470b31c1893e8bae2ce9 Mon Sep 17 00:00:00 2001 From: HaiTranQuang Date: Tue, 18 Apr 2023 15:23:40 +0700 Subject: [PATCH 2/2] Remove redundant test --- .../halo-router/src/tests/integration_test.rs | 61 ++----------------- 1 file changed, 4 insertions(+), 57 deletions(-) diff --git a/contracts/halo-router/src/tests/integration_test.rs b/contracts/halo-router/src/tests/integration_test.rs index f8293ed..5a95a99 100644 --- a/contracts/halo-router/src/tests/integration_test.rs +++ b/contracts/halo-router/src/tests/integration_test.rs @@ -32,10 +32,7 @@ mod tests { use cw_multi_test::Executor; use haloswap::{ asset::{Asset, LPTokenInfo, LP_TOKEN_RESERVED_AMOUNT}, - pair::{ - ExecuteMsg, PoolResponse, QueryMsg as RouterQueryMsg, ReverseSimulationResponse, - SimulationResponse, - }, + pair::{ExecuteMsg, PoolResponse, QueryMsg}, router::{ExecuteMsg as RouterExecuteMsg, SwapOperation}, }; @@ -309,7 +306,7 @@ mod tests { // Query Pool let response: PoolResponse = app .wrap() - .query_wasm_smart("contract3".to_string(), &RouterQueryMsg::Pool {}) + .query_wasm_smart("contract3".to_string(), &QueryMsg::Pool {}) .unwrap(); // Assert Pool: total_share amount @@ -373,56 +370,6 @@ mod tests { } ); - // Query SimulateSwapOperations for native token token to cw20 - let msg = RouterQueryMsg::Simulation { - offer_asset: Asset { - info: AssetInfo::NativeToken { - denom: NATIVE_DENOM_2.to_string(), - }, - amount: Uint128::from(1000u128), - }, - }; - - let response: SimulationResponse = app - .wrap() - .query_wasm_smart("contract3".to_string(), &msg) - .unwrap(); - - // Assert SimulateSwapOperations for native token token to cw20 - assert_eq!( - response, - SimulationResponse { - return_amount: Uint128::from(485u128), - spread_amount: Uint128::from(1u128), - commission_amount: Uint128::from(14u128), - } - ); - - // Query ReverseSimulation for cw20 token to native token - let msg = RouterQueryMsg::ReverseSimulation { - ask_asset: Asset { - info: AssetInfo::Token { - contract_addr: cw20_token_contract.clone(), - }, - amount: Uint128::from(485u128), - }, - }; - - let response: ReverseSimulationResponse = app - .wrap() - .query_wasm_smart("contract3".to_string(), &msg) - .unwrap(); - - // Assert ReverseSimulation for cw20 token to native token - assert_eq!( - response, - ReverseSimulationResponse { - offer_amount: Uint128::from(998u128), - spread_amount: Uint128::from(0u128), - commission_amount: Uint128::from(14u128), - } - ); - // Swap native token to cw20 token let msg = RouterExecuteMsg::ExecuteSwapOperations { operations: vec![SwapOperation::HaloSwap { @@ -452,7 +399,7 @@ mod tests { // Query Pool let response: PoolResponse = app .wrap() - .query_wasm_smart("contract3".to_string(), &RouterQueryMsg::Pool {}) + .query_wasm_smart("contract3".to_string(), &QueryMsg::Pool {}) .unwrap(); // Assert Pool: total_share amount @@ -537,7 +484,7 @@ mod tests { // Query Pool let response: PoolResponse = app .wrap() - .query_wasm_smart("contract3".to_string(), &RouterQueryMsg::Pool {}) + .query_wasm_smart("contract3".to_string(), &QueryMsg::Pool {}) .unwrap(); // Assert Pool: total_share amount