Skip to content

Commit

Permalink
trustwallet#255 - implement ERC721Parser.updateTransactionOperationsI…
Browse files Browse the repository at this point in the history
…nDatabase() and it's test
  • Loading branch information
johnnynanjiang committed Jun 9, 2018
1 parent 9b50449 commit cd44a81
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/common/CommonInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface IBlock {
}

export interface ITransactionOperation {
originalTransactionId: string,
transactionId: string,
type: string,
from: string,
Expand Down
26 changes: 16 additions & 10 deletions src/common/erc721/ERC721Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,20 @@ export class ERC721Parser {
}
}

private updateDatabase(transactionId: string, index: number, from: string, to: string, value: string, erc20ContractId?: any): Promise<ITransactionOperation[]> {
const operation = this.createOperationObject(transactionId, index, from, to, value, erc20ContractId);
const indexedOperation = this.getIndexedOperation(transactionId, index);
public updateTransactionOperationsInDatabase(transactionOperations: any[]): Promise<any[]> {
return Promise.all(
transactionOperations.map((transactionOperation) => {
return this.updateTransactionOperationInDatabase(transactionOperation);
})
);
}

return TransactionOperation.findOneAndUpdate({transactionId: indexedOperation}, operation, {upsert: true, new: true})
private updateTransactionOperationInDatabase(transactionOperation): Promise<ITransactionOperation[]> {
return TransactionOperation.findOneAndUpdate({transactionId: transactionOperation.transactionId}, transactionOperation, {upsert: true, new: true})
.then((operation: any) => {
return Transaction.findOneAndUpdate({_id: transactionId}, {$push: {operations: operation._id, addresses: {$each: [operation.to]}}})
return Transaction.findOneAndUpdate({_id: transactionOperation.originalTransactionId}, {$push: {operations: operation._id, addresses: {$each: [operation.to]}}})
.catch((error: Error) => {
winston.error(`Could not update operation and address to transactionID ${transactionId} with error: ${error}`);
winston.error(`Could not update operation and address to transactionID ${transactionOperation.transactionId} with error: ${error}`);
})
}).catch((error: Error) => {
winston.error(`Could not save transaction operation with error: ${error}`);
Expand All @@ -123,12 +128,12 @@ export class ERC721Parser {

public updateERC721ContractsInDatabase(erc721Contracts: any[]): Promise<any[]> {
return Promise.all(erc721Contracts.map((contract) => {
return this.updateContractInDatabase(contract);
return this.updateERC721ContractInDatabase(contract);
})
)
}

public updateContractInDatabase(erc721Contract: any): Promise<any> {
public updateERC721ContractInDatabase(erc721Contract: any): Promise<any> {
erc721Contract.verified = this.isContractVerified(erc721Contract.address);
erc721Contract.enabled = true;

Expand Down Expand Up @@ -218,14 +223,15 @@ export class ERC721Parser {
return `${transactionId}-${index}`.toLowerCase();
}

private createOperationObject(transactionId: string, index: number, from: string, to: string, value: string, type: string, contractID?: any): ITransactionOperation {
private createOperationObject(transactionId: string, index: number, from: string, to: string, value: string, type: string, contractId?: any): ITransactionOperation {
return {
originalTransactionId: transactionId,
transactionId: this.getIndexedOperation(transactionId, index),
type: type,
from: from.toLocaleLowerCase(),
to: to,
value: value,
contract: contractID,
contract: contractId,
};
}

Expand Down
9 changes: 7 additions & 2 deletions test/Common/ERC721/ERC721Parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ describe("Test ERC721Parser", () => {
return txOps.type === "Approval"
})[0];

expect(approvalTxOps.originalTransactionId).to.equal("0x39f5aa0e8782662503910daefa905876cd7b798dab3c15dc0f361ea98ab55cdb");
expect(approvalTxOps.transactionId).to.equal("0x39f5aa0e8782662503910daefa905876cd7b798dab3c15dc0f361ea98ab55cdb-0");
expect(approvalTxOps.type).to.equal("Approval");
expect(approvalTxOps.from).to.equal("0xdcf005aa5550f76cd32c925c06a570bc36b0ac6f");
expect(approvalTxOps.to).to.equal("0xb2c3531f77ee0a7ec7094a0bc87ef4a269e0bcfc");
expect(approvalTxOps.value).to.equal("0");
expect(approvalTxOps.transactionId).to.equal("0x39f5aa0e8782662503910daefa905876cd7b798dab3c15dc0f361ea98ab55cdb-0");
expect(mongoose.Types.ObjectId.isValid(approvalTxOps.contract)).is.true;

const results = await erc721Parser.updateTransactionOperationsInDatabase(transactionOperations);

expect(results.length).to.equal(1);
})

it("Should get ERC721 contract", async () => {
Expand All @@ -98,7 +103,7 @@ describe("Test ERC721Parser", () => {
expect(erc721Contract_CF).to.have.property("totalSupply").a("string");
expect(erc721Contract_CF).to.have.property("implementsERC721").eql(true);

const result = await erc721Parser.updateContractInDatabase(erc721Contract_CF);
const result = await erc721Parser.updateERC721ContractInDatabase(erc721Contract_CF);
// NOTE: check the database, delete the record then run the test, it should appear again.
})
})

0 comments on commit cd44a81

Please sign in to comment.