Skip to content

Commit

Permalink
Remove permit support and modify node ack logic (#1764)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchung authored Nov 12, 2024
1 parent e69ca7a commit e9d5705
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 31 deletions.
1 change: 0 additions & 1 deletion src/core/raps/actions/swap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ test('[rap/swap] :: should execute swap', async () => {
},
quote: quote as Quote,
wallet,
permit: false,
});

expect(swapTx?.hash).toBeDefined();
Expand Down
7 changes: 2 additions & 5 deletions src/core/raps/actions/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,13 @@ export const executeSwap = async ({
quote,
gasParams,
wallet,
permit = false,
}: {
chainId: ChainId;
gasLimit: string;
gasParams: TransactionGasParams | TransactionLegacyGasParams;
nonce?: number;
quote: Quote;
wallet: Signer;
permit: boolean;
}): Promise<Transaction | null> => {
if (!wallet || !quote) return null;

Expand Down Expand Up @@ -260,7 +258,7 @@ export const executeSwap = async ({
quote,
transactionParams,
wallet,
permit,
false,
chainId as unknown as SwapChainId,
REFERRER,
);
Expand All @@ -276,7 +274,7 @@ export const swap = async ({
}: ActionProps<'swap'>): Promise<RapActionResult> => {
const { selectedGas, gasFeeParamsBySpeed } = gasStore.getState();

const { quote, permit, chainId, requiresApprove } = parameters;
const { quote, chainId, requiresApprove } = parameters;
let gasParams = selectedGas.transactionGasParams;
// if swap isn't the last action, use fast gas or custom (whatever is faster)

Expand Down Expand Up @@ -312,7 +310,6 @@ export const swap = async ({
chainId,
gasLimit,
nonce,
permit: !!permit,
quote,
wallet,
};
Expand Down
23 changes: 16 additions & 7 deletions src/core/raps/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable no-promise-executor-return */
import { Signer } from '@ethersproject/abstract-signer';

import { ChainId } from '~/core/types/chains';
import { RainbowError, logger } from '~/logger';

import { claim, swap, unlock } from './actions';
Expand Down Expand Up @@ -111,9 +112,12 @@ function getRapFullName<T extends RapActionTypes>(actions: RapAction<T>[]) {

const delay = (ms: number) => new Promise((res) => setTimeout(res, ms));

const NODE_ACK_MAX_TRIES = 10;

const waitForNodeAck = async (
hash: string,
provider: Signer['provider'],
tries = 0,
): Promise<void> => {
return new Promise(async (resolve) => {
const tx = await provider?.getTransaction(hash);
Expand All @@ -125,8 +129,10 @@ const waitForNodeAck = async (
resolve();
} else {
// Wait for 1 second and try again
await delay(1000);
return waitForNodeAck(hash, provider);
if (tries < NODE_ACK_MAX_TRIES) {
await delay(1000);
return waitForNodeAck(hash, provider, tries + 1);
}
}
});
};
Expand Down Expand Up @@ -163,13 +169,16 @@ export const walletExecuteRap = async (
const {
baseNonce,
errorMessage: error,
hash,
hash: firstHash,
} = await executeAction(actionParams);
const shouldWaitForNodeAck = parameters.chainId !== ChainId.mainnet;

if (typeof baseNonce === 'number') {
actions.length > 1 &&
hash &&
(await waitForNodeAck(hash, wallet.provider));
let latestHash = firstHash;
for (let index = 1; index < actions.length; index++) {
latestHash &&
shouldWaitForNodeAck &&
(await waitForNodeAck(latestHash, wallet.provider));
const action = actions[index];
const actionParams = {
action,
Expand All @@ -181,7 +190,7 @@ export const walletExecuteRap = async (
flashbots: parameters?.flashbots,
};
const { hash } = await executeAction(actionParams);
hash && (await waitForNodeAck(hash, wallet.provider));
latestHash = hash;
if (index === actions.length - 1) {
txHash = hash;
}
Expand Down
1 change: 0 additions & 1 deletion src/core/raps/unlockAndCrosschainSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export const createUnlockAndCrosschainSwapRap = async (
// create a swap rap
const swap = createNewAction('crosschainSwap', {
chainId,
permit: swapAssetNeedsUnlocking,
requiresApprove: swapAssetNeedsUnlocking,
quote,
meta: swapParameters.meta,
Expand Down
1 change: 0 additions & 1 deletion src/core/raps/unlockAndSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ export const createUnlockAndSwapRap = async (
const swap = createNewAction('swap', {
chainId,
sellAmount,
permit: swapAssetNeedsUnlocking,
requiresApprove: swapAssetNeedsUnlocking,
quote,
meta: swapParameters.meta,
Expand Down
17 changes: 1 addition & 16 deletions src/core/raps/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { MaxUint256 } from '@ethersproject/constants';
import { Contract, PopulatedTransaction } from '@ethersproject/contracts';
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import {
ALLOWS_PERMIT,
CrosschainQuote,
Quote,
getQuoteExecutionDetails,
Expand Down Expand Up @@ -207,22 +206,8 @@ export const getDefaultGasLimitForTrade = (
quote: Quote,
chainId: ChainId,
): string => {
const allowsPermit =
chainId === mainnet.id &&
ALLOWS_PERMIT[quote?.sellTokenAddress?.toLowerCase()];

let defaultGasLimit = quote?.defaultGasLimit;

if (allowsPermit) {
defaultGasLimit = Math.max(
Number(defaultGasLimit),
Number(
multiply(getChainGasUnits(chainId).basic.swapPermit, EXTRA_GAS_PADDING),
),
).toString();
}
return (
defaultGasLimit ||
quote?.defaultGasLimit ||
multiply(getChainGasUnits(chainId).basic.swap, EXTRA_GAS_PADDING)
);
};
Expand Down

0 comments on commit e9d5705

Please sign in to comment.