-
Notifications
You must be signed in to change notification settings - Fork 5
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 #467 from sarcophagus-org/rewrap-swap
Rewrap swap
- Loading branch information
Showing
7 changed files
with
196 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Checkbox, Flex, Text } from '@chakra-ui/react'; | ||
import { sarco } from '@sarcophagus-org/sarcophagus-v2-sdk-client'; | ||
import React from 'react'; | ||
import { useDispatch, useSelector } from '../../store'; | ||
import { toggleIsBuyingSarco } from '../../store/embalm/actions'; | ||
import { useNetworkConfig } from '../../lib/config'; | ||
import { BigNumber } from 'ethers'; | ||
|
||
interface SwapInfoProps { | ||
sarcoQuoteError: string; | ||
sarcoQuoteETHAmount: string; | ||
sarcoDeficit: BigNumber; | ||
balance: BigNumber | undefined; | ||
totalFeesWithBuffer: BigNumber; | ||
isRewrap?: boolean; | ||
} | ||
|
||
export const SwapInfo = ({ | ||
sarcoQuoteError, | ||
sarcoQuoteETHAmount, | ||
sarcoDeficit, | ||
balance, | ||
totalFeesWithBuffer, | ||
isRewrap, | ||
}: SwapInfoProps) => { | ||
const { isBuyingSarco } = useSelector(s => s.embalmState); | ||
const dispatch = useDispatch(); | ||
const networkConfig = useNetworkConfig(); | ||
function handleChangeBuySarcoChecked() { | ||
dispatch(toggleIsBuyingSarco()); | ||
} | ||
|
||
return ( | ||
<Flex flexDirection="column"> | ||
<Checkbox | ||
defaultChecked | ||
isChecked={isBuyingSarco} | ||
onChange={handleChangeBuySarcoChecked} | ||
> | ||
<Text>Swap {networkConfig.tokenSymbol} for SARCO</Text> | ||
</Checkbox> | ||
<Text | ||
mt={3} | ||
variant="secondary" | ||
> | ||
{isBuyingSarco | ||
? sarcoQuoteError | ||
? `There was a problem getting a SARCO quote: ${sarcoQuoteError}` | ||
: `${sarco.utils.formatSarco(sarcoQuoteETHAmount, 18)} ${ | ||
networkConfig.tokenSymbol | ||
} will be swapped for ${sarco.utils.formatSarco( | ||
sarcoDeficit.toString() | ||
)} SARCO before the sarcophagus is ${isRewrap ? 'rewrapped' : 'created'}.` | ||
: `Your current SARCO balance is ${sarco.utils.formatSarco( | ||
balance ? balance.toString() : '0' | ||
)} SARCO, but required balance is ${sarco.utils.formatSarco( | ||
totalFeesWithBuffer.toString() | ||
)} SARCO. You can check the box to automatically swap ${ | ||
networkConfig.tokenSymbol | ||
} to purchase the required balance during the ${ | ||
isRewrap ? 'rewrapp' : 'creation' | ||
} process.`} | ||
</Text> | ||
</Flex> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,38 +1,42 @@ | ||
import { sarco } from '@sarcophagus-org/sarcophagus-v2-sdk-client'; | ||
import { BigNumber } from 'ethers'; | ||
import { useEffect, useState } from 'react'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { useSelector } from 'store/index'; | ||
|
||
export function useSarcoQuote(amount: BigNumber) { | ||
const [sarcoQuoteETHAmount, setSarcoQuoteETHAmount] = useState('0'); | ||
const [sarcoQuoteError, setSarcoQuoteError] = useState(''); | ||
const [isPolling, setIsPolling] = useState(false); | ||
const isPollingRef = useRef(false); | ||
const [quoteIntervalState, setSarcoQuoteInterval] = useState<NodeJS.Timer>(); | ||
|
||
const { sarcoQuoteInterval } = useSelector(x => x.embalmState); | ||
|
||
useEffect(() => { | ||
async function getQuote() { | ||
if (sarcoQuoteInterval || isPolling || amount.lte(0)) return; | ||
if (isPollingRef.current || amount.lte(0)) return; | ||
|
||
setIsPolling(true); | ||
isPollingRef.current = true; | ||
|
||
const runGetQuote = async () => { | ||
try { | ||
const quote = await sarco.utils.getSarcoQuote(amount); | ||
setSarcoQuoteETHAmount(quote.sellAmount); | ||
} catch (e: any) { | ||
setSarcoQuoteError(e.message); | ||
} finally { | ||
isPollingRef.current = false; | ||
} | ||
}; | ||
|
||
runGetQuote(); | ||
|
||
const quoteInterval = setInterval(() => runGetQuote(), 100_000_000); // Temp set very high to avoid rate limits | ||
setSarcoQuoteInterval(quoteInterval); | ||
|
||
return () => clearInterval(quoteInterval); | ||
} | ||
getQuote(); | ||
}, [amount, isPolling, sarcoQuoteInterval]); | ||
}, [amount, sarcoQuoteInterval]); | ||
|
||
return { sarcoQuoteETHAmount, sarcoQuoteError, sarcoQuoteInterval: quoteIntervalState }; | ||
} |
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.