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

49
cli.js

@ -11,7 +11,6 @@ const bigInt = snarkjs.bigInt;
const merkleTree = require('@tornado/fixed-merkle-tree');
const Web3 = require('web3');
const Web3HttpProvider = require('@tornado/web3-providers-http');
const { serialize } = require('@ethersproject/transactions');
const buildGroth16 = require('@tornado/websnark/src/groth16');
const websnarkUtils = require('@tornado/websnark/src/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;
const { gasPrice, gasLimit, l1Fee } = await feeOracle.getGasParams(incompletedTx, txType);
const gasCosts = toBN(gasPrice).mul(toBN(gasLimit)).add(toBN(l1Fee));
const { gasPrice, gasLimit } = await feeOracle.getGasParams({ tx: incompletedTx, txType });
const gasCosts = toBN(gasPrice).mul(toBN(gasLimit));
const totalCosts = value.add(gasCosts);
/** Transaction details */
console.log('Gas price: ', web3.utils.hexToNumber(gasPrice));
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 cost: ', rmDecimalBN(fromWei(totalCosts), 12), `${netSymbol}`);
/** ----------------------------------------- **/
@ -415,7 +412,7 @@ async function generateMerkleProof(deposit, currency, amount) {
* @param {string} args.recipient Funds recipient
* @param {string | 0 } args.relayer Relayer address
* @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
* @returns {Promise<ProofData>} Proof data
*/
@ -464,17 +461,19 @@ async function generateProof({ deposit, currency, amount, recipient, relayerAddr
* @param noteString Note to withdraw
* @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 = {};
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');
}
if (!isNaN(Number(refund))) refund = toWei(refund, 'ether');
else refund = toBN(await feeOracle.fetchRefundInETH(currency.toLowerCase()));
if (!web3.utils.isAddress(recipient)) {
throw new Error('Recipient address is not valid');
}
refund = toWei(refund);
if (relayerURL) {
if (relayerURL.endsWith('.eth')) {
throw new Error(
@ -511,7 +510,8 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, refu
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 incompleteWithdrawalTx = {
@ -520,22 +520,31 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, refu
value: toBN(dummyArgs[5]) || 0
};
const totalWithdrawalFee = await feeOracle.calculateWithdrawalFeeViaRelayer(
'user_withdrawal',
incompleteWithdrawalTx,
Number(tornadoServiceFee),
const totalWithdrawalFeeViaRelayer = await feeOracle.calculateWithdrawalFeeViaRelayer({
tx: incompleteWithdrawalTx,
txType: 'user_withdrawal',
relayerFeePercent: tornadoServiceFee,
currency,
amount,
decimals,
refund,
ethPrices?.[currency]
);
const { proof, args } = await calculateDataForRelayer(totalWithdrawalFee);
tokenPriceInEth: ethPrices?.[currency]
});
const { proof, args } = await calculateDataForRelayer(totalWithdrawalFeeViaRelayer);
console.log('Sending withdraw transaction through relay');
/** 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) {
@ -558,12 +567,8 @@ async function withdraw({ deposit, currency, amount, recipient, relayerURL, refu
const result = await getStatus(id, relayerURL, options);
console.log('STATUS', result);
} catch (e) {
if (e.response) {
console.error(e.response.data.error);
} else {
console.error(e.message);
}
}
} else {
// using private key

50
package-lock.json generated

@ -540,6 +540,26 @@
"@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": {
"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",
@ -604,9 +624,9 @@
"integrity": "sha512-7EkpWNfEm34VEOrbLnPpvd/aUJYnA1L+6/qx2fZ/AfmuJFkjSZ18Z4jvVGNY7ktKIhTu3/Tbze+9l3eNueCNIA=="
},
"@tornado/tornado-oracles": {
"version": "2.1.0",
"resolved": "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Ftornado-oracles/-/2.1.0/tornado-oracles-2.1.0.tgz",
"integrity": "sha512-Y6FPAGnCvHLWzUnNYgGoOv+X7KY3CF02rRSawataYaLyl+v2ivh7RYZZZ3G/B5hXf+pD3IFeCdm4PDnTNyNe1g==",
"version": "3.3.0",
"resolved": "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Ftornado-oracles/-/3.3.0/tornado-oracles-3.3.0.tgz",
"integrity": "sha512-OBJ+TmygY6VMcYJCPxSOAzPDZpmF4WhRVhjgTEhqw+hg70WW9L4b3DC1B1P/4Gmar2lzi6BnTI4ckkDy/xsHQQ==",
"requires": {
"@tornado/gas-price-oracle": "^0.5.3",
"@tornado/tornado-config": "^2.0.0",
@ -615,28 +635,10 @@
"ethers": "5.7"
},
"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": {
"version": "20.5.8",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.8.tgz",
"integrity": "sha512-eajsR9aeljqNhK028VG0Wuw+OaY5LLxYmxeoXynIoE6jannr9/Ucd1LL0hSSoafk5LTYG+FfqsyGt81Q6Zkybw=="
},
"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"
}
"version": "20.5.9",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.9.tgz",
"integrity": "sha512-PcGNd//40kHAS3sTlzKB9C9XL4K0sTup8nbG5lC14kzEteTNuAFh9u5nA0o5TWnSG2r/JNPRXFVcHJIIeRlmqQ=="
}
}
},

@ -14,7 +14,7 @@
"@tornado/circomlib": "0.0.20-p2",
"@tornado/fixed-merkle-tree": "0.6.1-p1",
"@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/websnark": "0.0.4-p1",
"axios": "^0.19.2",