-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from laina-protocol/refactor/pool-context
refactor: Move pool interactions into a React Context
- Loading branch information
Showing
15 changed files
with
183 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { type PropsWithChildren, createContext, useCallback, useContext, useEffect, useState } from 'react'; | ||
|
||
import { contractClient as loanManagerClient } from '@contracts/loan_manager'; | ||
import type { SupportedCurrency } from 'currencies'; | ||
import { CURRENCY_BINDINGS } from 'src/currency-bindings'; | ||
|
||
export type PriceRecord = { | ||
[K in SupportedCurrency]: bigint; | ||
}; | ||
|
||
export type PoolState = { | ||
totalBalance: bigint; | ||
availableBalance: bigint; | ||
apr: bigint; | ||
}; | ||
|
||
export type PoolRecord = { | ||
[K in SupportedCurrency]: PoolState; | ||
}; | ||
|
||
export type PoolContext = { | ||
prices: PriceRecord | null; | ||
pools: PoolRecord | null; | ||
refetchPools: () => void; | ||
}; | ||
|
||
const Context = createContext<PoolContext>({ | ||
prices: null, | ||
pools: null, | ||
refetchPools: () => {}, | ||
}); | ||
|
||
const fetchAllPrices = async (): Promise<PriceRecord> => { | ||
const [XLM, wBTC, wETH, USDC, EURC] = await Promise.all([ | ||
fetchPriceData('XLM'), | ||
fetchPriceData('BTC'), | ||
fetchPriceData('ETH'), | ||
fetchPriceData('USDC'), | ||
fetchPriceData('EURC'), | ||
]); | ||
return { XLM, wBTC, wETH, USDC, EURC }; | ||
}; | ||
|
||
const fetchPriceData = async (ticker: string): Promise<bigint> => { | ||
try { | ||
const { result } = await loanManagerClient.get_price({ token: ticker }); | ||
return result; | ||
} catch (error) { | ||
console.error(`Error fetching price data: for ${ticker}`, error); | ||
return 0n; | ||
} | ||
}; | ||
|
||
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'), | ||
fetchPoolState('wBTC'), | ||
fetchPoolState('wETH'), | ||
fetchPoolState('USDC'), | ||
fetchPoolState('EURC'), | ||
]); | ||
return { XLM, wBTC, wETH, USDC, EURC }; | ||
}; | ||
|
||
export const PoolProvider = ({ children }: PropsWithChildren) => { | ||
const [prices, setPrices] = useState<PriceRecord | null>(null); | ||
const [pools, setPools] = useState<PoolRecord | null>(null); | ||
|
||
const refetchPools = useCallback(() => { | ||
fetchAllPrices() | ||
.then((res) => setPrices(res)) | ||
.catch((err) => console.error('Error fetching prices', err)); | ||
fetchPools() | ||
.then((res) => setPools(res)) | ||
.catch((err) => console.error('Error fetching pools', err)); | ||
}, []); | ||
|
||
useEffect(() => { | ||
refetchPools(); | ||
|
||
// Set up a timer for every ledger (~6 secs) to refetch state. | ||
const intervalId = setInterval(refetchPools, 6000); | ||
return () => clearInterval(intervalId); | ||
}, [refetchPools]); | ||
|
||
return <Context.Provider value={{ prices, pools, refetchPools }}>{children}</Context.Provider>; | ||
}; | ||
|
||
export const usePools = (): PoolContext => useContext(Context); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.