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

feat: custom gas in all networks #1631

Merged
merged 9 commits into from
Aug 2, 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
9 changes: 7 additions & 2 deletions lavamoat/build-webpack/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1156,8 +1156,13 @@
"define": true
},
"packages": {
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/sourcemap-codec": true,
"webpack>terser-webpack-plugin>@jridgewell/trace-mapping>@jridgewell/resolve-uri": true
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/resolve-uri": true,
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/sourcemap-codec": true
}
},
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/resolve-uri": {
"globals": {
"define": true
}
},
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/sourcemap-codec": {
Expand Down
14 changes: 10 additions & 4 deletions src/core/resources/gas/meteorology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export type MeteorologyResponse = {
provider: string;
};
};
meta: {
feeType: 'legacy' | 'eip1559';
blockNumber: string;
provider: string;
};
};

export type MeteorologyLegacyResponse = {
Expand All @@ -58,10 +63,11 @@ export type MeteorologyLegacyResponse = {
proposeGasPrice: string;
safeGasPrice: string;
};
meta: {
blockNumber: number;
provider: string;
};
};
meta: {
feeType: 'legacy' | 'eip1559';
blockNumber: number;
provider: string;
};
};

Expand Down
9 changes: 5 additions & 4 deletions src/core/resources/gas/providerGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ async function providerGasQueryFunction({
proposeGasPrice: gweiGasPrice,
safeGasPrice: gweiGasPrice,
},
meta: {
blockNumber: 0,
provider: 'provider',
},
},
meta: {
feeType: 'legacy',
blockNumber: 0,
provider: 'provider',
},
};

Expand Down
11 changes: 11 additions & 0 deletions src/core/state/gas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface GasStore {
gasFeeParamsBySpeed: GasFeeParamsBySpeed | GasFeeLegacyParamsBySpeed;
customGasModified: boolean;
setCustomSpeed: (speed: GasFeeParams) => void;
setCustomLegacySpeed: (speed: GasFeeLegacyParams) => void;
setSelectedGas: ({
selectedGas,
}: {
Expand Down Expand Up @@ -54,6 +55,16 @@ export const gasStore = createStore<GasStore>(
customGasModified: true,
});
},
setCustomLegacySpeed: (speed: GasFeeLegacyParams) => {
const { gasFeeParamsBySpeed } = get();
set({
gasFeeParamsBySpeed: {
...gasFeeParamsBySpeed,
[GasSpeed.CUSTOM]: speed,
} as GasFeeLegacyParamsBySpeed,
customGasModified: true,
});
},
clearCustomGasModified: () => {
set({ customGasModified: false });
},
Expand Down
106 changes: 95 additions & 11 deletions src/core/utils/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ import { getMinimalTimeUnitStringForMs } from './time';

export const FLASHBOTS_MIN_TIP = 6;

const formatDisplayNumber = (number: number | string) => {
const n = Number(number);
if (n === 0) {
return '0';
} else if (n < 1) {
return n.toFixed(3);
} else if (n < 2) {
return n.toFixed(2);
} else {
return n.toFixed(0);
}
};

export const parseGasDataConfirmationTime = ({
maxBaseFee,
maxPriorityFee,
Expand Down Expand Up @@ -151,11 +164,15 @@ export const parseCustomGasFeeParams = ({
? currentBaseFee
: maxBaseFee.amount;

const display = `${new BigNumber(
weiToGwei(add(baseFee, maxPriorityFeePerGas.amount)),
).toFixed(0)} - ${new BigNumber(
weiToGwei(add(baseFeeWei, maxPriorityFeePerGas.amount)),
).toFixed(0)} Gwei`;
const display = `${formatDisplayNumber(
new BigNumber(
weiToGwei(add(baseFee, maxPriorityFeePerGas.amount)),
).toNumber(),
)} - ${formatDisplayNumber(
new BigNumber(
weiToGwei(add(baseFeeWei, maxPriorityFeePerGas.amount)),
).toNumber(),
)} Gwei`;

const estimatedTime = parseGasDataConfirmationTime({
maxBaseFee: maxBaseFee.amount,
Expand Down Expand Up @@ -198,6 +215,69 @@ export const parseCustomGasFeeParams = ({
};
};

export const parseCustomGasFeeLegacyParams = ({
speed,
gasPriceWei,
gasLimit,
nativeAsset,
currency,
waitTime,
}: {
speed: GasSpeed;
gasPriceWei: string;
gasLimit: string;
nativeAsset?: ParsedAsset;
currency: SupportedCurrencyKey;
waitTime: number | null;
}): GasFeeLegacyParams => {
const gasPrice = parseGasFeeParam({
wei: gasPriceWei || '0',
});
const display = `${formatDisplayNumber(gasPrice.gwei)} Gwei`;

const estimatedTime = {
amount: waitTime || 0,
display: waitTime
? `${waitTime >= 3600 ? '>' : '~'} ${getMinimalTimeUnitStringForMs(
Number(multiply(waitTime, 1000)),
)}`
: '',
};
const transactionGasParams = {
gasPrice: toHex(gasPrice.amount),
};

const amount = gasPrice.amount;
const totalWei = multiply(gasLimit, amount);

const nativeTotalWei = convertRawAmountToBalance(
totalWei,
supportedCurrencies[nativeAsset?.symbol as SupportedCurrencyKey],
).amount;

const nativeDisplay = nativeAsset?.price?.value
? convertAmountAndPriceToNativeDisplayWithThreshold(
nativeTotalWei,
nativeAsset?.price?.value,
currency,
)
: convertRawAmountToBalance(totalWei, {
decimals: nativeAsset?.decimals || 18,
symbol: nativeAsset?.symbol,
});

const gasFee = { amount: totalWei, display: nativeDisplay.display };

return {
display,
estimatedTime,
gasFee,
gasPrice,
option: speed,
transactionGasParams,
};
};

export const parseGasFeeParams = ({
wei,
currentBaseFee,
Expand Down Expand Up @@ -238,11 +318,15 @@ export const parseGasFeeParams = ({
? currentBaseFee
: maxBaseFee.amount;

const display = `${new BigNumber(
weiToGwei(add(baseFee, maxPriorityFeePerGas.amount)),
).toFixed(0)} - ${new BigNumber(
weiToGwei(add(maxBaseFee.amount, maxPriorityFeePerGas.amount)),
).toFixed(0)} Gwei`;
const display = `${formatDisplayNumber(
new BigNumber(
weiToGwei(add(baseFee, maxPriorityFeePerGas.amount)),
).toNumber(),
)} - ${formatDisplayNumber(
new BigNumber(
weiToGwei(add(maxBaseFee.amount, maxPriorityFeePerGas.amount)),
).toNumber(),
)} Gwei`;

const estimatedTime = parseGasDataConfirmationTime({
maxBaseFee: maxBaseFee.amount,
Expand Down Expand Up @@ -314,7 +398,7 @@ export const parseGasFeeLegacyParams = ({
const gasPrice = parseGasFeeParam({
wei: new BigNumber(multiply(wei, getBaseFeeMultiplier(speed))).toFixed(0),
});
const display = parseGasFeeParam({ wei }).display;
const display = `${formatDisplayNumber(gasPrice.gwei)} Gwei`;

const estimatedTime = {
amount: waitTime || 0,
Expand Down
Loading
Loading