Compare commits
2 Commits
b7769d4548
...
c50caf3a48
Author | SHA1 | Date | |
---|---|---|---|
c50caf3a48 | |||
2da3bf094f |
87
README.md
Normal file
87
README.md
Normal 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)
|
@ -60,7 +60,7 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle {
|
|||||||
* @param {TxType} type Tornado transaction type (to select correct default bump percent)
|
* @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 {LegacyGasPriceKey} speed Preferred transaction speed, if uses legacy gas (before EIP-1559)
|
||||||
* @param {number} bumpPercent Gas bump percent to prioritize transaction
|
* @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
|
* and maxPriorityFeePerGas properties
|
||||||
*/
|
*/
|
||||||
async getGasPrice(
|
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)
|
* Estimates gas limit for transaction (or basic gas limit, if no tx data provided)
|
||||||
* @param {TransactionData} tx Transaction data (object in web3 / ethers format)
|
* @param {TransactionData} tx Transaction data (object in web3 / ethers format)
|
||||||
|
@ -17,12 +17,12 @@ export class TornadoFeeOracleV4 extends TornadoFeeOracle implements ITornadoFeeO
|
|||||||
super(chainId, rpcUrl, gasPriceOracle);
|
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]);
|
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
|
// 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
|
// 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);
|
if (!tx) return BigNumber.from(21_000);
|
||||||
|
|
||||||
return this.provider.estimateGas(tx);
|
return this.provider.estimateGas(tx);
|
||||||
|
@ -28,6 +28,7 @@ export interface TransactionData {
|
|||||||
export interface ITornadoFeeOracle {
|
export interface ITornadoFeeOracle {
|
||||||
getGas: (tx?: TransactionData, type?: TxType) => Promise<BigNumber>;
|
getGas: (tx?: TransactionData, type?: TxType) => Promise<BigNumber>;
|
||||||
getGasPrice: (type?: TxType, speed?: LegacyGasPriceKey, bumpPercent?: number) => Promise<GasPrice>;
|
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>;
|
getGasLimit: (tx?: TransactionData, type?: TxType, bumpPercent?: number) => Promise<BigNumber>;
|
||||||
calculateRefundInETH: () => Promise<BigNumber>;
|
calculateRefundInETH: () => Promise<BigNumber>;
|
||||||
calculateWithdrawalFeeViaRelayer: (
|
calculateWithdrawalFeeViaRelayer: (
|
||||||
|
@ -15,7 +15,7 @@ export function serializeTx(tx?: TransactionData | string): string {
|
|||||||
|
|
||||||
export function calculateGasPriceInWei(gasPrice: GasPrice): BigNumber {
|
export function calculateGasPriceInWei(gasPrice: GasPrice): BigNumber {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
return gasPrice.gasPrice || gasPrice.maxFeePerGas;
|
return BigNumber.from(gasPrice.gasPrice || gasPrice.maxFeePerGas);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function bump(value: BigNumberish, percent: number): BigNumber {
|
export function bump(value: BigNumberish, percent: number): BigNumber {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user