Skip to content

Commit

Permalink
Dex: Use custom queries CoreumQueries from coreum-wasm-sdk to allow q…
Browse files Browse the repository at this point in the history
…uerying smart token balances
  • Loading branch information
ueco-jb committed Oct 5, 2023
1 parent 9d95439 commit 48ca1a4
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository = "https://github.com/WHELP-project/whelp-contracts"
[workspace.dependencies]
anyhow = "1"
cw20-base = { version = "1.1", package = "cw20-base", features = ["library"] }
coreum-wasm-sdk = "0.1.1"
cosmwasm-schema = "1.4"
cosmwasm-std = "1.4"
cw2 = "1.1"
Expand Down
1 change: 1 addition & 0 deletions contracts/pair/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
coreum-wasm-sdk = { workspace = true }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw2 = { workspace = true }
Expand Down
9 changes: 6 additions & 3 deletions contracts/pair/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::state::{Config, CIRCUIT_BREAKER, CONFIG, FROZEN};
use std::str::FromStr;
use std::vec;

use coreum_wasm_sdk::{assetft, core::CoreumQueries};

Check failure on line 4 in contracts/pair/src/contract.rs

View workflow job for this annotation

GitHub Actions / Lints

unused imports: `assetft`, `core::CoreumQueries`

Check warning on line 4 in contracts/pair/src/contract.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused imports: `assetft`, `core::CoreumQueries`
use cosmwasm_std::{
attr, ensure, entry_point, from_binary, to_binary, Addr, Binary, CosmosMsg, Decimal,
Decimal256, Deps, DepsMut, Env, Isqrt, MessageInfo, QuerierWrapper, Reply, Response, StdError,
Expand All @@ -8,6 +10,7 @@ use cosmwasm_std::{

use cw2::set_contract_version;
use cw20::{Cw20ExecuteMsg, Cw20ReceiveMsg};

use dex::asset::{
addr_opt_validate, check_swap_parameters, Asset, AssetInfoValidated, AssetValidated,
MINIMUM_LIQUIDITY_AMOUNT,
Expand All @@ -26,8 +29,8 @@ use dex::pair::{
ReverseSimulationResponse, SimulationResponse, TWAP_PRECISION,
};
use dex::querier::{query_factory_config, query_supply};
use std::str::FromStr;
use std::vec;

use crate::state::{Config, CIRCUIT_BREAKER, CONFIG, FROZEN};

/// Contract name that is used for migration.
const CONTRACT_NAME: &str = "dex-pair";
Expand Down
1 change: 1 addition & 0 deletions packages/dex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
thiserror = { workspace = true }
coreum-wasm-sdk = { workspace = true }
cw20 = { workspace = true }
cw20-base = { workspace = true }
cw-utils = { workspace = true }
Expand Down
23 changes: 12 additions & 11 deletions packages/dex/src/asset.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use itertools::Itertools;
use std::fmt;

use coreum_wasm_sdk::core::CoreumQueries;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Env;
use cosmwasm_std::{
to_binary, Addr, Api, BankMsg, Coin, ConversionOverflowError, CosmosMsg, Decimal256, Env,
Fraction, MessageInfo, QuerierWrapper, StdError, StdResult, Uint128, Uint256, WasmMsg,
};
use cw20::{Cw20ExecuteMsg, Cw20QueryMsg, MinterResponse, TokenInfoResponse};
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
use std::fmt;

use crate::pair::PairInfo;
use crate::pair::QueryMsg as PairQueryMsg;
use crate::querier::{
query_balance, query_token_balance, query_token_symbol, NATIVE_TOKEN_PRECISION,
};
use cosmwasm_std::{
to_binary, Addr, Api, BankMsg, Coin, ConversionOverflowError, CosmosMsg, Decimal256, Fraction,
MessageInfo, QuerierWrapper, StdError, StdResult, Uint128, Uint256, WasmMsg,
};
use cw20::{Cw20ExecuteMsg, Cw20QueryMsg, MinterResponse, TokenInfoResponse};
use itertools::Itertools;

/// Minimum initial LP share
pub const MINIMUM_LIQUIDITY_AMOUNT: Uint128 = Uint128::new(1_000);
Expand Down Expand Up @@ -207,7 +208,7 @@ impl AssetInfo {
}
pub fn query_pool(
&self,
querier: &QuerierWrapper,
querier: &QuerierWrapper<CoreumQueries>,
pool_addr: impl Into<String>,
) -> StdResult<Uint128> {
match self {
Expand Down Expand Up @@ -292,7 +293,7 @@ impl AssetInfoValidated {
/// * **account_addr** is the address whose token balance we check.
pub fn query_balance(
&self,
querier: &QuerierWrapper,
querier: &QuerierWrapper<CoreumQueries>,
account_addr: impl Into<String>,
) -> StdResult<Uint128> {
match self {
Expand All @@ -304,7 +305,7 @@ impl AssetInfoValidated {
}

/// Returns the number of decimals that a token has.
pub fn decimals(&self, querier: &QuerierWrapper) -> StdResult<u8> {
pub fn decimals(&self, querier: &QuerierWrapper<CoreumQueries>) -> StdResult<u8> {
let decimals = match &self {
AssetInfoValidated::Native { .. } => NATIVE_TOKEN_PRECISION,
AssetInfoValidated::Token(contract_addr) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/dex/src/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
oracle::{SamplePeriod, TwapResponse},
};

use coreum_wasm_sdk::core::CoreumQueries;
use cosmwasm_std::{
to_binary, Addr, Binary, Decimal, Decimal256, QuerierWrapper, StdError, StdResult, Uint128,
WasmMsg,
Expand Down Expand Up @@ -53,7 +54,7 @@ impl PairInfo {
/// * **contract_addr** is pair's pool address.
pub fn query_pools(
&self,
querier: &QuerierWrapper,
querier: &QuerierWrapper<CoreumQueries>,
contract_addr: impl Into<String>,
) -> StdResult<Vec<AssetValidated>> {
let contract_addr = contract_addr.into();
Expand All @@ -73,7 +74,7 @@ impl PairInfo {
/// * **contract_addr** is pair's pool address.
pub fn query_pools_decimal(
&self,
querier: &QuerierWrapper,
querier: &QuerierWrapper<CoreumQueries>,
contract_addr: impl Into<String>,
) -> StdResult<Vec<DecimalAsset>> {
let contract_addr = contract_addr.into();
Expand Down
19 changes: 14 additions & 5 deletions packages/dex/src/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::pair::{
PairInfo, QueryMsg as PairQueryMsg, ReverseSimulationResponse, SimulationResponse,
};

use coreum_wasm_sdk::{assetft, core::CoreumQueries};
use cosmwasm_std::{
Addr, AllBalanceResponse, BankQuery, Coin, Decimal, QuerierWrapper, QueryRequest, StdResult,
Uint128,
Expand All @@ -21,13 +22,21 @@ pub const NATIVE_TOKEN_PRECISION: u8 = 6;
///
/// * **denom** specifies the denomination used to return the balance (e.g uluna).
pub fn query_balance(
querier: &QuerierWrapper,
querier: &QuerierWrapper<CoreumQueries>,
account_addr: impl Into<String>,
denom: impl Into<String>,
) -> StdResult<Uint128> {
querier
.query_balance(account_addr, denom)
.map(|coin| coin.amount)
let request: QueryRequest<CoreumQueries> = CoreumQueries::AssetFT(assetft::Query::Balance {
account: account_addr.into(),
denom: denom.into(),
})
.into();
let balance_response: assetft::BalanceResponse = querier.query(&request)?;
Ok(balance_response
.balance
.parse::<u128>()
.expect("Failed to parse the string")
.into())
}

/// Returns the total balances for all coins at a specified account address.
Expand All @@ -47,7 +56,7 @@ pub fn query_all_balances(querier: &QuerierWrapper, account_addr: Addr) -> StdRe
///
/// * **account_addr** account address for which we return a balance.
pub fn query_token_balance(
querier: &QuerierWrapper,
querier: &QuerierWrapper<CoreumQueries>,
contract_addr: impl Into<String>,
account_addr: impl Into<String>,
) -> StdResult<Uint128> {
Expand Down

0 comments on commit 48ca1a4

Please sign in to comment.