Skip to content

Commit

Permalink
Merge pull request #467 from sarcophagus-org/rewrap-swap
Browse files Browse the repository at this point in the history
Rewrap swap
  • Loading branch information
sethhrbek authored Jan 9, 2024
2 parents 8dd88a0 + 367231b commit 5868c7b
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 109 deletions.
66 changes: 66 additions & 0 deletions src/components/SwapUX/SwapInfo.tsx
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>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export function RecoverPublicKey() {
const ErrorStatusMap: { [key: number]: IErrorStatusMap } = {
[RecoverPublicKeyErrorStatus.INVALID_ADDRESS]: {
alertStatus: 'warning',
alertMessage: 'Address is not a value Etherum address.',
alertMessage: 'Address is not a value Ethereum address.',
},
[RecoverPublicKeyErrorStatus.CANNOT_RECOVER]: {
alertStatus: 'warning',
alertMessage: 'This address has no transactions in which to recover the pubilc key.',
alertMessage: 'This address has no transactions in which to recover the public key.',
},
[RecoverPublicKeyErrorStatus.ERROR]: {
alertStatus: 'error',
Expand Down
14 changes: 9 additions & 5 deletions src/features/embalm/stepContent/hooks/useSarcoQuote.ts
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 };
}
48 changes: 9 additions & 39 deletions src/features/embalm/stepContent/steps/CreateSarcophagus.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Button, Center, Checkbox, Flex, Heading, Spinner, Text } from '@chakra-ui/react';
import { sarco } from '@sarcophagus-org/sarcophagus-v2-sdk-client';
import { Button, Center, Flex, Heading, Spinner, Text } from '@chakra-ui/react';
import { RetryCreateModal } from 'components/RetryCreateModal';
import { BigNumber } from 'ethers';
import { useSarcoBalance } from 'hooks/sarcoToken/useSarcoBalance';
Expand All @@ -14,7 +13,6 @@ import {
setArchaeologists,
setCancelToken,
setSarcoQuoteInterval,
toggleIsBuyingSarco,
} from '../../../../store/embalm/actions';
import { Step } from '../../../../store/embalm/reducer';
import { PageBlockModal } from '../components/PageBlockModal';
Expand All @@ -32,7 +30,7 @@ import { useSarcoFees } from '../hooks/useSarcoFees';
import { useSarcoQuote } from '../hooks/useSarcoQuote';
import { useSarcophagusParameters } from '../hooks/useSarcophagusParameters';
import { CreateSarcophagusStage, defaultCreateSarcophagusStages } from '../utils/createSarcophagus';
import { useNetworkConfig } from '../../../../lib/config';
import { SwapInfo } from '../../../../components/SwapUX/SwapInfo';

// TODO -- remove need for this, see RetryCreateModal reference
const SHOW_RETRY_CREATE_MODAL = false;
Expand All @@ -45,7 +43,6 @@ export function CreateSarcophagus() {
const [createSarcophagusStages, setCreateSarcophagusStages] = useState<Record<number, string>>(
defaultCreateSarcophagusStages
);
const networkConfig = useNetworkConfig();

const { archaeologists } = useSelector(x => x.embalmState);

Expand Down Expand Up @@ -160,10 +157,6 @@ export function CreateSarcophagus() {
}, 10);
}

function handleChangeBuySarcoChecked() {
dispatch(toggleIsBuyingSarco());
}

if (areFeesLoading || feesError) {
return (
<Center
Expand Down Expand Up @@ -199,36 +192,13 @@ export function CreateSarcophagus() {
pt={6}
>
{sarcoDeficit.gt(0) && (
<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 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 creation process.`}
</Text>
</Flex>
<SwapInfo
sarcoQuoteError={sarcoQuoteError}
sarcoQuoteETHAmount={sarcoQuoteETHAmount}
sarcoDeficit={sarcoDeficit}
balance={balance}
totalFeesWithBuffer={totalFeesWithBuffer}
/>
)}
<Button
w="full"
Expand Down
Loading

0 comments on commit 5868c7b

Please sign in to comment.