Skip to content

Commit

Permalink
pass additional tx context to wallet API
Browse files Browse the repository at this point in the history
  • Loading branch information
qrtp committed Nov 13, 2024
1 parent 3c467a8 commit 924fd5a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 259 deletions.
1 change: 1 addition & 0 deletions packages/ui-components/src/actions/fireBlocksActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export const createTransactionOperation = async (
destinationAddress: tx.to,
data: tx.data,
value: tx.value,
gas: tx.gas,
}),
},
);
Expand Down
69 changes: 0 additions & 69 deletions packages/ui-components/src/actions/swingActionsV1.ts

This file was deleted.

77 changes: 40 additions & 37 deletions packages/ui-components/src/components/Wallet/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import Slider from '@mui/material/Slider';
import Typography from '@mui/material/Typography';
import type {Theme} from '@mui/material/styles';
import cloneDeep from 'lodash/cloneDeep';
import Markdown from 'markdown-to-jsx';
import numeral from 'numeral';
import React, {useEffect, useRef, useState} from 'react';
import Animation from 'react-canvas-confetti/dist/presets/fireworks';
Expand Down Expand Up @@ -187,12 +186,8 @@ const Swap: React.FC<Props> = ({
750,
);
const [sourceToken, setSourceToken] = useState<SwapToken>();
const [sourceTokenDescription, setSourceTokenDescription] =
useState<string>();
const [destinationTokenAmountUsd, setDestinationTokenAmountUsd] = useState(0);
const [destinationToken, setDestinationToken] = useState<SwapToken>();
const [destinationTokenDescription, setDestinationTokenDescription] =
useState<string>();

// quote state
const [quoteRequest, setQuoteRequest] = useState<SwingV2QuoteRequest>();
Expand Down Expand Up @@ -334,6 +329,10 @@ const Swap: React.FC<Props> = ({
? getTokenEntry(sourceToken)!.value <
sourceTokenAmountUsd + getSourceGasFees(quoteSelected)
: false;
const isFundingPossible =
quoteSelected && sourceToken && getTokenEntry(sourceToken)
? getTokenEntry(sourceToken)!.value > getSourceGasFees(quoteSelected)
: false;

// build list of supported source tokens with sufficient balance
const sourceTokens: SwapToken[] =
Expand Down Expand Up @@ -436,6 +435,19 @@ const Swap: React.FC<Props> = ({
setDestinationToken(destinationTokens[0]);
}, [sourceToken, destinationToken, destinationTokens]);

// update the destination token if it conflicts with source token
useEffect(() => {
if (!sourceToken || !destinationToken) {
return;
}
if (
`${sourceToken.swing.chain}/${sourceToken.swing.symbol}` ===
`${destinationToken.swing.chain}/${destinationToken.swing.symbol}`
) {
setDestinationToken(destinationTokens[0]);
}
}, [sourceToken]);

// retrieve quote when a swap pair is selected
useEffect(() => {
if (!sourceTokenAmountUsdDebounced || !sourceToken || !destinationToken) {
Expand Down Expand Up @@ -468,8 +480,20 @@ const Swap: React.FC<Props> = ({
}
if (isCheapestQuote(quoteSelected)) {
setQuoteType('fastest');
const v = parseFloat(
numeral(quoteFastest?.quote.amountUSD || '0').format('0.00'),
);
if (v) {
setDestinationTokenAmountUsd(v);
}
} else {
setQuoteType('cheapest');
const v = parseFloat(
numeral(quoteCheapest?.quote.amountUSD || '0').format('0.00'),
);
if (v) {
setDestinationTokenAmountUsd(v);
}
}
};

Expand All @@ -492,11 +516,8 @@ const Swap: React.FC<Props> = ({
setIsTxComplete(false);
setTxId(undefined);
setQuotes(undefined);
setQuoteType('cheapest');
setErrorMessage(undefined);
setDestinationTokenAmountUsd(0);
setSourceTokenDescription(undefined);
setDestinationTokenDescription(undefined);

// optional items to clear
if (opts?.sourceAmtUsd) {
Expand Down Expand Up @@ -592,8 +613,6 @@ const Swap: React.FC<Props> = ({
// reset state
setIsGettingQuote(true);
setErrorMessage(undefined);
setSourceTokenDescription(undefined);
setDestinationTokenDescription(undefined);

// retrieve swap token definitions
try {
Expand Down Expand Up @@ -664,23 +683,9 @@ const Swap: React.FC<Props> = ({
setQuotes(quotesResponse.routes);

// set quote amounts for each token
setSourceTokenDescription(
`${t('swap.pay')} **${new Intl.NumberFormat('en-US', {
maximumSignificantDigits: 4,
}).format(sourceAmt)}** ${getBlockchainDisplaySymbol(
sourceToken.swing.symbol,
)}`,
);
const destinationTokenBalance =
parseFloat(quotesResponse.routes[0].quote.amount) /
Math.pow(10, quotesResponse.routes[0].quote.decimals);
setDestinationTokenDescription(
`${t('swap.receive')} **${new Intl.NumberFormat('en-US', {
maximumSignificantDigits: 4,
}).format(destinationTokenBalance)}** ${getBlockchainDisplaySymbol(
destinationToken.swing.symbol,
)}`,
);
const destinationUsd =
quotesResponse.routes[0].quote.amountUSD &&
parseFloat(quotesResponse.routes[0].quote.amountUSD)
Expand Down Expand Up @@ -746,6 +751,7 @@ const Swap: React.FC<Props> = ({
to: txResponse.tx.to,
data: txResponse.tx.data,
value: txResponse.tx.value,
gas: txResponse.tx.gas,
};
const operationResponse = await createTransactionOperation(
accessToken,
Expand Down Expand Up @@ -1068,11 +1074,6 @@ const Swap: React.FC<Props> = ({
</Select>
}
/>
<FormHelperText className={classes.tokenBalanceContainer}>
<Box className={classes.tokenActionText}>
<Markdown>{destinationTokenDescription || ''}</Markdown>
</Box>
</FormHelperText>
</FormControl>
</Box>
)}
Expand Down Expand Up @@ -1134,14 +1135,16 @@ const Swap: React.FC<Props> = ({
{!isButtonHidden && quoteSelected && (
<Box className={classes.content}>
{isInsufficientFunds ? (
<Button
fullWidth
size="small"
variant="text"
onClick={handleUseMax}
>
{t('swap.useMax')}
</Button>
isFundingPossible && (
<Button
fullWidth
size="small"
variant="text"
onClick={handleUseMax}
>
{t('swap.useMax')}
</Button>
)
) : isMultipleQuotes() ? (
<Button
fullWidth
Expand Down
1 change: 1 addition & 0 deletions packages/ui-components/src/lib/types/fireBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface CreateTransaction {
to: string;
data: string;
value?: string;
gas?: string;
}

export const EIP_712_KEY = 'EIP712Domain';
Expand Down
Loading

0 comments on commit 924fd5a

Please sign in to comment.