Skip to content

Commit

Permalink
feat: Combine PoolState into one endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kovipu committed Dec 8, 2024
1 parent 102f6bf commit 291aff0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
10 changes: 10 additions & 0 deletions contracts/loan_pool/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::dto::PoolState;
use crate::interest::{self, get_interest};
use crate::pool::Currency;
use crate::positions;
Expand Down Expand Up @@ -223,6 +224,15 @@ impl LoanPoolContract {
interest::get_interest(e)
}

pub fn get_pool_state(e: Env) -> PoolState {
PoolState {
total_balance: pool::read_total_balance(&e),
available_balance: pool::read_available_balance(&e),
total_shares: pool::read_total_shares(&e),
annual_interest_rate: interest::get_interest(e),
}
}

pub fn increase_liabilities(e: Env, user: Address, amount: i128) {
let loan_manager_addr = pool::read_loan_manager_addr(&e);
loan_manager_addr.require_auth();
Expand Down
9 changes: 9 additions & 0 deletions contracts/loan_pool/src/dto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use soroban_sdk::contracttype;

#[contracttype]
pub struct PoolState {
pub total_balance: i128,
pub available_balance: i128,
pub total_shares: i128,
pub annual_interest_rate: i128,
}
1 change: 1 addition & 0 deletions contracts/loan_pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(clippy::unused_unit)]

mod contract;
mod dto;
mod interest;
mod pool;
mod positions;
Expand Down
14 changes: 9 additions & 5 deletions src/contexts/pool-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export type PriceRecord = {
export type PoolState = {
totalBalance: bigint;
availableBalance: bigint;
apr: bigint;
totalShares: bigint;
annualInterestRate: bigint;
};

export type PoolRecord = {
Expand Down Expand Up @@ -53,10 +54,13 @@ const fetchPriceData = async (ticker: string): Promise<bigint> => {

const fetchPoolState = async (ticker: SupportedCurrency): Promise<PoolState> => {
const { contractClient } = CURRENCY_BINDINGS[ticker];
const { result: totalBalance } = await contractClient.get_contract_balance();
const { result: availableBalance } = await contractClient.get_available_balance();
const { result: apr } = await contractClient.get_interest();
return { totalBalance, availableBalance, apr };
const { result } = await contractClient.get_pool_state();
return {
totalBalance: result.total_balance,
availableBalance: result.available_balance,
totalShares: result.total_shares,
annualInterestRate: result.annual_interest_rate,
};
};

const fetchPools = async (): Promise<PoolRecord> => {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/_borrow/BorrowModal/BorrowStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const BorrowStep = ({ onClose, currency }: BorrowStepProps) => {

if (!pools || !prices || !walletBalances) return null;

const { apr, availableBalance } = pools[ticker];
const { annualInterestRate, availableBalance } = pools[ticker];

const collateralOptions: SupportedCurrency[] = CURRENCY_BINDINGS_ARR.filter((c) => c.ticker !== ticker).map(
({ ticker }) => ticker,
Expand Down Expand Up @@ -146,7 +146,7 @@ export const BorrowStep = ({ onClose, currency }: BorrowStepProps) => {
collateral, causing you to lose some of your collateral.
</p>
<p className="my-4">The interest rate changes as the amount of assets borrowed from the pools changes.</p>
<p className="my-4">The annual interest rate is currently {formatAPR(apr)}.</p>
<p className="my-4">The annual interest rate is currently {formatAPR(annualInterestRate)}.</p>

<p className="font-bold mb-2 mt-6">Amount to borrow</p>
<CryptoAmountSelector
Expand Down
4 changes: 3 additions & 1 deletion src/pages/_borrow/BorrowableAsset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export const BorrowableAsset = ({ currency }: BorrowableAssetCardProps) => {
</td>

<td>
<p className="text-xl font-semibold leading-6">{pool ? formatAPR(pool.apr) : <Loading size="xs" />}</p>
<p className="text-xl font-semibold leading-6">
{pool ? formatAPR(pool.annualInterestRate) : <Loading size="xs" />}
</p>
</td>

<td>
Expand Down

0 comments on commit 291aff0

Please sign in to comment.