2023-08-18 23:01:33 -07:00
# 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 >
2023-08-21 09:18:49 -07:00
- 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`
2023-08-18 23:01:33 -07:00
### Import
< hr >
```typescript
2023-08-21 09:18:49 -07:00
const { TornadoFeeOracleV5, TornadoFeeOracleV5, TokenPriceOracle } = require('@tornado/tornado-oracles');
2023-08-18 23:01:33 -07:00
or
2023-08-21 09:18:49 -07:00
import { TornadoFeeOracleV5, TornadoFeeOracleV5, TokenPriceOracle } from '@tornado/tornado-oracles';
2023-08-18 23:01:33 -07:00
```
### Usage
< hr >
##### Estimate withdrawal gas costs
```typescript
2023-08-21 09:18:49 -07:00
import { TornadoFeeOracleV5 } from '@tornado/tornado-oracles';
2023-08-18 23:01:33 -07:00
const tx: TransactionData = {
2023-08-21 09:18:49 -07:00
to: tornadoProxyLightAddress,
data: poolInstance.methods.withdraw(...params).encodeABI(),
value: withdrawalProofArgs[5],
};
2023-08-18 23:01:33 -07:00
2023-08-21 09:18:49 -07:00
const feeOracle = new TornadoFeeOracleV5(1, 'https://eth.llamarpc.com'); // First parameter - chain ID
2023-09-04 13:26:46 -07:00
const withdrawalGas = await feeOracle.getGas({tx, txType: 'relayer_withdrawal'});
2023-08-18 23:01:33 -07:00
```
##### Estimate gas price and gas limit to send transaction
```typescript
2023-08-21 09:18:49 -07:00
import { TornadoFeeOracleV5 } from '@tornado/tornado-oracles';
2023-08-18 23:01:33 -07:00
const incompleteTx: TransactionData = {
2023-08-21 09:18:49 -07:00
to: tornadoProxyLightAddress,
data: poolInstance.methods.withdraw(...params).encodeABI(),
value: withdrawalProofArgs[5],
};
2023-08-18 23:01:33 -07:00
2023-08-21 09:18:49 -07:00
const feeOracle = new TornadoFeeOracleV5(1, 'https://eth.llamarpc.com');
const transactionType: TxType = 'relayer_withdrawal';
2023-09-04 13:26:46 -07:00
const { gasPrice, gasLimit } = await feeOracle.getGasParams({tx: incompleteTx, txType: transactionType});
2023-08-18 23:01:33 -07:00
2023-09-04 13:26:46 -07:00
const tx: TransactionData = {...incompleteTx, gasPrice, gasLimit}
2023-08-18 23:01:33 -07:00
```
##### Get token prices (rate to ETH) for tokens that used in Tornado
```typescript
2023-08-21 09:18:49 -07:00
import { TokenPriceOracle } from '@tornado/tornado-oracles';
2023-08-18 23:01:33 -07:00
2023-08-21 09:18:49 -07:00
const priceOracle = new TokenPriceOracle('https://eth.llamarpc.com');
2023-08-18 23:01:33 -07:00
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
2023-08-21 09:18:49 -07:00
[MIT ](LICENSE )