|
|
|
@ -6,6 +6,7 @@ class LogsManager { |
|
|
|
|
constructor() { |
|
|
|
|
this.notificationCallbacks = [] |
|
|
|
|
this.subscriptions = {} |
|
|
|
|
this.oldLogs = [] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
checkBlock(blockNumber, block, web3) { |
|
|
|
@ -14,6 +15,7 @@ class LogsManager { |
|
|
|
|
|
|
|
|
|
web3.eth.getTransactionReceipt(txHash, (_error, receipt) => { |
|
|
|
|
for (let log of receipt.logs) { |
|
|
|
|
this.oldLogs.push({ type: 'block', blockNumber, block, tx, log, txNumber: i }) |
|
|
|
|
let subscriptions = this.getSubscriptionsFor({ type: 'block', blockNumber, block, tx, log }) |
|
|
|
|
|
|
|
|
|
for (let subscriptionId of subscriptions) { |
|
|
|
@ -42,6 +44,25 @@ class LogsManager { |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
eventMatchesFilter(changeEvent, queryType, queryFilter) { |
|
|
|
|
console.dir("--> matching topics") |
|
|
|
|
if (queryFilter.topics.filter((logTopic) => changeEvent.log.topics.indexOf(logTopic) >= 0).length === 0) return false |
|
|
|
|
console.dir("topic matched") |
|
|
|
|
|
|
|
|
|
if (queryType === 'logs') { |
|
|
|
|
if ((queryFilter.address === ("0x" + changeEvent.tx.to.toString('hex'))) |
|
|
|
|
&& (queryFilter.address === ('0x' + changeEvent.tx.from.toString('hex')))) { |
|
|
|
|
if (!queryFilter.toBlock) { |
|
|
|
|
return true; |
|
|
|
|
} else if (parseInt(queryFilter.toBlock) > parseInt(changeEvent.blockNumber)) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
|
// * need to get address of deployed contract if it's a tx that create a contract
|
|
|
|
|
getSubscriptionsFor(changeEvent) { |
|
|
|
@ -49,21 +70,9 @@ class LogsManager { |
|
|
|
|
for (let subscriptionId of Object.keys(this.subscriptions)) { |
|
|
|
|
const subscriptionParams = this.subscriptions[subscriptionId] |
|
|
|
|
const [queryType, queryFilter] = subscriptionParams |
|
|
|
|
if (queryType === 'logs') { |
|
|
|
|
if (queryFilter.address === ("0x" + changeEvent.tx.to.toString('hex'))) { |
|
|
|
|
if (!queryFilter.toBlock) { |
|
|
|
|
matchedSubscriptions.push(subscriptionId) |
|
|
|
|
} else if (parseInt(queryFilter.toBlock) > parseInt(changeEvent.blockNumber)) { |
|
|
|
|
matchedSubscriptions.push(subscriptionId) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (queryFilter.address === ('0x' + changeEvent.tx.from.toString('hex'))) { |
|
|
|
|
if (!queryFilter.toBlock) { |
|
|
|
|
matchedSubscriptions.push(subscriptionId) |
|
|
|
|
} else if (parseInt(queryFilter.toBlock) > parseInt(changeEvent.blockNumber)) { |
|
|
|
|
matchedSubscriptions.push(subscriptionId) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (this.eventMatchesFilter(changeEvent, queryType, queryFilter)) { |
|
|
|
|
matchedSubscriptions.push(subscriptionId) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return matchedSubscriptions; |
|
|
|
@ -76,13 +85,24 @@ class LogsManager { |
|
|
|
|
console.dir(result) |
|
|
|
|
|
|
|
|
|
// TODO: manage subscriptions
|
|
|
|
|
// need to associate subscriptions to notificationCallbacks
|
|
|
|
|
|
|
|
|
|
this.notificationCallbacks.forEach((callback) => { |
|
|
|
|
if (result.params.result.raw) { |
|
|
|
|
result.params.result.data = result.params.result.raw.data |
|
|
|
|
result.params.result.topics = result.params.result.raw.topics |
|
|
|
|
} |
|
|
|
|
console.dir("transmitting back") |
|
|
|
|
console.dir(result) |
|
|
|
|
// console.dir(result.params.result.returnValues)
|
|
|
|
|
// console.dir(result.params.result.raw)
|
|
|
|
|
callback(result) |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
addListener(type, cb) { |
|
|
|
|
console.dir("<<<<<<<<<<<------------------------->>>>>>>>>>>>>"); |
|
|
|
|
console.dir("adding listener..."); |
|
|
|
|
this.notificationCallbacks.push(cb) |
|
|
|
|
console.dir("--------------------------------------------------------->") |
|
|
|
|
console.dir("==========================") |
|
|
|
@ -103,16 +123,22 @@ class LogsManager { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getLogsFor(params) { |
|
|
|
|
let results = [{ |
|
|
|
|
"logIndex": "0x1", // 1
|
|
|
|
|
"blockNumber": "0x1b4", // 436
|
|
|
|
|
"blockHash": "0x8216c5785ac562ff41e2dcfdf5785ac562ff41e2dcfdf829c5a142f1fccd7d", |
|
|
|
|
"transactionHash": "0xdf829c5a142f1fccd7d8216c5785ac562ff41e2dcfdf5785ac562ff41e2dcf", |
|
|
|
|
"transactionIndex": "0x0", // 0
|
|
|
|
|
"address": "0x16c5785ac562ff41e2dcfdf829c5a142f1fccd7d", |
|
|
|
|
"data": "0x0000000000000000000000000000000000000000000000000000000000000000", |
|
|
|
|
"topics": ["0x59ebeb90bc63057b6515673c3ecf9438e5058bca0f92585014eced636878c9a5"] |
|
|
|
|
}] |
|
|
|
|
let results = [] |
|
|
|
|
for (let log of this.oldLogs) { |
|
|
|
|
if (this.eventMatchesFilter(log, 'logs', params)) { |
|
|
|
|
results.push({ |
|
|
|
|
"logIndex": "0x1", // 1
|
|
|
|
|
"blockNumber": log.blockNumber, |
|
|
|
|
"blockHash": ('0x' + log.block.hash().toString('hex')), |
|
|
|
|
"transactionHash": ('0x' + log.tx.hash().toString('hex')), |
|
|
|
|
"transactionIndex": "0x" + logs.txNumber.toString(16), |
|
|
|
|
// TODO: if it's a contract deploy, it should be that address instead
|
|
|
|
|
"address": log.log.address, |
|
|
|
|
"data": log.log.data, |
|
|
|
|
"topics": log.log.topics, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return results |
|
|
|
|
} |
|
|
|
|