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