Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

893: Add FullSwapERC20 to order transactions #897

Merged
3 commits merged into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/@widgets/SwapWidget/SwapWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { Registry, Server, Wrapper } from "@airswap/libraries";
import { OrderERC20, Pricing, ProtocolIds, ADDRESS_ZERO } from "@airswap/utils";
import { Web3Provider } from "@ethersproject/providers";
import { useToggle } from "@react-hookz/web";
import { unwrapResult } from "@reduxjs/toolkit";
import { UnsupportedChainIdError, useWeb3React } from "@web3-react/core";

import { useAppDispatch, useAppSelector } from "../../../app/hooks";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ const useBestTradeOptionTransaction = (

const senderAmount = toAtomicString(quoteAmount, tokenInfo.decimals);

return (
transaction.order.nonce === nonce ||
transaction.order.senderAmount === senderAmount
);
return transaction.order.nonce
? transaction.order.nonce === nonce
: transaction.order.senderAmount === senderAmount;
});
}, [transactions, nonce, quoteAmount]);
};
Expand Down
14 changes: 8 additions & 6 deletions src/components/TransactionsTab/TransactionsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import AnimatedWalletTransaction from "./subcomponents/AnimatedWalletTransaction
import ClearTransactionsSelector from "./subcomponents/ClearTransactionsSelector/ClearTransactionsSelector";

