Minor updates

* Use socks5h to use DNS through proxy

* fix withdrawal transaction gaslimit

* change default priority gas fee value to match with a8e504054e/src/TxManager.js (L18)

* fix typo

* Use estimateGas

* default goerli network gasprice to 1 gwei

* Add other networks
This commit is contained in:
Ayanami 2021-12-12 15:19:49 +09:00
parent 54ae465f16
commit 610200b610
No known key found for this signature in database
GPG Key ID: 0CABDF03077D92E4
2 changed files with 116 additions and 20 deletions

58
cli.js
View File

@ -54,24 +54,42 @@ async function printERC20Balance({ address, name, tokenAddress }) {
console.log(`${name}`,(await erc20.methods.name().call()),`Token Balance is`,toDecimals(balance, decimals, (balance.length + decimals)).toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ","),(await erc20.methods.symbol().call())) console.log(`${name}`,(await erc20.methods.name().call()),`Token Balance is`,toDecimals(balance, decimals, (balance.length + decimals)).toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ","),(await erc20.methods.symbol().call()))
} }
async function generateTransaction(contractAddress, gasLimit, encodedData, value = 0) { async function generateTransaction(to, encodedData, value = 0) {
const nonce = await web3.eth.getTransactionCount(senderAccount)
const gasPrice = await fetchGasPrice() const gasPrice = await fetchGasPrice()
let gasLimit;
let tx = {} let tx = {}
async function estimateGas() {
const fetchedGas = await web3.eth.estimateGas({
from : senderAccount,
to : to,
value : value,
nonce : nonce,
data : encodedData
})
const bumped = Math.floor(fetchedGas * 1.3)
gasLimit = web3.utils.toHex(bumped)
}
await estimateGas();
async function txoptions() { async function txoptions() {
// Generate EIP-1559 transaction // Generate EIP-1559 transaction
if (netId == 1 || netId == 5) { if (netId == 1 || netId == 5) {
tx = { tx = {
to : contractAddress, to : to,
value : value, value : value,
maxFeePerGas: gasPrice, nonce : nonce,
maxPriorityFeePerGas: web3.utils.toHex(web3.utils.toWei('1', 'gwei')), maxFeePerGas : gasPrice,
gas : gasLimit, maxPriorityFeePerGas : web3.utils.toHex(web3.utils.toWei('3', 'gwei')),
data : encodedData gas : gasLimit,
data : encodedData
} }
} else { } else {
tx = { tx = {
to : contractAddress, to : to,
value : value, value : value,
nonce : nonce,
gasPrice : gasPrice, gasPrice : gasPrice,
gas : gasLimit, gas : gasLimit,
data : encodedData data : encodedData
@ -131,7 +149,7 @@ async function deposit({ currency, amount }) {
await printETHBalance({ address: senderAccount, name: 'Sender account', symbol: currency.toUpperCase() }) await printETHBalance({ address: senderAccount, name: 'Sender account', symbol: currency.toUpperCase() })
const value = isLocalRPC ? ETH_AMOUNT : fromDecimals({ amount, decimals: 18 }) const value = isLocalRPC ? ETH_AMOUNT : fromDecimals({ amount, decimals: 18 })
console.log('Submitting deposit transaction') console.log('Submitting deposit transaction')
await generateTransaction(contractAddress, 1200000, await tornado.methods.deposit(tornadoInstance, toHex(deposit.commitment), []).encodeABI(), value) 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: tornadoContract._address, name: 'Tornado contract', symbol: currency.toUpperCase() })
await printETHBalance({ address: senderAccount, name: 'Sender account', symbol: currency.toUpperCase() }) await printETHBalance({ address: senderAccount, name: 'Sender account', symbol: currency.toUpperCase() })
} else { } else {
@ -142,18 +160,18 @@ async function deposit({ currency, amount }) {
const tokenAmount = isLocalRPC ? TOKEN_AMOUNT : fromDecimals({ amount, decimals }) const tokenAmount = isLocalRPC ? TOKEN_AMOUNT : fromDecimals({ amount, decimals })
if (isLocalRPC) { if (isLocalRPC) {
console.log('Minting some test tokens to deposit') console.log('Minting some test tokens to deposit')
await generateTransaction(erc20Address, 2000000, await erc20.methods.mint(senderAccount, tokenAmount).encodeABI()) await generateTransaction(erc20Address, await erc20.methods.mint(senderAccount, tokenAmount).encodeABI())
} }
const allowance = await erc20.methods.allowance(senderAccount, tornado._address).call({ from: senderAccount }) const allowance = await erc20.methods.allowance(senderAccount, tornado._address).call({ from: senderAccount })
console.log('Current allowance is', fromWei(allowance)) console.log('Current allowance is', fromWei(allowance))
if (toBN(allowance).lt(toBN(tokenAmount))) { if (toBN(allowance).lt(toBN(tokenAmount))) {
console.log('Approving tokens for deposit') console.log('Approving tokens for deposit')
await generateTransaction(erc20Address, 500000, await erc20.methods.approve(tornado._address, tokenAmount).encodeABI()) await generateTransaction(erc20Address, await erc20.methods.approve(tornado._address, tokenAmount).encodeABI())
} }
console.log('Submitting deposit transaction') console.log('Submitting deposit transaction')
await generateTransaction(contractAddress, 2000000, await tornado.methods.deposit(toHex(deposit.commitment)).encodeABI()) await generateTransaction(contractAddress, await tornado.methods.deposit(toHex(deposit.commitment)).encodeABI())
await printERC20Balance({ address: tornadoContract._address, name: 'Tornado contract' }) await printERC20Balance({ address: tornadoContract._address, name: 'Tornado contract' })
await printERC20Balance({ address: senderAccount, name: 'Sender account' }) await printERC20Balance({ address: senderAccount, name: 'Sender account' })
} }
@ -260,7 +278,7 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, torP
throw new Error('ENS name resolving is not supported. Please provide DNS name of the relayer. See instuctions in README.md') throw new Error('ENS name resolving is not supported. Please provide DNS name of the relayer. See instuctions in README.md')
} }
if (torPort) { if (torPort) {
options = { httpsAgent: new SocksProxyAgent('socks://127.0.0.1:'+torPort), headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0' } } options = { httpsAgent: new SocksProxyAgent('socks5h://127.0.0.1:'+torPort), headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0' } }
} }
const relayerStatus = await axios.get(relayerURL + '/status', options) const relayerStatus = await axios.get(relayerURL + '/status', options)
@ -315,7 +333,7 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, torP
const { proof, args } = await generateProof({ deposit, currency, amount, recipient, refund }) const { proof, args } = await generateProof({ deposit, currency, amount, recipient, refund })
console.log('Submitting withdraw transaction') console.log('Submitting withdraw transaction')
await generateTransaction(contractAddress, 400000, await tornado.methods.withdraw(tornadoInstance, proof, ...args).encodeABI()) await generateTransaction(contractAddress, await tornado.methods.withdraw(tornadoInstance, proof, ...args).encodeABI())
} }
console.log('Done withdrawal from Tornado Cash') console.log('Done withdrawal from Tornado Cash')
} }
@ -492,7 +510,7 @@ function getCurrentNetworkSymbol() {
case 56: case 56:
return 'BNB' return 'BNB'
case 100: case 100:
return 'DAI' return 'xDAI'
case 137: case 137:
return 'MATIC' return 'MATIC'
case 43114: case 43114:
@ -523,7 +541,7 @@ async function fetchGasPrice() {
const oracle = new GasPriceOracle(options) const oracle = new GasPriceOracle(options)
const gas = await oracle.gasPrices() const gas = await oracle.gasPrices()
return gasPricesETH(gas.instant) return gasPricesETH(gas.instant)
} else if (isLocalRPC) { } else if (netId == 5 || isLocalRPC) {
return gasPrices(1) return gasPrices(1)
} else { } else {
const oracle = new GasPriceOracle(options) const oracle = new GasPriceOracle(options)
@ -600,7 +618,7 @@ function waitForTxReceipt({ txHash, attempts = 60, delay = 1000 }) {
}) })
} }
function loadCachedEvents({type, currency, amount }) { function loadCachedEvents({ type, currency, amount }) {
try { try {
const module = require(`./cache/${netName.toLowerCase()}/${type}s_${currency}_${amount}.json`) const module = require(`./cache/${netName.toLowerCase()}/${type}s_${currency}_${amount}.json`)
@ -621,7 +639,7 @@ function loadCachedEvents({type, currency, amount }) {
} }
} }
async function fetchEvents({type, currency, amount}) { async function fetchEvents({ type, currency, amount}) {
let leafIndex = -1 let leafIndex = -1
let events = []; let events = [];
let fetchedEvents, chunks, targetBlock; let fetchedEvents, chunks, targetBlock;
@ -807,7 +825,7 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', torPort,
} else { } else {
if (torPort) { if (torPort) {
console.log("Using tor network") console.log("Using tor network")
web3Options = { agent: { https: new SocksProxyAgent('socks://127.0.0.1:'+torPort) }, timeout: 60000 } web3Options = { agent: { https: new SocksProxyAgent('socks5h://127.0.0.1:'+torPort) }, timeout: 60000 }
// Use forked web3-providers-http from local file to modify user-agent header value which improves privacy. // Use forked web3-providers-http from local file to modify user-agent header value which improves privacy.
web3 = new Web3(new Web3HttpProvider(rpc, web3Options), null, { transactionConfirmationBlocks: 1 }) web3 = new Web3(new Web3HttpProvider(rpc, web3Options), null, { transactionConfirmationBlocks: 1 })
} else if (rpc.includes("ipc")) { } else if (rpc.includes("ipc")) {
@ -845,7 +863,7 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100', torPort,
if (noteNetId && Number(noteNetId) !== netId) { if (noteNetId && Number(noteNetId) !== netId) {
throw new Error('This note is for a different network. Specify the --rpc option explicitly') throw new Error('This note is for a different network. Specify the --rpc option explicitly')
} }
if (getCurrentNetworkName() === "localRPC") { if (netName === "localRPC") {
isLocalRPC = true; isLocalRPC = true;
} }

View File

@ -252,5 +252,83 @@ module.exports = {
}, },
proxy: '0x0D5550d52428E7e3175bfc9550207e4ad3859b17', proxy: '0x0D5550d52428E7e3175bfc9550207e4ad3859b17',
}, },
netId100: {
'xdai': {
'instanceAddress': {
'100': '0x1E34A77868E19A6647b1f2F47B51ed72dEDE95DD',
'1000': '0xdf231d99Ff8b6c6CBF4E9B9a945CBAcEF9339178',
'10000': '0xaf4c0B70B2Ea9FB7487C7CbB37aDa259579fe040',
'100000': '0xa5C2254e4253490C54cef0a4347fddb8f75A4998'
},
'deployedBlockNumber': {
'100': 17754566,
'1000': 17754568,
'10000': 17754572,
'100000': 17754574
},
'miningEnabled': false,
'symbol': 'xDAI',
'decimals': 18
},
proxy: '0x0D5550d52428E7e3175bfc9550207e4ad3859b17',
},
netId137: {
'matic': {
'instanceAddress': {
'100': '0x1E34A77868E19A6647b1f2F47B51ed72dEDE95DD',
'1000': '0xdf231d99Ff8b6c6CBF4E9B9a945CBAcEF9339178',
'10000': '0xaf4c0B70B2Ea9FB7487C7CbB37aDa259579fe040',
'100000': '0xa5C2254e4253490C54cef0a4347fddb8f75A4998'
},
'deployedBlockNumber': {
'100': 16258013,
'1000': 16258032,
'10000': 16258046,
'100000': 16258053
},
'miningEnabled': false,
'symbol': 'MATIC',
'decimals': 18
},
proxy: '0x0D5550d52428E7e3175bfc9550207e4ad3859b17',
},
netId42161: {
'eth': {
'instanceAddress': {
'0.1': '0x84443CFd09A48AF6eF360C6976C5392aC5023a1F',
'1': '0xd47438C816c9E7f2E2888E060936a499Af9582b3',
'10': '0x330bdFADE01eE9bF63C209Ee33102DD334618e0a',
'100': '0x1E34A77868E19A6647b1f2F47B51ed72dEDE95DD'
},
'deployedBlockNumber': {
'0.1': 3300000,
'1': 3300000,
'10': 3300000,
'100': 3300000
},
'miningEnabled': false,
'symbol': 'ETH',
'decimals': 18
},
proxy: '0x0D5550d52428E7e3175bfc9550207e4ad3859b17',
},
netId43114: {
'avax': {
'instanceAddress': {
'10': '0x330bdFADE01eE9bF63C209Ee33102DD334618e0a',
'100': '0x1E34A77868E19A6647b1f2F47B51ed72dEDE95DD',
'500': '0xaf8d1839c3c67cf571aa74B5c12398d4901147B3'
},
'deployedBlockNumber': {
'10': 4429830,
'100': 4429851,
'500': 4429837
},
'miningEnabled': false,
'symbol': 'AVAX',
'decimals': 18
},
proxy: '0x0D5550d52428E7e3175bfc9550207e4ad3859b17',
},
} }
} }