sdk-monorepo/docs/USAGE.md

183 lines
5.0 KiB
Markdown
Raw Normal View History

2023-05-11 18:49:07 +00:00
# Usage Examples
2023-05-11 18:49:07 +00:00
### Build a deposit transaction
```ts
import * as Tornado from "@tornado/sdk"
2023-05-11 18:49:07 +00:00
async function main() {
// Get the core Tornado Cash logic.
const core = new Tornado.Core()
// Build tx
const tx = core.buildDepositTransaction(core.getInstance("usdc", "100"))
// Print the tx to console
console.log(tx)
}
main()
```
### Build a withdrawal transaction
```ts
import * as Tornado from "@tornado/sdk"
async function main() {
// The address to receive the funds...
const receiverAddress = "0x0000000000000000000000000000000000000000"
// Get the core Tornado Cash logic.
const core = new Tornado.Core()
// Get a regular ethers v5 provider.
const provider = new providers.JsonRpcProvider("ENTER YOUR RPC URL HERE!")
// This time we need to connect the provider
await core.connect(provider)
// We also need a relayer
const relayer = new Tornado.Web.Relayer({
url: "https://" + "ENTER YOUR CHOSEN RELAYER DOMAIN HERE",
// Web can also instead provide a TorHttpClient or an (ethers v5) TorProvider
httpClient: new Tornado.Web.RegularHttpClient()
})
// We always have to fetch the current properties of a relayer
await relayer.fetchProperties()
// Once that is done let's get an instance we have a proof of
const instance = core.getInstance("eth", "0.1")
// We have to load the note, the arguments can be
// indexes - indexes according to which you may choose the notes in cache
// keys - the keys according to which you may choose the notes in cache
// In our case we've set indexes to undefined and choosing our notes according to the instance
// And then selecting the first one of those
const note = (await core.loadNotes(undefined, {
network: core.chain.id,
token: "eth",
denomination: "0.1"
}))[0]
// Now build the proof
const proof = await core.buildDepositProof(instance, relayer.properties, receiverAddress, note, {
// Defaults
checkNotesSpent: true,
checkKnownRoot: true,
merkleTreeHeight: 20,
tokenDecimals: 18,
ethPurchaseAmounts: [BigNumber.from(0)],
gasPrice: undefined,
gasPriceCushion: undefined,
})
console.log(proof)
}
main()
```
### Synchronize Deposit Events Over Clearnet Without Sync Logs
```ts
import { providers } from "ethers"
import * as Tornado from "@tornado/sdk"
async function sync() {
2023-05-11 18:49:07 +00:00
// Get a regular ethers v5 provider.
const provider = new providers.JsonRpcProvider("ENTER YOUR RPC URL HERE!")
// Get the core Tornado Cash logic.
const core = new Tornado.Core()
// Connect the provider (necessary)
await core.connect(provider)
// Get the instance to sync
const instance = await core.getInstance("eth", 0.1)
// Sync! Output will be in the project dir in the cache folder
await core.syncDeposits(instance)
// Now export it as an archive!
await core.exportAsArchive('Deposits1ETH0.1')
}
2023-05-11 18:49:07 +00:00
```
### Synchronize Deposit Events Over Tor Without Sync Logs
```ts
import * as Tornado from "@tornado/sdk"
async function sync() {
// Get a torified ethers v5 provider.
// You can set the port to 9150 if you use Tor Browser!
const provider = new Tornado.Web.TorProvider("ENTER YOUR RPC URL HERE!", { port: 9050 })
// Get the core Tornado Cash logic.
const core = new Tornado.Core()
// Connect the provider (necessary)
await core.connect(provider)
// Get the instance to sync
const instance = await core.getInstance("eth", 0.1)
// Sync!
await core.syncDeposits(instance,
{
// In this example, we're forcing to sync from the start block such that
// if some RPC doesn't index all events, that we can rerun and insert other
// elements not found in the former. PouchDB handles the insertions.
startBlock: Tornado.Data.Onchain.getInstanceDeployBlockNumSync("1", "eth", "0.1")
}
)
// Export as archive again!
await core.exportAsArchive('Deposits1ETH0.1')
}
sync()
```
### Synchronize Deposit Events With Custom Logs
```ts
import * as Tornado from "@tornado/sdk"
/// You can implement this however you want.
function logListener(...args: any[]) {
if (args.length === 3) {
console.debug(`\nSync will be started with SB: ${args[0]}, TB: ${args[1]}, BD: ${args[2]}\n`)
} else if (args.length == 2) {
console.debug(`Syncing from block ${args[0]} to ${args[1]}`)
}
}
async function sync() {
// Get a torified ethers v5 provider.
const provider = new Tornado.Web.TorProvider("https://eth.llamarpc.com", { port: 9150 })
// Get the core Tornado Cash logic.
const core = new Tornado.Core()
// Connect the provider (necessary)
await core.connect(provider)
// Get the instance to sync
const instance = await core.getInstance("eth", 0.1)
// Log
core.on('debug', logListener)
// Sync!
await core.syncDeposits(instance)
// Log
core.off('debug', logListener)
}
sync()
```