type TransactionsTabType = {
address: string;
account: string;
chainId: number;
open: boolean;
protocolFee: number;
Expand All @@ -67,7 +67,7 @@ type TransactionsTabType = {
};

const TransactionsTab = ({
address = "",
account = "",
chainId,
open,
protocolFee,
Expand All @@ -92,7 +92,7 @@ const TransactionsTab = ({
const transactionsScrollRef = useRef<HTMLDivElement>(null);
const buttonRef = useRef<HTMLDivElement>(null);

const addressOrName = useAddressOrEnsName(address);
const addressOrName = useAddressOrEnsName(account);
const walletInfoText = useMemo(() => {
return isUnsupportedNetwork
? t("wallet.unsupported")
Expand All @@ -101,8 +101,8 @@ const TransactionsTab = ({
: t("wallet.notConnected");
}, [addressOrName, isUnsupportedNetwork, t]);
const walletUrl = useMemo(
() => getAccountUrl(chainId, address),
[chainId, address]
() => getAccountUrl(chainId, account),
[chainId, account]
);
useKeyPress(() => setTransactionsTabOpen(false), ["Escape"]);

Expand Down Expand Up @@ -205,7 +205,7 @@ const TransactionsTab = ({
{showMobileMenu && (
<StyledWalletMobileMenu
walletUrl={walletUrl}
address={address}
address={account}
onDisconnectButtonClick={onDisconnectWalletClicked}
/>
)}
Expand All @@ -230,6 +230,7 @@ const TransactionsTab = ({
protocolFee={protocolFee}
transaction={transaction}
chainId={chainId!}
account={account}
/>
))}
</AnimatePresence>
Expand All @@ -248,6 +249,7 @@ const TransactionsTab = ({
protocolFee={protocolFee}
transaction={transaction}
chainId={chainId!}
account={account}
/>
))}
</AnimatePresence>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { TokenInfo } from "@airswap/utils";
import { formatUnits } from "@ethersproject/units";

import { BigNumber } from "bignumber.js";

import {
SubmittedDepositTransaction,
SubmittedOrder,
SubmittedTransaction,
SubmittedWithdrawTransaction,
} from "../../../entities/SubmittedTransaction/SubmittedTransaction";
import {
getDepositOrWithdrawalTransactionLabel,
getOrderTransactionLabel,
isDepositTransaction,
isLastLookOrderTransaction,
isSubmittedOrder,
isWithdrawTransaction,
} from "../../../entities/SubmittedTransaction/SubmittedTransactionHelpers";
import { compareAddresses } from "../../../helpers/string";
import i18n from "../../../i18n/i18n";

const getWalletTransactionOrderText = (
transaction:
| SubmittedOrder
| SubmittedWithdrawTransaction
| SubmittedDepositTransaction,
signerToken: TokenInfo,
senderToken: TokenInfo,
account: string,
protocolFee: number
): string => {
if (isWithdrawTransaction(transaction) || isDepositTransaction(transaction)) {
return getDepositOrWithdrawalTransactionLabel(
transaction,
signerToken,
senderToken
);
}

return getOrderTransactionLabel(
transaction,
signerToken,
senderToken,
account,
protocolFee
);
};

export default getWalletTransactionOrderText;
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useEffect, useMemo, useState } from "react";

import { TokenInfo } from "@airswap/utils";

import { useReducedMotion } from "framer-motion";
import { useTheme } from "styled-components";

Expand All @@ -14,12 +12,14 @@ interface AnimatedWalletTransactionProps {
protocolFee: number;
transaction: SubmittedTransaction;
chainId: number;
account: string;
}

const AnimatedWalletTransaction = ({
protocolFee,
transaction,
chainId,
account,
}: AnimatedWalletTransactionProps) => {
const theme = useTheme();
const shouldReduceMotion = useReducedMotion();
Expand Down Expand Up @@ -69,6 +69,7 @@ const AnimatedWalletTransaction = ({
}}
transaction={transaction}
chainId={chainId}
account={account}
/>
</Container>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { TransactionStatusType } from "../../../../types/transactionTypes";
import ProgressBar from "../../../ProgressBar/ProgressBar";
import getTimeAgoTranslation from "../../helpers/getTimeAgoTranslation";
import getWalletTransactionOrderText from "../../helpers/getWalletTransactionOrderText";
import getWalletTransactionStatusText from "../../helpers/getWalletTransactionStatusText";
import {
Container,
Expand All @@ -33,6 +34,7 @@ interface WalletTransactionProps extends HTMLMotionProps<"div"> {
protocolFee: number;
transaction: SubmittedTransaction;
chainId: number;
account: string;
}

const WalletTransaction = ({
Expand All @@ -42,6 +44,7 @@ const WalletTransaction = ({
animate,
initial,
transition,
account,
}: WalletTransactionProps) => {
const { t } = useTranslation();

Expand Down Expand Up @@ -102,7 +105,6 @@ const WalletTransaction = ({

if (
isSubmittedOrder(transaction) ||
isSubmittedOrderUnderConsideration(transaction) ||
isWithdrawTransaction(transaction) ||
isDepositTransaction(transaction)
) {
Expand All @@ -111,16 +113,6 @@ const WalletTransaction = ({
? transaction.order.expiry
: undefined;

// For last look transactions, the user has sent the signer amount plus
// the fee:
let signerAmountWithFee: string | null = null;
if (isLastLookOrderTransaction(transaction)) {
signerAmountWithFee = new BigNumber(transaction.order.signerAmount)
.multipliedBy(1 + protocolFee / 10000)
.integerValue(BigNumber.ROUND_FLOOR)
.toString();
}

const timeBetween = getTimeAgoTranslation(
new Date(transaction.timestamp),
t
Expand All @@ -140,30 +132,12 @@ const WalletTransaction = ({
transaction.status === TransactionStatusType.processing
}
>
{t(
isSubmittedOrder(transaction) && transaction.isLastLook
? "wallet.lastLookTransaction"
: "wallet.transaction",
{
senderAmount: parseFloat(
Number(
formatUnits(
transaction.order.senderAmount,
senderToken.decimals
)
).toFixed(5)
),
senderToken: senderToken.symbol,
signerAmount: parseFloat(
Number(
formatUnits(
signerAmountWithFee || transaction.order.signerAmount,
signerToken.decimals
)
).toFixed(5)
),
signerToken: signerToken.symbol,
}
{getWalletTransactionOrderText(
transaction,
signerToken,
senderToken,
account,
protocolFee
)}
</SpanTitle>
{!!expiry &&
Expand Down
3 changes: 2 additions & 1 deletion src/entities/SubmittedTransaction/SubmittedTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OrderERC20, TokenInfo } from "@airswap/utils";
import { FullSwapERC20, OrderERC20, TokenInfo } from "@airswap/utils";

import {
TransactionStatusType,
Expand Down Expand Up @@ -27,6 +27,7 @@ export interface SubmittedOrder extends SubmittedTransactionWithHash {
isLastLook?: boolean;
type: TransactionTypes.order;
order: OrderERC20;
swap?: FullSwapERC20;
senderToken: TokenInfo;
signerToken: TokenInfo;
}
Expand Down
103 changes: 101 additions & 2 deletions src/entities/SubmittedTransaction/SubmittedTransactionHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { TokenInfo } from "@airswap/utils";
import { formatUnits } from "@ethersproject/units";

import { BigNumber } from "bignumber.js";

import { compareAddresses } from "../../helpers/string";
import i18n from "../../i18n/i18n";
import { TransactionTypes } from "../../types/transactionTypes";
import {
SubmittedApprovalTransaction,
Expand Down Expand Up @@ -33,7 +40,7 @@ export const isWithdrawTransaction = (
export const isSubmittedOrder = (
transaction: SubmittedTransaction
): transaction is SubmittedOrder => {
return transaction.type === TransactionTypes.order && !!transaction.hash;
return transaction.type === TransactionTypes.order;
};

export const isSubmittedOrderUnderConsideration = (
Expand Down Expand Up @@ -69,7 +76,7 @@ export const getSubmittedTransactionKey = (
return transaction.hash;
};

export const doesTransactionsMatch = (
export const doTransactionsMatch = (
transaction: SubmittedTransaction,
match: SubmittedTransaction,
hash?: string
Expand All @@ -83,3 +90,95 @@ export const doesTransactionsMatch = (

return transaction.hash === match.hash || transaction.hash === hash;
};

export const getDepositOrWithdrawalTransactionLabel = (
transaction: SubmittedDepositTransaction | SubmittedWithdrawTransaction,
signerToken: TokenInfo,
senderToken: TokenInfo
): string => {
const signerAmount = parseFloat(
Number(
formatUnits(transaction.order.signerAmount, signerToken.decimals)
).toFixed(5)
);

const senderAmount = parseFloat(
Number(
formatUnits(transaction.order.senderAmount, senderToken.decimals)
).toFixed(5)
);

return i18n.t("wallet.transaction", {
signerAmount,
signerToken: signerToken.symbol,
senderAmount,
senderToken: senderToken.symbol,
});
};

const isSenderWalletAccount = (
transaction: SubmittedTransaction,
account: string
) => {
if (isSubmittedOrder(transaction) && transaction.swap) {
return compareAddresses(transaction.swap.senderWallet, account);
}

return false;
};

export const getOrderTransactionLabel = (
transaction: SubmittedOrder,
signerToken: TokenInfo,
senderToken: TokenInfo,
account: string,
protocolFee: number
) => {
const { order, swap } = transaction;

const signerAmountWithFee =
!swap && transaction.isLastLook
? new BigNumber(transaction.order.signerAmount)
.multipliedBy(1 + protocolFee / 10000)
.integerValue(BigNumber.ROUND_FLOOR)
.toString()
: undefined;

const translation =
isSubmittedOrder(transaction) && transaction.isLastLook
? "wallet.lastLookTransaction"
: "wallet.transaction";

const signerAmount = parseFloat(
Number(
formatUnits(
signerAmountWithFee || (swap || order).signerAmount,
signerToken.decimals
)
).toFixed(5)
);

const senderAmount = parseFloat(
Number(
formatUnits((swap || order).senderAmount, senderToken.decimals)
).toFixed(5)
);

const accountIsSender = isSenderWalletAccount(transaction, account);

if (accountIsSender) {
return i18n.t(translation, {
signerAmount,
signerToken: signerToken.symbol,
senderAmount,
senderToken: senderToken.symbol,
});
}

return i18n.t(translation, {
signerAmount: senderAmount,
signerToken: senderToken.symbol,
senderAmount: signerAmount,
senderToken: signerToken.symbol,
});
};
Loading
Loading