From 66e781ea831c7e7524d2ed490b8fdc43ba318434 Mon Sep 17 00:00:00 2001 From: Ayanami Date: Wed, 26 Jan 2022 21:21:28 +0900 Subject: [PATCH 1/8] Minor console bug fix --- cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli.js b/cli.js index 546c2f3..fd04f65 100755 --- a/cli.js +++ b/cli.js @@ -724,7 +724,7 @@ async function fetchEvents({ type, currency, amount}) { await tornadoContract.getPastEvents(capitalizeFirstLetter(type), { fromBlock: i, toBlock: i+chunks-1, - }).then(r => { fetchedEvents = fetchedEvents.concat(r); console.log("Fetched",amount,currency.toUpperCase(),type,"events to block:", i) }, err => { console.error(i + " failed fetching",type,"events from node", err); process.exit(1); }).catch(console.log); + }).then(r => { fetchedEvents = fetchedEvents.concat(r); console.log("Fetched",amount,currency.toUpperCase(),type,"events to block:", i+chunks-1) }, err => { console.error(i + " failed fetching",type,"events from node", err); process.exit(1); }).catch(console.log); } async function mapDepositEvents() { From 00f68385e322cef110edf622d3757eca4e6ac06f Mon Sep 17 00:00:00 2001 From: Ayanami Date: Wed, 26 Jan 2022 22:22:50 +0900 Subject: [PATCH 2/8] Fixed bug on ERC20 support --- cli.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli.js b/cli.js index fd04f65..f0f0fd1 100755 --- a/cli.js +++ b/cli.js @@ -951,9 +951,9 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', torPort, } if (isLocalRPC) { - tornadoAddress = currency === 'eth' ? contractJson.networks[netId].address : erc20tornadoJson.networks[netId].address - tokenAddress = currency !== 'eth' ? erc20ContractJson.networks[netId].address : null netSymbol = getCurrentNetworkSymbol() + tornadoAddress = currency === netSymbol.toLowerCase() ? contractJson.networks[netId].address : erc20tornadoJson.networks[netId].address + tokenAddress = currency !== netSymbol.toLowerCase() ? erc20ContractJson.networks[netId].address : null deployedBlockNumber = 0 senderAccount = (await web3.eth.getAccounts())[0] } else { @@ -970,7 +970,7 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', torPort, if (!tornadoAddress) { throw new Error() } - tokenAddress = config.deployments[`netId${netId}`][currency].tokenAddress + tokenAddress = currency !== netSymbol.toLowerCase() ? config.deployments[`netId${netId}`][currency].tokenAddress : null } catch (e) { console.error('There is no such tornado instance, check the currency and amount you provide', e) process.exit(1) @@ -980,7 +980,7 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', torPort, tornado = new web3.eth.Contract(contractJson, tornadoAddress) tornadoContract = new web3.eth.Contract(instanceJson, tornadoInstance) contractAddress = tornadoAddress - erc20 = currency !== 'eth' ? new web3.eth.Contract(erc20ContractJson.abi, tokenAddress) : {} + erc20 = currency !== netSymbol.toLowerCase() ? new web3.eth.Contract(erc20ContractJson.abi, tokenAddress) : {} erc20Address = tokenAddress } From 8b63de6392bb2bd8947d5ad39a38088f60d218bb Mon Sep 17 00:00:00 2001 From: Ayanami Date: Wed, 26 Jan 2022 22:24:58 +0900 Subject: [PATCH 3/8] Fix send command --- cli.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/cli.js b/cli.js index f0f0fd1..bcc8d0d 100755 --- a/cli.js +++ b/cli.js @@ -349,6 +349,11 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, torP console.log('Submitting withdraw transaction') await generateTransaction(contractAddress, await tornado.methods.withdraw(tornadoInstance, proof, ...args).encodeABI()) } + if (currency === netSymbol.toLowerCase()) { + await printETHBalance({ address: recipient, name: 'Recipient', symbol: currency.toUpperCase() }) + } else { + await printERC20Balance({ address: recipient, name: 'Recipient' }) + } console.log('Done withdrawal from Tornado Cash') } @@ -379,23 +384,25 @@ async function send({ address, amount, tokenAddress }) { console.error("You have 0 balance, can't send") process.exit(1); } - if (!amount) { + if (amount) { + toSend = amount * Math.pow(10, 18) + if (balance < toSend) { + console.error("You have",web3.utils.fromWei(toHex(balance)),netSymbol+", you can't send more than you have.") + process.exit(1); + } + } else { console.log('Amount not defined, sending all available amounts') const gasPrice = await fetchGasPrice() const gasLimit = 21000; if (netId == 1 || netId == 5) { const priorityFee = await gasPrices(3) - amount = (balance - (gasLimit * (parseInt(gasPrice) + parseInt(priorityFee)))) + toSend = (balance - (gasLimit * (parseInt(gasPrice) + parseInt(priorityFee)))) } else { - amount = (balance - (gasLimit * parseInt(gasPrice))) + toSend = (balance - (gasLimit * parseInt(gasPrice))) } } - if (balance < amount) { - console.error("You have",web3.utils.fromWei(toHex(balance)),netSymbol,", you can't send more than you have.") - process.exit(1); - } - await generateTransaction(address, null, amount) - console.log('Sent',web3.utils.fromWei(toHex(amount)),netSymbol,'to',address); + await generateTransaction(address, null, toSend) + console.log('Sent',web3.utils.fromWei(toHex(toSend)),netSymbol,'to',address); } } From 15fe31445aebb91c4ba32eed9a05f6418d3c4cf8 Mon Sep 17 00:00:00 2001 From: Ayanami Date: Wed, 26 Jan 2022 23:08:37 +0900 Subject: [PATCH 4/8] Add some error messages --- cli.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cli.js b/cli.js index bcc8d0d..bdebc83 100755 --- a/cli.js +++ b/cli.js @@ -341,8 +341,9 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, torP // using private key // check if the address of recepient matches with the account of provided private key from environment to prevent accidental use of deposit address for withdrawal transaction. - const { address } = await web3.eth.accounts.privateKeyToAccount('0x' + PRIVATE_KEY) - assert(recipient.toLowerCase() == address.toLowerCase(), 'Withdrawal amount recepient mismatches with the account of provided private key from environment file') + assert(recipient.toLowerCase() == senderAccount.toLowerCase(), 'Withdrawal recepient mismatches with the account of provided private key from environment file') + const checkBalance = await web3.eth.getBalance(senderAccount) + assert(checkBalance !== 0, 'You have 0 balance, make sure to fund account by withdrawing from tornado using relayer first') const { proof, args } = await generateProof({ deposit, currency, amount, recipient, refund }) @@ -380,10 +381,7 @@ async function send({ address, amount, tokenAddress }) { console.log('Sent',amount,(await erc20.methods.symbol().call()),'to',address); } else { const balance = await web3.eth.getBalance(senderAccount) - if (balance == 0) { - console.error("You have 0 balance, can't send") - process.exit(1); - } + assert(balance !== 0, "You have 0 balance, can't send transaction") if (amount) { toSend = amount * Math.pow(10, 18) if (balance < toSend) { From e9ff6d1488153daa6823cac5494888be6f39cb87 Mon Sep 17 00:00:00 2001 From: Ayanami Date: Wed, 26 Jan 2022 23:40:00 +0900 Subject: [PATCH 5/8] Define netSymbol in advance --- cli.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cli.js b/cli.js index bdebc83..8aebdf8 100755 --- a/cli.js +++ b/cli.js @@ -963,8 +963,8 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', torPort, senderAccount = (await web3.eth.getAccounts())[0] } else { try { + netSymbol = getCurrentNetworkSymbol() if (balanceCheck) { - netSymbol = getCurrentNetworkSymbol() currency = netSymbol.toLowerCase() amount = Object.keys(config.deployments[`netId${netId}`][currency].instanceAddress)[0] } @@ -981,7 +981,6 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', torPort, process.exit(1) } } - netSymbol = getCurrentNetworkSymbol() tornado = new web3.eth.Contract(contractJson, tornadoAddress) tornadoContract = new web3.eth.Contract(instanceJson, tornadoInstance) contractAddress = tornadoAddress From 4bea523346e25616fb1beaa3363764a36264212d Mon Sep 17 00:00:00 2001 From: Ayanami Date: Thu, 27 Jan 2022 14:50:49 +0900 Subject: [PATCH 6/8] Remove duplicated definition --- cli.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cli.js b/cli.js index 8aebdf8..ac8a1a8 100755 --- a/cli.js +++ b/cli.js @@ -41,8 +41,8 @@ function toHex(number, length = 32) { } /** Display ETH account balance */ -async function printETHBalance({ address, name, symbol }) { - console.log(`${name} balance is`, web3.utils.fromWei(await web3.eth.getBalance(address)),`${symbol}`) +async function printETHBalance({ address, name }) { + console.log(`${name} balance is`, web3.utils.fromWei(await web3.eth.getBalance(address)),`${netSymbol}`) } /** Display ERC20 account balance */ @@ -159,13 +159,13 @@ async function deposit({ currency, amount }) { console.log(`Your note: ${noteString}`) await backupNote({ currency, amount, netId, note, noteString }) if (currency === netSymbol.toLowerCase()) { - await printETHBalance({ address: tornadoContract._address, name: 'Tornado contract', symbol: currency.toUpperCase() }) - await printETHBalance({ address: senderAccount, name: 'Sender account', symbol: currency.toUpperCase() }) + await printETHBalance({ address: tornadoContract._address, name: 'Tornado contract' }) + await printETHBalance({ address: senderAccount, name: 'Sender account' }) const value = isLocalRPC ? ETH_AMOUNT : fromDecimals({ amount, decimals: 18 }) console.log('Submitting deposit transaction') await generateTransaction(contractAddress, await tornado.methods.deposit(tornadoInstance, toHex(deposit.commitment), []).encodeABI(), value) - await printETHBalance({ address: tornadoContract._address, name: 'Tornado contract', symbol: currency.toUpperCase() }) - await printETHBalance({ address: senderAccount, name: 'Sender account', symbol: currency.toUpperCase() }) + await printETHBalance({ address: tornadoContract._address, name: 'Tornado contract' }) + await printETHBalance({ address: senderAccount, name: 'Sender account' }) } else { // a token await printERC20Balance({ address: tornadoContract._address, name: 'Tornado contract' }) @@ -351,7 +351,7 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, torP await generateTransaction(contractAddress, await tornado.methods.withdraw(tornadoInstance, proof, ...args).encodeABI()) } if (currency === netSymbol.toLowerCase()) { - await printETHBalance({ address: recipient, name: 'Recipient', symbol: currency.toUpperCase() }) + await printETHBalance({ address: recipient, name: 'Recipient' }) } else { await printERC20Balance({ address: recipient, name: 'Recipient' }) } @@ -1045,7 +1045,7 @@ async function main() { console.log("Using address",senderAccount,"from private key") address = senderAccount; } - await printETHBalance({ address, name: 'Account', symbol: netSymbol }) + await printETHBalance({ address, name: 'Account' }) if (tokenAddress) { await printERC20Balance({ address, name: 'Account', tokenAddress }) } From 940f3e81947f75a70151b03fd15b216225deb605 Mon Sep 17 00:00:00 2001 From: Ayanami Date: Thu, 27 Jan 2022 14:56:46 +0900 Subject: [PATCH 7/8] Remove duplicated definition --- cli.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cli.js b/cli.js index ac8a1a8..2ac2a21 100755 --- a/cli.js +++ b/cli.js @@ -948,6 +948,7 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', torPort, groth16 = await buildGroth16() netId = await web3.eth.net.getId() netName = getCurrentNetworkName() + netSymbol = getCurrentNetworkSymbol() if (noteNetId && Number(noteNetId) !== netId) { throw new Error('This note is for a different network. Specify the --rpc option explicitly') } @@ -956,14 +957,12 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', torPort, } if (isLocalRPC) { - netSymbol = getCurrentNetworkSymbol() tornadoAddress = currency === netSymbol.toLowerCase() ? contractJson.networks[netId].address : erc20tornadoJson.networks[netId].address tokenAddress = currency !== netSymbol.toLowerCase() ? erc20ContractJson.networks[netId].address : null deployedBlockNumber = 0 senderAccount = (await web3.eth.getAccounts())[0] } else { try { - netSymbol = getCurrentNetworkSymbol() if (balanceCheck) { currency = netSymbol.toLowerCase() amount = Object.keys(config.deployments[`netId${netId}`][currency].instanceAddress)[0] From 7398ac9bbcdea80fdebe7bf1085c1ee4578de56a Mon Sep 17 00:00:00 2001 From: Ayanami Date: Thu, 27 Jan 2022 15:17:19 +0900 Subject: [PATCH 8/8] Don't use await for encodeABI() since it is not a promise --- cli.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/cli.js b/cli.js index 2ac2a21..af54652 100755 --- a/cli.js +++ b/cli.js @@ -42,16 +42,19 @@ function toHex(number, length = 32) { /** Display ETH account balance */ async function printETHBalance({ address, name }) { - console.log(`${name} balance is`, web3.utils.fromWei(await web3.eth.getBalance(address)),`${netSymbol}`) + const checkBalance = await web3.eth.getBalance(address) + console.log(`${name} balance is`, web3.utils.fromWei(checkBalance),`${netSymbol}`) } /** Display ERC20 account balance */ async function printERC20Balance({ address, name, tokenAddress }) { const erc20ContractJson = require('./build/contracts/ERC20Mock.json') erc20 = tokenAddress ? new web3.eth.Contract(erc20ContractJson.abi, tokenAddress) : erc20 - balance = await erc20.methods.balanceOf(address).call() - decimals = await erc20.methods.decimals().call() - console.log(`${name}`,(await erc20.methods.name().call()),`Token Balance is`,toDecimals(balance, decimals, (balance.length + decimals)).toString().replace(/\B(?