mirror of
https://github.com/tornadocash/tornado-core.git
synced 2025-04-28 10:56:29 -04:00
cli updates
This commit is contained in:
parent
6d383235bb
commit
ea1435b115
51
cli.js
51
cli.js
@ -24,7 +24,6 @@ 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 isLocalRPC = false
|
||||||
const networks = { '1': 'mainnet', '42': 'kovan' }
|
|
||||||
|
|
||||||
/** 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))
|
||||||
@ -78,7 +77,7 @@ async function deposit({ currency, amount }) {
|
|||||||
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 = isLocalRPC ? ETH_AMOUNT : fromDecimals({ amount, decimals: 18 })
|
||||||
console.log('Submitting deposit transaction')
|
console.log('Submitting deposit transaction')
|
||||||
await tornado.methods.deposit(toHex(deposit.commitment)).send({ value, from: senderAccount, gas:2e6 })
|
await tornado.methods.deposit(toHex(deposit.commitment)).send({ value, from: senderAccount, gas: 2e6 })
|
||||||
await printETHBalance({ address: tornado._address, name: 'Tornado' })
|
await printETHBalance({ address: tornado._address, name: 'Tornado' })
|
||||||
await printETHBalance({ address: senderAccount, name: 'Sender account' })
|
await printETHBalance({ address: senderAccount, name: 'Sender account' })
|
||||||
} else { // a token
|
} else { // a token
|
||||||
@ -86,7 +85,7 @@ async function deposit({ currency, amount }) {
|
|||||||
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 = isLocalRPC ? 18 : config.deployments[`netId${netId}`][currency].decimals
|
||||||
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 erc20.methods.mint(senderAccount, tokenAmount).send({ from: senderAccount, gas: 2e6 })
|
await erc20.methods.mint(senderAccount, tokenAmount).send({ from: senderAccount, gas: 2e6 })
|
||||||
}
|
}
|
||||||
@ -95,11 +94,11 @@ async function deposit({ currency, amount }) {
|
|||||||
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 erc20.methods.approve(tornado._address, tokenAmount).send({ from: senderAccount, gas:1e6 })
|
await erc20.methods.approve(tornado._address, tokenAmount).send({ from: senderAccount, gas: 1e6 })
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Submitting deposit transaction')
|
console.log('Submitting deposit transaction')
|
||||||
await tornado.methods.deposit(toHex(deposit.commitment)).send({ from: senderAccount, gas:2e6 })
|
await tornado.methods.deposit(toHex(deposit.commitment)).send({ from: senderAccount, gas: 2e6 })
|
||||||
await printERC20Balance({ address: tornado._address, name: 'Tornado' })
|
await printERC20Balance({ address: tornado._address, name: 'Tornado' })
|
||||||
await printERC20Balance({ address: senderAccount, name: 'Sender account' })
|
await printERC20Balance({ address: senderAccount, name: 'Sender account' })
|
||||||
}
|
}
|
||||||
@ -196,7 +195,7 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, refu
|
|||||||
}
|
}
|
||||||
refund = toWei(refund)
|
refund = toWei(refund)
|
||||||
if (relayerURL) {
|
if (relayerURL) {
|
||||||
if(relayerURL.endsWith('.eth')) {
|
if (relayerURL.endsWith('.eth')) {
|
||||||
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')
|
||||||
}
|
}
|
||||||
const relayerStatus = await axios.get(relayerURL + '/status')
|
const relayerStatus = await axios.get(relayerURL + '/status')
|
||||||
@ -212,17 +211,17 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, refu
|
|||||||
const { proof, args } = await generateProof({ deposit, recipient, relayerAddress, fee, refund })
|
const { proof, args } = await generateProof({ deposit, recipient, relayerAddress, fee, refund })
|
||||||
|
|
||||||
console.log('Sending withdraw transaction through relay')
|
console.log('Sending withdraw transaction through relay')
|
||||||
try{
|
try {
|
||||||
const relay = await axios.post(relayerURL + '/relay', { contract: tornado._address, proof, args })
|
const relay = await axios.post(relayerURL + '/relay', { contract: tornado._address, proof, args })
|
||||||
if (netId === 1 || netId === 42) {
|
if (netId === 1 || netId === 42) {
|
||||||
console.log(`Transaction submitted through the relay. View transaction on etherscan https://${networks[netId]}.etherscan.io/tx/${relay.data.txHash}`)
|
console.log(`Transaction submitted through the relay. View transaction on etherscan https://${getCurrentNetworkName()}etherscan.io/tx/${relay.data.txHash}`)
|
||||||
} else {
|
} else {
|
||||||
console.log(`Transaction submitted through the relay. The transaction hash is ${relay.data.txHash}`)
|
console.log(`Transaction submitted through the relay. The transaction hash is ${relay.data.txHash}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const receipt = await waitForTxReceipt({ txHash: relay.data.txHash })
|
const receipt = await waitForTxReceipt({ txHash: relay.data.txHash })
|
||||||
console.log('Transaction mined in block', receipt.blockNumber)
|
console.log('Transaction mined in block', receipt.blockNumber)
|
||||||
} catch(e) {
|
} catch (e) {
|
||||||
if (e.response) {
|
if (e.response) {
|
||||||
console.error(e.response.data.error)
|
console.error(e.response.data.error)
|
||||||
} else {
|
} else {
|
||||||
@ -234,13 +233,13 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, refu
|
|||||||
|
|
||||||
console.log('Submitting withdraw transaction')
|
console.log('Submitting withdraw transaction')
|
||||||
await tornado.methods.withdraw(proof, ...args).send({ from: senderAccount, value: refund.toString(), gas: 1e6 })
|
await tornado.methods.withdraw(proof, ...args).send({ from: senderAccount, value: refund.toString(), gas: 1e6 })
|
||||||
.on('transactionHash', function(txHash){
|
.on('transactionHash', function (txHash) {
|
||||||
if (netId === 1 || netId === 42) {
|
if (netId === 1 || netId === 42) {
|
||||||
console.log(`View transaction on etherscan https://${networks[netId]}.etherscan.io/tx/${txHash}`)
|
console.log(`View transaction on etherscan https://${getCurrentNetworkName()}etherscan.io/tx/${txHash}`)
|
||||||
} else {
|
} else {
|
||||||
console.log(`The transaction hash is ${txHash}`)
|
console.log(`The transaction hash is ${txHash}`)
|
||||||
}
|
}
|
||||||
}).on('error', function(e){
|
}).on('error', function (e) {
|
||||||
console.error('on transactionHash error', e.message)
|
console.error('on transactionHash error', e.message)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -336,7 +335,7 @@ function toDecimals(value, decimals, fixed) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getCurrentNetworkName() {
|
function getCurrentNetworkName() {
|
||||||
switch(netId) {
|
switch (netId) {
|
||||||
case 1:
|
case 1:
|
||||||
return ''
|
return ''
|
||||||
case 42:
|
case 42:
|
||||||
@ -346,7 +345,12 @@ function getCurrentNetworkName() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function calculateFee({ gasPrices, currency, amount, refund, ethPrices, relayerServiceFee, decimals }) {
|
function calculateFee({ gasPrices, currency, amount, refund, ethPrices, relayerServiceFee, decimals }) {
|
||||||
const feePercent = toBN(fromDecimals({ amount, decimals })).mul(toBN(relayerServiceFee * 10)).div(toBN('1000'))
|
const decimalsPoint = Math.floor(relayerServiceFee) === Number(relayerServiceFee) ?
|
||||||
|
0 :
|
||||||
|
relayerServiceFee.toString().split('.')[1].length
|
||||||
|
const roundDecimal = 10 ** decimalsPoint
|
||||||
|
const total = toBN(fromDecimals({ amount, decimals }))
|
||||||
|
const feePercent = total.mul(toBN(relayerServiceFee * roundDecimal)).div(toBN(roundDecimal * 100))
|
||||||
const expense = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN(5e5))
|
const expense = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN(5e5))
|
||||||
let desiredFee
|
let desiredFee
|
||||||
switch (currency) {
|
switch (currency) {
|
||||||
@ -355,8 +359,7 @@ function calculateFee({ gasPrices, currency, amount, refund, ethPrices, relayerS
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
desiredFee =
|
desiredFee = expense.add(toBN(refund))
|
||||||
expense.add(toBN(refund))
|
|
||||||
.mul(toBN(10 ** decimals))
|
.mul(toBN(10 ** decimals))
|
||||||
.div(toBN(ethPrices[currency]))
|
.div(toBN(ethPrices[currency]))
|
||||||
desiredFee = desiredFee.add(feePercent)
|
desiredFee = desiredFee.add(feePercent)
|
||||||
@ -516,22 +519,12 @@ async function init({ rpc, noteNetId, currency = 'dai', amount = '100' }) {
|
|||||||
senderAccount = (await web3.eth.getAccounts())[0]
|
senderAccount = (await web3.eth.getAccounts())[0]
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
const account = web3.eth.accounts.privateKeyToAccount('0x' + PRIVATE_KEY)
|
|
||||||
web3.eth.accounts.wallet.add('0x' + PRIVATE_KEY)
|
|
||||||
// eslint-disable-next-line require-atomic-updates
|
|
||||||
web3.eth.defaultAccount = account.address
|
|
||||||
senderAccount = account.address
|
|
||||||
} catch(e) {
|
|
||||||
console.error('Please provide PRIVATE_KEY in .env file')
|
|
||||||
process.exit(1)
|
|
||||||
}
|
|
||||||
try{
|
|
||||||
tornadoAddress = config.deployments[`netId${netId}`][currency].instanceAddress[amount]
|
tornadoAddress = config.deployments[`netId${netId}`][currency].instanceAddress[amount]
|
||||||
if (!tornadoAddress) {
|
if (!tornadoAddress) {
|
||||||
throw new Error()
|
throw new Error()
|
||||||
}
|
}
|
||||||
tokenAddress = config.deployments[`netId${netId}`][currency].tokenAddress
|
tokenAddress = config.deployments[`netId${netId}`][currency].tokenAddress
|
||||||
} catch(e) {
|
} catch (e) {
|
||||||
console.error('There is no such tornado instance, check the currency and amount you provide')
|
console.error('There is no such tornado instance, check the currency and amount you provide')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
@ -630,13 +623,13 @@ async function main() {
|
|||||||
amount = '100'
|
amount = '100'
|
||||||
await init({ rpc: program.rpc, currency, amount })
|
await init({ rpc: program.rpc, currency, amount })
|
||||||
noteString = await deposit({ currency, amount })
|
noteString = await deposit({ currency, amount })
|
||||||
;(parsedNote = parseNote(noteString))
|
; (parsedNote = parseNote(noteString))
|
||||||
await withdraw({ deposit: parsedNote.deposit, currency, amount, recipient: senderAccount, refund: '0.02', relayerURL: program.relayer })
|
await withdraw({ deposit: parsedNote.deposit, currency, amount, recipient: senderAccount, refund: '0.02', relayerURL: program.relayer })
|
||||||
})
|
})
|
||||||
try {
|
try {
|
||||||
await program.parseAsync(process.argv)
|
await program.parseAsync(process.argv)
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
} catch(e) {
|
} catch (e) {
|
||||||
console.log('Error:', e)
|
console.log('Error:', e)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user