Compare commits

..

2 Commits
main ... 2.1.0

8 changed files with 124 additions and 211 deletions

View File

@ -35,7 +35,7 @@ const tx: TransactionData = {
};
const feeOracle = new TornadoFeeOracleV5(1, 'https://eth.llamarpc.com'); // First parameter - chain ID
const withdrawalGas = await feeOracle.getGas({tx, txType: 'relayer_withdrawal'});
const withdrawalGas = await feeOracle.getGas(tx, 'relayer_withdrawal');
```
##### Estimate gas price and gas limit to send transaction
@ -51,9 +51,10 @@ const incompleteTx: TransactionData = {
const feeOracle = new TornadoFeeOracleV5(1, 'https://eth.llamarpc.com');
const transactionType: TxType = 'relayer_withdrawal';
const { gasPrice, gasLimit } = await feeOracle.getGasParams({tx: incompleteTx, txType: transactionType});
const gasPrice = await feeOracle.getGasPrice(transactionType);
const gasLimit = await feeOracle.getGasLimit(incompleteTx, transactionType);
const tx: TransactionData = {...incompleteTx, gasPrice, gasLimit}
const tx: TransactionData = Object.assign({ gasPrice, gasLimit }, incompleteTx);
```
##### Get token prices (rate to ETH) for tokens that used in Tornado

View File

