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",
"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",

@ -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<HexadecimalStringifiedNumber>} Fee in WEI (hexed string)
*/
async calculateWithdrawalFeeViaRelayer(
params: GetWithdrawalFeeViaRelayerInput,
): Promise<HexadecimalStringifiedNumber> {
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<HexadecimalStringifiedNumber> {
const relayerFee = this.calculateRelayerFeeInWei(relayerFeePercent, amount, decimals);
const { gasPrice, gasLimit } = await this.getGasParams({ tx, txType, predefinedGasLimit, predefinedGasPrice });
const gasCosts = BigNumber.from(gasPrice).mul(gasLimit);