Skip to content

Commit 910a789

Browse files
brunobar79jinchung
andauthored
Fix approval + swaps (#6259)
* remove permit usage on mainnet swaps * remove logging * make approval + swap parallel again * remove import * Skip node ack check if txn params are for mainnet (#6261) * Skip node ack check if txn params are for mainnet * Cap retries to 10 retries for node ack * Remove ALLOWS_PERMIT gas estimation checks --------- Co-authored-by: Jin <[email protected]>
1 parent 9209139 commit 910a789

File tree

5 files changed

+38
-59
lines changed

5 files changed

+38
-59
lines changed

src/handlers/swap.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { BigNumberish } from '@ethersproject/bignumber';
22
import { Block, StaticJsonRpcProvider } from '@ethersproject/providers';
33
import {
4-
ALLOWS_PERMIT,
54
CrosschainQuote,
65
getQuoteExecutionDetails,
76
getRainbowRouterContractAddress,
@@ -86,14 +85,7 @@ const getCrosschainSwapRainbowDefaultGasLimit = (chainId: ChainId) =>
8685
ethereumUtils.getBasicSwapGasLimit(Number(chainId)) * EXTRA_GAS_PADDING;
8786

8887
export const getDefaultGasLimitForTrade = (tradeDetails: Quote, chainId: ChainId): number => {
89-
const allowsPermit =
90-
chainId === ChainId.mainnet && ALLOWS_PERMIT[tradeDetails?.sellTokenAddress?.toLowerCase() as keyof PermitSupportedTokenList];
91-
92-
let defaultGasLimit = tradeDetails?.defaultGasLimit;
93-
94-
if (allowsPermit) {
95-
defaultGasLimit = Math.max(Number(defaultGasLimit), Number(ethUnits.basic_swap_permit) * EXTRA_GAS_PADDING).toString();
96-
}
88+
const defaultGasLimit = tradeDetails?.defaultGasLimit;
9789
return Number(defaultGasLimit || 0) || ethereumUtils.getBasicSwapGasLimit(Number(chainId)) * EXTRA_GAS_PADDING;
9890
};
9991

src/raps/actions/swap.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ export const executeSwap = async ({
205205
wallet: Signer;
206206
permit: boolean;
207207
}): Promise<Transaction | null> => {
208-
if (!wallet || !quote) return null;
208+
if (!wallet || !quote) {
209+
return null;
210+
}
209211

210-
const { sellTokenAddress, buyTokenAddress } = quote;
211212
const transactionParams = {
212213
gasLimit: toHex(gasLimit) || undefined,
213214
nonce: nonce ? toHex(`${nonce}`) : undefined,
@@ -236,9 +237,8 @@ export const swap = async ({
236237
gasFeeParamsBySpeed,
237238
}: ActionProps<'swap'>): Promise<RapActionResult> => {
238239
let gasParamsToUse = gasParams;
239-
// let gasParams = parseGasParamAmounts(selectedGasFee);
240240

241-
const { quote, permit, chainId, requiresApprove } = parameters;
241+
const { quote, chainId, requiresApprove } = parameters;
242242
// if swap isn't the last action, use fast gas or custom (whatever is faster)
243243

244244
if (currentRap.actions.length - 1 > index) {
@@ -272,7 +272,7 @@ export const swap = async ({
272272
chainId,
273273
gasLimit,
274274
nonce,
275-
permit: !!permit,
275+
permit: false,
276276
quote,
277277
wallet,
278278
};

src/raps/execute.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable no-async-promise-executor */
33
/* eslint-disable no-promise-executor-return */
44
import { Signer } from '@ethersproject/abstract-signer';
5-
5+
import { ChainId } from '@/chains/types';
66
import { RainbowError, logger } from '@/logger';
77

88
import { claim, swap, unlock } from './actions';
@@ -111,18 +111,29 @@ function getRapFullName<T extends RapActionTypes>(actions: RapAction<T>[]) {
111111

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

114-
const waitForNodeAck = async (hash: string, provider: Signer['provider']): Promise<void> => {
115-
return new Promise(async resolve => {
114+
const NODE_ACK_MAX_TRIES = 10;
115+
116+
const waitForNodeAck = async (hash: string, provider: Signer['provider'], tries = 0): Promise<void> => {
117+
try {
116118
const tx = await provider?.getTransaction(hash);
119+
117120
// This means the node is aware of the tx, we're good to go
118121
if ((tx && tx.blockNumber === null) || (tx && tx?.blockNumber && tx?.blockNumber > 0)) {
119-
resolve();
120-
} else {
121-
// Wait for 1 second and try again
122+
return;
123+
}
124+
125+
// Wait for 1 second and try again
126+
if (tries < NODE_ACK_MAX_TRIES) {
122127
await delay(1000);
123-
return waitForNodeAck(hash, provider);
128+
return waitForNodeAck(hash, provider, tries + 1);
124129
}
125-
});
130+
} catch (e) {
131+
// Wait for 1 second and try again
132+
if (tries < NODE_ACK_MAX_TRIES) {
133+
await delay(1000);
134+
return waitForNodeAck(hash, provider, tries + 1);
135+
}
136+
}
126137
};
127138

128139
export const walletExecuteRap = async (
@@ -161,11 +172,13 @@ export const walletExecuteRap = async (
161172
gasFeeParamsBySpeed: parameters?.gasFeeParamsBySpeed,
162173
};
163174

164-
const { baseNonce, errorMessage: error, hash } = await executeAction(actionParams);
175+
const { baseNonce, errorMessage: error, hash: firstHash } = await executeAction(actionParams);
176+
const shouldWaitForNodeAck = parameters.chainId !== ChainId.mainnet;
165177

166178
if (typeof baseNonce === 'number') {
167-
actions.length > 1 && hash && (await waitForNodeAck(hash, wallet.provider));
179+
let latestHash = firstHash;
168180
for (let index = 1; index < actions.length; index++) {
181+
latestHash && shouldWaitForNodeAck && (await waitForNodeAck(latestHash, wallet.provider));
169182
const action = actions[index];
170183
const actionParams = {
171184
action,
@@ -178,8 +191,8 @@ export const walletExecuteRap = async (
178191
gasParams: parameters?.gasParams,
179192
gasFeeParamsBySpeed: parameters?.gasFeeParamsBySpeed,
180193
};
181-
const { hash } = await executeAction(actionParams);
182-
hash && (await waitForNodeAck(hash, wallet.provider));
194+
const { hash: nextHash } = await executeAction(actionParams);
195+
latestHash = nextHash;
183196
}
184197
nonce = baseNonce + actions.length - 1;
185198
} else {

src/raps/unlockAndSwap.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
import {
2-
ALLOWS_PERMIT,
3-
ETH_ADDRESS as ETH_ADDRESS_AGGREGATOR,
4-
getRainbowRouterContractAddress,
5-
PermitSupportedTokenList,
6-
} from '@rainbow-me/swaps';
1+
import { getRainbowRouterContractAddress } from '@rainbow-me/swaps';
72
import { Address } from 'viem';
83

9-
import { ChainId } from '@/chains/types';
10-
import { isNativeAsset } from '@/handlers/assets';
114
import { add } from '@/helpers/utilities';
12-
import { isLowerCaseMatch } from '@/utils';
135

146
import { assetNeedsUnlocking, estimateApprove, estimateSwapGasLimit } from './actions';
157
import { estimateUnlockAndSwapFromMetadata } from './actions/swap';
@@ -56,7 +48,6 @@ export const estimateUnlockAndSwap = async ({
5648
if (gasLimitFromMetadata) {
5749
return gasLimitFromMetadata;
5850
}
59-
6051
const unlockGasLimit = await estimateApprove({
6152
owner: accountAddress,
6253
tokenAddress: sellTokenAddress,
@@ -89,19 +80,12 @@ export const createUnlockAndSwapRap = async (swapParameters: RapSwapActionParame
8980

9081
const { sellAmount, quote, chainId, assetToSell, assetToBuy } = swapParameters;
9182

92-
const {
93-
from: accountAddress,
94-
sellTokenAddress,
95-
allowanceNeeded,
96-
} = quote as {
83+
const { from: accountAddress, allowanceNeeded } = quote as {
9784
from: Address;
9885
sellTokenAddress: Address;
9986
allowanceNeeded: boolean;
10087
};
10188

102-
// Aggregators represent native asset as 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
103-
const nativeAsset = isLowerCaseMatch(ETH_ADDRESS_AGGREGATOR, sellTokenAddress) || isNativeAsset(sellTokenAddress, chainId);
104-
10589
let swapAssetNeedsUnlocking = false;
10690

10791
if (allowanceNeeded) {
@@ -114,10 +98,7 @@ export const createUnlockAndSwapRap = async (swapParameters: RapSwapActionParame
11498
});
11599
}
116100

117-
const allowsPermit =
118-
!nativeAsset && chainId === ChainId.mainnet && ALLOWS_PERMIT[assetToSell.address?.toLowerCase() as keyof PermitSupportedTokenList];
119-
120-
if (swapAssetNeedsUnlocking && !allowsPermit) {
101+
if (swapAssetNeedsUnlocking) {
121102
const unlock = createNewAction('unlock', {
122103
fromAddress: accountAddress,
123104
amount: sellAmount,
@@ -132,8 +113,8 @@ export const createUnlockAndSwapRap = async (swapParameters: RapSwapActionParame
132113
const swap = createNewAction('swap', {
133114
chainId,
134115
sellAmount,
135-
permit: swapAssetNeedsUnlocking && allowsPermit,
136-
requiresApprove: swapAssetNeedsUnlocking && !allowsPermit,
116+
permit: false,
117+
requiresApprove: swapAssetNeedsUnlocking,
137118
quote,
138119
meta: swapParameters.meta,
139120
assetToSell,

src/raps/utils.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Block, Provider } from '@ethersproject/abstract-provider';
22
import { MaxUint256 } from '@ethersproject/constants';
33
import { Contract, PopulatedTransaction } from '@ethersproject/contracts';
44
import { StaticJsonRpcProvider } from '@ethersproject/providers';
5-
import { ALLOWS_PERMIT, CrosschainQuote, Quote, getQuoteExecutionDetails, getRainbowRouterContractAddress } from '@rainbow-me/swaps';
5+
import { CrosschainQuote, Quote, getQuoteExecutionDetails, getRainbowRouterContractAddress } from '@rainbow-me/swaps';
66
import { mainnet } from 'viem/chains';
77
import { Chain, erc20Abi } from 'viem';
88
import { GasFeeParamsBySpeed, LegacyGasFeeParamsBySpeed, LegacyTransactionGasParamAmounts, TransactionGasParamAmounts } from '@/entities';
@@ -140,14 +140,7 @@ const getClosestGasEstimate = async (estimationFn: (gasEstimate: number) => Prom
140140
};
141141

142142
export const getDefaultGasLimitForTrade = (quote: Quote, chainId: Chain['id']): string => {
143-
const allowsPermit = chainId === mainnet.id && ALLOWS_PERMIT[quote?.sellTokenAddress?.toLowerCase()];
144-
145-
let defaultGasLimit = quote?.defaultGasLimit;
146-
147-
if (allowsPermit) {
148-
defaultGasLimit = Math.max(Number(defaultGasLimit), Number(multiply(gasUnits.basic_swap_permit, EXTRA_GAS_PADDING))).toString();
149-
}
150-
return defaultGasLimit || multiply(gasUnits.basic_swap[chainId], EXTRA_GAS_PADDING);
143+
return quote?.defaultGasLimit || multiply(gasUnits.basic_swap[chainId], EXTRA_GAS_PADDING);
151144
};
152145

153146
export const estimateSwapGasLimitWithFakeApproval = async (

0 commit comments

Comments
 (0)