forked from tornadocash/tornado-cli
Improve coding style for getProof func and save events using promise
This commit is contained in:
parent
94b03abd19
commit
e55994068c
110
dist/cli.js
vendored
110
dist/cli.js
vendored
@ -189900,7 +189900,7 @@ class BaseEventsService {
|
||||
});
|
||||
const lastBlock = newEvents ? newEvents.lastBlock : allEvents[allEvents.length - 1] ? allEvents[allEvents.length - 1].blockNumber : null;
|
||||
this.validateEvents({ events: allEvents, lastBlock });
|
||||
yield this.saveEvents({ events: allEvents, lastBlock });
|
||||
this.saveEventsPromise = this.saveEvents({ events: allEvents, lastBlock });
|
||||
return {
|
||||
events: allEvents,
|
||||
lastBlock
|
||||
@ -189908,7 +189908,7 @@ class BaseEventsService {
|
||||
});
|
||||
}
|
||||
}
|
||||
class BaseDepositsService extends BaseEventsService {
|
||||
class BaseTornadoService extends BaseEventsService {
|
||||
constructor({
|
||||
netId,
|
||||
provider,
|
||||
@ -190406,7 +190406,7 @@ var __async$9 = (__this, __arguments, generator) => {
|
||||
step((generator = generator.apply(__this, __arguments)).next());
|
||||
});
|
||||
};
|
||||
class NodeDepositsService extends BaseDepositsService {
|
||||
class NodeTornadoService extends BaseTornadoService {
|
||||
constructor({
|
||||
netId,
|
||||
provider,
|
||||
@ -193369,21 +193369,19 @@ function getProgramRelayer(_0) {
|
||||
const relayerStatus2 = yield relayerClient.askRelayerStatus({
|
||||
hostname: new URL(relayer).hostname
|
||||
});
|
||||
if (relayerStatus2) {
|
||||
relayerClient.selectedRelayer = {
|
||||
netId: relayerStatus2.netId,
|
||||
url: relayerStatus2.url,
|
||||
hostname: new URL(relayerStatus2.url).hostname,
|
||||
rewardAccount: address_getAddress(relayerStatus2.rewardAccount),
|
||||
instances: getSupportedInstances(relayerStatus2.instances),
|
||||
gasPrice: (_a = relayerStatus2.gasPrices) == null ? void 0 : _a.fast,
|
||||
ethPrices: relayerStatus2.ethPrices,
|
||||
currentQueue: relayerStatus2.currentQueue,
|
||||
tornadoServiceFee: relayerStatus2.tornadoServiceFee
|
||||
};
|
||||
}
|
||||
relayerClient.selectedRelayer = {
|
||||
netId: relayerStatus2.netId,
|
||||
url: relayerStatus2.url,
|
||||
hostname: new URL(relayerStatus2.url).hostname,
|
||||
rewardAccount: address_getAddress(relayerStatus2.rewardAccount),
|
||||
instances: getSupportedInstances(relayerStatus2.instances),
|
||||
gasPrice: (_a = relayerStatus2.gasPrices) == null ? void 0 : _a.fast,
|
||||
ethPrices: relayerStatus2.ethPrices,
|
||||
currentQueue: relayerStatus2.currentQueue,
|
||||
tornadoServiceFee: relayerStatus2.tornadoServiceFee
|
||||
};
|
||||
return {
|
||||
validRelayers: relayerClient.selectedRelayer ? [relayerClient.selectedRelayer] : [],
|
||||
validRelayers: [relayerClient.selectedRelayer],
|
||||
invalidRelayers: [],
|
||||
relayerClient
|
||||
};
|
||||
@ -193405,6 +193403,7 @@ function getProgramRelayer(_0) {
|
||||
yield registryService.fetchRelayers(),
|
||||
subdomains
|
||||
);
|
||||
yield registryService.saveEventsPromise;
|
||||
const relayerStatus = relayerClient.pickWeightedRandomRelayer(validRelayers);
|
||||
if (relayerStatus) {
|
||||
relayerClient.selectedRelayer = relayerStatus;
|
||||
@ -193743,7 +193742,7 @@ function tornadoProgram() {
|
||||
Multicall,
|
||||
offchainOracleContract ? OffchainOracle__factory.connect(offchainOracleContract, provider) : void 0
|
||||
);
|
||||
const depositsServiceConstructor = {
|
||||
const TornadoServiceConstructor = {
|
||||
netId,
|
||||
provider,
|
||||
graphApi,
|
||||
@ -193756,10 +193755,10 @@ function tornadoProgram() {
|
||||
cacheDirectory: EVENTS_DIR,
|
||||
userDirectory: SAVED_DIR
|
||||
};
|
||||
const depositsService = new NodeDepositsService(program_spreadProps(program_spreadValues({}, depositsServiceConstructor), {
|
||||
const depositsService = new NodeTornadoService(program_spreadProps(program_spreadValues({}, TornadoServiceConstructor), {
|
||||
type: "Deposit"
|
||||
}));
|
||||
const withdrawalsService = new NodeDepositsService(program_spreadProps(program_spreadValues({}, depositsServiceConstructor), {
|
||||
const withdrawalsService = new NodeTornadoService(program_spreadProps(program_spreadValues({}, TornadoServiceConstructor), {
|
||||
type: "Withdrawal"
|
||||
}));
|
||||
const merkleTreeService = new MerkleTreeService({
|
||||
@ -193783,6 +193782,7 @@ function tornadoProgram() {
|
||||
depositTreePromise = depositTreeInitiator;
|
||||
}
|
||||
const withdrawalEvents = (yield withdrawalsService.updateEvents()).events;
|
||||
yield Promise.all([depositsService.saveEventsPromise, withdrawalsService.saveEventsPromise]);
|
||||
const depositEvent = depositEvents.find(({ commitment }) => commitment === commitmentHex);
|
||||
const withdrawalEvent = withdrawalEvents.find(({ nullifierHash }) => nullifierHash === nullifierHex);
|
||||
if (!depositEvent) {
|
||||
@ -193834,52 +193834,54 @@ function tornadoProgram() {
|
||||
!isEth ? tokenPriceOracle.fetchPrices([{ tokenAddress, decimals }]).then((p) => p[0]) : BigInt(0),
|
||||
provider.getFeeData()
|
||||
]);
|
||||
const { pathElements, pathIndices } = tree.path(depositEvent.leafIndex);
|
||||
if (!walletWithdrawal && !(relayerClient == null ? void 0 : relayerClient.selectedRelayer)) {
|
||||
throw new Error(
|
||||
"No valid relayer found for the network, you can either try again, or find any relayers using the relayers command and set with --relayer option"
|
||||
);
|
||||
}
|
||||
const { url, rewardAccount, tornadoServiceFee } = (relayerClient == null ? void 0 : relayerClient.selectedRelayer) || {};
|
||||
let gasPrice = feeData.maxFeePerGas ? feeData.maxFeePerGas + (feeData.maxPriorityFeePerGas || BigInt(0)) : feeData.gasPrice;
|
||||
if (netId === 56 && gasPrice < parseUnits("3.3", "gwei")) {
|
||||
if (netId === NetId.BSC && gasPrice < parseUnits("3.3", "gwei")) {
|
||||
gasPrice = parseUnits("3.3", "gwei");
|
||||
}
|
||||
const defaultGasLimit = instanceGasLimit ? BigInt(instanceGasLimit) : BigInt(DEFAULT_GAS_LIMIT);
|
||||
let gasLimit = defaultGasLimit;
|
||||
const refundGasLimit = isFirstAmount && tokenGasLimit ? BigInt(tokenGasLimit) : void 0;
|
||||
const ethRefund = ethPurchase ? parseEther(`${ethPurchase}`) : !isEth ? tornadoFeeOracle.defaultEthRefund(gasPrice, refundGasLimit) : BigInt(0);
|
||||
if (isEth && ethRefund) {
|
||||
throw new Error("Can not purchase native assets on native asset withdrawal");
|
||||
}
|
||||
function getProof() {
|
||||
return program_async(this, null, function* () {
|
||||
let relayerFee2 = BigInt(0);
|
||||
let relayer2 = ZeroAddress;
|
||||
let fee2 = BigInt(0);
|
||||
let refund2 = BigInt(0);
|
||||
const { url: url2, rewardAccount, tornadoServiceFee } = (relayerClient == null ? void 0 : relayerClient.selectedRelayer) || {};
|
||||
if (!walletWithdrawal) {
|
||||
relayerFee2 = tornadoFeeOracle.calculateRelayerFee({
|
||||
fee2 = tornadoFeeOracle.calculateRelayerFee({
|
||||
gasPrice,
|
||||
gasLimit,
|
||||
l1Fee,
|
||||
denomination,
|
||||
ethRefund,
|
||||
ethRefund: refund2,
|
||||
tokenPriceInWei,
|
||||
tokenDecimals: decimals,
|
||||
relayerFeePercent: tornadoServiceFee,
|
||||
isEth
|
||||
});
|
||||
if (relayerFee2 > denomination) {
|
||||
const errMsg = `Relayer fee ${formatUnits(relayerFee2, decimals)} ${currency.toUpperCase()} exceeds the deposit amount ${amount} ${currency.toUpperCase()}.`;
|
||||
relayer2 = rewardAccount;
|
||||
if (fee2 > denomination) {
|
||||
const errMsg = `Relayer fee ${formatUnits(fee2, decimals)} ${currency.toUpperCase()} exceeds the deposit amount ${amount} ${currency.toUpperCase()}.`;
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
}
|
||||
const { pathElements, pathIndices } = tree.path(depositEvent.leafIndex);
|
||||
if (!isEth) {
|
||||
refund2 = ethPurchase ? parseEther(`${ethPurchase}`) : tornadoFeeOracle.defaultEthRefund(gasPrice, refundGasLimit);
|
||||
}
|
||||
const { proof: proof2, args: args2 } = yield calculateSnarkProof(
|
||||
{
|
||||
root: tree.root,
|
||||
nullifierHex,
|
||||
recipient,
|
||||
relayer: !walletWithdrawal ? rewardAccount : ZeroAddress,
|
||||
fee: relayerFee2,
|
||||
refund: ethRefund,
|
||||
fee: fee2,
|
||||
refund: refund2,
|
||||
nullifier,
|
||||
secret,
|
||||
pathElements,
|
||||
@ -193889,20 +193891,24 @@ function tornadoProgram() {
|
||||
provingKey
|
||||
);
|
||||
return {
|
||||
relayerFee: relayerFee2,
|
||||
url: url2,
|
||||
relayer: relayer2,
|
||||
fee: fee2,
|
||||
feePercent: tornadoServiceFee,
|
||||
refund: refund2,
|
||||
proof: proof2,
|
||||
args: args2
|
||||
};
|
||||
});
|
||||
}
|
||||
let { relayerFee, proof, args } = yield getProof();
|
||||
let { url, relayer, fee, feePercent, refund, proof, args } = yield getProof();
|
||||
const withdrawOverrides = {
|
||||
from: !walletWithdrawal ? rewardAccount : signer == null ? void 0 : signer.address,
|
||||
from: !walletWithdrawal ? relayer : signer == null ? void 0 : signer.address,
|
||||
value: args[5] || 0
|
||||
};
|
||||
gasLimit = yield TornadoProxy.withdraw.estimateGas(instanceAddress, proof, ...args, withdrawOverrides);
|
||||
if (gasLimit > defaultGasLimit) {
|
||||
({ relayerFee, proof, args } = yield getProof());
|
||||
({ url, relayer, fee, feePercent, refund, proof, args } = yield getProof());
|
||||
gasLimit = yield TornadoProxy.withdraw.estimateGas(instanceAddress, proof, ...args, withdrawOverrides);
|
||||
}
|
||||
const withdrawTable = new (cli_table3_default())();
|
||||
@ -193914,14 +193920,14 @@ function tornadoProgram() {
|
||||
["Relayer", `${url}`],
|
||||
[
|
||||
"Relayer Fee",
|
||||
`${formatUnits(relayerFee, decimals)} ${currency.toUpperCase()} (${(Number(relayerFee) / Number(denomination) * 100).toFixed(5)}%)`
|
||||
`${formatUnits(fee, decimals)} ${currency.toUpperCase()} (${(Number(fee) / Number(denomination) * 100).toFixed(5)}%)`
|
||||
],
|
||||
["Relayer Fee Percent", `${tornadoServiceFee}%`],
|
||||
["Relayer Fee Percent", `${feePercent}%`],
|
||||
[
|
||||
"Amount to receive",
|
||||
`${Number(formatUnits(denomination - relayerFee, decimals)).toFixed(5)} ${currency.toUpperCase()}`
|
||||
`${Number(formatUnits(denomination - fee, decimals)).toFixed(5)} ${currency.toUpperCase()}`
|
||||
],
|
||||
[`${nativeCurrency.toUpperCase()} purchase`, `${formatEther(ethRefund)} ${nativeCurrency.toUpperCase()}`],
|
||||
[`${nativeCurrency.toUpperCase()} purchase`, `${formatEther(refund)} ${nativeCurrency.toUpperCase()}`],
|
||||
["To", recipient],
|
||||
["Nullifier", nullifierHex]
|
||||
);
|
||||
@ -193981,7 +193987,7 @@ function tornadoProgram() {
|
||||
const provider = getProgramProvider(netId, rpc, config, program_spreadValues({}, fetchDataOptions2));
|
||||
const graphApi = getProgramGraphAPI(options, config);
|
||||
const Tornado = Tornado__factory.connect(instanceAddress, provider);
|
||||
const depositsServiceConstructor = {
|
||||
const TornadoServiceConstructor = {
|
||||
netId,
|
||||
provider,
|
||||
graphApi,
|
||||
@ -193994,10 +194000,10 @@ function tornadoProgram() {
|
||||
cacheDirectory: EVENTS_DIR,
|
||||
userDirectory: SAVED_DIR
|
||||
};
|
||||
const depositsService = new NodeDepositsService(program_spreadProps(program_spreadValues({}, depositsServiceConstructor), {
|
||||
const depositsService = new NodeTornadoService(program_spreadProps(program_spreadValues({}, TornadoServiceConstructor), {
|
||||
type: "Deposit"
|
||||
}));
|
||||
const withdrawalsService = new NodeDepositsService(program_spreadProps(program_spreadValues({}, depositsServiceConstructor), {
|
||||
const withdrawalsService = new NodeTornadoService(program_spreadProps(program_spreadValues({}, TornadoServiceConstructor), {
|
||||
type: "Withdrawal"
|
||||
}));
|
||||
const merkleTreeService = new MerkleTreeService({
|
||||
@ -194055,6 +194061,7 @@ function tornadoProgram() {
|
||||
);
|
||||
}
|
||||
console.log("\n\n" + complianceTable.toString() + "\n");
|
||||
yield Promise.all([depositsService.saveEventsPromise, withdrawalsService.saveEventsPromise]);
|
||||
external_process_default().exit(0);
|
||||
}));
|
||||
program.command("updateEvents").description("Sync the local cache file of tornado cash events.\n\n").argument("[netId]", "Network Chain ID to connect with (see https://chainlist.org for examples)", parseNumber).argument("[currency]", "Currency to sync events").action(
|
||||
@ -194092,6 +194099,7 @@ function tornadoProgram() {
|
||||
userDirectory: SAVED_DIR
|
||||
});
|
||||
yield governanceService.updateEvents();
|
||||
yield governanceService.saveEventsPromise;
|
||||
}
|
||||
if (registryContract) {
|
||||
const registryService = new NodeRegistryService({
|
||||
@ -194106,6 +194114,7 @@ function tornadoProgram() {
|
||||
userDirectory: SAVED_DIR
|
||||
});
|
||||
yield registryService.updateEvents();
|
||||
yield registryService.saveEventsPromise;
|
||||
}
|
||||
const echoService = new NodeEchoService({
|
||||
netId,
|
||||
@ -194119,6 +194128,7 @@ function tornadoProgram() {
|
||||
userDirectory: SAVED_DIR
|
||||
});
|
||||
yield echoService.updateEvents();
|
||||
yield echoService.saveEventsPromise;
|
||||
const encryptedNotesService = new NodeEncryptedNotesService({
|
||||
netId,
|
||||
provider,
|
||||
@ -194131,6 +194141,7 @@ function tornadoProgram() {
|
||||
userDirectory: SAVED_DIR
|
||||
});
|
||||
yield encryptedNotesService.updateEvents();
|
||||
yield encryptedNotesService.saveEventsPromise;
|
||||
const currencies = currencyOpts ? [currencyOpts.toLowerCase()] : Object.keys(tokens);
|
||||
for (const currency of currencies) {
|
||||
const currencyConfig = tokens[currency];
|
||||
@ -194138,7 +194149,7 @@ function tornadoProgram() {
|
||||
for (const amount of amounts) {
|
||||
const instanceAddress = currencyConfig.instanceAddress[amount];
|
||||
const Tornado = Tornado__factory.connect(instanceAddress, provider);
|
||||
const depositsServiceConstructor = {
|
||||
const TornadoServiceConstructor = {
|
||||
netId,
|
||||
provider,
|
||||
graphApi,
|
||||
@ -194151,10 +194162,10 @@ function tornadoProgram() {
|
||||
cacheDirectory: EVENTS_DIR,
|
||||
userDirectory: SAVED_DIR
|
||||
};
|
||||
const depositsService = new NodeDepositsService(program_spreadProps(program_spreadValues({}, depositsServiceConstructor), {
|
||||
const depositsService = new NodeTornadoService(program_spreadProps(program_spreadValues({}, TornadoServiceConstructor), {
|
||||
type: "Deposit"
|
||||
}));
|
||||
const withdrawalsService = new NodeDepositsService(program_spreadProps(program_spreadValues({}, depositsServiceConstructor), {
|
||||
const withdrawalsService = new NodeTornadoService(program_spreadProps(program_spreadValues({}, TornadoServiceConstructor), {
|
||||
type: "Withdrawal"
|
||||
}));
|
||||
const merkleTreeService = new MerkleTreeService({
|
||||
@ -194184,6 +194195,7 @@ function tornadoProgram() {
|
||||
if (nativeCurrency === currency) {
|
||||
yield treeCache.createTree(depositEvents, tree);
|
||||
}
|
||||
yield Promise.all([depositsService.saveEventsPromise, withdrawalsService.saveEventsPromise]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -194280,6 +194292,7 @@ function tornadoProgram() {
|
||||
});
|
||||
console.log("Getting historic note accounts would take a while\n");
|
||||
const echoEvents = (yield echoService.updateEvents()).events;
|
||||
yield echoService.saveEventsPromise;
|
||||
const userEvents = echoEvents.filter(({ address }) => address === signer.address);
|
||||
const existingAccounts = newAccount.decryptAccountsWithWallet(signer, userEvents);
|
||||
const accountsTable = new (cli_table3_default())();
|
||||
@ -194359,6 +194372,7 @@ function tornadoProgram() {
|
||||
userDirectory: SAVED_DIR
|
||||
});
|
||||
const encryptedNoteEvents = (yield encryptedNotesService.updateEvents()).events;
|
||||
yield encryptedNotesService.saveEventsPromise;
|
||||
const accountsTable = new (cli_table3_default())();
|
||||
accountsTable.push(
|
||||
[{ colSpan: 2, content: `Note Accounts (${netId})`, hAlign: "center" }],
|
||||
|
@ -53,7 +53,7 @@
|
||||
"@colors/colors": "1.5.0",
|
||||
"@metamask/eth-sig-util": "^7.0.1",
|
||||
"@tornado/contracts": "1.0.0",
|
||||
"@tornado/core": "git+https://git.tornado.ws/tornadocontrib/tornado-core.git#e7412f3d986f6a89382f444b6fa26e770226643f",
|
||||
"@tornado/core": "git+https://git.tornado.ws/tornadocontrib/tornado-core.git#4623e386a158a01438ac2d7a8b7107cffd10242e",
|
||||
"@tornado/fixed-merkle-tree": "0.7.3",
|
||||
"@tornado/snarkjs": "0.1.20",
|
||||
"@tornado/websnark": "0.0.4",
|
||||
|
135
src/program.ts
135
src/program.ts
@ -50,7 +50,7 @@ import {
|
||||
TornadoVoidSigner,
|
||||
tokenBalances,
|
||||
Deposit,
|
||||
NodeDepositsService,
|
||||
NodeTornadoService,
|
||||
DepositsEvents,
|
||||
WithdrawalsEvents,
|
||||
Relayer,
|
||||
@ -331,22 +331,20 @@ export async function getProgramRelayer({
|
||||
hostname: new URL(relayer).hostname,
|
||||
});
|
||||
|
||||
if (relayerStatus) {
|
||||
relayerClient.selectedRelayer = {
|
||||
netId: relayerStatus.netId,
|
||||
url: relayerStatus.url,
|
||||
hostname: new URL(relayerStatus.url).hostname,
|
||||
rewardAccount: getAddress(relayerStatus.rewardAccount),
|
||||
instances: getSupportedInstances(relayerStatus.instances),
|
||||
gasPrice: relayerStatus.gasPrices?.fast,
|
||||
ethPrices: relayerStatus.ethPrices,
|
||||
currentQueue: relayerStatus.currentQueue,
|
||||
tornadoServiceFee: relayerStatus.tornadoServiceFee,
|
||||
};
|
||||
}
|
||||
relayerClient.selectedRelayer = {
|
||||
netId: relayerStatus.netId,
|
||||
url: relayerStatus.url,
|
||||
hostname: new URL(relayerStatus.url).hostname,
|
||||
rewardAccount: getAddress(relayerStatus.rewardAccount),
|
||||
instances: getSupportedInstances(relayerStatus.instances),
|
||||
gasPrice: relayerStatus.gasPrices?.fast,
|
||||
ethPrices: relayerStatus.ethPrices,
|
||||
currentQueue: relayerStatus.currentQueue,
|
||||
tornadoServiceFee: relayerStatus.tornadoServiceFee,
|
||||
};
|
||||
|
||||
return {
|
||||
validRelayers: relayerClient.selectedRelayer ? [relayerClient.selectedRelayer] : [],
|
||||
validRelayers: [relayerClient.selectedRelayer],
|
||||
invalidRelayers: [],
|
||||
relayerClient,
|
||||
};
|
||||
@ -372,6 +370,7 @@ export async function getProgramRelayer({
|
||||
await registryService.fetchRelayers(),
|
||||
subdomains,
|
||||
);
|
||||
await registryService.saveEventsPromise;
|
||||
|
||||
const relayerStatus = relayerClient.pickWeightedRandomRelayer(validRelayers);
|
||||
|
||||
@ -847,7 +846,7 @@ export function tornadoProgram() {
|
||||
offchainOracleContract ? OffchainOracle__factory.connect(offchainOracleContract, provider) : undefined,
|
||||
);
|
||||
|
||||
const depositsServiceConstructor = {
|
||||
const TornadoServiceConstructor = {
|
||||
netId,
|
||||
provider,
|
||||
graphApi,
|
||||
@ -861,13 +860,13 @@ export function tornadoProgram() {
|
||||
userDirectory: SAVED_DIR,
|
||||
};
|
||||
|
||||
const depositsService = new NodeDepositsService({
|
||||
...depositsServiceConstructor,
|
||||
const depositsService = new NodeTornadoService({
|
||||
...TornadoServiceConstructor,
|
||||
type: 'Deposit',
|
||||
});
|
||||
|
||||
const withdrawalsService = new NodeDepositsService({
|
||||
...depositsServiceConstructor,
|
||||
const withdrawalsService = new NodeTornadoService({
|
||||
...TornadoServiceConstructor,
|
||||
type: 'Withdrawal',
|
||||
});
|
||||
|
||||
@ -899,6 +898,8 @@ export function tornadoProgram() {
|
||||
|
||||
const withdrawalEvents = (await withdrawalsService.updateEvents()).events as WithdrawalsEvents[];
|
||||
|
||||
await Promise.all([depositsService.saveEventsPromise, withdrawalsService.saveEventsPromise]);
|
||||
|
||||
const depositEvent = depositEvents.find(({ commitment }) => commitment === commitmentHex);
|
||||
|
||||
const withdrawalEvent = withdrawalEvents.find(({ nullifierHash }) => nullifierHash === nullifierHex);
|
||||
@ -965,19 +966,19 @@ export function tornadoProgram() {
|
||||
provider.getFeeData(),
|
||||
]);
|
||||
|
||||
const { pathElements, pathIndices } = tree.path((depositEvent as DepositsEvents).leafIndex);
|
||||
|
||||
if (!walletWithdrawal && !relayerClient?.selectedRelayer) {
|
||||
throw new Error(
|
||||
'No valid relayer found for the network, you can either try again, or find any relayers using the relayers command and set with --relayer option',
|
||||
);
|
||||
}
|
||||
|
||||
const { url, rewardAccount, tornadoServiceFee } = relayerClient?.selectedRelayer || {};
|
||||
|
||||
let gasPrice: bigint = feeData.maxFeePerGas
|
||||
? feeData.maxFeePerGas + (feeData.maxPriorityFeePerGas || BigInt(0))
|
||||
: (feeData.gasPrice as bigint);
|
||||
|
||||
if (netId === 56 && gasPrice < parseUnits('3.3', 'gwei')) {
|
||||
if (netId === NetId.BSC && gasPrice < parseUnits('3.3', 'gwei')) {
|
||||
gasPrice = parseUnits('3.3', 'gwei');
|
||||
}
|
||||
|
||||
@ -988,41 +989,41 @@ export function tornadoProgram() {
|
||||
// If the denomination is small only refund small amount otherwise use the default value
|
||||
const refundGasLimit = isFirstAmount && tokenGasLimit ? BigInt(tokenGasLimit) : undefined;
|
||||
|
||||
const ethRefund = ethPurchase
|
||||
? parseEther(`${ethPurchase}`)
|
||||
: !isEth
|
||||
? tornadoFeeOracle.defaultEthRefund(gasPrice, refundGasLimit)
|
||||
: BigInt(0);
|
||||
|
||||
if (isEth && ethRefund) {
|
||||
throw new Error('Can not purchase native assets on native asset withdrawal');
|
||||
}
|
||||
|
||||
async function getProof() {
|
||||
let relayerFee = BigInt(0);
|
||||
let relayer = ZeroAddress;
|
||||
let fee = BigInt(0);
|
||||
let refund = BigInt(0);
|
||||
|
||||
const { url, rewardAccount, tornadoServiceFee } = relayerClient?.selectedRelayer || {};
|
||||
|
||||
if (!walletWithdrawal) {
|
||||
relayerFee = tornadoFeeOracle.calculateRelayerFee({
|
||||
fee = tornadoFeeOracle.calculateRelayerFee({
|
||||
gasPrice,
|
||||
gasLimit,
|
||||
l1Fee,
|
||||
denomination,
|
||||
ethRefund,
|
||||
ethRefund: refund,
|
||||
tokenPriceInWei,
|
||||
tokenDecimals: decimals,
|
||||
relayerFeePercent: tornadoServiceFee,
|
||||
isEth,
|
||||
});
|
||||
|
||||
if (relayerFee > denomination) {
|
||||
relayer = rewardAccount as string;
|
||||
|
||||
if (fee > denomination) {
|
||||
const errMsg =
|
||||
`Relayer fee ${formatUnits(relayerFee, decimals)} ${currency.toUpperCase()} ` +
|
||||
`Relayer fee ${formatUnits(fee, decimals)} ${currency.toUpperCase()} ` +
|
||||
`exceeds the deposit amount ${amount} ${currency.toUpperCase()}.`;
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
const { pathElements, pathIndices } = tree.path((depositEvent as DepositsEvents).leafIndex);
|
||||
if (!isEth) {
|
||||
refund = ethPurchase
|
||||
? parseEther(`${ethPurchase}`)
|
||||
: tornadoFeeOracle.defaultEthRefund(gasPrice, refundGasLimit);
|
||||
}
|
||||
|
||||
const { proof, args } = await calculateSnarkProof(
|
||||
{
|
||||
@ -1030,8 +1031,8 @@ export function tornadoProgram() {
|
||||
nullifierHex,
|
||||
recipient,
|
||||
relayer: !walletWithdrawal ? (rewardAccount as string) : ZeroAddress,
|
||||
fee: relayerFee,
|
||||
refund: ethRefund,
|
||||
fee,
|
||||
refund,
|
||||
nullifier,
|
||||
secret,
|
||||
pathElements,
|
||||
@ -1042,23 +1043,27 @@ export function tornadoProgram() {
|
||||
);
|
||||
|
||||
return {
|
||||
relayerFee,
|
||||
url,
|
||||
relayer,
|
||||
fee,
|
||||
feePercent: tornadoServiceFee,
|
||||
refund,
|
||||
proof,
|
||||
args,
|
||||
};
|
||||
}
|
||||
|
||||
let { relayerFee, proof, args } = await getProof();
|
||||
let { url, relayer, fee, feePercent, refund, proof, args } = await getProof();
|
||||
|
||||
const withdrawOverrides = {
|
||||
from: !walletWithdrawal ? (rewardAccount as string) : (signer?.address as string),
|
||||
from: !walletWithdrawal ? relayer : (signer?.address as string),
|
||||
value: args[5] || 0,
|
||||
};
|
||||
|
||||
gasLimit = await TornadoProxy.withdraw.estimateGas(instanceAddress, proof, ...args, withdrawOverrides);
|
||||
|
||||
if (gasLimit > defaultGasLimit) {
|
||||
({ relayerFee, proof, args } = await getProof());
|
||||
({ url, relayer, fee, feePercent, refund, proof, args } = await getProof());
|
||||
gasLimit = await TornadoProxy.withdraw.estimateGas(instanceAddress, proof, ...args, withdrawOverrides);
|
||||
}
|
||||
|
||||
@ -1073,15 +1078,15 @@ export function tornadoProgram() {
|
||||
['Relayer', `${url}`],
|
||||
[
|
||||
'Relayer Fee',
|
||||
`${formatUnits(relayerFee, decimals)} ${currency.toUpperCase()} ` +
|
||||
`(${((Number(relayerFee) / Number(denomination)) * 100).toFixed(5)}%)`,
|
||||
`${formatUnits(fee, decimals)} ${currency.toUpperCase()} ` +
|
||||
`(${((Number(fee) / Number(denomination)) * 100).toFixed(5)}%)`,
|
||||
],
|
||||
['Relayer Fee Percent', `${tornadoServiceFee}%`],
|
||||
['Relayer Fee Percent', `${feePercent}%`],
|
||||
[
|
||||
'Amount to receive',
|
||||
`${Number(formatUnits(denomination - relayerFee, decimals)).toFixed(5)} ${currency.toUpperCase()}`,
|
||||
`${Number(formatUnits(denomination - fee, decimals)).toFixed(5)} ${currency.toUpperCase()}`,
|
||||
],
|
||||
[`${nativeCurrency.toUpperCase()} purchase`, `${formatEther(ethRefund)} ${nativeCurrency.toUpperCase()}`],
|
||||
[`${nativeCurrency.toUpperCase()} purchase`, `${formatEther(refund)} ${nativeCurrency.toUpperCase()}`],
|
||||
['To', recipient],
|
||||
['Nullifier', nullifierHex],
|
||||
);
|
||||
@ -1174,7 +1179,7 @@ export function tornadoProgram() {
|
||||
|
||||
const Tornado = Tornado__factory.connect(instanceAddress, provider);
|
||||
|
||||
const depositsServiceConstructor = {
|
||||
const TornadoServiceConstructor = {
|
||||
netId,
|
||||
provider,
|
||||
graphApi,
|
||||
@ -1188,13 +1193,13 @@ export function tornadoProgram() {
|
||||
userDirectory: SAVED_DIR,
|
||||
};
|
||||
|
||||
const depositsService = new NodeDepositsService({
|
||||
...depositsServiceConstructor,
|
||||
const depositsService = new NodeTornadoService({
|
||||
...TornadoServiceConstructor,
|
||||
type: 'Deposit',
|
||||
});
|
||||
|
||||
const withdrawalsService = new NodeDepositsService({
|
||||
...depositsServiceConstructor,
|
||||
const withdrawalsService = new NodeTornadoService({
|
||||
...TornadoServiceConstructor,
|
||||
type: 'Withdrawal',
|
||||
});
|
||||
|
||||
@ -1266,6 +1271,8 @@ export function tornadoProgram() {
|
||||
|
||||
console.log('\n\n' + complianceTable.toString() + '\n');
|
||||
|
||||
await Promise.all([depositsService.saveEventsPromise, withdrawalsService.saveEventsPromise]);
|
||||
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
@ -1316,6 +1323,7 @@ export function tornadoProgram() {
|
||||
});
|
||||
|
||||
await governanceService.updateEvents();
|
||||
await governanceService.saveEventsPromise;
|
||||
}
|
||||
|
||||
if (registryContract) {
|
||||
@ -1332,6 +1340,7 @@ export function tornadoProgram() {
|
||||
});
|
||||
|
||||
await registryService.updateEvents();
|
||||
await registryService.saveEventsPromise;
|
||||
}
|
||||
|
||||
const echoService = new NodeEchoService({
|
||||
@ -1347,6 +1356,7 @@ export function tornadoProgram() {
|
||||
});
|
||||
|
||||
await echoService.updateEvents();
|
||||
await echoService.saveEventsPromise;
|
||||
|
||||
const encryptedNotesService = new NodeEncryptedNotesService({
|
||||
netId,
|
||||
@ -1361,6 +1371,7 @@ export function tornadoProgram() {
|
||||
});
|
||||
|
||||
await encryptedNotesService.updateEvents();
|
||||
await encryptedNotesService.saveEventsPromise;
|
||||
|
||||
const currencies = currencyOpts ? [currencyOpts.toLowerCase()] : Object.keys(tokens);
|
||||
|
||||
@ -1374,7 +1385,7 @@ export function tornadoProgram() {
|
||||
const instanceAddress = currencyConfig.instanceAddress[amount];
|
||||
const Tornado = Tornado__factory.connect(instanceAddress, provider);
|
||||
|
||||
const depositsServiceConstructor = {
|
||||
const TornadoServiceConstructor = {
|
||||
netId,
|
||||
provider,
|
||||
graphApi,
|
||||
@ -1388,13 +1399,13 @@ export function tornadoProgram() {
|
||||
userDirectory: SAVED_DIR,
|
||||
};
|
||||
|
||||
const depositsService = new NodeDepositsService({
|
||||
...depositsServiceConstructor,
|
||||
const depositsService = new NodeTornadoService({
|
||||
...TornadoServiceConstructor,
|
||||
type: 'Deposit',
|
||||
});
|
||||
|
||||
const withdrawalsService = new NodeDepositsService({
|
||||
...depositsServiceConstructor,
|
||||
const withdrawalsService = new NodeTornadoService({
|
||||
...TornadoServiceConstructor,
|
||||
type: 'Withdrawal',
|
||||
});
|
||||
|
||||
@ -1431,6 +1442,8 @@ export function tornadoProgram() {
|
||||
if (nativeCurrency === currency) {
|
||||
await treeCache.createTree(depositEvents, tree);
|
||||
}
|
||||
|
||||
await Promise.all([depositsService.saveEventsPromise, withdrawalsService.saveEventsPromise]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1565,6 +1578,7 @@ export function tornadoProgram() {
|
||||
console.log('Getting historic note accounts would take a while\n');
|
||||
|
||||
const echoEvents = (await echoService.updateEvents()).events;
|
||||
await echoService.saveEventsPromise;
|
||||
|
||||
const userEvents = echoEvents.filter(({ address }) => address === signer.address);
|
||||
|
||||
@ -1675,6 +1689,7 @@ export function tornadoProgram() {
|
||||
});
|
||||
|
||||
const encryptedNoteEvents = (await encryptedNotesService.updateEvents()).events;
|
||||
await encryptedNotesService.saveEventsPromise;
|
||||
|
||||
const accountsTable = new Table();
|
||||
|
||||
|
@ -771,9 +771,9 @@
|
||||
"@openzeppelin/contracts-v3" "npm:@openzeppelin/contracts@3.2.0-rc.0"
|
||||
ethers "^6.4.0"
|
||||
|
||||
"@tornado/core@git+https://git.tornado.ws/tornadocontrib/tornado-core.git#e7412f3d986f6a89382f444b6fa26e770226643f":
|
||||
"@tornado/core@git+https://git.tornado.ws/tornadocontrib/tornado-core.git#4623e386a158a01438ac2d7a8b7107cffd10242e":
|
||||
version "1.0.0"
|
||||
resolved "git+https://git.tornado.ws/tornadocontrib/tornado-core.git#e7412f3d986f6a89382f444b6fa26e770226643f"
|
||||
resolved "git+https://git.tornado.ws/tornadocontrib/tornado-core.git#4623e386a158a01438ac2d7a8b7107cffd10242e"
|
||||
dependencies:
|
||||
"@metamask/eth-sig-util" "^7.0.1"
|
||||
"@tornado/contracts" "^1.0.0"
|
||||
|
Loading…
Reference in New Issue
Block a user