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

preprocess gasParams for raps #5751

Merged
merged 1 commit into from
May 21, 2024
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
2 changes: 0 additions & 2 deletions src/entities/gas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { GasFeeLegacyParams } from '@/__swaps__/types/gas';

type Numberish = number | string;

export interface Fee {
Expand Down
11 changes: 6 additions & 5 deletions src/raps/actions/crosschainSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
import { ethereumUtils } from '@/utils';
import { TokenColors } from '@/graphql/__generated__/metadata';
import { ParsedAsset } from '@/resources/assets/types';
import { parseGasParamAmounts } from '@/parsers';

const getCrosschainSwapDefaultGasLimit = (quote: CrosschainQuote) => quote?.routes?.[0]?.userTxs?.[0]?.gasFees?.gasLimit;

Expand Down Expand Up @@ -104,13 +103,15 @@ export const crosschainSwap = async ({
index,
parameters,
baseNonce,
selectedGasFee,
gasParams,
gasFeeParamsBySpeed,
}: ActionProps<'crosschainSwap'>): Promise<RapActionResult> => {
const { quote, chainId, requiresApprove } = parameters;
let gasParams = parseGasParamAmounts(selectedGasFee);

let gasParamsToUse = gasParams;
// let gasParams = parseGasParamAmounts(selectedGasFee);
if (currentRap.actions.length - 1 > index) {
gasParams = overrideWithFastSpeedIfNeeded({
gasParamsToUse = overrideWithFastSpeedIfNeeded({
gasParams,
chainId,
gasFeeParamsBySpeed,
Expand Down Expand Up @@ -139,7 +140,7 @@ export const crosschainSwap = async ({
nonce,
quote,
wallet,
gasParams,
gasParams: gasParamsToUse,
};

let swap;
Expand Down
10 changes: 5 additions & 5 deletions src/raps/actions/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import { populateApprove } from './unlock';
import { TokenColors } from '@/graphql/__generated__/metadata';
import { swapMetadataStorage } from '../common';
import { ParsedAsset } from '@/resources/assets/types';
import { parseGasParamAmounts } from '@/parsers';

const WRAP_GAS_PADDING = 1.002;

Expand Down Expand Up @@ -239,16 +238,17 @@ export const swap = async ({
index,
parameters,
baseNonce,
selectedGasFee,
gasParams,
gasFeeParamsBySpeed,
}: ActionProps<'swap'>): Promise<RapActionResult> => {
let gasParams = parseGasParamAmounts(selectedGasFee);
let gasParamsToUse = gasParams;
// let gasParams = parseGasParamAmounts(selectedGasFee);

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

if (currentRap.actions.length - 1 > index) {
gasParams = overrideWithFastSpeedIfNeeded({
gasParamsToUse = overrideWithFastSpeedIfNeeded({
gasParams,
chainId,
gasFeeParamsBySpeed,
Expand All @@ -274,7 +274,7 @@ export const swap = async ({
try {
const nonce = baseNonce ? baseNonce + index : undefined;
const swapParams = {
gasParams,
gasParams: gasParamsToUse,
chainId,
gasLimit,
nonce,
Expand Down
10 changes: 5 additions & 5 deletions src/raps/actions/unlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { ethereumUtils } from '@/utils';
import { toHex } from '@/__swaps__/utils/hex';
import { TokenColors } from '@/graphql/__generated__/metadata';
import { ParsedAsset } from '@/resources/assets/types';
import { parseGasParamAmounts } from '@/parsers';

export const getAssetRawAllowance = async ({
owner,
Expand Down Expand Up @@ -215,11 +214,13 @@ export const unlock = async ({
index,
parameters,
wallet,
selectedGasFee,
gasParams,
gasFeeParamsBySpeed,
}: ActionProps<'unlock'>): Promise<RapActionResult> => {
const { assetToUnlock, contractAddress, chainId } = parameters;

let gasParamsToUse = gasParams;

const { address: assetAddress } = assetToUnlock;

if (assetAddress === ETH_ADDRESS) throw new RainbowError('unlock: Native ETH cannot be unlocked');
Expand All @@ -239,8 +240,7 @@ export const unlock = async ({
throw e;
}

let gasParams = parseGasParamAmounts(selectedGasFee);
gasParams = overrideWithFastSpeedIfNeeded({
gasParamsToUse = overrideWithFastSpeedIfNeeded({
gasParams,
chainId,
gasFeeParamsBySpeed,
Expand All @@ -254,7 +254,7 @@ export const unlock = async ({
tokenAddress: assetAddress,
spender: contractAddress,
gasLimit,
gasParams,
gasParams: gasParamsToUse,
wallet,
nonce,
chainId,
Expand Down
12 changes: 6 additions & 6 deletions src/raps/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from './references';
import { createUnlockAndCrosschainSwapRap } from './unlockAndCrosschainSwap';
import { createUnlockAndSwapRap } from './unlockAndSwap';
import { GasFeeParamsBySpeed, LegacyGasFeeParamsBySpeed, LegacySelectedGasFee, SelectedGasFee } from '@/entities';
import { GasFeeParamsBySpeed, LegacyGasFeeParamsBySpeed, LegacyTransactionGasParamAmounts, TransactionGasParamAmounts } from '@/entities';

export function createSwapRapByType<T extends RapTypes>(type: T, swapParameters: RapSwapActionParameters<T>) {
switch (type) {
Expand Down Expand Up @@ -54,7 +54,7 @@ export async function executeAction<T extends RapActionTypes>({
baseNonce,
rapName,
flashbots,
selectedGasFee,
gasParams,
gasFeeParamsBySpeed,
}: {
action: RapAction<T>;
Expand All @@ -64,7 +64,7 @@ export async function executeAction<T extends RapActionTypes>({
baseNonce?: number;
rapName: string;
flashbots?: boolean;
selectedGasFee: SelectedGasFee | LegacySelectedGasFee;
gasParams: TransactionGasParamAmounts | LegacyTransactionGasParamAmounts;
gasFeeParamsBySpeed: GasFeeParamsBySpeed | LegacyGasFeeParamsBySpeed;
}): Promise<RapActionResponse> {
const { type, parameters } = action;
Expand All @@ -75,7 +75,7 @@ export async function executeAction<T extends RapActionTypes>({
index,
parameters: { ...parameters, flashbots },
baseNonce,
selectedGasFee,
gasParams,
gasFeeParamsBySpeed,
};
const { nonce, hash } = (await typeAction<T>(type, actionProps)()) as RapActionResult;
Expand Down Expand Up @@ -133,7 +133,7 @@ export const walletExecuteRap = async (
baseNonce: nonce,
rapName,
flashbots: parameters?.flashbots,
selectedGasFee: parameters?.selectedGasFee,
gasParams: parameters?.gasParams,
gasFeeParamsBySpeed: parameters?.gasFeeParamsBySpeed,
};

Expand All @@ -151,7 +151,7 @@ export const walletExecuteRap = async (
baseNonce,
rapName,
flashbots: parameters?.flashbots,
selectedGasFee: parameters?.selectedGasFee,
gasParams: parameters?.gasParams,
gasFeeParamsBySpeed: parameters?.gasFeeParamsBySpeed,
};
const { hash } = await executeAction(actionParams);
Expand Down
6 changes: 3 additions & 3 deletions src/raps/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CrosschainQuote, Quote } from '@rainbow-me/swaps';
import { Address } from 'viem';

import { ParsedAsset } from '@/__swaps__/types/assets';
import { GasFeeParamsBySpeed, LegacyGasFeeParamsBySpeed, LegacySelectedGasFee, SelectedGasFee } from '@/entities';
import { GasFeeParamsBySpeed, LegacyGasFeeParamsBySpeed, LegacyTransactionGasParamAmounts, TransactionGasParamAmounts } from '@/entities';

export enum SwapModalField {
input = 'inputAmount',
Expand Down Expand Up @@ -49,7 +49,7 @@ export interface RapSwapActionParameters<T extends 'swap' | 'crosschainSwap'> {
meta?: SwapMetadata;
assetToSell: ParsedAsset;
assetToBuy: ParsedAsset;
selectedGasFee: SelectedGasFee | LegacySelectedGasFee;
gasParams: TransactionGasParamAmounts | LegacyTransactionGasParamAmounts;
gasFeeParamsBySpeed: GasFeeParamsBySpeed | LegacyGasFeeParamsBySpeed;
nonce?: number;
flashbots?: boolean;
Expand Down Expand Up @@ -117,7 +117,7 @@ export interface ActionProps<T extends RapActionTypes> {
parameters: RapActionParameterMap[T];
wallet: Signer;
currentRap: Rap;
selectedGasFee: SelectedGasFee | LegacySelectedGasFee;
gasParams: TransactionGasParamAmounts | LegacyTransactionGasParamAmounts;
gasFeeParamsBySpeed: GasFeeParamsBySpeed | LegacyGasFeeParamsBySpeed;
}

Expand Down
2 changes: 1 addition & 1 deletion src/raps/unlockAndCrosschainSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const createUnlockAndCrosschainSwapRap = async (swapParameters: RapSwapAc
assetToSell,
sellAmount,
assetToBuy,
selectedGasFee: swapParameters.selectedGasFee,
gasParams: swapParameters.gasParams,
gasFeeParamsBySpeed: swapParameters.gasFeeParamsBySpeed,
} satisfies RapSwapActionParameters<'crosschainSwap'>);
actions = actions.concat(swap);
Expand Down
2 changes: 1 addition & 1 deletion src/raps/unlockAndSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const createUnlockAndSwapRap = async (swapParameters: RapSwapActionParame
meta: swapParameters.meta,
assetToSell,
assetToBuy,
selectedGasFee: swapParameters.selectedGasFee,
gasParams: swapParameters.gasParams,
gasFeeParamsBySpeed: swapParameters.gasFeeParamsBySpeed,
} satisfies RapSwapActionParameters<'swap'>);
actions = actions.concat(swap);
Expand Down
3 changes: 2 additions & 1 deletion src/screens/ExchangeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import { TokenColors } from '@/graphql/__generated__/metadata';
import { estimateSwapGasLimit } from '@/raps/actions';
import { estimateCrosschainSwapGasLimit } from '@/raps/actions/crosschainSwap';
import { isUnwrapEth, isWrapEth } from '@/__swaps__/utils/swaps';
import { parseGasParamAmounts } from '@/parsers';

export const DEFAULT_SLIPPAGE_BIPS = {
[Network.mainnet]: 100,
Expand Down Expand Up @@ -487,7 +488,7 @@ export default function ExchangeModal({ fromDiscover, ignoreInitialTypeCheck, te
slippage: slippageInBips,
route: source,
},
selectedGasFee,
gasParams: parseGasParamAmounts(selectedGasFee),
gasFeeParamsBySpeed,
});

Expand Down
Loading