Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions contracts/params/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Coin, Decimal};
use cosmwasm_std::{Decimal, Uint128};
use mars_owner::OwnerUpdate;

use crate::types::{asset::AssetParamsUnchecked, vault::VaultConfigUnchecked};
Expand Down Expand Up @@ -57,12 +57,19 @@ pub enum QueryMsg {

/// Compute the total amount deposited of the given asset across Red Bank
/// and Credit Manager.
#[returns(Coin)]
#[returns(TotalDepositResponse)]
TotalDeposit {
denom: String,
},
}

#[cw_serde]
pub struct TotalDepositResponse {
pub denom: String,
pub cap: Uint128,
pub amount: Uint128,
}

#[cw_serde]
pub enum AssetParamsUpdate {
AddOrUpdate {
Expand Down
20 changes: 16 additions & 4 deletions contracts/params/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Addr, Coin, Deps, Env, Order, StdResult, Uint128};
use cosmwasm_std::{Addr, Deps, Env, Order, StdResult, Uint128};
use cw_storage_plus::Bound;
use mars_interest_rate::get_underlying_liquidity_amount;
use mars_red_bank_types::{
Expand All @@ -7,6 +7,7 @@ use mars_red_bank_types::{
};

use crate::{
msg::TotalDepositResponse,
state::{ADDRESS_PROVIDER, ASSET_PARAMS, VAULT_CONFIGS},
types::{asset::AssetParams, vault::VaultConfig},
};
Expand Down Expand Up @@ -73,7 +74,11 @@ pub fn query_all_vault_configs(
/// For example, when computing the deposited amount of ATOM, we only include
/// ATOM deposited in RB and CM; we don't include the ATOM-OSMO LP token, or
/// the ATOM-OSMO farming vault.
pub fn query_total_deposit(deps: Deps, env: &Env, denom: String) -> StdResult<Coin> {
pub fn query_total_deposit(
deps: Deps,
env: &Env,
denom: String,
) -> StdResult<TotalDepositResponse> {
let current_timestamp = env.block.time.seconds();

// query contract addresses
Expand Down Expand Up @@ -124,8 +129,15 @@ pub fn query_total_deposit(deps: Deps, env: &Env, denom: String) -> StdResult<Co
// note that this way, we don't include LP tokens or vault positions
let cm_deposit = deps.querier.query_balance(credit_manager_addr, &denom)?.amount;

Ok(Coin {
// total deposited amount
let amount = rb_deposit.checked_add(cm_deposit)?.checked_sub(cm_debt)?;

// additionally, we include the deposit cap in the response
let asset_params = ASSET_PARAMS.load(deps.storage, &denom)?;

Ok(TotalDepositResponse {
denom,
amount: rb_deposit.checked_add(cm_deposit)?.checked_sub(cm_debt)?,
amount,
cap: asset_params.deposit_cap,
})
}
25 changes: 22 additions & 3 deletions contracts/params/tests/test_deposit_cap.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
pub mod helpers;

use std::str::FromStr;

use cosmwasm_std::{coins, Addr, Decimal, Uint128};
use mars_interest_rate::get_underlying_liquidity_amount;
use mars_params::{query::query_total_deposit, state::ADDRESS_PROVIDER};
use mars_params::{
msg::TotalDepositResponse,
query::query_total_deposit,
state::{ADDRESS_PROVIDER, ASSET_PARAMS},
};
use mars_red_bank_types::red_bank::{Market, UserDebtResponse};
use mars_testing::{mock_dependencies, mock_env_at_block_time};
use test_case::test_case;

use crate::helpers::default_asset_params;

const CREDIT_MANAGER: &str = "credit_manager";
const MOCK_DENOM: &str = "utoken";
const TIMESTAMP: u64 = 1690573960;
Expand Down Expand Up @@ -49,11 +57,15 @@ fn querying_total_deposit(rb_market: Market, rb_debt: UserDebtResponse, cm_balan
let mut deps = mock_dependencies(&[]);
let env = mock_env_at_block_time(TIMESTAMP);

let params_unchecked = default_asset_params(MOCK_DENOM);
let params = params_unchecked.check(deps.as_ref().api).unwrap();

// setup
deps.querier.set_redbank_market(rb_market.clone());
deps.querier.set_red_bank_user_debt(CREDIT_MANAGER, rb_debt.clone());
deps.querier.update_balances(CREDIT_MANAGER, coins(cm_balance.u128(), MOCK_DENOM));
ADDRESS_PROVIDER.save(deps.as_mut().storage, &Addr::unchecked("address_provider")).unwrap();
ASSET_PARAMS.save(deps.as_mut().storage, MOCK_DENOM, &params).unwrap();

// compute the correct, expected total deposit
let rb_deposit =
Expand All @@ -62,6 +74,13 @@ fn querying_total_deposit(rb_market: Market, rb_debt: UserDebtResponse, cm_balan
let exp_total_deposit = rb_deposit + cm_balance - rb_debt.amount;

// query total deposit
let total_deposit = query_total_deposit(deps.as_ref(), &env, MOCK_DENOM.into()).unwrap();
assert_eq!(total_deposit.amount, exp_total_deposit);
let res = query_total_deposit(deps.as_ref(), &env, MOCK_DENOM.into()).unwrap();
assert_eq!(
res,
TotalDepositResponse {
denom: MOCK_DENOM.into(),
amount: exp_total_deposit,
cap: params.deposit_cap,
}
);
}
7 changes: 6 additions & 1 deletion schemas/mars-params/mars-params.json
Original file line number Diff line number Diff line change
Expand Up @@ -1341,20 +1341,25 @@
},
"total_deposit": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Coin",
"title": "TotalDepositResponse",
"type": "object",
"required": [
"amount",
"cap",
"denom"
],
"properties": {
"amount": {
"$ref": "#/definitions/Uint128"
},
"cap": {
"$ref": "#/definitions/Uint128"
},
"denom": {
"type": "string"
}
},
"additionalProperties": false,
"definitions": {
"Uint128": {
"description": "A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u128` to get the value out:\n\n``` # use cosmwasm_std::Uint128; let a = Uint128::from(123u128); assert_eq!(a.u128(), 123);\n\nlet b = Uint128::from(42u64); assert_eq!(b.u128(), 42);\n\nlet c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```",
Expand Down
5 changes: 3 additions & 2 deletions scripts/types/generated/mars-params/MarsParams.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
ArrayOfVaultConfigBaseForAddr,
VaultConfigBaseForAddr,
OwnerResponse,
TotalDepositResponse,
} from './MarsParams.types'
export interface MarsParamsReadOnlyInterface {
contractAddress: string
Expand All @@ -57,7 +58,7 @@ export interface MarsParamsReadOnlyInterface {
startAfter?: string
}) => Promise<ArrayOfVaultConfigBaseForAddr>
targetHealthFactor: () => Promise<Decimal>
totalDeposit: ({ denom }: { denom: string }) => Promise<Coin>
totalDeposit: ({ denom }: { denom: string }) => Promise<TotalDepositResponse>
}
export class MarsParamsQueryClient implements MarsParamsReadOnlyInterface {
client: CosmWasmClient
Expand Down Expand Up @@ -127,7 +128,7 @@ export class MarsParamsQueryClient implements MarsParamsReadOnlyInterface {
target_health_factor: {},
})
}
totalDeposit = async ({ denom }: { denom: string }): Promise<Coin> => {
totalDeposit = async ({ denom }: { denom: string }): Promise<TotalDepositResponse> => {
return this.client.queryContractSmart(this.contractAddress, {
total_deposit: {
denom,
Expand Down
8 changes: 5 additions & 3 deletions scripts/types/generated/mars-params/MarsParams.react-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
ArrayOfVaultConfigBaseForAddr,
VaultConfigBaseForAddr,
OwnerResponse,
TotalDepositResponse,
} from './MarsParams.types'
import { MarsParamsQueryClient, MarsParamsClient } from './MarsParams.client'
export const marsParamsQueryKeys = {
Expand Down Expand Up @@ -79,17 +80,18 @@ export interface MarsParamsReactQuery<TResponse, TData = TResponse> {
initialData?: undefined
}
}
export interface MarsParamsTotalDepositQuery<TData> extends MarsParamsReactQuery<Coin, TData> {
export interface MarsParamsTotalDepositQuery<TData>
extends MarsParamsReactQuery<TotalDepositResponse, TData> {
args: {
denom: string
}
}
export function useMarsParamsTotalDepositQuery<TData = Coin>({
export function useMarsParamsTotalDepositQuery<TData = TotalDepositResponse>({
client,
args,
options,
}: MarsParamsTotalDepositQuery<TData>) {
return useQuery<Coin, Error, TData>(
return useQuery<TotalDepositResponse, Error, TData>(
marsParamsQueryKeys.totalDeposit(client?.contractAddress, args),
() =>
client
Expand Down
5 changes: 5 additions & 0 deletions scripts/types/generated/mars-params/MarsParams.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,8 @@ export interface OwnerResponse {
owner?: string | null
proposed?: string | null
}
export interface TotalDepositResponse {
amount: Uint128
cap: Uint128
denom: string
}