-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finished first versions of Stake and Swap
- Loading branch information
1 parent
109ad1c
commit deedbbe
Showing
14 changed files
with
455 additions
and
207 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 was deleted.
Oops, something went wrong.
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,163 @@ | ||
import * as React from 'react' | ||
import { Box, Button, Text, TextInput } from 'grommet' | ||
|
||
import * as t from './types' | ||
|
||
export const SwapInput: React.FC<t.SwapInputPropsType> = ({ | ||
value, | ||
maxValue, | ||
swapQuote, | ||
onChange, | ||
onChangeCurrency, | ||
}) => { | ||
const [currency, setCurrency] = React.useState<t.CurrencyTypeEnum>( | ||
t.CurrencyTypeEnum.NEAR | ||
) | ||
const handleChangeCurrency = () => { | ||
const newCurrency = | ||
currency === t.CurrencyTypeEnum.NEAR | ||
? t.CurrencyTypeEnum.QUID | ||
: t.CurrencyTypeEnum.NEAR | ||
setCurrency(newCurrency) | ||
onChangeCurrency && onChangeCurrency(newCurrency) | ||
} | ||
|
||
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">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> | ||
<Button | ||
label="max" | ||
alignSelf="end" | ||
size="small" | ||
onClick={() => { | ||
onChange(maxValue) | ||
}} | ||
/> | ||
</Box> | ||
</Box> | ||
) : null} | ||
<Box | ||
margin="5px auto 0 auto" | ||
border={{ size: 'xsmall' }} | ||
width="90%" | ||
flex | ||
direction="column" | ||
round="small" | ||
pad="small" | ||
background="background-back" | ||
> | ||
<Box width="100%" flex direction="row"> | ||
<TextInput | ||
plain={true} | ||
focusIndicator={false} | ||
type="number" | ||
name="withdraw" | ||
step="0.1" | ||
placeholder="0.000" | ||
size="xxlarge" | ||
textAlign="start" | ||
min={0} | ||
value={value} | ||
onChange={(e) => { | ||
onChange(e?.target?.value) | ||
}} | ||
/> | ||
<Button | ||
primary | ||
plain={false} | ||
alignSelf="end" | ||
margin="auto 0" | ||
label={currency} | ||
onClick={handleChangeCurrency} | ||
/> | ||
</Box> | ||
</Box> | ||
<Box | ||
width="90%" | ||
margin="20px auto 0 auto" | ||
gap="small" | ||
pad="none" | ||
align="center" | ||
justify="between" | ||
direction="row" | ||
> | ||
<Text alignSelf="start">Receive: (approximately)</Text> | ||
</Box> | ||
<Box | ||
margin="5px auto 0 auto" | ||
border={{ size: 'xsmall' }} | ||
width="90%" | ||
flex | ||
direction="column" | ||
round="small" | ||
pad="small" | ||
background="background-back" | ||
> | ||
<Box width="100%" flex direction="row"> | ||
<TextInput | ||
plain={true} | ||
focusIndicator={false} | ||
type="number" | ||
name="withdraw" | ||
step="0.1" | ||
placeholder="0.000" | ||
size="xxlarge" | ||
textAlign="start" | ||
min={0} | ||
value={ | ||
swapQuote && value ? String(Number(value) * swapQuote) : undefined | ||
} | ||
onChange={(e) => { | ||
onChange(String(Number(e?.target?.value) / (swapQuote || 1))) | ||
}} | ||
/> | ||
<Button | ||
primary | ||
plain={false} | ||
alignSelf="end" | ||
margin="auto 0" | ||
// size="small" | ||
// style={{ cursor: 'pointer' }} | ||
label={ | ||
currency === t.CurrencyTypeEnum.NEAR | ||
? t.CurrencyTypeEnum.QUID | ||
: t.CurrencyTypeEnum.NEAR | ||
} | ||
onClick={handleChangeCurrency} | ||
/> | ||
</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> | ||
) | ||
} |
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,12 @@ | ||
export interface SwapInputPropsType { | ||
value?: string | ||
onChange: (value: string) => void | ||
onChangeCurrency?: (e: string) => void | ||
maxValue?: string | ||
swapQuote?: number | ||
} | ||
|
||
export enum CurrencyTypeEnum { | ||
NEAR = 'Near', | ||
QUID = 'qUid', | ||
} |
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,52 @@ | ||
import { useState, useEffect, useContext } from 'react' | ||
import { utils } from 'near-api-js' | ||
|
||
import { NearContext } from 'src/near/nearContext' | ||
|
||
// Todo: replace by redux-toolkit query | ||
export const useGetBalance = (): { | ||
quidBalance: string | ||
nearBalance: string | ||
isLoading: boolean | ||
refetch: () => void | ||
} => { | ||
const { contract, currentUser, walletConnection } = useContext(NearContext) | ||
const [quidBalance, setQuidBalance] = useState<string>('') | ||
const [nearBalance, setNearBalance] = useState<string>('') | ||
const [isLoading, setIsLoading] = useState<boolean>(true) | ||
|
||
const fetchQuidBalance = async () => { | ||
try { | ||
setIsLoading(true) | ||
// Get qUid balance | ||
const quidRes = await walletConnection | ||
?.account() | ||
.viewFunction(contract?.contractId || '', 'ft_balance_of', { | ||
account_id: currentUser?.accountId, | ||
}) | ||
const newQuidBalance = utils.format.formatNearAmount(quidRes) | ||
setQuidBalance(Number(newQuidBalance).toFixed(3)) | ||
// Get Near balance | ||
const newNearBalance = utils.format.formatNearAmount( | ||
currentUser?.balance || '0' | ||
) | ||
setNearBalance(Number(newNearBalance).toFixed(3)) | ||
} catch (e) { | ||
// Todo: add a toaster warning of errors | ||
console.error(e) | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
fetchQuidBalance() | ||
}, [contract]) | ||
|
||
return { | ||
quidBalance, | ||
nearBalance, | ||
isLoading, | ||
refetch: fetchQuidBalance, | ||
} | ||
} |
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,63 @@ | ||
import { useState, useEffect, useContext } from 'react' | ||
import { utils } from 'near-api-js' | ||
|
||
import { NearContext } from 'src/near/nearContext' | ||
|
||
export interface StatsType { | ||
nearSpTotal: string | ||
quidSpTotal: string | ||
nearSpStaked: string | ||
quidSpStaked: string | ||
} | ||
// Todo: replace by redux-toolkit query | ||
export const useGetStats = (): { | ||
stats: StatsType | null | ||
isLoading: boolean | ||
refetch: () => void | ||
} => { | ||
const { contract, currentUser } = useContext(NearContext) | ||
const [stats, setStats] = useState<StatsType | null>(null) | ||
const [isLoading, setIsLoading] = useState<boolean>(true) | ||
|
||
const getPledged = async () => { | ||
return contract?.get_pledge({ | ||
account: currentUser?.accountId, | ||
}) | ||
} | ||
const getStats = async () => { | ||
return contract?.get_pool_stats({}) | ||
} | ||
|
||
const fetchStats = async () => { | ||
try { | ||
setIsLoading(true) | ||
const newStats = await getStats() | ||
const newPledged = await getPledged() | ||
setStats({ | ||
nearSpTotal: utils.format.formatNearAmount( | ||
newStats?.blood_debit || '0' | ||
), | ||
quidSpTotal: utils.format.formatNearAmount( | ||
newStats?.blood_credit || '0' | ||
), | ||
nearSpStaked: utils.format.formatNearAmount(newPledged?.near_sp || '0'), | ||
quidSpStaked: utils.format.formatNearAmount(newPledged?.quid_sp || '0'), | ||
}) | ||
} catch (e) { | ||
// Todo: add a toaster warning of errors | ||
console.error(e) | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
fetchStats() | ||
}, [contract]) | ||
|
||
return { | ||
stats, | ||
isLoading, | ||
refetch: fetchStats, | ||
} | ||
} |
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
Oops, something went wrong.