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

This commit is contained in:
Theo 2023-09-06 15:43:31 -07:00
parent 2ce8cafdbd
commit 65af25e76b
2 changed files with 28 additions and 18 deletions

@ -1,6 +1,6 @@
{ {
"name": "@tornado/tornado-oracles", "name": "@tornado/tornado-oracles",
"version": "3.2.1", "version": "3.3.0",
"description": "Oracles for Tornado-specific transactions & actions", "description": "Oracles for Tornado-specific transactions & actions",
"main": "./lib/index.js", "main": "./lib/index.js",
"types": "./lib/index.d.ts", "types": "./lib/index.d.ts",
@ -11,7 +11,8 @@
"prettier:fix": "prettier --write . --config .prettierrc", "prettier:fix": "prettier --write . --config .prettierrc",
"build:abi": "yarn typechain --target ethers-v5 --out-dir src/contracts ./abis/*.abi.json", "build:abi": "yarn typechain --target ethers-v5 --out-dir src/contracts ./abis/*.abi.json",
"test": "echo \"Error: no test specified\" && exit 1", "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": { "repository": {
"type": "git", "type": "git",

@ -18,6 +18,7 @@ import { ChainId, defaultGasPrices, defaultInstanceTokensGasLimit, InstanceToken
import { bump, calculateGasPriceInWei, convertETHToToken, fromGweiToWeiHex, serializeTx } from './utils'; import { bump, calculateGasPriceInWei, convertETHToToken, fromGweiToWeiHex, serializeTx } from './utils';
import { getOptimismL1FeeOracle } from './contracts/factories'; import { getOptimismL1FeeOracle } from './contracts/factories';
import { GetWithdrawalFeeViaRelayerInput } from './types'; import { GetWithdrawalFeeViaRelayerInput } from './types';
import { AvailableTokenSymbols } from '@tornado/tornado-config';
export abstract class TornadoFeeOracle implements ITornadoFeeOracle { export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
protected provider: JsonRpcProvider; protected provider: JsonRpcProvider;
@ -163,6 +164,18 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
* @returns {HexadecimalStringifiedNumber} Refund amount in WEI (in hex format) * @returns {HexadecimalStringifiedNumber} Refund amount in WEI (in hex format)
*/ */
calculateRefundInETH(gasPrice: BigNumberish, tokenSymbol: InstanceTokenSymbol): HexadecimalStringifiedNumber { 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 // In Tornado we need to calculate refund only on user side, relayer get refund value in proof
const gasLimit = defaultInstanceTokensGasLimit[tokenSymbol]; const gasLimit = defaultInstanceTokensGasLimit[tokenSymbol];
return BigNumber.from(gasPrice).mul(gasLimit).mul(2).toHexString(); return BigNumber.from(gasPrice).mul(gasLimit).mul(2).toHexString();
@ -232,10 +245,7 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
* *
* @returns {Promise<HexadecimalStringifiedNumber>} Fee in WEI (hexed string) * @returns {Promise<HexadecimalStringifiedNumber>} Fee in WEI (hexed string)
*/ */
async calculateWithdrawalFeeViaRelayer( async calculateWithdrawalFeeViaRelayer({
params: GetWithdrawalFeeViaRelayerInput,
): Promise<HexadecimalStringifiedNumber> {
let {
tx, tx,
txType, txType,
relayerFeePercent, relayerFeePercent,
@ -246,8 +256,7 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
tokenPriceInEth, tokenPriceInEth,
predefinedGasLimit, predefinedGasLimit,
predefinedGasPrice, predefinedGasPrice,
} = params; }: GetWithdrawalFeeViaRelayerInput): Promise<HexadecimalStringifiedNumber> {
const relayerFee = this.calculateRelayerFeeInWei(relayerFeePercent, amount, decimals); const relayerFee = this.calculateRelayerFeeInWei(relayerFeePercent, amount, decimals);
const { gasPrice, gasLimit } = await this.getGasParams({ tx, txType, predefinedGasLimit, predefinedGasPrice }); const { gasPrice, gasLimit } = await this.getGasParams({ tx, txType, predefinedGasLimit, predefinedGasPrice });
const gasCosts = BigNumber.from(gasPrice).mul(gasLimit); const gasCosts = BigNumber.from(gasPrice).mul(gasLimit);