mars-v2-frontend/hooks/useMarkets.tsx
Gustavo Mauricio 1deba2059e
MP-1699: Trade on Margin Account (#52)
* update generated types

* added CRO to token info

* update contract addresses to match latest deployment

* feat: token prices fetched from oracle contract

* trade page initial commit

* trade asset action hook

* extract max swap amount logic into custom hook

* trade component ui adjustments

* trade container min-width and some styling improvements

* trade success message and loading indicator

* normalize naming conventions on trading

* max swap amount formula adjustments

* trade execute msg with borrow. code cleanup

* fix: click max update tokenOut amount. remove wallet from fund denom

* delay token amount decimal conversion. input formatting

* increase hardcoded gas

* renamed max swappable amount hook

* display token prices and market information on landing page

* reset trade amounts when selected account change

* max trade amount cleanup and minor performance optimizations

* fix: liabilities value with 1 hour interest buffer for trade action

* add token symbol to wallet and account labels

* swap trade pairs icon and basic functionality

* remove unnecessary optional chaining. comment adjusted

* refactor useTokenPrices to build query dynamically on tokens data

* extracted trade container and respective functionality into separate file

* fix: properly calculate positions after full swap

* mp-1218: trading using wallet
2022-11-22 10:14:12 +01:00

124 lines
3.6 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useMemo } from 'react'
interface Market {
denom: string
max_loan_to_value: string
liquidation_threshold: string
liquidation_bonus: string
reserve_factor: string
interest_rate_model: {
optimal_utilization_rate: string
base: string
slope_1: string
slope_2: string
}
borrow_index: string
liquidity_index: string
borrow_rate: string
liquidity_rate: string
indexes_last_updated: number
collateral_total_scaled: string
debt_total_scaled: string
deposit_enabled: boolean
borrow_enabled: boolean
deposit_cap: string
}
interface Result {
wasm: {
[key: string]: Market
}
}
const useMarkets = () => {
const result = useQuery<Result>(
['marketInfo'],
() => ({
wasm: {
uosmo: {
denom: 'uosmo',
max_loan_to_value: '0.55',
liquidation_threshold: '0.65',
liquidation_bonus: '0.1',
reserve_factor: '0.2',
interest_rate_model: {
optimal_utilization_rate: '0.7',
base: '0.3',
slope_1: '0.25',
slope_2: '0.3',
},
borrow_index: '1.009983590233269535',
liquidity_index: '1.002073497939302451',
borrow_rate: '0.350254719559196173',
liquidity_rate: '0.039428374060840366',
indexes_last_updated: 1668271634,
collateral_total_scaled: '8275583285688290',
debt_total_scaled: '1155363812346122',
deposit_enabled: true,
borrow_enabled: true,
deposit_cap: '15000000000000',
},
'ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2': {
denom: 'ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2',
max_loan_to_value: '0.65',
liquidation_threshold: '0.7',
liquidation_bonus: '0.1',
reserve_factor: '0.2',
interest_rate_model: {
optimal_utilization_rate: '0.1',
base: '0.3',
slope_1: '0.25',
slope_2: '0.3',
},
borrow_index: '1.015550607619308095',
liquidity_index: '1.003284932040106733',
borrow_rate: '0.75632500115230499',
liquidity_rate: '0.435023016254423759',
indexes_last_updated: 1668273756,
collateral_total_scaled: '3309105730887721',
debt_total_scaled: '2350429206911653',
deposit_enabled: true,
borrow_enabled: true,
deposit_cap: '15000000000000',
},
'ibc/E6931F78057F7CC5DA0FD6CEF82FF39373A6E0452BF1FD76910B93292CF356C1': {
denom: 'ibc/E6931F78057F7CC5DA0FD6CEF82FF39373A6E0452BF1FD76910B93292CF356C1',
max_loan_to_value: '0.65',
liquidation_threshold: '0.7',
liquidation_bonus: '0.1',
reserve_factor: '0.2',
interest_rate_model: {
optimal_utilization_rate: '0.1',
base: '0.3',
slope_1: '0.25',
slope_2: '0.3',
},
borrow_index: '1.001519837645865043',
liquidity_index: '1',
borrow_rate: '0.3',
liquidity_rate: '0',
indexes_last_updated: 1667995650,
collateral_total_scaled: '1000000000000000',
debt_total_scaled: '0',
deposit_enabled: true,
borrow_enabled: true,
deposit_cap: '15000000000000',
},
},
}),
{
staleTime: Infinity,
},
)
return {
...result,
data: useMemo(() => {
return result?.data && result.data.wasm
}, [result.data]),
}
}
export default useMarkets