@ -1,7 +1,7 @@
{
"name": "@tornado/tornado-oracles",
"version": "3.3.0",
"description": "Oracles for Tornado-specific transactions & actions",
"version": "2.1.0",
"description": "Gas oracle for Tornado-specific transactions",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"scripts": {
@ -11,8 +11,7 @@
"prettier:fix": "prettier --write . --config .prettierrc",
"build:abi": "yarn typechain --target ethers-v5 --out-dir src/contracts ./abis/*.abi.json",
"test": "echo \"Error: no test specified\" && exit 1",
"prepare": "npm run build && npm run build:esm",
"prepublish": "npm run prettier:fix"
"prepare": "npm run build && npm run build:esm"
},
"repository": {
"type": "git",

View File

@ -1,6 +1,5 @@
import { GasPriceOracle } from '@tornado/gas-price-oracle';
import { BigNumber, BigNumberish, ethers } from 'ethers';
import BigNumberFloat from 'bignumber.js';
import { parseUnits } from 'ethers/lib/utils';
import {
TransactionData,
@ -10,21 +9,17 @@ import {
GasPriceParams,
GetGasParamsRes,
HexadecimalStringifiedNumber,
GetGasInput,
GetGasParamsInput,
} from './types';
import { JsonRpcProvider } from '@ethersproject/providers';
import { ChainId, defaultGasPrices, defaultInstanceTokensGasLimit, InstanceTokenSymbol } from './config';
import { bump, calculateGasPriceInWei, convertETHToToken, fromGweiToWeiHex, serializeTx } from './utils';
import { getOptimismL1FeeOracle } from './contracts/factories';
import { GetWithdrawalFeeViaRelayerInput } from './types';
import { AvailableTokenSymbols } from '@tornado/tornado-config';
export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
protected provider: JsonRpcProvider;
public constructor(
public version: 4 | 5,
protected chainId: ChainId,
rpcUrl: string,
protected oracle: GasPriceOracle,
@ -35,7 +30,7 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
/**
* Because Optimism transaction published on Mainnet, for each OP transaction we need to calculate L1 security fee:
* https://community.optimism.io/docs/developers/build/transaction-fees/#priority-fee
* @param {TransactionData} [tx] Transaction data to estimate L1 additional fee
* @param {TransactionData} tx Transaction data to estimate L1 additional fee
* @returns {Promise<HexadecimalStringifiedNumber>} Fee in WEI (MATIC), '0' if chain is not Optimism
*/
async fetchL1OptimismFee(tx?: TransactionData): Promise<HexadecimalStringifiedNumber> {
@ -49,71 +44,55 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
/**
* Estimate gas price, gas limit and l1Fee for sidechain (if exists)
* @param {GetGasParamsInput} [params] Function input arguments object
* @param {TransactionData} [params.tx] Transaction data in web3 / ethers format
* @param {TxType} [params.txType=other] Tornado transaction type: withdrawal by user, withdrawal by relayer or 'other'
* @param {number} [params.predefinedGasLimit] Predefined gas limit, if already calculated (no refetching)
* @param {number} [params.predefinedGasPrice] Predefined gas price, if already calculated (no refetching)
* @param {number} [params.bumpGasLimitPercent] Gas limit bump percent to prioritize transaction (if gas limit not predefined, recenlty used)
* @param {number} [params.bumpGasPricePercent] Gas price bump percent to prioritize transaction (if gas limit not predefined, rarely used)
* @param {LegacyGasPriceKey} [params.speed] Preferred transaction speed, if uses legacy gas (before EIP-1559)
* @param {boolean} [params.includeL1FeeToGasLimit=true] Include L1 additional fee on Optimism to gas limit (get fee and divide by gas price)
* @returns {Promise<GetGasParamsRes>} Object with fields 'gasPrice' and 'gasLimit', L1 fee, if exists, included in gasLimit
* @param {TransactionData} [tx] Transaction data in web3 / ethers format
* @param {TxType} [txType=other] Tornado transaction type: withdrawal by user, withdrawal by relayer or 'other'
* @param {number} [bumpGasLimitPercent] Gas limit bump percent to prioritize transaction (recenlty used)
* @param {number} [bumpGasPricePercent] Gas price bump percent to prioritize transaction (rarely used)
* @param {LegacyGasPriceKey} [speed] Preferred transaction speed, if uses legacy gas (before EIP-1559)
* @returns {Promise<GetGasParamsRes>} Object with fields 'gasPrice', 'gasLimit' and 'l1Fee'
*/
async getGasParams(params: GetGasParamsInput = {}): Promise<GetGasParamsRes> {
let {
tx,
txType = 'other',
bumpGasLimitPercent,
bumpGasPricePercent,
predefinedGasLimit: gasLimit,
predefinedGasPrice: gasPrice,
speed,
includeL1FeeToGasLimit = true,
} = params;
async getGasParams(
tx?: TransactionData,
txType: TxType = 'other',
bumpGasLimitPercent?: number,
bumpGasPricePercent?: number,
speed?: LegacyGasPriceKey,
): Promise<GetGasParamsRes> {
const [gasPrice, gasLimit, l1Fee] = await Promise.all([
this.getGasPrice(speed, bumpGasPricePercent),
this.getGasLimit(tx, txType, bumpGasLimitPercent),
this.fetchL1OptimismFee(tx),
]);
let l1Fee: string = '0';
if (!gasLimit && !gasPrice) {
[gasPrice, gasLimit, l1Fee] = await Promise.all([
this.getGasPrice(speed, bumpGasPricePercent),
this.getGasLimit(tx, txType, bumpGasLimitPercent),
this.fetchL1OptimismFee(tx),
]);
}
if (!gasLimit) {
[gasLimit, l1Fee] = await Promise.all([
this.getGasLimit(tx, txType, bumpGasLimitPercent),
this.fetchL1OptimismFee(tx),
]);
}
if (!gasPrice) gasPrice = await this.getGasPrice(speed, bumpGasPricePercent);
if (includeL1FeeToGasLimit)
// Include L1 fee in gas limit (divide by gas price before), if l1 fee is 0, gas limit wont change
gasLimit = BigNumberFloat(gasLimit)
.plus(BigNumberFloat(l1Fee).div(BigNumberFloat(gasPrice)))
.decimalPlaces(0, 1)
.toNumber();
return { gasLimit, gasPrice };
return { gasLimit, gasPrice, l1Fee };
}
/**
* Estimates next block gas for signed, unsigned or incomplete Tornado transaction
* @param {GetGasInput} [params] Function input arguments object
* @param {TransactionData} [params.tx] Transaction data in web3 / ethers format
* @param {TxType} [params.txType] Tornado transaction type: withdrawal by user, withdrawal by relayer or 'other'
* @param {number} [params.predefinedGasLimit] Predefined gas limit, if already calculated (no refetching)
* @param {number} [params.predefinedGasPrice] Predefined gas price, if already calculated (no refetching)
* @param {number} [params.bumpGasLimitPercent] Gas limit bump percent to prioritize transaction (if gas limit not predefined, recenlty used)
* @param {number} [params.bumpGasPricePercent] Gas price bump percent to prioritize transaction (if gas price not predefined, rarely used)
* @param {LegacyGasPriceKey} [params.speed] Preferred transaction speed, if uses legacy gas (before EIP-1559)
* @param {TransactionData} [tx] Transaction data in web3 / ethers format
* @param {TxType} [txType=other] Tornado transaction type: withdrawal by user, withdrawal by relayer or 'other'
* @param {number} [bumpGasLimitPercent] Gas limit bump percent to prioritize transaction (recenlty used)
* @param {number} [bumpGasPricePercent] Gas price bump percent to prioritize transaction (rarely used)
* @param {LegacyGasPriceKey} [speed] Preferred transaction speed, if uses legacy gas (before EIP-1559)
* @returns {Promise<HexadecimalStringifiedNumber>} Gas value in WEI (hex-format)
*/
async getGas(params: GetGasInput = {}): Promise<HexadecimalStringifiedNumber> {
const { gasPrice, gasLimit } = await this.getGasParams({ ...params, includeL1FeeToGasLimit: true });
async getGas(
tx?: TransactionData,
txType: TxType = 'other',
bumpGasLimitPercent?: number,
bumpGasPricePercent?: number,
speed?: LegacyGasPriceKey,
): Promise<HexadecimalStringifiedNumber> {
const { gasPrice, gasLimit, l1Fee } = await this.getGasParams(
tx,
txType,
bumpGasLimitPercent,
bumpGasPricePercent,
speed,
);
const gas = BigNumber.from(gasPrice).mul(gasLimit).add(l1Fee);
return BigNumber.from(gasPrice).mul(gasLimit).toHexString();
return gas.toHexString();
}
/**
@ -135,8 +114,8 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
/**
* Estimate next block gas price
* @param {LegacyGasPriceKey} [speed] Preferred transaction speed, if uses legacy gas (before EIP-1559)
* @param {number} [bumpPercent] Gas bump percent to prioritize transaction
* @param {LegacyGasPriceKey} speed Preferred transaction speed, if uses legacy gas (before EIP-1559)
* @param {number} bumpPercent Gas bump percent to prioritize transaction
* @returns {Promise<HexadecimalStringifiedNumber>} Gas price in WEI (hex string)
*/
async getGasPrice(speed?: LegacyGasPriceKey, bumpPercent?: number): Promise<HexadecimalStringifiedNumber> {
@ -146,9 +125,9 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
/**
* Estimates gas limit for transaction (or basic gas limit, if no tx data provided)
* @param {TransactionData} [tx] Transaction data (object in web3 / ethers format)
* @param {TxType} [type] Tornado transaction type: withdrawal by user, withdrawal by relayer, relayer fee check or 'other'
* @param {number} [bumpPercent] Gas bump percent to prioritize transaction
* @param {TransactionData} tx Transaction data (object in web3 / ethers format)
* @param {TxType} type Tornado transaction type: withdrawal by user, withdrawal by relayer, relayer fee check or 'other'
* @param {number} bumpPercent Gas bump percent to prioritize transaction
* @returns {Promise<number>} Gas limit
*/
abstract getGasLimit(tx?: TransactionData, type?: TxType, bumpPercent?: number): Promise<number>;
@ -159,107 +138,61 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
* and if the relayer pays a commission and the transfer of tokens fails, this commission will remain to the relayer.
*
* Refund needed that recipient can use tokens after withdrawal (covers gas fee for send/swap)
* @param {BigNumberish} gasPrice Actual gas price
* @param {InstanceTokenSymbol} tokenSymbol Withdrawal token (currency) symbol - for example, 'dai'
* @returns {HexadecimalStringifiedNumber} Refund amount in WEI (in hex format)
* @returns {Promise<HexadecimalStringifiedNumber>} Refund amount in WEI (in hex format)
*/
calculateRefundInETH(gasPrice: BigNumberish, tokenSymbol: InstanceTokenSymbol): HexadecimalStringifiedNumber {
// Refund only available for non-native tokens on Ethereum Mainnet and Goerli
if (![ChainId.MAINNET, ChainId.GOERLI].includes(this.chainId) || (tokenSymbol as AvailableTokenSymbols) === 'eth')
return '0';
// Notify user about error if incorrect token symbol provided
if (!Object.values(InstanceTokenSymbol).includes(tokenSymbol)) {
console.error(
`Invalid token symbol: ${tokenSymbol}, must be lowercase token from one of Tornado ETH Mainnet pools`,
);
return '0';
}
async calculateRefundInETH(tokenSymbol: InstanceTokenSymbol): Promise<HexadecimalStringifiedNumber> {
// In Tornado we need to calculate refund only on user side, relayer get refund value in proof
const gasPrice = await this.getGasPrice();
const gasLimit = defaultInstanceTokensGasLimit[tokenSymbol];
return BigNumber.from(gasPrice).mul(gasLimit).mul(2).toHexString();
}
/**
* Fetched actual gas price and calculates refund amount
* @param {InstanceTokenSymbol} tokenSymbol Withdrawal token (currency) symbol - for example, 'dai'
* @returns {Promise<HexadecimalStringifiedNumber>} Refund amount in WEI (in hex format)
*/
async fetchRefundInETH(tokenSymbol: InstanceTokenSymbol): Promise<HexadecimalStringifiedNumber> {
const gasPrice = await this.getGasPrice();
return this.calculateRefundInETH(gasPrice, tokenSymbol);
}
/**
* Get refund amount on ETH or Goerli in non-native token
* @param {BigNumberish} gasPrice Actual gas price in ETH
* @param {BigNumberish} tokenPriceInEth Token price in WEI in ETH
* @param {BigNumberish} tokenPriceInEth Token price in WEI in native currency
* @param {HexadecimalStringifiedNumber | number} tokenDecimals Token (currency) decimals
* @param {InstanceTokenSymbol} tokenSymbol Withdrawal token (currency) symbol - for example, 'dai'
* @returns {HexadecimalStringifiedNumber} Refund amount in WEI in selected token (hexed number)
* @returns {Promise<HexadecimalStringifiedNumber>} Refund amount in WEI in selected token (hexed number)
*/
calculateRefundInToken(
gasPrice: BigNumberish,
async calculateRefundInToken(
tokenPriceInEth: BigNumberish,
tokenDecimals: HexadecimalStringifiedNumber | number,
tokenSymbol: InstanceTokenSymbol,
): HexadecimalStringifiedNumber {
const refundInEth = this.calculateRefundInETH(gasPrice, tokenSymbol);
): Promise<HexadecimalStringifiedNumber> {
const refundInEth = await this.calculateRefundInETH(tokenSymbol);
return convertETHToToken(refundInEth, tokenDecimals, tokenPriceInEth).toHexString();
}
/**
* Calculates relayer fee in selected currency (ETH, DAI, BNB etc) in WEI
* @param {number | string} relayerFeePercent Relayer percent (0.4 for ETH Mainnet, for example)
* @param {HexadecimalStringifiedNumber | number} amount Amount in selected currency (10 for 10 ETH, 1000 for 1000 DAI)
* @param {string | number} decimals Decimal places in selected token (currency)
* @returns {HexadecimalStringifiedNumber} Fee in WEI (hexed stingified number)
*/
calculateRelayerFeeInWei(
relayerFeePercent: number | string,
amount: HexadecimalStringifiedNumber | number,
decimals: string | number,
): HexadecimalStringifiedNumber {
return parseUnits(amount.toString(), decimals)
.mul(`${Math.floor(Number(relayerFeePercent) * 1e10)}`)
.div(`${100 * 1e10}`)
.toHexString();
}
/**
* Estimates fee for withdrawal via relayer depending on type: gas bump percent is bigger, if it calculates by user,
* so that the real commission from the relayer side is a little less,
* in order to the relayer can send a transaction without fear that he will go into the red
* @param {GetWithdrawalFeeViaRelayerInput} params Function input arguments object
* @param {TxType} params.txType Tornado transaction type: withdrawal costs calculation from user side or from relayer side
* @param {TransactionData} [params.tx] Transaction data (object in web3 / ethers format)
* @param {number} params.relayerFeePercent Relayer fee percent from the transaction amount (for example, 0.15 for BNB or 0.4 for ETH Mainnet)
* @param {AvailableTokenSymbols | Uppercase<AvailableTokenSymbols>} params.currency Currency symbol
* @param {number | HexadecimalStringifiedNumber } params.amount Withdrawal amount in selected currency
* @param {number | HexadecimalStringifiedNumber } params.decimals Token (currency) decimals
* @param {BigNumberish} [params.refundInEth] Refund in ETH, if withdrawed other tokens on Mainnet (not ETH). Can not be provided, if user-side calculation
* @param {BigNumberish} [params.tokenPriceInEth] If withdrawing other token on Mainnet or Goerli, need to provide token price in ETH (in WEI)
* @param {number} [params.gasLimit] Predefined gas limit, if already calculated (no refetching)
* @param {number} [params.gasPrice] Predefined gas price, if already calculated (no refetching)
*
* @param {TxType} type Tornado transaction type: withdrawal costs calculation from user side or from relayer side
* @param {TransactionData} tx Transaction data (object in web3 / ethers format)
* @param {number} relayerFeePercent Relayer fee percent from the transaction amount (for example, 0.15 for BNB or 0.4 for ETH Mainnet)
* @param {AvailableTokenSymbols | Uppercase<AvailableTokenSymbols>} currency Currency symbol
* @param {number | HexadecimalStringifiedNumber } amount Withdrawal amount in selected currency
* @param {number | HexadecimalStringifiedNumber } decimals Token (currency) decimals
* @param {BigNumberish} [refundInEth=0] Refund in ETH, if withdrawed other tokens on Mainnet (not ETH)
* @param {BigNumberish} [tokenPriceInEth] If withdrawing other token on Mainnet or Goerli, need to provide token price in ETH (in WEI)
* @returns {Promise<HexadecimalStringifiedNumber>} Fee in WEI (hexed string)
*/
async calculateWithdrawalFeeViaRelayer({
tx,
txType,
relayerFeePercent,
currency,
amount,
decimals,
refundInEth,
tokenPriceInEth,
predefinedGasLimit,
predefinedGasPrice,
}: GetWithdrawalFeeViaRelayerInput): Promise<HexadecimalStringifiedNumber> {
const relayerFee = this.calculateRelayerFeeInWei(relayerFeePercent, amount, decimals);
const { gasPrice, gasLimit } = await this.getGasParams({ tx, txType, predefinedGasLimit, predefinedGasPrice });
const gasCosts = BigNumber.from(gasPrice).mul(gasLimit);
async calculateWithdrawalFeeViaRelayer(
type: TxType,
tx: TransactionData,
relayerFeePercent: number,
currency: AvailableTokenSymbols | Uppercase<AvailableTokenSymbols>,
amount: HexadecimalStringifiedNumber | number,
decimals: HexadecimalStringifiedNumber | number,
refundInEth: BigNumberish = 0,
tokenPriceInEth?: BigNumberish,
): Promise<HexadecimalStringifiedNumber> {
const gasCosts = BigNumber.from(await this.getGas(tx, type));
const relayerFee = parseUnits(amount.toString(), decimals)
.mul(`${Math.floor(relayerFeePercent * 1e10)}`)
.div(`${100 * 1e10}`);
if ((this.chainId === ChainId.MAINNET || this.chainId === ChainId.GOERLI) && currency.toLowerCase() != 'eth') {
if (!tokenPriceInEth) {
@ -267,13 +200,10 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
return '0';
}
if (txType === 'user_withdrawal' && refundInEth === undefined)
refundInEth = this.calculateRefundInETH(gasPrice, currency.toLowerCase() as InstanceTokenSymbol);
const feeInEth = BigNumber.from(gasCosts).add(refundInEth || 0);
const feeInEth = gasCosts.add(refundInEth);
return convertETHToToken(feeInEth, decimals, tokenPriceInEth).add(relayerFee).toHexString();
}
return BigNumber.from(gasCosts).add(relayerFee).toHexString();
return gasCosts.add(relayerFee).toHexString();
}
}

View File

@ -4,9 +4,6 @@ import { ITornadoFeeOracle, TransactionData, TxType, LegacyGasPrices } from './t
import { GasPriceOracle } from '@tornado/gas-price-oracle';
import { bump } from './utils';
/**
* Oracle for V4 (old-version) transactions - estimates fee with predefined gas limit and without smart bumping
*/
export class TornadoFeeOracleV4 extends TornadoFeeOracle implements ITornadoFeeOracle {
public constructor(chainId: number, rpcUrl: string, defaultGasPrices?: LegacyGasPrices) {
const oracleConfig = {
@ -16,7 +13,7 @@ export class TornadoFeeOracleV4 extends TornadoFeeOracle implements ITornadoFeeO
};
const gasPriceOracle = new GasPriceOracle(oracleConfig);
super(4, chainId, rpcUrl, gasPriceOracle);
super(chainId, rpcUrl, gasPriceOracle);
}
async getGasLimit(tx?: TransactionData, type: TxType = 'other', bumpPercent: number = 0): Promise<number> {
@ -29,7 +26,7 @@ export class TornadoFeeOracleV4 extends TornadoFeeOracle implements ITornadoFeeO
// For compatibility reasons, when wee check user-provided fee for V4 withdrawal transaction, we need dump gas limit
// for about 20 percent,so that the transaction will be sent, even if it results in some loss for the relayer
if (type === 'relayer_withdrawal_check_v4') return bump(defaultWithdrawalGasLimit[this.chainId], -25).toNumber();
if (!tx || Object.keys(tx).length === 0) return bump(23_000, bumpPercent).toNumber();
if (!tx || Object.keys(tx).length === 0) return bump(21_000, bumpPercent).toNumber();
return bump(await this.provider.estimateGas(tx), bumpPercent).toNumber();
}

View File

@ -12,9 +12,6 @@ import { GasPriceOracle } from '@tornado/gas-price-oracle';
import { bump } from './utils';
import { TornadoFeeOracleV4 } from './feeOracleV4';
/**
* Oracle for new V5 version - estimates transaction fees with smart gas limit & bumping
*/
export class TornadoFeeOracleV5 extends TornadoFeeOracle implements ITornadoFeeOracle {
private fallbackFeeOracle: TornadoFeeOracleV4;
@ -29,7 +26,7 @@ export class TornadoFeeOracleV5 extends TornadoFeeOracle implements ITornadoFeeO
};
const gasPriceOracle = new GasPriceOracle(oracleConfig);
super(5, chainId, rpcUrl, gasPriceOracle);
super(chainId, rpcUrl, gasPriceOracle);
this.fallbackFeeOracle = new TornadoFeeOracleV4(chainId, rpcUrl, defaultGasPrices);
}

View File

@ -40,10 +40,9 @@ export class TokenPriceOracle implements ITornadoPriceOracle {
private provider: Provider;
/**
* Constructs TokenPriceOracle class instance
* @param {string} rpcUrl http RPC (Ethereum Mainnet) url to fetch token prices from contract
* @param {Token[]} [tokens] Array of tokens
* @param {TokenPrices} [defaultTokenPrices] Default token prices, fallback if nothing loaded from contract
*
* @param rpcUrl
* @param tokens
*/
constructor(
rpcUrl: string,
@ -62,7 +61,7 @@ export class TokenPriceOracle implements ITornadoPriceOracle {
/**
* Prepare data for MultiCall contract
* @param {Token[]} [tokens] Tokens array
* @param {Token[]} tokens Tokens array
* @returns Valid structure to provide to MultiCall contract
*/
private prepareCallData(tokens: Token[] = this.tokens): MultiCall.CallStruct[] {
@ -74,7 +73,7 @@ export class TokenPriceOracle implements ITornadoPriceOracle {
/**
* Fetch actual tokens price rate to ETH from offchain oracles
* @param {Token[]} [tokens] Token array
* @param {Token[]} tokens Token array
* @returns {TokenPrices} Object with token price rate to ETH in WEI
*/
async fetchPrices(tokens: Token[] = this.tokens): Promise<TokenPrices> {

View File

@ -18,8 +18,7 @@ export type LegacyGasPrices = {
- 'relayer_withdrawal_check_v4' - Fee calculation on relayer side, when V4 relayer checks user-provided fee. For compatibility reasons
- 'other' - Any other non-specific transaction
*/
export type WithdrawalTxType = 'user_withdrawal' | 'relayer_withdrawal' | 'relayer_withdrawal_check_v4';
export type TxType = WithdrawalTxType | 'other';
export type TxType = 'user_withdrawal' | 'relayer_withdrawal' | 'relayer_withdrawal_check_v4' | 'other';
export interface TransactionData {
to: string;
@ -36,21 +35,40 @@ export interface TransactionData {
}
export interface ITornadoFeeOracle {
getGasParams: (params?: GetGasParamsInput) => Promise<GetGasParamsRes>;
getGas: (params?: GetGasInput) => Promise<HexadecimalStringifiedNumber>;
getGasParams: (
tx?: TransactionData,
txType?: TxType,
bumpGasLimitPercent?: number,
bumpGasPricePercent?: number,
speed?: LegacyGasPriceKey,
) => Promise<GetGasParamsRes>;
getGas: (
tx?: TransactionData,
type?: TxType,
bumpGasLimitPercent?: number,
bumpGasPricePercent?: number,
speed?: LegacyGasPriceKey,
) => Promise<HexadecimalStringifiedNumber>;
getGasPriceParams: (speed?: LegacyGasPriceKey, bumpPercent?: number) => Promise<GasPriceParams>;
getGasPrice: (speed?: LegacyGasPriceKey, bumpPercent?: number) => Promise<HexadecimalStringifiedNumber>;
getGasLimit: (tx?: TransactionData, type?: TxType, bumpPercent?: number) => Promise<number>;
fetchL1OptimismFee: (tx?: TransactionData) => Promise<HexadecimalStringifiedNumber>;
calculateRefundInETH: (gasPrice: BigNumberish, tokenSymbol: InstanceTokenSymbol) => HexadecimalStringifiedNumber;
fetchRefundInETH: (tokenSymbol: InstanceTokenSymbol) => Promise<HexadecimalStringifiedNumber>;
calculateRefundInETH: (tokenSymbol: InstanceTokenSymbol) => Promise<HexadecimalStringifiedNumber>;
calculateRefundInToken: (
gasPrice: BigNumberish,
tokenPriceInEth: BigNumberish,
tokenDecimals: HexadecimalStringifiedNumber | number,
tokenSymbol: InstanceTokenSymbol,
) => HexadecimalStringifiedNumber;
calculateWithdrawalFeeViaRelayer: (params: GetWithdrawalFeeViaRelayerInput) => Promise<HexadecimalStringifiedNumber>;
) => Promise<HexadecimalStringifiedNumber>;
calculateWithdrawalFeeViaRelayer: (
type: TxType,
tx: TransactionData,
relayerFeePercent: number,
currency: AvailableTokenSymbols,
amount: HexadecimalStringifiedNumber | number,
decimals: number,
refundInEth: BigNumberish,
tokenPriceInEth?: BigNumberish,
) => Promise<HexadecimalStringifiedNumber>;
}
export interface ITornadoPriceOracle {
@ -81,34 +99,6 @@ export type TokenPrices = { [tokenSymbol in TokenSymbol]?: BigNumberish };
// Reponse type for getGasParams function of fee oracle
export type GetGasParamsRes = {
gasLimit: number;
gasPrice: HexadecimalStringifiedNumber; // Gas price in native currency
};
export type GetGasInput = {
// Transaction type: user-side calculation, relayer-side calculation or
// relayer calculation to check user-provided fee in old V4 relayer (for backwards compatibility)
txType?: TxType;
tx?: TransactionData; // Transaction data in ethers format
predefinedGasPrice?: HexadecimalStringifiedNumber; // Predefined gas price for withdrawal tx (wont be calculated again in function)
predefinedGasLimit?: number; // Predefined gas limit for withdrawal tx (wont be calculated again in function)
bumpGasLimitPercent?: number; // Gas limit bump percent to prioritize transaction (recenlty used)
bumpGasPricePercent?: number; // Gas price bump percent to prioritize transaction (rarely used)
speed?: LegacyGasPriceKey; // Preferred transaction speed, if uses legacy gas (before EIP-1559)
};
export type GetGasParamsInput = GetGasInput & { includeL1FeeToGasLimit?: boolean };
export type GetWithdrawalFeeViaRelayerInput = {
// Transaction type: user-side calculation, relayer-side calculation or
// relayer calculation to check user-provided fee in old V4 relayer (for backwards compatibility)
txType: WithdrawalTxType;
tx?: TransactionData; // Transaction data in ethers format
relayerFeePercent: number | string; // Relayer fee percent from withdrawal amount (for example, 0.15 for BNB or 0.4 for ETH Mainnet)
currency: AvailableTokenSymbols | Uppercase<AvailableTokenSymbols>; // Currency (token) symbol
amount: string | number; // Withdrawal amount in selected currency (10 for 10 ETH, 10000 for 10000 DAI)
decimals: string | number; // Token (currency) decimal places
refundInEth?: HexadecimalStringifiedNumber; // Refund amount in ETH, if withdrawing non-native currency on ETH Mainnet or Goerli
tokenPriceInEth?: HexadecimalStringifiedNumber | string; // Token (currency) price in ETH wei, if withdrawing non-native currency
predefinedGasPrice?: HexadecimalStringifiedNumber; // Predefined gas price for withdrawal tx (wont be calculated again in function)
predefinedGasLimit?: number; // Predefined gas limit for withdrawal tx (wont be calculated again in function)
gasPrice: HexadecimalStringifiedNumber;
l1Fee: HexadecimalStringifiedNumber;
};

View File

@ -380,7 +380,7 @@
"@tornado/tornado-config@^2.0.0":
version "2.0.0"
resolved "https://git.tornado.is/api/packages/tornado-packages/npm/%40tornado%2Ftornado-config/-/2.0.0/tornado-config-2.0.0.tgz"
resolved "https://git.tornado.is/api/packages/tornado-packages/npm/%40tornado%2Ftornado-config/-/2.0.0/tornado-config-2.0.0.tgz#81b67bb946326ae8b1d359e8829d2f23ca6aeb2d"
integrity sha512-XckaC3A214du4UWXqBCB75NpqYGX8qL7tkTL9ttS+IQKpjOhqehmClOJGl51JXQGlQlgNSg8l2E+GUWt1kHaBw==
"@tsconfig/node10@^1.0.7":