Add mode not to submit signed tx to local node

This commit is contained in:
Ayanami 2022-02-04 11:28:11 +09:00
parent a9c6a24204
commit 62451ec5ce
No known key found for this signature in database
GPG Key ID: 0CABDF03077D92E4

76
cli.js
View File

@ -22,12 +22,12 @@ const program = require('commander')
const { GasPriceOracle } = require('gas-price-oracle') const { GasPriceOracle } = require('gas-price-oracle')
const SocksProxyAgent = require('socks-proxy-agent') const SocksProxyAgent = require('socks-proxy-agent')
let web3, tornado, tornadoContract, tornadoInstance, circuit, proving_key, groth16, erc20, senderAccount, netId, netName, netSymbol let web3, tornado, tornadoContract, tornadoInstance, circuit, proving_key, groth16, erc20, senderAccount, netId, netName, netSymbol, isLocalNode
let MERKLE_TREE_HEIGHT, ETH_AMOUNT, TOKEN_AMOUNT, PRIVATE_KEY let MERKLE_TREE_HEIGHT, ETH_AMOUNT, TOKEN_AMOUNT, PRIVATE_KEY
/** Whether we are in a browser or node.js */ /** Whether we are in a browser or node.js */
const inBrowser = typeof window !== 'undefined' const inBrowser = typeof window !== 'undefined'
let isLocalRPC = false let isTestRPC = false
/** Generate random number of specified byte length */ /** Generate random number of specified byte length */
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes)) const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes))
@ -58,6 +58,17 @@ async function printERC20Balance({ address, name, tokenAddress }) {
console.log(`${name}`,tokenName,`Token Balance is`,tokenBalance.div(BigNumber(10).pow(tokenDecimals)).toString(),tokenSymbol) console.log(`${name}`,tokenName,`Token Balance is`,tokenBalance.div(BigNumber(10).pow(tokenDecimals)).toString(),tokenSymbol)
} }
async function submitTransaction(signedTX) {
console.log("Submitting raw transaction to the node");
await web3.eth.sendSignedTransaction(signedTX)
.on('transactionHash', function (txHash) {
console.log(`View transaction on block explorer https://${getExplorerLink()}/tx/${txHash}`)
})
.on('error', function (e) {
console.error('on transactionHash error', e.message)
});
}
async function generateTransaction(to, encodedData, value = 0) { async function generateTransaction(to, encodedData, value = 0) {
const nonce = await web3.eth.getTransactionCount(senderAccount) const nonce = await web3.eth.getTransactionCount(senderAccount)
const gasPrice = await fetchGasPrice() const gasPrice = await fetchGasPrice()
@ -116,13 +127,20 @@ async function generateTransaction(to, encodedData, value = 0) {
} }
await txoptions(); await txoptions();
const signed = await web3.eth.accounts.signTransaction(tx, PRIVATE_KEY); const signed = await web3.eth.accounts.signTransaction(tx, PRIVATE_KEY);
await web3.eth.sendSignedTransaction(signed.rawTransaction) if (!isLocalNode) {
.on('transactionHash', function (txHash) { await web3.eth.sendSignedTransaction(signed.rawTransaction)
console.log(`View transaction on block explorer https://${getExplorerLink()}/tx/${txHash}`) .on('transactionHash', function (txHash) {
}) console.log(`View transaction on block explorer https://${getExplorerLink()}/tx/${txHash}`)
.on('error', function (e) { })
console.error('on transactionHash error', e.message) .on('error', function (e) {
}); console.error('on transactionHash error', e.message)
});
} else {
console.log('\n=============Raw TX=================','\n')
console.log(`Please submit this raw tx to https://${getExplorerLink()}/pushTx, or otherwise broadcast with node cli.js broadcast command.`,`\n`)
console.log(signed.rawTransaction,`\n`)
console.log('=====================================','\n')
}
} }
/** /**
@ -165,7 +183,7 @@ async function deposit({ currency, amount }) {
if (currency === netSymbol.toLowerCase()) { if (currency === netSymbol.toLowerCase()) {
await printETHBalance({ address: tornadoContract._address, name: 'Tornado contract' }) await printETHBalance({ address: tornadoContract._address, name: 'Tornado contract' })
await printETHBalance({ address: senderAccount, name: 'Sender account' }) await printETHBalance({ address: senderAccount, name: 'Sender account' })
const value = isLocalRPC ? ETH_AMOUNT : fromDecimals({ amount, decimals: 18 }) const value = isTestRPC ? ETH_AMOUNT : fromDecimals({ amount, decimals: 18 })
console.log('Submitting deposit transaction') console.log('Submitting deposit transaction')
await generateTransaction(contractAddress, tornado.methods.deposit(tornadoInstance, toHex(deposit.commitment), []).encodeABI(), value) await generateTransaction(contractAddress, tornado.methods.deposit(tornadoInstance, toHex(deposit.commitment), []).encodeABI(), value)
await printETHBalance({ address: tornadoContract._address, name: 'Tornado contract' }) await printETHBalance({ address: tornadoContract._address, name: 'Tornado contract' })
@ -174,9 +192,9 @@ async function deposit({ currency, amount }) {
// a token // a token
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' })
const decimals = isLocalRPC ? 18 : config.deployments[`netId${netId}`][currency].decimals const decimals = isTestRPC ? 18 : config.deployments[`netId${netId}`][currency].decimals
const tokenAmount = isLocalRPC ? TOKEN_AMOUNT : fromDecimals({ amount, decimals }) const tokenAmount = isTestRPC ? TOKEN_AMOUNT : fromDecimals({ amount, decimals })
if (isLocalRPC) { if (isTestRPC) {
console.log('Minting some test tokens to deposit') console.log('Minting some test tokens to deposit')
await generateTransaction(erc20Address, erc20.methods.mint(senderAccount, tokenAmount).encodeABI()) await generateTransaction(erc20Address, erc20.methods.mint(senderAccount, tokenAmount).encodeABI())
} }
@ -306,7 +324,7 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, torP
const gasPrice = await fetchGasPrice() const gasPrice = await fetchGasPrice()
const decimals = isLocalRPC ? 18 : config.deployments[`netId${netId}`][currency].decimals const decimals = isTestRPC ? 18 : config.deployments[`netId${netId}`][currency].decimals
const fee = calculateFee({ const fee = calculateFee({
currency, currency,
gasPrice, gasPrice,
@ -577,7 +595,7 @@ function getCurrentNetworkName() {
case 137: case 137:
return 'Optimism' return 'Optimism'
default: default:
return 'localRPC' return 'testRPC'
} }
} }
@ -617,7 +635,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 (netId == 5 || isLocalRPC) { } else if (netId == 5 || isTestRPC) {
return gasPrices(1) return gasPrices(1)
} else { } else {
const oracle = new GasPriceOracle(options) const oracle = new GasPriceOracle(options)
@ -887,7 +905,7 @@ async function loadWithdrawalData({ amount, currency, deposit }) {
/** /**
* Init web3, contracts, and snark * Init web3, contracts, and snark
*/ */
async function init({ rpc, noteNetId, currency = 'dai', amount = '100', torPort, balanceCheck }) { async function init({ rpc, noteNetId, currency = 'dai', amount = '100', torPort, balanceCheck, localMode }) {
let contractJson, instanceJson, erc20ContractJson, erc20tornadoJson, tornadoAddress, tokenAddress let contractJson, instanceJson, erc20ContractJson, erc20tornadoJson, tornadoAddress, tokenAddress
// TODO do we need this? should it work in browser really? // TODO do we need this? should it work in browser really?
if (inBrowser) { if (inBrowser) {
@ -964,11 +982,15 @@ 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 (netName === "localRPC") { if (netName === "testRPC") {
isLocalRPC = true; isTestRPC = true;
}
if (localMode) {
console.log("Local mode detected: will not submit signed TX to remote node")
isLocalNode = true;
} }
if (isLocalRPC) { if (isTestRPC) {
tornadoAddress = currency === netSymbol.toLowerCase() ? contractJson.networks[netId].address : erc20tornadoJson.networks[netId].address tornadoAddress = currency === netSymbol.toLowerCase() ? contractJson.networks[netId].address : erc20tornadoJson.networks[netId].address
tokenAddress = currency !== netSymbol.toLowerCase() ? erc20ContractJson.networks[netId].address : null tokenAddress = currency !== netSymbol.toLowerCase() ? erc20ContractJson.networks[netId].address : null
deployedBlockNumber = 0 deployedBlockNumber = 0
@ -1019,6 +1041,7 @@ async function main() {
.option('-r, --rpc <URL>', 'The RPC that CLI should interact with', 'http://localhost:8545') .option('-r, --rpc <URL>', 'The RPC that CLI should interact with', 'http://localhost:8545')
.option('-R, --relayer <URL>', 'Withdraw via relayer') .option('-R, --relayer <URL>', 'Withdraw via relayer')
.option('-T, --tor <PORT>', 'Optional tor port') .option('-T, --tor <PORT>', 'Optional tor port')
.option('-L, --local', 'Local Node - Does not submit signed transaction to the node')
program program
.command('deposit <currency> <amount>') .command('deposit <currency> <amount>')
.description( .description(
@ -1026,7 +1049,7 @@ async function main() {
) )
.action(async (currency, amount) => { .action(async (currency, amount) => {
currency = currency.toLowerCase() currency = currency.toLowerCase()
await init({ rpc: program.rpc, currency, amount, torPort: program.tor }) await init({ rpc: program.rpc, currency, amount, torPort: program.tor, localMode: program.local })
await deposit({ currency, amount }) await deposit({ currency, amount })
}) })
program program
@ -1036,7 +1059,7 @@ async function main() {
) )
.action(async (noteString, recipient, refund) => { .action(async (noteString, recipient, refund) => {
const { currency, amount, netId, deposit } = parseNote(noteString) const { currency, amount, netId, deposit } = parseNote(noteString)
await init({ rpc: program.rpc, noteNetId: netId, currency, amount, torPort: program.tor }) await init({ rpc: program.rpc, noteNetId: netId, currency, amount, torPort: program.tor, localMode: program.local })
await withdraw({ await withdraw({
deposit, deposit,
currency, currency,
@ -1065,9 +1088,16 @@ async function main() {
.command('send <address> [amount] [token_address]') .command('send <address> [amount] [token_address]')
.description('Send ETH or ERC to address') .description('Send ETH or ERC to address')
.action(async (address, amount, tokenAddress) => { .action(async (address, amount, tokenAddress) => {
await init({ rpc: program.rpc, torPort: program.tor, balanceCheck: true }) await init({ rpc: program.rpc, torPort: program.tor, balanceCheck: true, localMode: program.local })
await send({ address, amount, tokenAddress }) await send({ address, amount, tokenAddress })
}) })
program
.command('broadcast <signedTX>')
.description('Submit signed TX to the remote node')
.action(async (signedTX) => {
await init({ rpc: program.rpc, torPort: program.tor, balanceCheck: true })
await submitTransaction(signedTX)
})
program program
.command('compliance <note>') .command('compliance <note>')
.description( .description(