From 45128343885d3c294d04e127e56b401f06c0c595 Mon Sep 17 00:00:00 2001 From: tornadocontrib Date: Fri, 11 Oct 2024 22:47:02 +0000 Subject: [PATCH] Improve withdrawal confirmation tables --- dist/cli.js | 42 ++++++++++++++++++++++++------------- src/program.ts | 57 +++++++++++++++++++++++++++++++------------------- 2 files changed, 62 insertions(+), 37 deletions(-) diff --git a/dist/cli.js b/dist/cli.js index 38cdf8b..9f39db2 100644 --- a/dist/cli.js +++ b/dist/cli.js @@ -188082,18 +188082,39 @@ Connected with Tovarish Relayer ${(_a = tovarishClient.selectedRelayer) == null ({ fee, refund, proof, args } = yield getProof()); yield TornadoProxy.withdraw.estimateGas(instanceAddress, proof, ...args, withdrawOverrides); } + const txFee = gasPrice * gasLimit; + const txFeeInToken = !isEth ? tornadoFeeOracle.calculateTokenAmount(txFee, tokenPriceInWei, decimals) : BigInt(0); + const txFeeString = !isEth ? `( ${Number(formatUnits(txFeeInToken, decimals)).toFixed(5)} ${currency.toUpperCase()} worth )( ${(Number(formatUnits(txFeeInToken, decimals)) / Number(amount) * 100).toFixed(5)}% )` : `( ${(Number(formatUnits(txFee, decimals)) / Number(amount) * 100).toFixed(5)}% )`; const withdrawTable = new (cli_table3_default())(); - withdrawTable.push([{ colSpan: 2, content: "Withdrawal Info", hAlign: "center" }]); + withdrawTable.push( + [{ colSpan: 2, content: "Withdrawal Info", hAlign: "center" }], + [ + "Deposit Date", + `${depositDate.toLocaleDateString()} ${depositDate.toLocaleTimeString()} (${moment_default().unix(depositEvent.timestamp).fromNow()})` + ], + ["From", depositEvent.from], + ["Deposit Transaction", depositEvent.transactionHash], + ["Commitment", depositEvent.commitment], + ["Gas Price", `${formatUnits(gasPrice, "gwei")} gwei`], + ["Gas Limit", gasLimit], + [ + "Transaction Fee", + `${Number(formatEther(txFee)).toFixed(5)} ${nativeCurrency.toUpperCase()} ${txFeeString}` + ] + ); if (!walletWithdrawal) { + const relayerFeePercent = Number((_c = relayerClient == null ? void 0 : relayerClient.selectedRelayer) == null ? void 0 : _c.tornadoServiceFee) || 0; + const relayerFee = BigInt(denomination) * BigInt(Math.floor(1e4 * relayerFeePercent)) / BigInt(1e4 * 100); withdrawTable.push( - [{ colSpan: 2, content: "Withdraw", hAlign: "center" }], - ["Withdrawal", `${amount} ${currency.toUpperCase()}`], - ["Relayer", `${(_c = relayerClient == null ? void 0 : relayerClient.selectedRelayer) == null ? void 0 : _c.url}`], + ["Relayer", `${(_d = relayerClient == null ? void 0 : relayerClient.selectedRelayer) == null ? void 0 : _d.url}`], [ "Relayer Fee", - `${formatUnits(fee, decimals)} ${currency.toUpperCase()} (${(Number(fee) / Number(denomination) * 100).toFixed(5)}%)` + `${formatUnits(relayerFee, decimals)} ${currency.toUpperCase()} ( ${relayerFeePercent}% )` + ], + [ + "Total Fee", + `${formatUnits(fee, decimals)} ${currency.toUpperCase()} ( ${(Number(fee) / Number(denomination) * 100).toFixed(5)}% )` ], - ["Relayer Fee Percent", `${(_d = relayerClient == null ? void 0 : relayerClient.selectedRelayer) == null ? void 0 : _d.tornadoServiceFee}%`], [ "Amount to receive", `${Number(formatUnits(denomination - fee, decimals)).toFixed(5)} ${currency.toUpperCase()}` @@ -188111,17 +188132,8 @@ Connected with Tovarish Relayer ${(_a = tovarishClient.selectedRelayer) == null args }); } else { - const txFee = gasPrice * gasLimit; - const txFeeInToken = !isEth ? tornadoFeeOracle.calculateTokenAmount(txFee, tokenPriceInWei, decimals) : BigInt(0); - const txFeeString = !isEth ? `(${Number(formatUnits(txFeeInToken, decimals)).toFixed(5)} ${currency.toUpperCase()} worth) (${(Number(formatUnits(txFeeInToken, decimals)) / Number(amount) * 100).toFixed(5)}%)` : `(${(Number(formatUnits(txFee, decimals)) / Number(amount) * 100).toFixed(5)}%)`; withdrawTable.push( - [{ colSpan: 2, content: "Withdraw", hAlign: "center" }], - ["Withdrawal", `${amount} ${currency.toUpperCase()}`], ["Signer", `${signer == null ? void 0 : signer.address}`], - [ - "Transaction Fee", - `${Number(formatEther(txFee)).toFixed(5)} ${nativeCurrency.toUpperCase()} ` + txFeeString - ], ["Amount to receive", `${amount} ${currency.toUpperCase()}`], ["To", recipient], ["Nullifier", nullifierHex] diff --git a/src/program.ts b/src/program.ts index 8a748e0..d9bade6 100644 --- a/src/program.ts +++ b/src/program.ts @@ -1204,21 +1204,50 @@ export function tornadoProgram() { await TornadoProxy.withdraw.estimateGas(instanceAddress, proof, ...args, withdrawOverrides); } + const txFee = gasPrice * gasLimit; + const txFeeInToken = !isEth + ? tornadoFeeOracle.calculateTokenAmount(txFee, tokenPriceInWei, decimals) + : BigInt(0); + const txFeeString = !isEth + ? `( ${Number(formatUnits(txFeeInToken, decimals)).toFixed(5)} ${currency.toUpperCase()} worth )` + + `( ${((Number(formatUnits(txFeeInToken, decimals)) / Number(amount)) * 100).toFixed(5)}% )` + : `( ${((Number(formatUnits(txFee, decimals)) / Number(amount)) * 100).toFixed(5)}% )`; + const withdrawTable = new Table(); - withdrawTable.push([{ colSpan: 2, content: 'Withdrawal Info', hAlign: 'center' }]); + withdrawTable.push( + [{ colSpan: 2, content: 'Withdrawal Info', hAlign: 'center' }], + [ + 'Deposit Date', + `${depositDate.toLocaleDateString()} ${depositDate.toLocaleTimeString()} (${moment.unix(depositEvent.timestamp).fromNow()})`, + ], + ['From', depositEvent.from], + ['Deposit Transaction', depositEvent.transactionHash], + ['Commitment', depositEvent.commitment], + ['Gas Price', `${formatUnits(gasPrice, 'gwei')} gwei`], + ['Gas Limit', gasLimit], + [ + 'Transaction Fee', + `${Number(formatEther(txFee)).toFixed(5)} ${nativeCurrency.toUpperCase()} ${txFeeString}`, + ], + ); // withdraw using relayer if (!walletWithdrawal) { + const relayerFeePercent = Number(relayerClient?.selectedRelayer?.tornadoServiceFee) || 0; + const relayerFee = + (BigInt(denomination) * BigInt(Math.floor(10000 * relayerFeePercent))) / BigInt(10000 * 100); + withdrawTable.push( - [{ colSpan: 2, content: 'Withdraw', hAlign: 'center' }], - ['Withdrawal', `${amount} ${currency.toUpperCase()}`], ['Relayer', `${relayerClient?.selectedRelayer?.url}`], [ 'Relayer Fee', - `${formatUnits(fee, decimals)} ${currency.toUpperCase()} ` + - `(${((Number(fee) / Number(denomination)) * 100).toFixed(5)}%)`, + `${formatUnits(relayerFee, decimals)} ${currency.toUpperCase()} ` + `( ${relayerFeePercent}% )`, + ], + [ + 'Total Fee', + `${formatUnits(fee, decimals)} ${currency.toUpperCase()} ` + + `( ${((Number(fee) / Number(denomination)) * 100).toFixed(5)}% )`, ], - ['Relayer Fee Percent', `${relayerClient?.selectedRelayer?.tornadoServiceFee}%`], [ 'Amount to receive', `${Number(formatUnits(denomination - fee, decimals)).toFixed(5)} ${currency.toUpperCase()}`, @@ -1241,24 +1270,8 @@ export function tornadoProgram() { }); } else { // withdraw from wallet - - const txFee = gasPrice * gasLimit; - const txFeeInToken = !isEth - ? tornadoFeeOracle.calculateTokenAmount(txFee, tokenPriceInWei, decimals) - : BigInt(0); - const txFeeString = !isEth - ? `(${Number(formatUnits(txFeeInToken, decimals)).toFixed(5)} ${currency.toUpperCase()} worth) ` + - `(${((Number(formatUnits(txFeeInToken, decimals)) / Number(amount)) * 100).toFixed(5)}%)` - : `(${((Number(formatUnits(txFee, decimals)) / Number(amount)) * 100).toFixed(5)}%)`; - withdrawTable.push( - [{ colSpan: 2, content: 'Withdraw', hAlign: 'center' }], - ['Withdrawal', `${amount} ${currency.toUpperCase()}`], ['Signer', `${signer?.address}`], - [ - 'Transaction Fee', - `${Number(formatEther(txFee)).toFixed(5)} ${nativeCurrency.toUpperCase()} ` + txFeeString, - ], ['Amount to receive', `${amount} ${currency.toUpperCase()}`], ['To', recipient], ['Nullifier', nullifierHex],