Skip to content

Commit

Permalink
trustwallet#255 - implement ERC721Parser.parseOperationsInBlock() and…
Browse files Browse the repository at this point in the history
… it's test
  • Loading branch information
johnnynanjiang committed Jun 10, 2018
1 parent 4279e84 commit 1cae655
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
56 changes: 55 additions & 1 deletion src/common/erc721/ERC721Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import * as Bluebird from "bluebird";
import { loadContractABIs, removeScientificNotationFromNumbString } from "../Utils";
import { Config } from "../Config";
import { nameABI, ownerOfABI, standardERC721ABI } from "../abi/ABI";
import { ERC721Contract } from "../../models/Erc721ContractModel";
import { contracts } from "../tokens/contracts";
import { IContract, IDecodedLog, ISavedTransaction, ITransactionOperation } from "../CommonInterfaces";
import { ERC721Contract } from "../../models/Erc721ContractModel";
import { ERC721TransactionOperation } from "../../models/Erc721TransactionOperationModel";
import { ERC721Transaction } from "../../models/Erc721TransactionModel";
import { ERC721Token } from "../../models/Erc721TokenModel";

export class ERC721Parser {
private abiDecoder = require("abi-decoder");
Expand Down Expand Up @@ -207,6 +208,16 @@ export class ERC721Parser {
});
}

public parseOperationsInBlock(blockNumber) {
return this.getSavedTransactionsInDatabase(blockNumber)
.then(transactions => {
const operations = this.createOperations(transactions)
return this.completeBulk(this.createBulk(operations))
}).catch((error: Error) => {
winston.error(`Error parsing operations in block ${blockNumber}`, error)
})
}

public createOperations(transactions: any[]): any[] {
const operations: any = [];
transactions.forEach(transaction => {
Expand All @@ -220,6 +231,49 @@ export class ERC721Parser {

// ###### private methods ######

private completeBulk(bulk: any): Promise<any> {
if (bulk.length > 0) {
return bulk.execute().catch((err: Error) => {
winston.error(`Could not update token with error: ${err}`);
});
} else {
return Promise.resolve();
}
}

private createBulk(operations: any) {
const bulk = ERC721Token.collection.initializeUnorderedBulkOp();
operations.forEach((operation: any) => {
const contract = operation.contract
const address = operation.address

bulk.find({
_id: address
}).upsert().updateOne({
"$setOnInsert": {
_id: address,
tokens: []
}
});

bulk.find({
_id: address,
tokens: {
"$not": {
"$elemMatch": {
"$in": [contract]
}
}
}
}).updateOne({
"$push": {
tokens: contract
}
});
})
return bulk
}

private updateTransactionOperationInDatabase(transactionOperation): Promise<ITransactionOperation[]> {
return ERC721TransactionOperation.findOneAndUpdate({transactionId: transactionOperation.transactionId}, transactionOperation, {upsert: true, new: true})
.then((operation: any) => {
Expand Down
21 changes: 21 additions & 0 deletions test/Common/ERC721/ERC721Parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BlockParser } from "../../../src/common/erc721/BlockParser";
import { BlockTransactionParser } from "../../../src/common/erc721/BlockTransactionParser";
import { ERC721Parser } from "../../../src/common/erc721/ERC721Parser";
import { Database } from "../../../src/models/Database";
import { ERC721Token } from "../../../src/models/Erc721TokenModel";

const mongoose = require("mongoose");
const config = require("config");
Expand Down Expand Up @@ -121,4 +122,24 @@ describe("Test ERC721Parser", () => {

expect(savedERC721Contract.name).to.equal("CryptoFighters");
})

it("Should create operations from saved transactions", async () => {
const erc721Parser = new ERC721Parser();

const savedTransactions = await erc721Parser.getSavedTransactionsInDatabase(5665445);

const operations = await erc721Parser.createOperations(savedTransactions);

expect(operations.length).to.equal(2);
expect(operations[0].address).to.equal("0xb2c3531f77ee0a7ec7094a0bc87ef4a269e0bcfc");
expect(operations[1].address).to.equal("0xdcf005aa5550f76cd32c925c06a570bc36b0ac6f");
expect(operations[0].contract).to.deep.equal(operations[1].contract);

await erc721Parser.parseOperationsInBlock(5665445);

const owner1 = await ERC721Token.find({_id: "0xb2c3531f77ee0a7ec7094a0bc87ef4a269e0bcfc"});
const owner2 = await ERC721Token.find({_id: "0xdcf005aa5550f76cd32c925c06a570bc36b0ac6f"});

expect(owner1[0].tokens[0]).to.deep.equal(owner2[0].tokens[0]);
})
})

0 comments on commit 1cae655

Please sign in to comment.