diff --git a/src/utils.js b/src/utils.js index 2d6486f..f9b4cb2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -10,19 +10,33 @@ const TOKENS = { }, } +const addressMap = new Map() +for (const [key, value] of Object.entries(instances)) { + const netId = Number(key.substring(5)) + for (const [currency, { instanceAddress, symbol, decimals }] of Object.entries(value)) { + Object.entries(instanceAddress).forEach(([amount, address]) => + addressMap.set( + { netId, address }, + { + currency, + amount, + symbol, + decimals, + }, + ), + ) + } +} + const sleep = ms => new Promise(res => setTimeout(res, ms)) function getInstance(address) { address = toChecksumAddress(address) - const inst = instances[`netId${netId}`] - for (const currency of Object.keys(inst)) { - for (const amount of Object.keys(inst[currency].instanceAddress)) { - if (inst[currency].instanceAddress[amount] === address) { - return { currency, amount } - } - } + if (addressMap.has({ netId, address })) { + return addressMap.get({ netId, address }) + } else { + throw new Error('Unknown contact address') } - return null } const poseidonHash = items => toBN(poseidon(items).toString()) diff --git a/src/validator.js b/src/validator.js index 092b9f1..f99b372 100644 --- a/src/validator.js +++ b/src/validator.js @@ -19,7 +19,7 @@ ajv.addKeyword('isAddress', { ajv.addKeyword('isKnownContract', { validate: (schema, data) => { try { - return getInstance(data) !== null + return !!getInstance(data) } catch (e) { return false } diff --git a/src/worker.js b/src/worker.js index 0c3dc8b..354d52e 100644 --- a/src/worker.js +++ b/src/worker.js @@ -19,7 +19,6 @@ const { netId, redisUrl, gasLimits, - instances, privateKey, httpRpcUrl, oracleRpcUrl, @@ -128,8 +127,7 @@ async function getGasPrice() { } async function checkTornadoFee({ args, contract }) { - const { currency, amount } = getInstance(contract) - const { decimals } = instances[`netId${netId}`][currency] + const { currency, amount, decimals } = getInstance(contract) const [fee, refund] = [args[4], args[5]].map(toBN) const gasPrice = await getGasPrice()