Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- improve tests for BlockTransactionParser
- add BlockTransactionParser.updateDatabase()
  • Loading branch information
johnnynanjiang committed Jun 6, 2018
1 parent 734ef15 commit 5be9afc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
14 changes: 14 additions & 0 deletions src/common/erc721/BlockTransactionParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ export class BlockTransactionParser {
return Promise.resolve(results);
}

public updateDatabase(transactions: any) {
const bulkTransactions = Transaction.collection.initializeUnorderedBulkOp();

transactions.forEach((transaction: IExtractedTransaction) =>
bulkTransactions.find({_id: transaction._id}).upsert().replaceOne(transaction)
);

if (bulkTransactions.length === 0) return Promise.resolve();

return bulkTransactions.execute().then((bulkResult: any) => {
return Promise.resolve(transactions);
});
}

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

private getRawTransactions(block): any[] {
Expand Down
2 changes: 2 additions & 0 deletions src/common/erc721/ERC721ContractParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export class ERC721ContractParser {
public extractContracts(transactions: any[]) {

}
}
59 changes: 41 additions & 18 deletions test/Common/ERC721/BlockTransactionParser.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { BlockParser } from "../../../src/common/erc721/BlockParser";
import { BlockTransactionParser } from "../../../src/common/erc721/BlockTransactionParser";
import { Database } from "../../../src/models/Database";

const config = require("config");
const chai = require("chai")
chai.use(require("chai-as-promised"))
const expect = chai.expect

describe("Test BlockTransactionParser", () => {
const getTransactionByID = (transactions: any[], id: string) => {
const results = transactions.filter(t => t._id === "0xb2c6a21504db37e36c5daae3663c704bbba7f1c4b0d16441fc347756e6bbfc9b" );
const results = transactions.filter(t => t._id === id );
return results[0];
}

let db: Database;

before(async () => {
db = new Database(config.get("MONGO.URI"));
db.connect();
})

it("Should parse transactions from a block", async () => {
const blockParser = new BlockParser();
const blockTransactionParser = new BlockTransactionParser();
Expand All @@ -20,22 +29,23 @@ describe("Test BlockTransactionParser", () => {

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

const transaction = getTransactionByID(transactions, "0xb2c6a21504db37e36c5daae3663c704bbba7f1c4b0d16441fc347756e6bbfc9b");
const transaction = getTransactionByID(transactions, "0xa22465a41c60485f29eb4f8f57a04836ab56cd43faafe2439c6de8938f10e974");

expect(transaction._id).to.equal("0xb2c6a21504db37e36c5daae3663c704bbba7f1c4b0d16441fc347756e6bbfc9b");
expect(transaction._id).to.equal("0xa22465a41c60485f29eb4f8f57a04836ab56cd43faafe2439c6de8938f10e974");
expect(transaction.blockNumber).to.equal(5665445);
expect(transaction.timeStamp).to.equal("1527114762");
expect(transaction.nonce).to.equal(4);
expect(transaction.from).to.equal("0xe9e9f607d59da01e1c9a12a708ccfe7c9fdf8c32");
expect(transaction.to).to.equal("0xbe98850613ae66d49d1da1abeaed09daa0e90660");
expect(transaction.value).to.equal("123151800000000000");
expect(transaction.gas).to.equal("21000");
expect(transaction.gasPrice).to.equal("10000000000");
expect(transaction.nonce).to.equal(730986);
expect(transaction.from).to.equal("0x0681d8db095565fe8a346fa0277bffde9c0edbbf");
expect(transaction.to).to.equal("0xd850942ef8811f2a866692a623011bde52a462c1");
expect(transaction.value).to.equal("0");
expect(transaction.gas).to.equal("109670");
expect(transaction.gasPrice).to.equal("50000000000");
expect(transaction.gasUsed).to.equal("0");
expect(transaction.input).to.equal("0x");
expect(transaction.addresses.toString()).to.equal("0xe9e9f607d59da01e1c9a12a708ccfe7c9fdf8c32,0xbe98850613ae66d49d1da1abeaed09daa0e90660");
expect(transaction.hasOwnProperty("receipt")).to.equal(false);
expect(transaction.hasOwnProperty("contract")).to.equal(false);
expect(transaction.input).to.equal("0xa9059cbb00000000000000000000000008865fc372e98ba7f7d7c462fa0aea94ae44109900000000000000000000000000000000000000000000001d070ab97ed4fd8000");
expect(transaction.addresses.toString()).to.equal("0x0681d8db095565fe8a346fa0277bffde9c0edbbf,0xd850942ef8811f2a866692a623011bde52a462c1");

expect(transaction.contract).is.null;
expect(transaction.receipt).is.undefined;

const transactionIDs = blockTransactionParser.getTransactionIDs(transactions);

Expand All @@ -55,21 +65,34 @@ describe("Test BlockTransactionParser", () => {
expect(receipt.from).to.equal("0x0681d8db095565fe8a346fa0277bffde9c0edbbf");
expect(receipt.to).to.equal("0xd850942ef8811f2a866692a623011bde52a462c1");
expect(receipt.gasUsed).to.equal(54835);
expect(receipt.status).to.equal(true);
expect(receipt.transactionHash).to.equal("0xa22465a41c60485f29eb4f8f57a04836ab56cd43faafe2439c6de8938f10e974");
expect(receipt.transactionIndex).to.equal(0);

expect(receipt.logs.length).to.equal(1);
expect(receipt.logs[0].address).to.equal("0xD850942eF8811f2A866692A623011bDE52a462C1");
expect(receipt.logs[0].topics.length).to.equal(3);
expect(receipt.logs[0].topics[0]).to.equal("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef");
expect(receipt.status).to.equal(true);
expect(receipt.transactionHash).to.equal("0xa22465a41c60485f29eb4f8f57a04836ab56cd43faafe2439c6de8938f10e974");
expect(receipt.transactionIndex).to.equal(0);

const mergedTransactions = await blockTransactionParser.mergeTransactionsAndReceipts(transactions, receipts);

expect(mergedTransactions.length).to.equal(178);

const mergedTransaction = getTransactionByID(mergedTransactions, "0xb2c6a21504db37e36c5daae3663c704bbba7f1c4b0d16441fc347756e6bbfc9b");
const mergedTransaction = getTransactionByID(mergedTransactions, "0xa22465a41c60485f29eb4f8f57a04836ab56cd43faafe2439c6de8938f10e974");

expect(mergedTransaction.hasOwnProperty("receipt")).to.equal(true);
expect(mergedTransaction.contract).is.null;
expect(mergedTransaction.receipt).is.not.undefined;

expect(mergedTransaction.receipt.logs.length).to.equal(1);
expect(mergedTransaction.receipt.logs[0].address).to.equal("0xD850942eF8811f2A866692A623011bDE52a462C1");
expect(mergedTransaction.receipt.logs[0].topics.length).to.equal(3);
expect(mergedTransaction.receipt.logs[0].topics[0]).to.equal("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef");

const results = await blockTransactionParser.updateDatabase(mergedTransactions);

expect(results.length).to.equal(178);
// NOTE: check the database to see
// if transaction _id = 0xb2c6a21504db37e36c5daae3663c704bbba7f1c4b0d16441fc347756e6bbfc9b
// is there, delete it then run the test, it should appear again.
})
})

0 comments on commit 5be9afc

Please sign in to comment.