Skip to content

Commit

Permalink
trustwallet#255 - refactor ERC721Parser.extractDecodedLogsFromTransac…
Browse files Browse the repository at this point in the history
…tion()
  • Loading branch information
johnnynanjiang committed Jun 8, 2018
1 parent 01b73d8 commit bd18fc2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/common/erc721/ERC721Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,35 @@ export class ERC721Parser {
}
}

public extractDecodedLogsFromTransaction(transaction): any[] {
const results = [];

if (transaction.receipt.logs.length === 0 ) return results;

const decodedLogs = this.abiDecoder.decodeLogs(transaction.receipt.logs).filter((log: any) => log);

if (decodedLogs.length === 0) return results;

decodedLogs.forEach((decodedLog: any) => {
if (this.operationTypes.indexOf(decodedLog.name) >= 0) {
results.push(decodedLog);
}
})

return results;
}

public extractContracts(transactions: any[]): Promise<any[]> {
if (!transactions) return Promise.resolve([]);

const contractAddresses: string[] = [];

transactions.map((transaction: any) => {
if (transaction.receipt.logs.length === 0 ) return;

const decodedLogs = this.abiDecoder.decodeLogs(transaction.receipt.logs).filter((log: any) => log);

if (decodedLogs.length === 0) return;
const decodedLogs = this.extractDecodedLogsFromTransaction(transaction);

decodedLogs.forEach((decodedLog: any) => {
if (this.operationTypes.indexOf(decodedLog.name) >= 0) {
winston.info(`ERC721Parser.extractContracts(), decodedLog.name: ${decodedLog.name}, transaction: ${transaction._id}, contract: ${decodedLog.address.toLowerCase()}`)
contractAddresses.push(decodedLog.address.toLowerCase());
}
winston.info(`ERC721Parser.extractContracts(), decodedLog.name: ${decodedLog.name}, transaction: ${transaction._id}, contract: ${decodedLog.address.toLowerCase()}`)
contractAddresses.push(decodedLog.address.toLowerCase());
})
});

Expand Down
20 changes: 20 additions & 0 deletions test/Common/ERC721/ERC721Parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ describe("Test ERC721Parser", () => {

const block = await blockParser.getBlockByNumber(5665445);
const transactions = await blockTransactionParser.parse(block);

const emptyDecodedLogs = erc721Parser.extractDecodedLogsFromTransaction(
transactions.filter((tx) => {
return tx._id === "0x8fbcf855223bc60f996865e7a05297dc41907b8cbddd03613db462dcb103117a"
})[0]
);

expect(emptyDecodedLogs.length).to.equal(0);

const decodedLogs = erc721Parser.extractDecodedLogsFromTransaction(
transactions.filter((tx) => {
return tx._id === "0x1020494743d0a85187c9cc92f1e8f649552df9502464f72e219e7363d7446a17"
})[0]
);

expect(decodedLogs.length).to.equal(1);
expect(decodedLogs[0].name).to.equal("Approval");
expect(decodedLogs[0].address).to.equal("0x80A7E048F37A50500351C204Cb407766fA3baE7f");
expect(decodedLogs[0].events.length).to.equal(3);

const contractAddresses = await erc721Parser.extractContracts(transactions);

expect(transactions.length).to.equal(178);
Expand Down

0 comments on commit bd18fc2

Please sign in to comment.