fix contract address validation

This commit is contained in:
smart_ex 2022-04-18 15:40:45 +10:00
parent 3c5eaa2c4b
commit 29847087f2
3 changed files with 24 additions and 12 deletions

View File

@ -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())

View File

@ -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
}

View File

@ -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()