-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
888: Refactor of useHistoricalTransactions
- Loading branch information
piersss
committed
Mar 29, 2024
1 parent
efd735d
commit efab39c
Showing
19 changed files
with
424 additions
and
443 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { OrderERC20, Signature } from "@airswap/utils"; | ||
import { FullSwapERC20 } from "@airswap/utils/build/src/swap-erc20"; | ||
|
||
export const transformFullSwapERC20ToOrderERC20 = ( | ||
swap: FullSwapERC20, | ||
nonce: string, | ||
signature: Signature = { v: "", r: "", s: "" } | ||
): OrderERC20 => { | ||
return { | ||
nonce, | ||
expiry: Math.round(+nonce / 1000).toString(), | ||
signerWallet: swap.signerWallet, | ||
signerToken: swap.signerToken, | ||
signerAmount: swap.signerAmount, | ||
senderToken: swap.senderToken, | ||
senderAmount: swap.senderAmount, | ||
...signature, | ||
}; | ||
}; |
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
104 changes: 104 additions & 0 deletions
104
src/features/transactions/hooks/useHistoricalTransactions.ts
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,104 @@ | ||
import { useMemo, useState } from "react"; | ||
|
||
import { useAppSelector } from "../../../app/hooks"; | ||
import { SubmittedTransaction } from "../../../entities/SubmittedTransaction/SubmittedTransaction"; | ||
import { sortSubmittedTransactionsByExpiry } from "../../../entities/SubmittedTransaction/SubmittedTransactionHelpers"; | ||
import { transformToSubmittedRFQOrder } from "../../../entities/SubmittedTransaction/SubmittedTransactionTransformers"; | ||
import { getUniqueArrayChildren } from "../../../helpers/array"; | ||
import { getOrdersFromLogs } from "../../../helpers/getOrdersFromLogs"; | ||
import { selectAllTokenInfo } from "../../metadata/metadataSlice"; | ||
import useSwapLogs from "./useSwapLogs"; | ||
|
||
interface HistoricalTransactionsCollection { | ||
chainId: number; | ||
account: string; | ||
transactions: SubmittedTransaction[]; | ||
} | ||
|
||
const useHistoricalTransactions = ( | ||
chainId?: number, | ||
account?: string | ||
): [HistoricalTransactionsCollection | undefined, boolean] => { | ||
const tokens = useAppSelector(selectAllTokenInfo); | ||
const { result: swapLogs, status: swapLogStatus } = useSwapLogs( | ||
chainId, | ||
account | ||
); | ||
|
||
const [isLoading, setIsLoading] = useState(false); | ||
const [transactions, setTransactions] = | ||
useState<HistoricalTransactionsCollection>(); | ||
|
||
useMemo(() => { | ||
if ( | ||
!chainId || | ||
!swapLogs || | ||
swapLogStatus === "loading" || | ||
swapLogStatus === "not-executed" || | ||
swapLogs.chainId !== chainId || | ||
swapLogs.account !== account | ||
) { | ||
return; | ||
} | ||
|
||
setIsLoading(true); | ||
setTransactions(undefined); | ||
|
||
const getTransactionsFromLogs = async () => { | ||
const rfqOrders = await getOrdersFromLogs(chainId, swapLogs.swapLogs); | ||
// TODO: Add support for lastLook orders https://github.com/airswap/airswap-web/issues/891 | ||
// const lastLookOrders = await getOrdersFromLogs(swapLogs.swapLogs); | ||
|
||
const rfqSubmittedTransactions = rfqOrders | ||
.filter( | ||
(order) => | ||
order.params.signerWallet.toLowerCase() === account.toLowerCase() || | ||
order.senderWallet.toLowerCase() === account.toLowerCase() | ||
) | ||
.map((order) => { | ||
const signerToken = tokens.find( | ||
(token) => token.address === order.params.signerToken | ||
); | ||
const senderToken = tokens.find( | ||
(token) => token.address === order.params.senderToken | ||
); | ||
|
||
if (!signerToken || !senderToken) return; | ||
|
||
return transformToSubmittedRFQOrder( | ||
order.hash, | ||
order.params, | ||
signerToken, | ||
senderToken, | ||
"succeeded", | ||
order.timestamp | ||
); | ||
}); | ||
|
||
const transactions = rfqSubmittedTransactions.filter( | ||
(order) => !!order | ||
) as SubmittedTransaction[]; | ||
const uniqueTransactions = getUniqueArrayChildren<SubmittedTransaction>( | ||
transactions, | ||
"nonce" | ||
); | ||
|
||
const sortedTransactions = uniqueTransactions.sort( | ||
sortSubmittedTransactionsByExpiry | ||
); | ||
|
||
setIsLoading(false); | ||
setTransactions({ | ||
chainId, | ||
account, | ||
transactions: sortedTransactions, | ||
}); | ||
}; | ||
|
||
getTransactionsFromLogs(); | ||
}, [swapLogs, swapLogStatus]); | ||
|
||
return [transactions, isLoading]; | ||
}; | ||
|
||
export default useHistoricalTransactions; |
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,92 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
import { SwapERC20, Wrapper } from "@airswap/libraries"; | ||
import { Contract } from "@ethersproject/contracts"; | ||
import { useAsync } from "@react-hookz/web/esm"; | ||
import { IAsyncState } from "@react-hookz/web/esm/useAsync/useAsync"; | ||
import { useWeb3React } from "@web3-react/core"; | ||
|
||
import { Event } from "ethers"; | ||
import { providers } from "ethers"; | ||
|
||
import getContractEvents from "../../../helpers/getContractEvents"; | ||
|
||
interface SwapLogs { | ||
swapLogs: Event[]; | ||
wrappedSwapLogs: Event[]; | ||
chainId: number; | ||
account: string; | ||
} | ||
|
||
const useSwapLogs = ( | ||
chainId?: number, | ||
account?: string | ||
): IAsyncState<SwapLogs | null> => { | ||
const { library: provider } = useWeb3React<providers.Provider>(); | ||
|
||
const [accountState, setAccountState] = useState<string>(); | ||
const [chainIdState, setChainIdState] = useState<number>(); | ||
|
||
const [state, actions] = useAsync( | ||
async ( | ||
swapContract: Contract, | ||
wrapperContract: Contract, | ||
account: string | ||
) => { | ||
const signerSwapFilter = swapContract.filters.SwapERC20(null); | ||
const wrapperSwapFilter = wrapperContract.filters.WrappedSwapFor(null); | ||
|
||
const firstTxBlockSwapContract = | ||
chainId && SwapERC20.deployedBlocks[chainId]; | ||
const firstTxBlockWrapperContract = | ||
chainId && Wrapper.deployedBlocks[chainId]; | ||
const currentBlock = await provider?.getBlockNumber(); | ||
|
||
if ( | ||
!firstTxBlockSwapContract || | ||
!firstTxBlockWrapperContract || | ||
!currentBlock | ||
) { | ||
throw new Error("Could not get block numbers"); | ||
} | ||
|
||
const swapLogs = await getContractEvents( | ||
swapContract, | ||
signerSwapFilter, | ||
firstTxBlockSwapContract, | ||
currentBlock | ||
); | ||
const wrappedSwapLogs = await getContractEvents( | ||
wrapperContract, | ||
wrapperSwapFilter, | ||
firstTxBlockWrapperContract, | ||
currentBlock | ||
); | ||
|
||
return { | ||
swapLogs, | ||
wrappedSwapLogs, | ||
chainId, | ||
account, | ||
}; | ||
}, | ||
null | ||
); | ||
|
||
useEffect(() => { | ||
if (!chainId || !account || !provider) return; | ||
|
||
if (account === accountState && chainId === chainIdState) return; | ||
|
||
const swapContract = SwapERC20.getContract(provider, chainId); | ||
const wrapperContract = Wrapper.getContract(provider, chainId); | ||
actions.execute(swapContract, wrapperContract, account); | ||
|
||
setAccountState(account); | ||
setChainIdState(chainId); | ||
}, [chainId, account, provider, actions]); | ||
|
||
return state; | ||
}; | ||
|
||
export default useSwapLogs; |
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.