2023-04-24 01:01:45 +03:00
|
|
|
import chai from 'chai'
|
|
|
|
import * as ganache from 'ganache'
|
|
|
|
|
|
|
|
// External
|
2023-05-07 02:59:38 +03:00
|
|
|
import { once } from 'events'
|
2023-04-24 01:01:45 +03:00
|
|
|
import { solidity } from 'ethereum-waffle'
|
2023-05-03 01:19:28 +03:00
|
|
|
import { providers, BigNumber } from 'ethers'
|
2023-04-24 01:01:45 +03:00
|
|
|
import { parseUnits } from 'ethers/lib/utils'
|
|
|
|
// @ts-expect-error
|
|
|
|
import { parseIndexableString } from 'pouchdb-collate'
|
|
|
|
|
|
|
|
// Local
|
2023-05-03 01:19:28 +03:00
|
|
|
import { ERC20, TornadoInstance } from './deth'
|
|
|
|
import { Files, Onchain, RelayerProperties } from '@tornado/sdk-data'
|
|
|
|
import { Chain, Contracts } from '@tornado/sdk-chain'
|
|
|
|
import { ErrorUtils } from '@tornado/sdk-utils'
|
|
|
|
import { TorProvider } from '@tornado/sdk-web'
|
|
|
|
import { Core } from '@tornado/sdk-core'
|
2023-04-24 01:01:45 +03:00
|
|
|
|
|
|
|
// Data
|
2023-05-03 01:19:28 +03:00
|
|
|
import eth01DepositsReference from './resources/deposits_eth_0.1.json'
|
|
|
|
import eth1DepositsReference from './resources/deposits_eth_1.json'
|
|
|
|
import eth10DepositsReference from './resources/deposits_eth_10.json'
|
|
|
|
import eth100DepositsReference from './resources/deposits_eth_100.json'
|
2023-05-07 02:59:38 +03:00
|
|
|
|
|
|
|
import dai100DepositsReference from './resources/deposits_dai_100.json'
|
|
|
|
import dai1000DepositsReference from './resources/deposits_dai_1000.json'
|
|
|
|
import dai10000DepositsReference from './resources/deposits_dai_10000.json'
|
2023-05-03 01:19:28 +03:00
|
|
|
import dai100KDepositsReference from './resources/deposits_dai_100000.json'
|
2023-04-24 01:01:45 +03:00
|
|
|
|
|
|
|
chai.use(solidity)
|
|
|
|
|
|
|
|
const expect = chai.expect
|
|
|
|
|
|
|
|
describe('Core', () => {
|
|
|
|
const torify = process.env.TORIFY === 'true'
|
|
|
|
const debug = process.env.DEBUG === 'true'
|
|
|
|
|
|
|
|
if (!process.env.ETH_MAINNET_TEST_RPC) throw ErrorUtils.getError('need a mainnet rpc endpoint.')
|
|
|
|
|
|
|
|
console.log('\nNote that these tests are time intensive. ⏳. ⏳.. ⏳...\n')
|
|
|
|
console.log(
|
|
|
|
'Also, we are using ganache because we just need a forked blockchain and not an entire environment. 🐧'
|
|
|
|
)
|
|
|
|
|
|
|
|
let daiAddress: string
|
|
|
|
const daiWhale = '0x5777d92f208679db4b9778590fa3cab3ac9e2168' // Uniswap V3 Something/Dai Pool
|
|
|
|
|
|
|
|
const mainnetProvider: providers.Provider = torify
|
|
|
|
? new TorProvider(process.env.ETH_MAINNET_TEST_RPC!, { port: +process.env.TOR_PORT! })
|
|
|
|
: new providers.JsonRpcProvider(process.env.ETH_MAINNET_TEST_RPC)
|
|
|
|
|
|
|
|
const _ganacheProvider = ganache.provider({
|
|
|
|
chain: { chainId: 1 },
|
|
|
|
// @ts-ignore
|
|
|
|
fork: { url: process.env.ETH_MAINNET_TEST_RPC },
|
|
|
|
logging: { quiet: true },
|
|
|
|
wallet: {
|
|
|
|
totalAccounts: 20,
|
|
|
|
unlockedAccounts: [daiWhale]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// @ts-expect-error
|
|
|
|
const ganacheProvider = new providers.Web3Provider(_ganacheProvider)
|
|
|
|
|
|
|
|
const chain = new Chain(ganacheProvider)
|
|
|
|
|
|
|
|
after(async function () {
|
|
|
|
this.timeout(0)
|
|
|
|
await Files.wipeCache()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('namespace Contracts', () => {
|
|
|
|
it('getClassicInstance: should be able to get a tornado instance', async () => {
|
2023-05-07 02:59:38 +03:00
|
|
|
let instance = Contracts.getInstance(String(1), 'eth', String(1), mainnetProvider)
|
2023-04-24 01:01:45 +03:00
|
|
|
expect(instance.address).to.equal('0x47CE0C6eD5B0Ce3d3A51fdb1C52DC66a7c3c2936')
|
|
|
|
}).timeout(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
context('Unforked', () => {
|
|
|
|
describe('class Classic', () => {
|
2023-05-03 01:19:28 +03:00
|
|
|
if (!process.env.SYNC_TEST_INSTANCES)
|
|
|
|
throw ErrorUtils.getError('SYNC_TEST_INSTANCES is required for sync tests.')
|
|
|
|
|
|
|
|
const denominations = process.env.SYNC_TEST_INSTANCES.split(',')
|
|
|
|
|
|
|
|
if (!denominations.length) throw ErrorUtils.getError('Instances entered were INVALID')
|
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
let depositReferences: { [key: string]: typeof eth01DepositsReference } = {}
|
2023-05-03 01:19:28 +03:00
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
depositReferences['1ETH0.1'] = eth01DepositsReference
|
|
|
|
depositReferences['1ETH1'] = eth1DepositsReference
|
|
|
|
depositReferences['1ETH10'] = eth10DepositsReference
|
|
|
|
depositReferences['1ETH100'] = eth100DepositsReference
|
|
|
|
|
|
|
|
depositReferences['1DAI100'] = dai100DepositsReference
|
|
|
|
depositReferences['1DAI1000'] = dai1000DepositsReference
|
|
|
|
depositReferences['1DAI10000'] = dai10000DepositsReference
|
|
|
|
depositReferences['1DAI100000'] = dai100KDepositsReference
|
|
|
|
|
|
|
|
const core = new Core()
|
2023-04-24 01:01:45 +03:00
|
|
|
|
2023-05-03 01:19:28 +03:00
|
|
|
let instances: TornadoInstance[] = []
|
|
|
|
|
2023-04-24 01:01:45 +03:00
|
|
|
let logListener = function (...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]}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
this.timeout(0)
|
2023-05-03 01:19:28 +03:00
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
await core.connect(mainnetProvider)
|
|
|
|
|
|
|
|
const regexp = /([0-9]+)([A-Za-z]+)([0-9.]+)/
|
2023-05-03 01:19:28 +03:00
|
|
|
|
|
|
|
const promises = denominations.map((denom) => {
|
|
|
|
const matches = denom.match(regexp)!.slice(2)
|
|
|
|
return core.getInstance(matches[0].toLowerCase(), +matches[1])
|
|
|
|
})
|
|
|
|
|
|
|
|
;(await Promise.all(promises)).forEach((instance) => instances.push(instance))
|
|
|
|
|
2023-04-24 01:01:45 +03:00
|
|
|
if (debug) core.on('debug', logListener)
|
|
|
|
})
|
2023-05-03 01:19:28 +03:00
|
|
|
|
2023-04-24 01:01:45 +03:00
|
|
|
after(async function () {
|
2023-05-31 23:53:28 +03:00
|
|
|
this.timeout(0)
|
2023-04-24 01:01:45 +03:00
|
|
|
if (debug) core.off('debug', logListener)
|
|
|
|
})
|
|
|
|
|
2023-05-04 00:42:42 +03:00
|
|
|
it('Should sync all instances.', async function () {
|
2023-05-03 01:19:28 +03:00
|
|
|
for (let i = 0; i < instances.length; i++) {
|
|
|
|
console.log('\n ♻️ Syncing ' + denominations[i] + '\n')
|
2023-04-24 01:01:45 +03:00
|
|
|
|
2023-05-03 01:19:28 +03:00
|
|
|
// This is going to try syncing the entire range
|
|
|
|
await core.syncDeposits(instances[i], {
|
2023-05-11 21:49:07 +03:00
|
|
|
concurrencyLimit: 8,
|
2023-06-10 23:27:32 +03:00
|
|
|
blockDelta: 10000,
|
2023-05-03 01:19:28 +03:00
|
|
|
msTimeout: 300
|
|
|
|
})
|
2023-04-24 01:01:45 +03:00
|
|
|
|
2023-05-03 01:19:28 +03:00
|
|
|
const cache = core.caches.get('Deposits' + denominations[i])
|
|
|
|
const rows = (await cache!.db.allDocs()).rows
|
|
|
|
const valid = Object.values(depositReferences[denominations[i]])
|
2023-04-24 01:01:45 +03:00
|
|
|
|
2023-05-03 01:19:28 +03:00
|
|
|
expect(rows.length).to.be.gte(valid.length)
|
2023-04-24 01:01:45 +03:00
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
console.log('\n📄 Validating inputs for ' + denominations[i] + '\n')
|
|
|
|
|
2023-05-03 01:19:28 +03:00
|
|
|
for (let i = 0, len = valid.length; i < len; i++) {
|
|
|
|
const id = rows[i].id
|
|
|
|
const [bn, leafIndex, commitment] = parseIndexableString(id)
|
|
|
|
const validDoc = valid[i]
|
|
|
|
expect(bn).to.equal(validDoc['blockNumber'])
|
|
|
|
expect(leafIndex).to.equal(validDoc['leafIndex'])
|
|
|
|
expect(commitment).to.equal(validDoc['commitment'])
|
|
|
|
}
|
2023-04-24 01:01:45 +03:00
|
|
|
}
|
|
|
|
}).timeout(0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Forked (Ganache)', async () => {
|
|
|
|
describe('class Classic', async () => {
|
|
|
|
// Init sync objects
|
2023-05-07 02:59:38 +03:00
|
|
|
const core = new Core()
|
2023-04-24 01:01:45 +03:00
|
|
|
const needsMoney = ganacheProvider.getSigner()
|
2023-05-07 02:59:38 +03:00
|
|
|
const withdrawer = ganacheProvider.getSigner(2)
|
2023-04-24 01:01:45 +03:00
|
|
|
const daiWhaleSigner = ganacheProvider.getSigner(daiWhale)
|
|
|
|
const debugListener = (message: string) => console.debug(message)
|
|
|
|
|
|
|
|
let snapshotId: any
|
|
|
|
let needsMoneyAddress: string
|
2023-05-07 02:59:38 +03:00
|
|
|
let withdrawerAddress: string
|
2023-04-24 01:01:45 +03:00
|
|
|
let dai: ERC20
|
|
|
|
let smallestEth: TornadoInstance
|
|
|
|
let dai100K: TornadoInstance
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
this.timeout(0)
|
2023-05-07 02:59:38 +03:00
|
|
|
|
|
|
|
// We need to connect core first
|
|
|
|
await core.connect(ganacheProvider)
|
|
|
|
|
2023-04-24 01:01:45 +03:00
|
|
|
// Get snapshot just in case
|
|
|
|
snapshotId = await ganacheProvider.send('evm_snapshot', [])
|
|
|
|
|
|
|
|
// Prep whale eth balance
|
|
|
|
await ganacheProvider.send('evm_setAccountBalance', [daiWhale, parseUnits('10').toHexString()])
|
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// Addresses
|
2023-04-24 01:01:45 +03:00
|
|
|
needsMoneyAddress = await needsMoney.getAddress()
|
2023-05-07 02:59:38 +03:00
|
|
|
withdrawerAddress = await withdrawer.getAddress()
|
2023-04-24 01:01:45 +03:00
|
|
|
daiAddress = await Onchain.getTokenAddress('1', 'dai')
|
2023-05-07 02:59:38 +03:00
|
|
|
|
|
|
|
// Contracts
|
|
|
|
dai = chain.getTokenContract(daiAddress)
|
|
|
|
smallestEth = core.getInstance('eth', 0.1)
|
|
|
|
dai100K = core.getInstance('dai', 100000)
|
2023-04-24 01:01:45 +03:00
|
|
|
|
|
|
|
// Set debug
|
|
|
|
if (debug) core.on('debug', debugListener)
|
|
|
|
})
|
|
|
|
after(async function () {
|
|
|
|
this.timeout(0)
|
|
|
|
await ganacheProvider.send('evm_revert', [snapshotId])
|
|
|
|
core.off('debug', debugListener)
|
|
|
|
})
|
2023-05-07 02:59:38 +03:00
|
|
|
beforeEach(() => {
|
2023-04-24 01:01:45 +03:00
|
|
|
dai = dai.connect(daiWhaleSigner)
|
|
|
|
})
|
|
|
|
|
2023-05-13 22:55:47 +03:00
|
|
|
it('createDepositTransaction: build a single eth deposit tx and succeed', async () => {
|
2023-04-24 01:01:45 +03:00
|
|
|
const initBal = await needsMoney.getBalance()
|
|
|
|
|
|
|
|
// Build tx and load cache for this test
|
2023-05-13 22:55:47 +03:00
|
|
|
const tx = core.createDepositTransaction(smallestEth)
|
2023-05-07 02:59:38 +03:00
|
|
|
|
|
|
|
// Listen to deposit events
|
|
|
|
core.listenForDeposits(smallestEth)
|
2023-04-24 01:01:45 +03:00
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// Get the promise we need
|
|
|
|
const promise = once(core, 'deposit')
|
2023-04-27 20:34:22 +03:00
|
|
|
|
2023-04-24 01:01:45 +03:00
|
|
|
// Deposit and await cache updated
|
|
|
|
const response = await needsMoney.sendTransaction(tx.request)
|
|
|
|
await response.wait()
|
|
|
|
const endBal = await needsMoney.getBalance()
|
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// Await deposit addition to cache
|
|
|
|
await promise
|
2023-04-24 01:01:45 +03:00
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// Remove listeners
|
|
|
|
core.clearListeners(smallestEth)
|
|
|
|
|
|
|
|
// Backup
|
|
|
|
await core.backupNote(smallestEth, tx)
|
2023-04-27 20:34:22 +03:00
|
|
|
|
2023-04-24 01:01:45 +03:00
|
|
|
// Check deposit predicates
|
|
|
|
expect(initBal).to.equal(parseUnits('1000'))
|
|
|
|
expect(endBal).to.be.lte(parseUnits('999.9'))
|
|
|
|
}).timeout(0)
|
|
|
|
|
2023-05-13 22:55:47 +03:00
|
|
|
it('createDepositProof: it should be able to build an eth proof', async () => {
|
2023-05-07 02:59:38 +03:00
|
|
|
// Get all of the notes
|
2023-04-24 01:01:45 +03:00
|
|
|
const notes = await core.loadNotes()
|
|
|
|
|
|
|
|
// Build proof
|
2023-04-27 20:34:22 +03:00
|
|
|
let proof: any
|
2023-04-24 01:01:45 +03:00
|
|
|
|
2023-05-13 22:55:47 +03:00
|
|
|
proof = await core.createDepositProof(
|
2023-05-07 02:59:38 +03:00
|
|
|
smallestEth,
|
|
|
|
{
|
|
|
|
address: withdrawerAddress
|
|
|
|
},
|
|
|
|
needsMoneyAddress,
|
|
|
|
notes[0],
|
|
|
|
{
|
|
|
|
// On by default but stating for visibility
|
|
|
|
checkNotesSpent: true,
|
|
|
|
checkKnownRoot: true
|
|
|
|
}
|
|
|
|
)
|
2023-04-24 01:01:45 +03:00
|
|
|
|
|
|
|
// Substract the calculated fee from the received amount
|
|
|
|
const ethDelta = parseUnits('0.1').sub(proof[5])
|
|
|
|
|
|
|
|
// Withdrawal time, let's see if it works
|
|
|
|
// The balance diff will be exact because withdrawer is paying for gas as relayer
|
2023-04-27 20:34:22 +03:00
|
|
|
await expect(() =>
|
|
|
|
smallestEth
|
2023-04-24 01:01:45 +03:00
|
|
|
.connect(withdrawer)
|
|
|
|
.withdraw(proof[0], proof[1], proof[2], proof[3], proof[4], proof[5], proof[6])
|
|
|
|
).to.changeEtherBalance(needsMoney, ethDelta)
|
|
|
|
}).timeout(0)
|
|
|
|
|
2023-05-13 22:55:47 +03:00
|
|
|
it('createDepositTransaction: build a single token deposit tx and succeed', async () => {
|
2023-04-24 01:01:45 +03:00
|
|
|
// Prep deposit amount, proxy for approval, cache, bal for comp
|
|
|
|
const depositAmount = parseUnits('100000')
|
|
|
|
const daiBalBef = await dai.balanceOf(dai100K.address)
|
2023-06-10 23:27:32 +03:00
|
|
|
const proxy = core.getRouter()
|
2023-04-24 01:01:45 +03:00
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// We listen for deposits
|
|
|
|
core.listenForDeposits(dai100K)
|
2023-04-24 01:01:45 +03:00
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// We will wait for the event
|
|
|
|
const promise = once(core, 'deposit')
|
2023-04-27 20:34:22 +03:00
|
|
|
|
2023-04-24 01:01:45 +03:00
|
|
|
// Prep for deposit
|
|
|
|
await dai.transfer(needsMoneyAddress, depositAmount)
|
2023-05-07 02:59:38 +03:00
|
|
|
|
2023-04-24 01:01:45 +03:00
|
|
|
dai = dai.connect(needsMoney)
|
2023-05-07 02:59:38 +03:00
|
|
|
|
2023-05-13 22:55:47 +03:00
|
|
|
const tx = core.createDepositTransaction(dai100K)
|
2023-05-07 02:59:38 +03:00
|
|
|
|
2023-04-24 01:01:45 +03:00
|
|
|
// Approve dai for the proxy first (transferFrom)
|
|
|
|
await dai.approve(proxy.address, depositAmount)
|
|
|
|
|
|
|
|
// Deposit
|
|
|
|
const response = await needsMoney.sendTransaction(tx.request)
|
|
|
|
await response.wait()
|
|
|
|
|
|
|
|
// Prep for check
|
|
|
|
const daiBalPost = await dai.balanceOf(dai100K.address)
|
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// Passing resolve as callback into put didn't work.
|
|
|
|
await promise
|
|
|
|
|
|
|
|
// Have to clear the listeners
|
|
|
|
core.clearListeners(dai100K)
|
2023-04-24 01:01:45 +03:00
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// Backup since we need it for later
|
|
|
|
await core.backupNote(dai100K, tx)
|
2023-04-27 20:34:22 +03:00
|
|
|
|
2023-04-24 01:01:45 +03:00
|
|
|
// Checks
|
|
|
|
expect(daiBalBef).to.equal(daiBalPost.sub(depositAmount))
|
|
|
|
expect(await dai.balanceOf(needsMoneyAddress)).to.equal(0)
|
|
|
|
}).timeout(0)
|
|
|
|
|
2023-05-13 22:55:47 +03:00
|
|
|
it('createDepositProof: it should be able to build a token proof', async () => {
|
2023-04-27 20:34:22 +03:00
|
|
|
if (!process.env.TEST_RELAYER_DOMAIN) throw ErrorUtils.getError('core.test.ts: Need a relayer name')
|
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// Get all of the notes
|
2023-04-24 01:01:45 +03:00
|
|
|
const notes = await core.loadNotes()
|
2023-05-07 02:59:38 +03:00
|
|
|
|
2023-04-24 01:01:45 +03:00
|
|
|
// We need to select last
|
|
|
|
const note = notes[notes.length - 1]
|
|
|
|
|
2023-05-03 01:19:28 +03:00
|
|
|
let properties: RelayerProperties = {
|
2023-05-07 02:59:38 +03:00
|
|
|
address: withdrawerAddress,
|
2023-05-03 01:19:28 +03:00
|
|
|
version: '2',
|
|
|
|
serviceFeePercent: 0.04,
|
|
|
|
miningFeePercent: 0.15,
|
|
|
|
status: 'whatever',
|
|
|
|
chainId: 1,
|
|
|
|
prices: new Map<string, BigNumber>()
|
|
|
|
}
|
2023-04-27 20:34:22 +03:00
|
|
|
|
2023-05-03 01:19:28 +03:00
|
|
|
properties.prices.set('dai', BigNumber.from(10).pow(18).div(1800))
|
2023-04-24 01:01:45 +03:00
|
|
|
|
|
|
|
// Build proof with relayer properties this time
|
2023-05-13 22:55:47 +03:00
|
|
|
const proof = await core.createDepositProof(dai100K, properties, needsMoneyAddress, note, {
|
2023-05-07 02:59:38 +03:00
|
|
|
// On by default but stating for visibility
|
|
|
|
checkNotesSpent: true,
|
|
|
|
checkKnownRoot: true
|
|
|
|
})
|
2023-04-24 01:01:45 +03:00
|
|
|
|
|
|
|
// Calc balance diff again... it will be expressed in dai
|
|
|
|
const daiDelta = parseUnits('100000').sub(proof[5])
|
|
|
|
|
|
|
|
await expect(
|
2023-04-27 20:34:22 +03:00
|
|
|
await dai100K
|
2023-04-24 01:01:45 +03:00
|
|
|
.connect(withdrawer)
|
|
|
|
.withdraw(proof[0], proof[1], proof[2], proof[3], proof[4], proof[5], proof[6])
|
|
|
|
).to.changeTokenBalance(dai, needsMoney, daiDelta)
|
|
|
|
}).timeout(0)
|
|
|
|
|
2023-05-14 22:02:34 +03:00
|
|
|
it('createDepositTransactions: multiple eth deposits', async () => {
|
2023-05-07 02:59:38 +03:00
|
|
|
const instances = core.getInstances(
|
2023-04-24 01:01:45 +03:00
|
|
|
[0.1, 1, 10, 100].map((el) => {
|
|
|
|
return { token: 'eth', denomination: el }
|
|
|
|
})
|
|
|
|
)
|
2023-05-03 01:19:28 +03:00
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// That easy
|
|
|
|
instances.forEach((instance) => core.listenForDeposits(instance))
|
|
|
|
|
|
|
|
const depositsPer = [1, 1, 2, 1]
|
|
|
|
|
2023-05-13 22:55:47 +03:00
|
|
|
const txs = core.createDepositTransactions(instances, {
|
2023-05-07 02:59:38 +03:00
|
|
|
depositsPerInstance: depositsPer
|
2023-04-24 01:01:45 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
for (let i = 0, len = txs.length; i < len; i++) {
|
2023-05-07 02:59:38 +03:00
|
|
|
const promise = once(core, 'deposit')
|
2023-05-03 01:19:28 +03:00
|
|
|
const response = await needsMoney.sendTransaction(txs[i].request)
|
|
|
|
await response.wait()
|
2023-05-07 02:59:38 +03:00
|
|
|
await promise
|
2023-04-24 01:01:45 +03:00
|
|
|
}
|
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// That easy
|
|
|
|
instances.forEach((instance) => core.clearListeners(instance))
|
|
|
|
|
|
|
|
// And backup the notes
|
|
|
|
await Promise.all(
|
|
|
|
instances.map((instance, index) => core.backupNotes(instance, txs.splice(0, depositsPer[index])))
|
|
|
|
)
|
|
|
|
|
|
|
|
//for (let i = 0, len = instances.length; i < len; i++) {
|
|
|
|
// await core.backupNotes(instances[i], txs.splice(0, depositsPer[i]))
|
|
|
|
//}
|
|
|
|
|
2023-05-04 00:42:42 +03:00
|
|
|
expect(await needsMoney.getBalance()).to.be.lte(parseUnits('888.8'))
|
2023-04-24 01:01:45 +03:00
|
|
|
}).timeout(0)
|
|
|
|
|
2023-05-14 22:02:34 +03:00
|
|
|
it('createDepositProofs: should be able to withdraw', async () => {
|
2023-05-07 02:59:38 +03:00
|
|
|
// ETH instances
|
|
|
|
const instances = core.getInstances(
|
|
|
|
[0.1, 1, 10, 100].map((el) => {
|
|
|
|
return { token: 'eth', denomination: el }
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// Number deposits per instance
|
|
|
|
const depositsPer = [1, 1, 2, 1]
|
|
|
|
|
|
|
|
// Get all of the notes
|
|
|
|
let notes = await core.loadNotes()
|
|
|
|
|
|
|
|
// Handle all withdrawals
|
|
|
|
for (let i = 0, len = instances.length; i < len; i++) {
|
2023-05-13 22:55:47 +03:00
|
|
|
const proofs = await core.createDepositProofs(
|
2023-05-07 02:59:38 +03:00
|
|
|
instances[i],
|
|
|
|
{
|
|
|
|
address: withdrawerAddress
|
|
|
|
},
|
|
|
|
new Array(depositsPer[i]).fill(needsMoneyAddress),
|
|
|
|
notes.splice(0, depositsPer[i]),
|
|
|
|
{
|
|
|
|
// On by default but stating for visibility
|
|
|
|
checkNotesSpent: true,
|
|
|
|
checkKnownRoot: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
for (let p = 0, plen = proofs.length; p < plen; p++) {
|
|
|
|
// Get proof
|
|
|
|
const proof = proofs[p]
|
|
|
|
|
|
|
|
// Substract the calculated fee from the received amount
|
|
|
|
const ethDelta = parseUnits('0.1')
|
|
|
|
.mul(10 ** i)
|
|
|
|
.sub(proof[5])
|
|
|
|
|
|
|
|
// Withdrawal time, let's see if it works
|
|
|
|
// The balance diff will be exact because withdrawer is paying for gas as relayer
|
|
|
|
await expect(() =>
|
|
|
|
instances[i]
|
|
|
|
.connect(withdrawer)
|
|
|
|
.withdraw(proof[0], proof[1], proof[2], proof[3], proof[4], proof[5], proof[6])
|
|
|
|
).to.changeEtherBalance(needsMoney, ethDelta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).timeout(0)
|
|
|
|
|
2023-06-10 23:27:32 +03:00
|
|
|
it.only('createDepositTransactions: multiple dai deposits', async () => {
|
2023-05-07 02:59:38 +03:00
|
|
|
// Prepare contracts
|
|
|
|
const denoms = [100, 1000, 10000, 100000]
|
2023-06-10 23:27:32 +03:00
|
|
|
const proxy = core.getRouter()
|
2023-05-07 02:59:38 +03:00
|
|
|
const instances = core.getInstances(
|
|
|
|
denoms.map((el) => {
|
2023-04-24 01:01:45 +03:00
|
|
|
return { token: 'dai', denomination: el }
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// Prep the money
|
|
|
|
const depositsPer = [1, 2, 1, 2]
|
|
|
|
|
|
|
|
await dai.transfer(needsMoneyAddress, parseUnits('212100'))
|
2023-04-24 01:01:45 +03:00
|
|
|
|
|
|
|
dai = dai.connect(needsMoney)
|
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
await dai.approve(proxy.address, parseUnits('212100'))
|
|
|
|
|
|
|
|
// Record the money
|
|
|
|
const daiBalancesBef = await Promise.all(instances.map((instance) => dai.balanceOf(instance.address)))
|
|
|
|
|
|
|
|
// Begin to listen
|
|
|
|
instances.forEach((instance) => core.listenForDeposits(instance))
|
|
|
|
|
|
|
|
// Build txs
|
|
|
|
|
2023-05-13 22:55:47 +03:00
|
|
|
const txs = core.createDepositTransactions(instances, {
|
2023-05-07 02:59:38 +03:00
|
|
|
depositsPerInstance: depositsPer
|
2023-04-24 01:01:45 +03:00
|
|
|
})
|
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// Send transactions
|
2023-04-24 01:01:45 +03:00
|
|
|
|
|
|
|
for (let i = 0, len = txs.length; i < len; i++) {
|
2023-05-07 02:59:38 +03:00
|
|
|
const promise = once(core, 'deposit')
|
|
|
|
const resp = await needsMoney.sendTransaction(txs[i].request)
|
|
|
|
await resp.wait()
|
|
|
|
await promise
|
2023-04-24 01:01:45 +03:00
|
|
|
}
|
|
|
|
|
2023-05-07 02:59:38 +03:00
|
|
|
// Clear listeners
|
|
|
|
instances.forEach((instance) => core.clearListeners(instance))
|
|
|
|
|
|
|
|
// Backup notes
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
instances.map((instance, index) => core.backupNotes(instance, txs.splice(0, depositsPer[index])))
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get new balances
|
|
|
|
|
|
|
|
const daiBalancesPost = await Promise.all(
|
|
|
|
instances.map((instance) => dai.balanceOf(instance.address))
|
|
|
|
)
|
|
|
|
|
|
|
|
// Check and done
|
|
|
|
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
expect(daiBalancesBef[i]).to.equal(
|
|
|
|
daiBalancesPost[i].sub(parseUnits('' + denoms[i] * depositsPer[i]))
|
|
|
|
)
|
|
|
|
}
|
2023-04-24 01:01:45 +03:00
|
|
|
expect(await dai.balanceOf(needsMoneyAddress)).to.equal(0)
|
|
|
|
}).timeout(0)
|
|
|
|
|
2023-06-10 23:27:32 +03:00
|
|
|
it.only('createDepositProofs: multiple dai withdrawals', async () => {
|
2023-05-07 02:59:38 +03:00
|
|
|
// ETH instances
|
|
|
|
const denoms = [100, 1000, 10000, 100000]
|
|
|
|
const instances = core.getInstances(
|
|
|
|
denoms.map((el) => {
|
|
|
|
return { token: 'dai', denomination: el }
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// Number deposits per instance
|
|
|
|
const depositsPer = [1, 2, 1, 2]
|
|
|
|
|
|
|
|
// Get all of the notes
|
|
|
|
let notes = await core.loadNotes()
|
|
|
|
|
|
|
|
// Fake relayer properties
|
|
|
|
let properties: RelayerProperties = {
|
|
|
|
address: withdrawerAddress,
|
|
|
|
version: '2',
|
|
|
|
serviceFeePercent: 0.04,
|
|
|
|
miningFeePercent: 0.15,
|
|
|
|
status: 'whatever',
|
|
|
|
chainId: 1,
|
|
|
|
prices: new Map<string, BigNumber>()
|
|
|
|
}
|
|
|
|
|
|
|
|
properties.prices.set('dai', BigNumber.from(10).pow(18).div(1800))
|
|
|
|
|
|
|
|
// Handle all withdrawals
|
|
|
|
for (let i = 0, len = instances.length; i < len; i++) {
|
2023-05-13 22:55:47 +03:00
|
|
|
const proofs = await core.createDepositProofs(
|
2023-05-07 02:59:38 +03:00
|
|
|
instances[i],
|
|
|
|
properties,
|
|
|
|
new Array(depositsPer[i]).fill(needsMoneyAddress),
|
|
|
|
notes.splice(0, depositsPer[i]),
|
|
|
|
{
|
|
|
|
// On by default but stating for visibility
|
|
|
|
checkNotesSpent: true,
|
|
|
|
checkKnownRoot: true
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
for (let p = 0, plen = proofs.length; p < plen; p++) {
|
|
|
|
// Get proof
|
|
|
|
const proof = proofs[p]
|
|
|
|
|
|
|
|
// Substract the calculated fee from the received amount
|
2023-05-09 01:07:42 +03:00
|
|
|
const daiDelta = parseUnits('100')
|
2023-05-07 02:59:38 +03:00
|
|
|
.mul(10 ** i)
|
|
|
|
.sub(proof[5])
|
|
|
|
|
|
|
|
// Withdrawal time, let's see if it works
|
|
|
|
// The balance diff will be exact because withdrawer is paying for gas as relayer
|
|
|
|
await expect(() =>
|
|
|
|
instances[i]
|
|
|
|
.connect(withdrawer)
|
|
|
|
.withdraw(proof[0], proof[1], proof[2], proof[3], proof[4], proof[5], proof[6])
|
|
|
|
).to.changeTokenBalance(dai, needsMoney, daiDelta)
|
|
|
|
}
|
|
|
|
}
|
2023-04-24 01:01:45 +03:00
|
|
|
}).timeout(0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|