Skip to content

Commit

Permalink
trustwallet#255 - implement backward parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnynanjiang committed Jun 10, 2018
1 parent e7db026 commit 88f2d61
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/common/BlockchainState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class BlockchainState {
lastPusherBlock: blockInChain,
lastTokensBlock: blockInChain,
lastTokensBackwardBlock: blockInChain,
lastTokensBlockForERC721: -1,
lastTokensBlockForERC721: blockInChain,
}).save()
}

Expand All @@ -39,7 +39,7 @@ export class BlockchainState {
}

if (!blockInDb.lastTokensBlockForERC721) {
blockInDb.lastTokensBlockForERC721 = -1
blockInDb.lastTokensBlockForERC721 = blockInChain
}

return blockInDb.save()
Expand Down
13 changes: 10 additions & 3 deletions src/common/erc721/ERC721Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class ERC721Parser {
BlockchainState.getBlockState()
.then(([blockInChain, blockInDb]) => {
const lastTokensBlockForERC721: number = blockInDb.lastTokensBlockForERC721
if (lastTokensBlockForERC721 <= blockInChain) {
const blockNumberToParse = lastTokensBlockForERC721 + 1;
if (lastTokensBlockForERC721 >= 0) {
const blockNumberToParse = lastTokensBlockForERC721 - 1;
new BlockParser().getBlockByNumber(blockNumberToParse)
.then((block) => {
return this.parse(block);
Expand All @@ -66,15 +66,22 @@ export class ERC721Parser {
const mergedTransactions = await this.attachReceiptsToTransactions(transactions, receipts);

const results = await this.updateTransactionsInDatabase(mergedTransactions);
winston.info(`ERC721Parser.updateTransactionsInDatabase, rows: ${results.length}, block: ${block.number}`);

const contractAddresses = await this.extractContractAddresses(transactions);
const contracts = await this.getERC721Contracts(contractAddresses);

const savedContracts = await this.updateERC721ContractsInDatabase(contracts);
winston.info(`ERC721Parser.updateERC721ContractsInDatabase, rows: ${savedContracts.length}, block: ${block.number}`);

const transactionOperations = await this.parseTransactionOperations(transactions, savedContracts);

const savedTransactions = await this.updateTransactionOperationsInDatabase(transactionOperations);
winston.info(`ERC721Parser.updateTransactionOperationsInDatabase, rows: ${savedTransactions.length}, block: ${block.number}`);

const savedTokenOwnerships = await this.updateTokenOwnership(block.number);
// TODO: print out the number of rows affected
winston.info(`ERC721Parser.updateTokenOwnership, rows: ${savedTokenOwnerships}, block: ${block.number}`);

return Promise.resolve(savedTransactions);
}
Expand Down Expand Up @@ -352,7 +359,7 @@ export class ERC721Parser {
});
}

public parseOperationsInBlock(blockNumber) {
public updateTokenOwnership(blockNumber) {
return this.getSavedTransactionsInDatabase(blockNumber)
.then(transactions => {
const operations = this.createOperations(transactions)
Expand Down
2 changes: 1 addition & 1 deletion test/Common/ERC721/ERC721Parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe("Test ERC721Parser", () => {
expect(operations[1].address).to.equal("0xdcf005aa5550f76cd32c925c06a570bc36b0ac6f");
expect(operations[0].contract).to.deep.equal(operations[1].contract);

await erc721Parser.parseOperationsInBlock(5665445);
await erc721Parser.updateTokenOwnership(5665445);

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

0 comments on commit 88f2d61

Please sign in to comment.