Calculate refund in calculateWithdrawalFeeViaRelayer, if correct value didn't provided when calculating for user_withdrawal transaction type & bump to 3.1.0

This commit is contained in:
Theo 2023-09-04 13:50:35 -07:00
parent be7f911a17
commit 9ef88948d2
3 changed files with 11 additions and 7 deletions

@ -1,6 +1,6 @@
{
"name": "@tornado/tornado-oracles",
"version": "3.0.0",
"version": "3.1.0",
"description": "Oracles for Tornado-specific transactions & actions",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",

@ -224,7 +224,7 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
* @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=0] Refund in ETH, if withdrawed other tokens on Mainnet (not ETH)
* @param {BigNumberish} [params.refundInEth=0] 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)
@ -243,12 +243,13 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
decimals,
refundInEth = 0,
tokenPriceInEth,
gasLimit,
gasPrice,
predefinedGasLimit,
predefinedGasPrice,
} = params;
const relayerFee = this.calculateRelayerFeeInWei(relayerFeePercent, amount, decimals);
const gasCosts = await this.getGas({ tx, txType, predefinedGasLimit: gasLimit, predefinedGasPrice: gasPrice });
const { gasPrice, gasLimit } = await this.getGasParams({ tx, txType, predefinedGasLimit, predefinedGasPrice });
const gasCosts = BigNumber.from(gasPrice).mul(gasLimit);
if ((this.chainId === ChainId.MAINNET || this.chainId === ChainId.GOERLI) && currency.toLowerCase() != 'eth') {
if (!tokenPriceInEth) {
@ -256,6 +257,9 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
return '0';
}
if (txType === 'user_withdrawal' && !refundInEth)
refundInEth = this.calculateRefundInETH(gasPrice, currency.toLowerCase() as InstanceTokenSymbol);
const feeInEth = BigNumber.from(gasCosts).add(refundInEth);
return convertETHToToken(feeInEth, decimals, tokenPriceInEth).add(relayerFee).toHexString();
}

@ -109,6 +109,6 @@ export type GetWithdrawalFeeViaRelayerInput = {
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
gasPrice?: HexadecimalStringifiedNumber; // Predefined gas price for withdrawal tx (wont be calculated again in function)
gasLimit?: number; // Predefined gas limit for withdrawal tx (wont be calculated again in function)
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)
};