From 65af25e76b7a4e81f0171e6441d43289c0470bd5 Mon Sep 17 00:00:00 2001 From: Theo Date: Wed, 6 Sep 2023 15:43:31 -0700 Subject: [PATCH] Return zero value from `calculateRefundInETH` function if called on incorrect chain or with native token or tokenSymbol is invalid & bump package version to 3.3.0 --- package.json | 5 +++-- src/feeOracle.ts | 41 +++++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 4c7ed06..1c5baa1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tornado/tornado-oracles", - "version": "3.2.1", + "version": "3.3.0", "description": "Oracles for Tornado-specific transactions & actions", "main": "./lib/index.js", "types": "./lib/index.d.ts", @@ -11,7 +11,8 @@ "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" + "prepare": "npm run build && npm run build:esm", + "prepublish": "npm run prettier:fix" }, "repository": { "type": "git", diff --git a/src/feeOracle.ts b/src/feeOracle.ts index 5b15fdc..e8c9019 100644 --- a/src/feeOracle.ts +++ b/src/feeOracle.ts @@ -18,6 +18,7 @@ import { ChainId, defaultGasPrices, defaultInstanceTokensGasLimit, InstanceToken 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; @@ -163,6 +164,18 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle { * @returns {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'; + } + // In Tornado we need to calculate refund only on user side, relayer get refund value in proof const gasLimit = defaultInstanceTokensGasLimit[tokenSymbol]; return BigNumber.from(gasPrice).mul(gasLimit).mul(2).toHexString(); @@ -232,22 +245,18 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle { * * @returns {Promise} Fee in WEI (hexed string) */ - async calculateWithdrawalFeeViaRelayer( - params: GetWithdrawalFeeViaRelayerInput, - ): Promise { - let { - tx, - txType, - relayerFeePercent, - currency, - amount, - decimals, - refundInEth, - tokenPriceInEth, - predefinedGasLimit, - predefinedGasPrice, - } = params; - + async calculateWithdrawalFeeViaRelayer({ + tx, + txType, + relayerFeePercent, + currency, + amount, + decimals, + refundInEth, + tokenPriceInEth, + predefinedGasLimit, + predefinedGasPrice, + }: GetWithdrawalFeeViaRelayerInput): Promise { const relayerFee = this.calculateRelayerFeeInWei(relayerFeePercent, amount, decimals); const { gasPrice, gasLimit } = await this.getGasParams({ tx, txType, predefinedGasLimit, predefinedGasPrice }); const gasCosts = BigNumber.from(gasPrice).mul(gasLimit);