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);