diff --git a/package.json b/package.json index ecac8ec..9cae251 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/feeOracle.ts b/src/feeOracle.ts index d5b2737..797b329 100644 --- a/src/feeOracle.ts +++ b/src/feeOracle.ts @@ -224,7 +224,7 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle { * @param {AvailableTokenSymbols | Uppercase} 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(); } diff --git a/src/types.ts b/src/types.ts index ebdd709..b21a705 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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) };