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

raps failures fix #740

Merged
merged 5 commits into from
Jul 17, 2023
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
9 changes: 6 additions & 3 deletions src/core/raps/actions/crosschainSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '../../types/gas';
import { estimateGasWithPadding } from '../../utils/gas';
import { toHex } from '../../utils/hex';
import { ActionProps } from '../references';
import { ActionProps, RapActionResult } from '../references';
import {
CHAIN_IDS_WITH_TRACE_SUPPORT,
SWAP_GAS_PADDING,
Expand Down Expand Up @@ -113,7 +113,7 @@ export const crosschainSwap = async ({
index,
parameters,
baseNonce,
}: ActionProps<'crosschainSwap'>): Promise<number | undefined> => {
}: ActionProps<'crosschainSwap'>): Promise<RapActionResult> => {
const { quote, chainId, requiresApprove } = parameters;
const { selectedGas, gasFeeParamsBySpeed } = gasStore.getState();

Expand Down Expand Up @@ -191,5 +191,8 @@ export const crosschainSwap = async ({
transaction,
});

return swap?.nonce;
return {
nonce: swap?.nonce,
hash: swap?.hash,
};
};
9 changes: 6 additions & 3 deletions src/core/raps/actions/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from '../../types/gas';
import { estimateGasWithPadding } from '../../utils/gas';
import { toHex } from '../../utils/hex';
import { ActionProps } from '../references';
import { ActionProps, RapActionResult } from '../references';
import {
CHAIN_IDS_WITH_TRACE_SUPPORT,
SWAP_GAS_PADDING,
Expand Down Expand Up @@ -192,7 +192,7 @@ export const swap = async ({
index,
parameters,
baseNonce,
}: ActionProps<'swap'>): Promise<number | undefined> => {
}: ActionProps<'swap'>): Promise<RapActionResult> => {
const { selectedGas, gasFeeParamsBySpeed } = gasStore.getState();

const { quote, permit, chainId, requiresApprove } = parameters;
Expand Down Expand Up @@ -267,5 +267,8 @@ export const swap = async ({
transaction,
});

return swap?.nonce;
return {
nonce: swap?.nonce,
hash: swap?.hash,
};
};
9 changes: 6 additions & 3 deletions src/core/raps/actions/unlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { gasStore } from '../../state';
import { ParsedAsset } from '../../types/assets';
import { toHex } from '../../utils/hex';
import { convertAmountToRawAmount, greaterThan } from '../../utils/numbers';
import { ActionProps } from '../references';
import { ActionProps, RapActionResult } from '../references';

import { overrideWithFastSpeedIfNeeded } from './../utils';

Expand Down Expand Up @@ -134,7 +134,7 @@ export const unlock = async ({
index,
parameters,
wallet,
}: ActionProps<'unlock'>): Promise<number | undefined> => {
}: ActionProps<'unlock'>): Promise<RapActionResult> => {
const { selectedGas, gasFeeParamsBySpeed } = gasStore.getState();

const { assetToUnlock, contractAddress, chainId } = parameters;
Expand Down Expand Up @@ -208,5 +208,8 @@ export const unlock = async ({
transaction,
});

return approval?.nonce;
return {
nonce: approval?.nonce,
hash: approval?.hash,
};
};
49 changes: 41 additions & 8 deletions src/core/raps/execute.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable no-async-promise-executor */
/* eslint-disable no-promise-executor-return */
import { Signer } from 'ethers';

import { RainbowError, logger } from '~/logger';
Expand All @@ -9,6 +12,7 @@ import {
Rap,
RapAction,
RapActionResponse,
RapActionResult,
RapActionTypes,
RapSwapActionParameters,
RapTypes,
Expand Down Expand Up @@ -66,7 +70,6 @@ export async function executeAction<T extends RapActionTypes>({
flashbots?: boolean;
}): Promise<RapActionResponse> {
const { type, parameters } = action;
let nonce;
try {
const actionProps = {
wallet,
Expand All @@ -75,8 +78,11 @@ export async function executeAction<T extends RapActionTypes>({
parameters: { ...parameters, flashbots },
baseNonce,
};
nonce = await typeAction<T>(type, actionProps)();
return { baseNonce: nonce, errorMessage: null };
const { nonce, hash } = (await typeAction<T>(
type,
actionProps,
)()) as RapActionResult;
return { baseNonce: nonce, errorMessage: null, hash };
} catch (error) {
logger.error(new RainbowError(`rap: ${rapName} - error execute action`), {
message: (error as Error)?.message,
Expand All @@ -93,6 +99,28 @@ function getRapFullName<T extends RapActionTypes>(actions: RapAction<T>[]) {
return actionTypes.join(' + ');
}

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

const waitForNodeAck = async (
hash: string,
provider: Signer['provider'],
): Promise<void> => {
return new Promise(async (resolve) => {
const tx = await provider?.getTransaction(hash);
// This means the node is aware of the tx, we're good to go
if (
(tx && tx.blockNumber === null) ||
(tx && tx?.blockNumber && tx?.blockNumber > 0)
) {
resolve();
} else {
// Wait for 1 second and try again
await delay(1000);
return waitForNodeAck(hash, provider);
}
});
};

export const walletExecuteRap = async (
wallet: Signer,
type: RapTypes,
Expand All @@ -116,11 +144,16 @@ export const walletExecuteRap = async (
flashbots: parameters?.flashbots,
};

const { baseNonce, errorMessage: error } = await executeAction(
actionParams,
);
const {
baseNonce,
errorMessage: error,
hash,
} = await executeAction(actionParams);

if (typeof baseNonce === 'number') {
actions.length > 1 &&
hash &&
(await waitForNodeAck(hash, wallet.provider));
for (let index = 1; index < actions.length; index++) {
const action = actions[index];
const actionParams = {
Expand All @@ -132,8 +165,8 @@ export const walletExecuteRap = async (
rapName,
flashbots: parameters?.flashbots,
};
// eslint-disable-next-line no-await-in-loop
await executeAction(actionParams);
const { hash } = await executeAction(actionParams);
hash && (await waitForNodeAck(hash, wallet.provider));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should only await if is not the last action, if is the last action we'd be delaying the execution of this method for no reason

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call!

}
nonce = baseNonce + actions.length - 1;
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/core/raps/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export type RapTypes = keyof typeof rapTypes;
export interface RapActionResponse {
baseNonce?: number | null;
errorMessage: string | null;
hash?: string | null;
}

export interface RapActionResult {
nonce?: number | undefined;
hash?: string | undefined;
}

export interface ActionProps<T extends RapActionTypes> {
Expand Down