Skip to content

Commit

Permalink
Added getOrdersFromDelegatedSwapLogs helper
Browse files Browse the repository at this point in the history
  • Loading branch information
makkelie-dev committed Feb 5, 2025
1 parent 22644d8 commit d2adc79
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Delegate } from "@airswap/libraries";
import { FullSwapERC20 } from "@airswap/utils";

import { BigNumber } from "bignumber.js";
import { Event } from "ethers";

import { DelegatedSwapEvent } from "../../../entities/DelegateRule/DelegateRule";
import { transformToDelegatedSwapEvent } from "../../../entities/DelegateRule/DelegateRuleTransformers";
import { compareAddresses } from "../../../helpers/string";
import { FullSwapErc20Log } from "./getOrdersFromLogs";

/**
* Gets orders where the delegate contract is the sender wallet and transforms them
* to show the user's wallet as the sender when nonces match
*/

export const getOrdersFromDelegatedSwapLogs = (
account: string,
chainId: number,
orderLogs: FullSwapErc20Log[],
events: Event[]
): FullSwapErc20Log[] => {
const delegateAddress = Delegate.getAddress(chainId);

if (!delegateAddress) return [];

const delegatedSwapsEvents = events
.filter((event) => compareAddresses(event.args?.[0] as string, account))
.map((event) => {
const args = event.args || [];

const senderWallet = args[0] as string;
const signerWallet = args[1] as string;
const nonce = args[2] as BigNumber;

return transformToDelegatedSwapEvent(
senderWallet,
signerWallet,
nonce.toString(),
chainId,
event.transactionHash
);
});

const delegatedOrderLogs = orderLogs
.filter((log) => compareAddresses(log.swap.senderWallet, delegateAddress))
.filter((log) =>
delegatedSwapsEvents.some((event) => event.nonce === log.order.nonce)
)
.map((log) => {
const matchingEvent = delegatedSwapsEvents.find(
(event) => event.nonce === log.order.nonce
);

if (!matchingEvent) return;

return getModifiedOrderLog(log, matchingEvent);
})
.filter(Boolean);

return delegatedOrderLogs as FullSwapErc20Log[];
};

const getModifiedOrderLog = (
log: FullSwapErc20Log,
event: DelegatedSwapEvent
) => {
const modifiedSwap: FullSwapERC20 = {
...log.swap,
senderWallet: event.senderWallet,
};

return {
...log,
swap: modifiedSwap,
} as FullSwapErc20Log;
};
16 changes: 15 additions & 1 deletion src/features/transactions/hooks/useHistoricalTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useMemo, useState } from "react";

import { Delegate } from "@airswap/libraries";

import { useAppSelector } from "../../../app/hooks";
import { SubmittedTransaction } from "../../../entities/SubmittedTransaction/SubmittedTransaction";
import { sortSubmittedTransactionsByExpiry } from "../../../entities/SubmittedTransaction/SubmittedTransactionHelpers";
Expand All @@ -9,6 +11,7 @@ import { compareAddresses } from "../../../helpers/string";
import useNativeToken from "../../../hooks/useNativeToken";
import { TransactionStatusType } from "../../../types/transactionTypes";
import { selectAllTokenInfo } from "../../metadata/metadataSlice";
import { getOrdersFromDelegatedSwapLogs } from "../helpers/getOrdersFromDelegatedSwapLogs";
import { getOrdersFromLogs } from "../helpers/getOrdersFromLogs";
import { getOrdersFromWrappedEventLogs } from "../helpers/getOrdersFromWrappedEventLogs";
import useSwapLogs from "./useSwapLogs";
Expand Down Expand Up @@ -64,7 +67,18 @@ const useHistoricalTransactions = (): [
swapLogs.wrappedSwapLogs
);

const submittedTransactions = [...logs, ...wrappedLogs]
const delegatedSwapLogs = getOrdersFromDelegatedSwapLogs(
account,
chainId,
logs,
swapLogs.delegatedSwapLogs
);

const submittedTransactions = [
...logs,
...wrappedLogs,
...delegatedSwapLogs,
]
.filter(
(order) =>
compareAddresses(order.order.signerWallet, account) ||
Expand Down
27 changes: 24 additions & 3 deletions src/features/transactions/hooks/useSwapLogs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useState } from "react";

import { SwapERC20, Wrapper } from "@airswap/libraries";
import { Delegate, 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";
Expand All @@ -15,6 +15,7 @@ import useNetworkSupported from "../../../hooks/useNetworkSupported";
interface SwapLogs {
swapLogs: Event[];
wrappedSwapLogs: Event[];
delegatedSwapLogs: Event[];
chainId: number;
account: string;
}
Expand All @@ -33,20 +34,26 @@ const useSwapLogs = (
async (
swapContract: Contract,
wrapperContract: Contract,
delegatedSwapContract: Contract,
account: string
) => {
const signerSwapFilter = swapContract.filters.SwapERC20(null);
const wrapperSwapFilter = wrapperContract.filters.WrappedSwapFor(null);
const delegatedSwapFilter =
delegatedSwapContract.filters.DelegatedSwapFor(null);

const firstTxBlockSwapContract =
chainId && SwapERC20.deployedBlocks[chainId];
const firstTxBlockWrapperContract =
chainId && Wrapper.deployedBlocks[chainId];
const firstTxBlockDelegatedSwapContract =
chainId && SwapERC20.deployedBlocks[chainId];
const currentBlock = await provider?.getBlockNumber();

if (
!firstTxBlockSwapContract ||
!firstTxBlockWrapperContract ||
!firstTxBlockDelegatedSwapContract ||
!currentBlock
) {
throw new Error("Could not get block numbers");
Expand All @@ -65,9 +72,17 @@ const useSwapLogs = (
currentBlock
);

const delegatedSwapLogs = await getContractEvents(
delegatedSwapContract,
delegatedSwapFilter,
firstTxBlockDelegatedSwapContract,
currentBlock
);

return {
swapLogs,
wrappedSwapLogs,
delegatedSwapLogs,
chainId,
account,
};
Expand All @@ -80,9 +95,15 @@ const useSwapLogs = (

if (account === accountState && chainId === chainIdState) return;

const swapContract = getSwapErc20Contract(provider, chainId);
const swapContract = SwapERC20.getContract(provider, chainId);
const wrapperContract = Wrapper.getContract(provider, chainId);
actions.execute(swapContract, wrapperContract, account);
const delegatedSwapContract = Delegate.getContract(provider, chainId);
actions.execute(
swapContract,
wrapperContract,
delegatedSwapContract,
account
);

setAccountState(account);
setChainIdState(chainId);
Expand Down

0 comments on commit d2adc79

Please sign in to comment.