Skip to content

Commit

Permalink
Fixed Gauge calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrosimao committed Feb 21, 2022
1 parent 07d6793 commit 4103109
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 77 deletions.
25 changes: 14 additions & 11 deletions src/components/CryptoInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const CryptoInput: React.FC<t.CryptoInputPropsType> = ({

return (
<>
{showBalance && maxValue ? (
{showBalance ? (
<Box
flex
width="90%"
Expand All @@ -49,14 +49,16 @@ export const CryptoInput: React.FC<t.CryptoInputPropsType> = ({
<Text as="p" size="small" margin="0">
balance: {Number(maxValue || 0).toFixed(3)}
</Text>
<Button
label="max"
alignSelf="end"
size="small"
onClick={() => {
onChange(maxValue)
}}
/>
{maxValue ? (
<Button
label="max"
alignSelf="end"
size="small"
onClick={() => {
onChange(maxValue)
}}
/>
) : null}
</Box>
) : null}
<Box
Expand Down Expand Up @@ -91,6 +93,7 @@ export const CryptoInput: React.FC<t.CryptoInputPropsType> = ({
margin="auto 0"
label={currency}
color="background-front"
size="small"
icon={
<Image
src={
Expand All @@ -99,8 +102,8 @@ export const CryptoInput: React.FC<t.CryptoInputPropsType> = ({
: '/images/logo_small.svg'
}
alt="Currency Icon"
height={77}
width={77}
height={66}
width={66}
objectFit="scale-down"
/>
}
Expand Down
61 changes: 23 additions & 38 deletions src/components/SwapInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ export const SwapInput: React.FC<t.SwapInputPropsType> = ({

return (
<Box flex align="center" direction="column">
{maxValue ? (
<Box
width="90%"
gap="small"
pad="none"
align="center"
justify="between"
direction="row"
margin="0 auto"
>
<Text alignSelf="start">{inputLabel ? inputLabel : 'Pay:'}</Text>
<Box flex justify="end" align="center" direction="row" gap="xsmall">
<Text as="p" size="xsmall" margin="0" alignSelf="center">
balance: {Number(maxValue || 0).toFixed(3)}
</Text>
<Box
width="90%"
gap="small"
pad="none"
align="center"
justify="between"
direction="row"
margin="0 auto"
>
<Text alignSelf="start">{inputLabel ? inputLabel : 'Pay:'}</Text>
<Box flex justify="end" align="center" direction="row" gap="xsmall">
<Text as="p" size="xsmall" margin="0" alignSelf="center">
balance: {Number(maxValue || 0).toFixed(3)}
</Text>
{maxValue ? (
<Button
label="max"
alignSelf="end"
Expand All @@ -67,9 +67,9 @@ export const SwapInput: React.FC<t.SwapInputPropsType> = ({
onChange(maxValue)
}}
/>
</Box>
) : null}
</Box>
) : null}
</Box>
<Box
margin="5px auto 0 auto"
border={{ size: 'xsmall' }}
Expand Down Expand Up @@ -98,6 +98,7 @@ export const SwapInput: React.FC<t.SwapInputPropsType> = ({
/>
<Button
primary
size="small"
icon={
<Image
alt="currency icon"
Expand All @@ -106,8 +107,8 @@ export const SwapInput: React.FC<t.SwapInputPropsType> = ({
? '/images/near_icon.svg'
: '/images/logo_small.svg'
}
height={77}
width={77}
height={66}
width={66}
objectFit="scale-down"
/>
}
Expand Down Expand Up @@ -159,6 +160,7 @@ export const SwapInput: React.FC<t.SwapInputPropsType> = ({
/>
<Button
primary
size="small"
plain={false}
alignSelf="end"
margin="auto 0"
Expand All @@ -171,8 +173,8 @@ export const SwapInput: React.FC<t.SwapInputPropsType> = ({
? '/images/logo_small.svg'
: '/images/near_icon.svg'
}
height={77}
width={77}
height={66}
width={66}
objectFit="scale-down"
/>
}
Expand All @@ -185,23 +187,6 @@ export const SwapInput: React.FC<t.SwapInputPropsType> = ({
/>
</Box>
</Box>
{/* Todo: display some info about the swap transaction */}
{/*<Box width="90%" justify="end">*/}
{/* <Text weight="lighter" size="xsmall" alignSelf="end" color="text-weak">*/}
{/* {fiatValue && Number(value)*/}
{/* ? `Approximate value: ~${fiatValue.toFixed(2)} usd`*/}
{/* : null}*/}
{/* </Text>*/}
{/* <Text weight="lighter" size="xsmall" alignSelf="end" color="text-weak">*/}
{/* Price Impact: ~1%*/}
{/* </Text>*/}
{/* <Text weight="lighter" size="xsmall" alignSelf="end" color="text-weak">*/}
{/* Max slippage: ~1%*/}
{/* </Text>*/}
{/* <Text weight="lighter" size="xsmall" alignSelf="end" color="text-weak">*/}
{/* Minimum received after slippage: ~${(fiatValue * 0.95).toFixed(2)} usd*/}
{/* </Text>*/}
{/*</Box>*/}
</Box>
)
}
2 changes: 1 addition & 1 deletion src/hooks/useGetBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const useGetBalance = (): {
.viewFunction(contract?.contractId || '', 'ft_balance_of', {
account_id: currentUser?.accountId,
})
const newQuidBalance = utils.format.formatNearAmount(quidRes)
const newQuidBalance = utils.format.formatNearAmount(quidRes || '0')
setQuidBalance(Number(newQuidBalance || 0).toFixed(3))
// Get Near balance
const newNearBalance = utils.format.formatNearAmount(
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/useGetStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export const useGetStats = (): {
setIsLoading(true)
const newStats = await getStats()
const newPledged = await getPledged()
// console.log({ newStats })
// console.log({ newPledged })

setStats({
nearSpTotal: utils.format.formatNearAmount(
newStats?.blood_debit || '0'
Expand Down
4 changes: 2 additions & 2 deletions src/near/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export const CONTRACT_NAME =
process.env.CONTRACT_NAME || 'dev-1645134464429-38710865502300'
process.env.CONTRACT_NAME || 'dev-1645458162959-30481065418494'

export enum NETWORK_TYPE {
PRODUCTION = 'production',
DEV = 'development',
TEST = 'test',
// TEST = 'test',
TESTNET = 'testnet',
}
export const getConfig = (env: string) => {
Expand Down
38 changes: 24 additions & 14 deletions src/pages/borrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ const Borrow: React.FC = () => {
return 0
}

const getGaugeNumber = () => {
return isWithdraw
? (Number(stats?.debit || 0) - Number(outputWithdrawAmount || 0)) /
((Number(stats?.credit || 0) - Number(inputWithdrawAmount || 0)) *
Number(nearQuote))
: (Number(stats?.debit || 0) + Number(outputAmount || 0)) /
((Number(stats?.credit || 0) + Number(inputAmount || 0)) *
Number(nearQuote))
}

const handleBorrow = async () => {
if (inputAmount && outputAmount) {
try {
Expand All @@ -84,28 +94,28 @@ const Borrow: React.FC = () => {
if (inputWithdrawAmount || outputWithdrawAmount) {
try {
setIsLoading(true)
// Withdraw
if (inputWithdrawAmount) {
contract?.renege(
// Repay first
if (outputWithdrawAmount) {
await contract?.swap(
{
amount: utils.format.parseNearAmount(inputWithdrawAmount),
sp: false,
amount: utils.format.parseNearAmount(outputWithdrawAmount),
repay: true,
// Todo: implement short
qd: false,
live: true,
short: false,
},
undefined,
'1'
)
}
// Repay
if (outputWithdrawAmount) {
contract?.swap(
// Then Withdraw
if (inputWithdrawAmount) {
await contract?.renege(
{
amount: utils.format.parseNearAmount(outputWithdrawAmount),
repay: true,
amount: utils.format.parseNearAmount(inputWithdrawAmount),
sp: false,
// Todo: implement short
short: false,
qd: false,
live: true,
},
undefined,
'1'
Expand Down Expand Up @@ -173,7 +183,7 @@ const Borrow: React.FC = () => {
<GaugeChart
id="gauge-chart2"
nrOfLevels={22}
percent={getCollateralRatio() ? 1 / getCollateralRatio() : 0}
percent={getGaugeNumber()}
colors={['yellow', '#FFC371', 'red']}
arcWidth={0.22}
/>
Expand Down
9 changes: 0 additions & 9 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,6 @@ const Home: NextPage = () => {
/>
Protocol
</Text>

<Image
src="/images/lemnis_logo.svg"
alt="Lemnis Logo"
width={100}
height={50}
objectPosition="bottom"
objectFit="scale-down"
/>
<br />
</Box>
</MainContainer>
Expand Down

0 comments on commit 4103109

Please sign in to comment.