Change tornado-oracles version & add more info to withdrawal prompt

This commit is contained in:
Theo 2023-09-07 14:18:30 -07:00
parent f1f0d8be2d
commit 4231f5ab09
3 changed files with 55 additions and 48 deletions

51
cli.js

@ -11,7 +11,6 @@ const bigInt = snarkjs.bigInt;
const merkleTree = require('@tornado/fixed-merkle-tree'); const merkleTree = require('@tornado/fixed-merkle-tree');
const Web3 = require('web3'); const Web3 = require('web3');
const Web3HttpProvider = require('@tornado/web3-providers-http'); const Web3HttpProvider = require('@tornado/web3-providers-http');
const { serialize } = require('@ethersproject/transactions');
const buildGroth16 = require('@tornado/websnark/src/groth16'); const buildGroth16 = require('@tornado/websnark/src/groth16');
const websnarkUtils = require('@tornado/websnark/src/utils'); const websnarkUtils = require('@tornado/websnark/src/utils');
const { toWei, fromWei, toBN, BN } = require('web3-utils'); const { toWei, fromWei, toBN, BN } = require('web3-utils');
@ -169,15 +168,13 @@ async function generateTransaction(to, encodedData, value = 0, txType = 'other')
}; };
if (txType === 'send') incompletedTx['from'] = senderAccount; if (txType === 'send') incompletedTx['from'] = senderAccount;
const { gasPrice, gasLimit, l1Fee } = await feeOracle.getGasParams(incompletedTx, txType); const { gasPrice, gasLimit } = await feeOracle.getGasParams({ tx: incompletedTx, txType });
const gasCosts = toBN(gasPrice).mul(toBN(gasLimit)).add(toBN(l1Fee)); const gasCosts = toBN(gasPrice).mul(toBN(gasLimit));
const totalCosts = value.add(gasCosts); const totalCosts = value.add(gasCosts);
/** Transaction details */ /** Transaction details */
console.log('Gas price: ', web3.utils.hexToNumber(gasPrice)); console.log('Gas price: ', web3.utils.hexToNumber(gasPrice));
console.log('Gas limit: ', gasLimit); console.log('Gas limit: ', gasLimit);
if (!toBN(l1Fee).eq(toBN(0)))
console.log('Additional gas fees (like L1 data fee): ', rmDecimalBN(fromWei(l1Fee), 12), `${netSymbol}`);
console.log('Transaction fee: ', rmDecimalBN(fromWei(gasCosts), 12), `${netSymbol}`); console.log('Transaction fee: ', rmDecimalBN(fromWei(gasCosts), 12), `${netSymbol}`);
console.log('Transaction cost: ', rmDecimalBN(fromWei(totalCosts), 12), `${netSymbol}`); console.log('Transaction cost: ', rmDecimalBN(fromWei(totalCosts), 12), `${netSymbol}`);
/** ----------------------------------------- **/ /** ----------------------------------------- **/
@ -415,7 +412,7 @@ async function generateMerkleProof(deposit, currency, amount) {
* @param {string} args.recipient Funds recipient * @param {string} args.recipient Funds recipient
* @param {string | 0 } args.relayer Relayer address * @param {string | 0 } args.relayer Relayer address
* @param {number} args.fee Relayer fee * @param {number} args.fee Relayer fee
* @param {number} args.refund Receive ether for exchanged tokens * @param {string} args.refund Receive ether for exchanged tokens
* @param {MerkleProof} [args.merkleProof] Valid merkle tree proof * @param {MerkleProof} [args.merkleProof] Valid merkle tree proof
* @returns {Promise<ProofData>} Proof data * @returns {Promise<ProofData>} Proof data
*/ */
@ -464,17 +461,19 @@ async function generateProof({ deposit, currency, amount, recipient, relayerAddr
* @param noteString Note to withdraw * @param noteString Note to withdraw
* @param recipient Recipient address * @param recipient Recipient address
*/ */
async function withdraw({ deposit, currency, amount, recipient, relayerURL, refund = '0' }) { async function withdraw({ deposit, currency, amount, recipient, relayerURL, refund }) {
let options = {}; let options = {};
if (currency === netSymbol.toLowerCase() && refund !== '0') { if (currency === netSymbol.toLowerCase() && refund && refund !== '0') {
throw new Error('The ETH purchase is supposed to be 0 for ETH withdrawals'); throw new Error('The ETH purchase is supposed to be 0 for ETH withdrawals');
} }
if (!isNaN(Number(refund))) refund = toWei(refund, 'ether');
else refund = toBN(await feeOracle.fetchRefundInETH(currency.toLowerCase()));
if (!web3.utils.isAddress(recipient)) { if (!web3.utils.isAddress(recipient)) {
throw new Error('Recipient address is not valid'); throw new Error('Recipient address is not valid');
} }
refund = toWei(refund);
if (relayerURL) { if (relayerURL) {
if (relayerURL.endsWith('.eth')) { if (relayerURL.endsWith('.eth')) {
throw new Error( throw new Error(
@ -511,7 +510,8 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, refu
return { proof, args }; return { proof, args };
} }
const { proof: dummyProof, args: dummyArgs } = await calculateDataForRelayer(); const relayerFee = feeOracle.calculateRelayerFeeInWei(tornadoServiceFee, amount, decimals);
const { proof: dummyProof, args: dummyArgs } = await calculateDataForRelayer(relayerFee);
const withdrawalTxCalldata = tornado.methods.withdraw(tornadoProxyAddress, dummyProof, ...dummyArgs); const withdrawalTxCalldata = tornado.methods.withdraw(tornadoProxyAddress, dummyProof, ...dummyArgs);
const incompleteWithdrawalTx = { const incompleteWithdrawalTx = {
@ -520,22 +520,31 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, refu
value: toBN(dummyArgs[5]) || 0 value: toBN(dummyArgs[5]) || 0
}; };
const totalWithdrawalFee = await feeOracle.calculateWithdrawalFeeViaRelayer( const totalWithdrawalFeeViaRelayer = await feeOracle.calculateWithdrawalFeeViaRelayer({
'user_withdrawal', tx: incompleteWithdrawalTx,
incompleteWithdrawalTx, txType: 'user_withdrawal',
Number(tornadoServiceFee), relayerFeePercent: tornadoServiceFee,
currency, currency,
amount, amount,
decimals, decimals,
refund, refund,
ethPrices?.[currency] tokenPriceInEth: ethPrices?.[currency]
); });
const { proof, args } = await calculateDataForRelayer(totalWithdrawalFee);
const { proof, args } = await calculateDataForRelayer(totalWithdrawalFeeViaRelayer);
console.log('Sending withdraw transaction through relay'); console.log('Sending withdraw transaction through relay');
/** Relayer fee details **/ /** Relayer fee details **/
console.log('Total fees: ', rmDecimalBN(fromWei(toBN(totalWithdrawalFee)), 12), `${netSymbol}`); console.log('Relayer fee: ', rmDecimalBN(fromWei(toBN(relayerFee)), 12), `${currency.toUpperCase()}`);
console.log('Total fees: ', rmDecimalBN(fromWei(toBN(totalWithdrawalFeeViaRelayer)), 12), `${currency.toUpperCase()}`);
const toReceive = toBN(fromDecimals({ amount, decimals })).sub(toBN(totalWithdrawalFeeViaRelayer));
console.log(
'Amount to receive: ',
rmDecimalBN(fromWei(toReceive), 12),
`${currency.toUpperCase()}`,
toBN(refund).gt(toBN(0)) ? ` + ${rmDecimalBN(fromWei(refund), 12)} ${netSymbol}` : ''
);
/** -------------------- **/ /** -------------------- **/
if (shouldPromptConfirmation) { if (shouldPromptConfirmation) {
@ -558,11 +567,7 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, refu
const result = await getStatus(id, relayerURL, options); const result = await getStatus(id, relayerURL, options);
console.log('STATUS', result); console.log('STATUS', result);
} catch (e) { } catch (e) {
if (e.response) { console.error(e.message);
console.error(e.response.data.error);
} else {
console.error(e.message);
}
} }
} else { } else {
// using private key // using private key

50
package-lock.json generated

@ -540,6 +540,26 @@
"@tornado/snarkjs": "0.1.20-p2" "@tornado/snarkjs": "0.1.20-p2"
} }
}, },
"@tornado/gas-price-oracle": {
"version": "0.5.3",
"resolved": "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Fgas-price-oracle/-/0.5.3/gas-price-oracle-0.5.3.tgz",
"integrity": "sha512-LpVfPiPIz3FOmJdiqJf/yoeO5n9/Pd5jgtdY+6hB9lNW0AiWhylhpScojICViS+3OL9QC8CoTlgr+kbfGeO9pQ==",
"requires": {
"axios": "^0.21.2",
"bignumber.js": "^9.0.0",
"node-cache": "^5.1.2"
},
"dependencies": {
"axios": {
"version": "0.21.4",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
"integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==",
"requires": {
"follow-redirects": "^1.14.0"
}
}
}
},
"@tornado/snarkjs": { "@tornado/snarkjs": {
"version": "0.1.20-p2", "version": "0.1.20-p2",
"resolved": "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Fsnarkjs/-/0.1.20-p2/snarkjs-0.1.20-p2.tgz", "resolved": "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Fsnarkjs/-/0.1.20-p2/snarkjs-0.1.20-p2.tgz",
@ -604,9 +624,9 @@
"integrity": "sha512-7EkpWNfEm34VEOrbLnPpvd/aUJYnA1L+6/qx2fZ/AfmuJFkjSZ18Z4jvVGNY7ktKIhTu3/Tbze+9l3eNueCNIA==" "integrity": "sha512-7EkpWNfEm34VEOrbLnPpvd/aUJYnA1L+6/qx2fZ/AfmuJFkjSZ18Z4jvVGNY7ktKIhTu3/Tbze+9l3eNueCNIA=="
}, },
"@tornado/tornado-oracles": { "@tornado/tornado-oracles": {
"version": "2.1.0", "version": "3.3.0",
"resolved": "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Ftornado-oracles/-/2.1.0/tornado-oracles-2.1.0.tgz", "resolved": "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Ftornado-oracles/-/3.3.0/tornado-oracles-3.3.0.tgz",
"integrity": "sha512-Y6FPAGnCvHLWzUnNYgGoOv+X7KY3CF02rRSawataYaLyl+v2ivh7RYZZZ3G/B5hXf+pD3IFeCdm4PDnTNyNe1g==", "integrity": "sha512-OBJ+TmygY6VMcYJCPxSOAzPDZpmF4WhRVhjgTEhqw+hg70WW9L4b3DC1B1P/4Gmar2lzi6BnTI4ckkDy/xsHQQ==",
"requires": { "requires": {
"@tornado/gas-price-oracle": "^0.5.3", "@tornado/gas-price-oracle": "^0.5.3",
"@tornado/tornado-config": "^2.0.0", "@tornado/tornado-config": "^2.0.0",
@ -615,28 +635,10 @@
"ethers": "5.7" "ethers": "5.7"
}, },
"dependencies": { "dependencies": {
"@tornado/gas-price-oracle": {
"version": "0.5.3",
"resolved": "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Fgas-price-oracle/-/0.5.3/gas-price-oracle-0.5.3.tgz",
"integrity": "sha512-LpVfPiPIz3FOmJdiqJf/yoeO5n9/Pd5jgtdY+6hB9lNW0AiWhylhpScojICViS+3OL9QC8CoTlgr+kbfGeO9pQ==",
"requires": {
"axios": "^0.21.2",
"bignumber.js": "^9.0.0",
"node-cache": "^5.1.2"
}
},
"@types/node": { "@types/node": {
"version": "20.5.8", "version": "20.5.9",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.8.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.9.tgz",
"integrity": "sha512-eajsR9aeljqNhK028VG0Wuw+OaY5LLxYmxeoXynIoE6jannr9/Ucd1LL0hSSoafk5LTYG+FfqsyGt81Q6Zkybw==" "integrity": "sha512-PcGNd//40kHAS3sTlzKB9C9XL4K0sTup8nbG5lC14kzEteTNuAFh9u5nA0o5TWnSG2r/JNPRXFVcHJIIeRlmqQ=="
},
"axios": {
"version": "0.21.4",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
"integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==",
"requires": {
"follow-redirects": "^1.14.0"
}
} }
} }
}, },

@ -14,7 +14,7 @@
"@tornado/circomlib": "0.0.20-p2", "@tornado/circomlib": "0.0.20-p2",
"@tornado/fixed-merkle-tree": "0.6.1-p1", "@tornado/fixed-merkle-tree": "0.6.1-p1",
"@tornado/snarkjs": "0.1.20-p2", "@tornado/snarkjs": "0.1.20-p2",
"@tornado/tornado-oracles": "^2.1.0", "@tornado/tornado-oracles": "^3.3.0",
"@tornado/web3-providers-http": "1.6.5-p1", "@tornado/web3-providers-http": "1.6.5-p1",
"@tornado/websnark": "0.0.4-p1", "@tornado/websnark": "0.0.4-p1",
"axios": "^0.19.2", "axios": "^0.19.2",