From c933a1c0a6c331f7eb7b5eefbe7c7caffa61651a Mon Sep 17 00:00:00 2001 From: kent-3 <100624004+kent-3@users.noreply.github.com> Date: Fri, 24 Nov 2023 08:43:21 -0500 Subject: [PATCH] use swap::core for TokenAmount and TokenType --- .../liquidity_book/lb_pair/src/contract.rs | 2 +- .../liquidity_book/lb_pair.rs | 9 +- .../src/utils/liquidity_book/mod.rs | 1 - .../src/utils/liquidity_book/tokens.rs | 326 ------------------ 4 files changed, 4 insertions(+), 334 deletions(-) delete mode 100644 packages/shade_protocol/src/utils/liquidity_book/tokens.rs diff --git a/contracts/liquidity_book/lb_pair/src/contract.rs b/contracts/liquidity_book/lb_pair/src/contract.rs index f03a69d2..f79eef6b 100644 --- a/contracts/liquidity_book/lb_pair/src/contract.rs +++ b/contracts/liquidity_book/lb_pair/src/contract.rs @@ -1551,7 +1551,7 @@ fn query_pair_info(deps: Deps) -> Result { fn query_swap_simulation( deps: Deps, env: Env, - offer: shade_protocol::lb_libraries::tokens::TokenAmount, + offer: shade_protocol::swap::core::TokenAmount, exclude_fee: Option, ) -> Result { let state = CONFIG.load(deps.storage)?; diff --git a/packages/shade_protocol/src/contract_interfaces/liquidity_book/lb_pair.rs b/packages/shade_protocol/src/contract_interfaces/liquidity_book/lb_pair.rs index fff2da74..e226a8f6 100644 --- a/packages/shade_protocol/src/contract_interfaces/liquidity_book/lb_pair.rs +++ b/packages/shade_protocol/src/contract_interfaces/liquidity_book/lb_pair.rs @@ -2,13 +2,10 @@ use crate::{ c_std::{Addr, ContractInfo, Decimal256, Uint128, Uint256}, cosmwasm_schema::{cw_serde, QueryResponses}, snip20::Snip20ReceiveMsg, - swap::core::TokenType, + swap::core::{TokenType, TokenAmount}, utils::{ asset::RawContract, - liquidity_book::{ - tokens::{SwapTokenAmount, TokenAmount}, - types::{Bytes32, ContractInstantiationInfo, StaticFeeParameters}, - }, + liquidity_book::types::{Bytes32, ContractInstantiationInfo, StaticFeeParameters}, ExecuteCallback, InstantiateCallback, Query, @@ -38,7 +35,7 @@ impl InstantiateCallback for InstantiateMsg { #[cw_serde] pub enum ExecuteMsg { SwapTokens { - offer: SwapTokenAmount, + offer: TokenAmount, expected_return: Option, to: Option, padding: Option, diff --git a/packages/shade_protocol/src/utils/liquidity_book/mod.rs b/packages/shade_protocol/src/utils/liquidity_book/mod.rs index b1f63dab..89281f38 100644 --- a/packages/shade_protocol/src/utils/liquidity_book/mod.rs +++ b/packages/shade_protocol/src/utils/liquidity_book/mod.rs @@ -9,7 +9,6 @@ pub mod math; pub mod oracle_helper; pub mod pair_parameter_helper; pub mod price_helper; -pub mod tokens; pub mod transfer; pub mod types; pub mod viewing_keys; diff --git a/packages/shade_protocol/src/utils/liquidity_book/tokens.rs b/packages/shade_protocol/src/utils/liquidity_book/tokens.rs deleted file mode 100644 index 2f02b419..00000000 --- a/packages/shade_protocol/src/utils/liquidity_book/tokens.rs +++ /dev/null @@ -1,326 +0,0 @@ -//! ### Liquidity Book token Helper Library -//! Author: Haseeb -//! - -use crate::{ - snip20::helpers::token_info, - utils::liquidity_book::transfer::{self, HandleMsg, QueryAnswer, QueryMsg}, - Contract, -}; - -use std::fmt::{Display, Formatter, Result}; - -use cosmwasm_schema::cw_serde; -use cosmwasm_std::{ - to_binary, - Addr, - BankMsg, - Coin, - ContractInfo, - CosmosMsg, - Deps, - MessageInfo, - QuerierWrapper, - QueryRequest, - StdError, - StdResult, - Uint128, - WasmMsg, - WasmQuery, -}; - -#[cw_serde] -pub struct SwapTokenAmount { - pub token: TokenType, - pub amount: Uint128, -} - -// #[cw_serde] -// pub enum TokenType { -// CustomToken { -// contract_addr: Addr, -// token_code_hash: String, -// }, -// NativeToken { -// denom: String, -// }, -// } - -pub use crate::swap::core::TokenType; - -// impl TokenType { -// pub fn query_decimals(&self, deps: &Deps) -> StdResult { -// match self { -// TokenType::CustomToken { -// contract_addr, -// token_code_hash, -// .. -// } => Ok(token_info( -// &deps.querier, -// &Contract { -// address: contract_addr.clone(), -// code_hash: token_code_hash.clone(), -// }, -// )? -// .decimals), -// TokenType::NativeToken { denom } => match denom.as_str() { -// "uscrt" => Ok(6), -// _ => Err(StdError::generic_err( -// "Cannot retrieve decimals for native token", -// )), -// }, -// } -// } -// -// pub fn is_native_token(&self) -> bool { -// match self { -// TokenType::NativeToken { .. } => true, -// TokenType::CustomToken { .. } => false, -// } -// } -// -// pub fn unique_key(&self) -> String { -// match self { -// TokenType::NativeToken { denom } => denom.to_string(), -// TokenType::CustomToken { -// contract_addr, -// token_code_hash: _, -// } => contract_addr.to_string(), -// } -// } -// -// pub fn address(&self) -> Addr { -// match self { -// TokenType::NativeToken { .. } => panic!("Doesn't work for native tokens"), -// TokenType::CustomToken { -// contract_addr, -// token_code_hash: _, -// } => contract_addr.clone(), -// } -// } -// -// pub fn code_hash(&self) -> String { -// match self { -// TokenType::NativeToken { .. } => panic!("Doesn't work for native tokens"), -// TokenType::CustomToken { -// contract_addr: _, -// token_code_hash, -// } => token_code_hash.to_string(), -// } -// } -// -// pub fn is_custom_token(&self) -> bool { -// match self { -// TokenType::NativeToken { .. } => false, -// TokenType::CustomToken { .. } => true, -// } -// } -// -// pub fn assert_sent_native_token_balance( -// &self, -// info: &MessageInfo, -// amount: Uint128, -// ) -> StdResult<()> { -// if let TokenType::NativeToken { denom } = &self { -// return match info.funds.iter().find(|x| x.denom == *denom) { -// Some(coin) => { -// if amount == coin.amount { -// Ok(()) -// } else { -// Err(StdError::generic_err( -// "Native token balance mismatch between the argument and the transferred", -// )) -// } -// } -// None => { -// if amount.is_zero() { -// Ok(()) -// } else { -// Err(StdError::generic_err( -// "Native token balance mismatch between the argument and the transferred", -// )) -// } -// } -// }; -// } -// -// Ok(()) -// } -// -// pub fn transfer(&self, amount: Uint128, recipient: Addr) -> Option { -// if amount.gt(&Uint128::zero()) { -// match &self { -// TokenType::CustomToken { -// contract_addr, -// token_code_hash, -// } => { -// let msg = HandleMsg::Send { -// recipient: recipient.to_string(), -// amount, -// padding: None, -// msg: None, -// recipient_code_hash: None, -// memo: None, -// }; -// let cosmos_msg = msg -// .to_cosmos_msg(token_code_hash.to_string(), contract_addr.to_string(), None) -// .unwrap(); -// -// Some(cosmos_msg) -// } -// -// TokenType::NativeToken { denom } => Some(CosmosMsg::Bank(BankMsg::Send { -// to_address: recipient.to_string(), -// amount: vec![Coin { -// denom: denom.clone(), -// amount, -// }], -// })), -// } -// } else { -// None -// } -// } -// -// pub fn transfer_from( -// &self, -// amount: Uint128, -// owner: Addr, -// recipient: Addr, -// ) -> Option { -// if amount.gt(&Uint128::zero()) { -// match &self { -// TokenType::CustomToken { -// contract_addr, -// token_code_hash, -// } => { -// let msg = HandleMsg::TransferFrom { -// owner: owner.to_string(), -// recipient: recipient.to_string(), -// amount, -// padding: None, -// memo: None, -// }; -// -// let cosmos_msg = msg -// .to_cosmos_msg(token_code_hash.to_string(), contract_addr.to_string(), None) -// .unwrap(); -// -// Some(cosmos_msg) -// } -// -// TokenType::NativeToken { denom } => Some(CosmosMsg::Bank(BankMsg::Send { -// to_address: recipient.to_string(), -// amount: vec![Coin { -// denom: denom.clone(), -// amount, -// }], -// })), -// } -// } else { -// None -// } -// } -// } -// -// impl TokenType { -// pub fn query_balance( -// &self, -// deps: Deps, -// exchange_addr: String, -// viewing_key: String, -// ) -> StdResult { -// match self { -// TokenType::NativeToken { denom } => { -// let result = deps.querier.query_balance(exchange_addr, denom)?; -// Ok(result.amount) -// } -// TokenType::CustomToken { -// contract_addr, -// token_code_hash, -// } => balance_query( -// &deps.querier, -// deps.api.addr_validate(&exchange_addr)?, -// viewing_key, -// &ContractInfo { -// address: contract_addr.clone(), -// code_hash: token_code_hash.clone(), -// }, -// ), -// } -// } -// -// pub fn create_send_msg(&self, recipient: String, amount: Uint128) -> StdResult { -// let msg = match self { -// TokenType::CustomToken { -// contract_addr, -// token_code_hash, -// } => CosmosMsg::Wasm(WasmMsg::Execute { -// contract_addr: contract_addr.clone().into_string(), -// code_hash: token_code_hash.to_string(), -// msg: to_binary(&transfer::HandleMsg::Send { -// recipient, -// amount, -// padding: None, -// msg: None, -// recipient_code_hash: None, -// memo: None, -// })?, -// funds: vec![], -// }), -// TokenType::NativeToken { denom } => CosmosMsg::Bank(BankMsg::Send { -// to_address: recipient, -// amount: vec![Coin { -// denom: denom.clone(), -// amount, -// }], -// }), -// }; -// Ok(msg) -// } -// } -// -// /// Returns a StdResult from performing a Balance query -// pub fn balance_query( -// querier: &QuerierWrapper, -// address: Addr, -// key: String, -// contract: &ContractInfo, -// ) -> StdResult { -// let msg: QueryMsg = QueryMsg::Balance { -// address: address.to_string(), -// key, -// }; -// -// let result: crate::utils::liquidity_book::transfer::QueryAnswer = -// querier.query(&QueryRequest::Wasm(WasmQuery::Smart { -// contract_addr: contract.address.to_string(), -// code_hash: contract.code_hash.clone(), -// msg: to_binary(&msg)?, -// }))?; -// -// let QueryAnswer::Balance { amount, .. } = result; -// Ok(amount) -// } -// -// impl Display for TokenType { -// fn fmt(&self, f: &mut Formatter) -> Result { -// match self { -// TokenType::NativeToken { denom, .. } => write!(f, "{}", denom), -// TokenType::CustomToken { contract_addr, .. } => write!(f, "{}", contract_addr), -// } -// } -// } - -#[cw_serde] -pub struct TokenAmount { - pub token: TokenType, - pub amount: Uint128, -} - -impl TokenAmount { - pub fn assert_sent_native_token_balance(&self, info: &MessageInfo) -> StdResult<()> { - self.token - .assert_sent_native_token_balance(info, self.amount) - } -}