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 06c780e
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 22 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
10 changes: 5 additions & 5 deletions src/components/AssetsModal/AssetsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ const AssetsModal = ({ modalId, onClose }: AssetsModalProps) => {
) : (
<WithdrawView ticker={tickerToWithdraw} onBack={handleBackClicked} onSuccess={handleWithdrawSuccess} />
)}
<form method="dialog" className="modal-backdrop">
<button type="button" onClick={handleClose}>
close
</button>
</form>
</div>
<form method="dialog" className="modal-backdrop">
<button type="button" onClick={handleClose}>
close
</button>
</form>
</dialog>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/AssetsModal/PositionsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const TableRow = ({ receivables, ticker, onWithdraw }: TableRowProps) => {
<p className="text-lg font-semibold leading-5">{formatAmount(receivables)}</p>
<p className="text-base">{!isNil(price) && toDollarsFormatted(price, receivables)}</p>
</td>
<td className="text-lg font-semibold">{pool && formatAPY(pool.apr)}</td>
<td className="text-lg font-semibold">{pool && formatAPY(pool.annualInterestRate)}</td>
<td>
<Button onClick={handleWithdrawClick}>Withdraw</Button>
</td>
Expand Down
2 changes: 1 addition & 1 deletion src/components/WalletCard/LoansModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const TableRow = ({ liabilities, ticker }: TableRowProps) => {
<p className="text-lg font-semibold leading-5">{formatAmount(liabilities)}</p>
<p className="text-base">{!isNil(price) && toDollarsFormatted(price, liabilities)}</p>
</td>
<td className="text-lg font-semibold">{pool ? formatAPR(pool.apr) : null}</td>
<td className="text-lg font-semibold">{pool ? formatAPR(pool.annualInterestRate) : null}</td>
<td>
{isRepaying ? (
<Button disabled>
Expand Down
24 changes: 14 additions & 10 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 All @@ -27,7 +28,7 @@ export type PoolContext = {
const Context = createContext<PoolContext>({
prices: null,
pools: null,
refetchPools: () => {},
refetchPools: () => { },
});

const fetchAllPrices = async (): Promise<PriceRecord> => {
Expand All @@ -51,14 +52,6 @@ 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 fetchPools = async (): Promise<PoolRecord> => {
const [XLM, wBTC, wETH, USDC, EURC] = await Promise.all([
fetchPoolState('XLM'),
Expand All @@ -70,6 +63,17 @@ const fetchPools = async (): Promise<PoolRecord> => {
return { XLM, wBTC, wETH, USDC, EURC };
};

const fetchPoolState = async (ticker: SupportedCurrency): Promise<PoolState> => {
const { contractClient } = CURRENCY_BINDINGS[ticker];
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,
};
};

export const PoolProvider = ({ children }: PropsWithChildren) => {
const [prices, setPrices] = useState<PriceRecord | null>(null);
const [pools, setPools] = useState<PoolRecord | null>(null);
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
4 changes: 2 additions & 2 deletions src/pages/_lend/LendableAsset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ export const LendableAsset = ({ currency }: LendableAssetProps) => {
</td>

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

<td className="pr-0">
{isPoor ? (
<div className="tooltip" data-tip={!wallet ? 'Connect a wallet first' : 'Not enough funds'}>
<Button disabled={true} onClick={() => {}}>
<Button disabled={true} onClick={() => { }}>
Deposit
</Button>
</div>
Expand Down

0 comments on commit 06c780e

Please sign in to comment.