Compare commits

...

2 Commits

Author SHA1 Message Date
c50caf3a48 Add readme with examples 2023-08-18 23:01:33 -07:00
2da3bf094f Add function to get gas price as hex string 2023-08-18 23:01:18 -07:00
5 changed files with 108 additions and 4 deletions

87
README.md Normal file
View File

@ -0,0 +1,87 @@
# Tornado oracles
This is a library providing convenient and fast access to oracles for Tornado-specific transactions, for example, withdrawal via relayer or getting rate to ETH for tokens that used in Tornado pools
### Installation
<hr>
* Create `.npmrc` file in project root with content `@tornado:registry=https://git.tornado.ws/api/packages/tornado-packages/npm/`
* Run `npm i @tornado/tornado-oracles`
### Import
<hr>
```typescript
const {TornadoFeeOracleV5, TornadoFeeOracleV5, TokenPriceOracle} = require('@tornado/tornado-oracles')
or
import {TornadoFeeOracleV5, TornadoFeeOracleV5, TokenPriceOracle} from '@tornado/tornado-oracles'
```
### Usage
<hr>
##### Estimate withdrawal gas costs
```typescript
import { TornadoFeeOracleV5 } from '@tornado/tornado-oracles'
const tx: TransactionData = {
to: tornadoProxyLightAddress,
data: poolInstance.methods.withdraw(...params).encodeABI(),
value: withdrawalProofArgs[5]
}
const feeOracle = new TornadoFeeOracleV5(1, "https://eth.llamarpc.com"); // First parameter - chain ID
const withdrawalGas = await feeOracle.getGas(tx, "relayer_withdrawal");
```
##### Estimate gas price and gas limit to send transaction
```typescript
import { TornadoFeeOracleV5 } from '@tornado/tornado-oracles'
const incompleteTx: TransactionData = {
to: tornadoProxyLightAddress,
data: poolInstance.methods.withdraw(...params).encodeABI(),
value: withdrawalProofArgs[5]
}
const feeOracle = new TornadoFeeOracleV5(1, "https://eth.llamarpc.com");
const transactionType: TxType = "relayer_withdrawal";
const gasPrice = await feeOracle.getGasPriceInHex(transactionType);
const gasLimit = await feeOracle.getGasLimit(incompleteTx, transactionType);
const tx: TransactionData = Object.assign({ gasPrice, gasLimit }, incompleteTx);
```
##### Get token prices (rate to ETH) for tokens that used in Tornado
```typescript
import { TokenPriceOracle } from '@tornado/tornado-oracles'
const priceOracle = new TokenPriceOracle("https://eth.llamarpc.com");
const tokenPrices = await priceOracle.fetchPrices();
console.log(tokenPrices); // All prices in WEI
/*
{
torn: '1653773547906175',
dai: '603108348359886',
cdai: '13487984643748',
usdc: '601311723569085',
usdt: '602058974373161',
wbtc: '15696224089898846959'
}
*/
```
### License
[MIT](LICENSE)

View File

@ -60,7 +60,7 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
* @param {TxType} type Tornado transaction type (to select correct default bump percent)
* @param {LegacyGasPriceKey} speed Preferred transaction speed, if uses legacy gas (before EIP-1559)
* @param {number} bumpPercent Gas bump percent to prioritize transaction
* @returns {GasPrice} Estimated gas price info in WEI (hexed) - legacy object with gasPrice property or EIP-1559 object with maxFeePerGas
* @returns {Promise<GasPrice>} Estimated gas price info in WEI (hexed) - legacy object with gasPrice property or EIP-1559 object with maxFeePerGas
* and maxPriorityFeePerGas properties
*/
async getGasPrice(
@ -75,6 +75,22 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
}
}
/**
* Estimate next block gas price
* @param {TxType} type Tornado transaction type (to select correct default bump percent)
* @param {LegacyGasPriceKey} speed Preferred transaction speed, if uses legacy gas (before EIP-1559)
* @param {number} bumpPercent Gas bump percent to prioritize transaction
* @returns {Promise<string>} Gas price in WEI (hex string)
*/
async getGasPriceInHex(
type: TxType = 'other',
speed: LegacyGasPriceKey = 'fast',
bumpPercent: number = 0,
): Promise<string> {
const gasPrice = await this.getGasPrice(type, speed, bumpPercent);
return calculateGasPriceInWei(gasPrice).toHexString();
}
/**
* Estimates gas limit for transaction (or basic gas limit, if no tx data provided)
* @param {TransactionData} tx Transaction data (object in web3 / ethers format)

View File

@ -17,12 +17,12 @@ export class TornadoFeeOracleV4 extends TornadoFeeOracle implements ITornadoFeeO
super(chainId, rpcUrl, gasPriceOracle);
}
async getGasLimit(tx?: TransactionData, type: TxType = 'other'): Promise<BigNumber> {
async getGasLimit(tx?: TransactionData, type: TxType = 'other', bumpPercent?: number): Promise<BigNumber> {
if (type === 'user_withdrawal') return BigNumber.from(defaultWithdrawalGasLimit[this.chainId]);
// Need to bump relayer gas limit for transaction, because predefined gas limit to small to be 100% sure that transaction will be sent
// This leads to fact that relayer often pays extra for gas from his own funds, however, this was designed by previous developers
if (type === 'relayer_withdrawal') return bump(defaultWithdrawalGasLimit[this.chainId], 20);
if (type === 'relayer_withdrawal') return bump(defaultWithdrawalGasLimit[this.chainId], bumpPercent || 20);
if (!tx) return BigNumber.from(21_000);
return this.provider.estimateGas(tx);

View File

@ -28,6 +28,7 @@ export interface TransactionData {
export interface ITornadoFeeOracle {
getGas: (tx?: TransactionData, type?: TxType) => Promise<BigNumber>;
getGasPrice: (type?: TxType, speed?: LegacyGasPriceKey, bumpPercent?: number) => Promise<GasPrice>;
getGasPriceInHex: (type?: TxType, speed?: LegacyGasPriceKey, bumpPercent?: number) => Promise<string>;
getGasLimit: (tx?: TransactionData, type?: TxType, bumpPercent?: number) => Promise<BigNumber>;
calculateRefundInETH: () => Promise<BigNumber>;
calculateWithdrawalFeeViaRelayer: (

View File

@ -15,7 +15,7 @@ export function serializeTx(tx?: TransactionData | string): string {
export function calculateGasPriceInWei(gasPrice: GasPrice): BigNumber {
// @ts-ignore
return gasPrice.gasPrice || gasPrice.maxFeePerGas;
return BigNumber.from(gasPrice.gasPrice || gasPrice.maxFeePerGas);
}
export function bump(value: BigNumberish, percent: number): BigNumber {