Skip to content

Commit

Permalink
trustwallet#255 - implement ERC721Parser.getERC721Contract() and it's…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
johnnynanjiang committed Jun 8, 2018
1 parent e2ff0b4 commit 91c4651
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
47 changes: 45 additions & 2 deletions src/common/erc721/ERC721Parser.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as winston from "winston";
import * as BluebirdPromise from "bluebird";

import { loadContractABIs } from "../Utils";
import { Config } from "../Config";
import { nameABI, ownerOfABI, standardERC721ABI } from "../abi/ABI";

export class ERC721Parser {
private abiDecoder = require("abi-decoder");
private abiList = loadContractABIs();

// ERC20 - Transfer
// ERC721 - Transfer, Approval, approve
private operationTypes = ["Transfer", "Approval", "approve"];

constructor() {
Expand Down Expand Up @@ -41,6 +42,48 @@ export class ERC721Parser {
return Promise.resolve(uniqueContractAddresses);
}

public convertHexToAscii(symbol: string): string {
if (symbol.startsWith("0x")) {
return Config.web3.utils.hexToAscii(symbol).replace(/\u0000*$/, "");
}
return symbol;
}

public getERC721Contract = async (contractAddress) => {
try {
const contract = await this.getContractInstance(contractAddress, standardERC721ABI)

if (contract.indexOf(undefined) != -1) {
throw new Error()
}

winston.info(`Successfully got ERC721 contract ${contractAddress}`)

return {
name: contract[0],
symbol: contract[1],
totalSupply: contract[2],
implementsERC721: contract[3],
}
} catch (error) {
winston.error(`Error getting ${contractAddress} as an ERC721 contract`, error)
Promise.resolve()
}
}

public getContractInstance = async (contractAddress, ABI, ... args: any[]) => {
const contractPromise = BluebirdPromise.map(ABI, async (abi: any) => {
try {
const contractInstance = new Config.web3.eth.Contract([abi], contractAddress);
return await contractInstance.methods[abi.name](...args).call()
} catch (error) {
winston.error(`Error getting ${contractAddress} as an ERC721 contract instance, method ${abi.name}\n${error}`)
Promise.resolve()
}
})
return contractPromise
}

/*
public parse(block) {
const transactions = this.parseTransactionsInBlock(block);
Expand Down
19 changes: 18 additions & 1 deletion test/Common/ERC721/ERC721Parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ chai.use(require("chai-as-promised"))
const expect = chai.expect

describe("Test ERC721Parser", () => {
it("Should extract ERC contract addresses from transactions", async () => {
it("Should extract all ERC* contract addresses from transactions", async () => {
const blockParser = new BlockParser();
const blockTransactionParser = new BlockTransactionParser();
const erc721Parser = new ERC721Parser();
Expand All @@ -24,4 +24,21 @@ describe("Test ERC721Parser", () => {
expect(contractAddresses.hasOwnProperty("0x1460a58096d80a50a2f1f956dda497611fa4f165"));
expect(contractAddresses.hasOwnProperty("0x87d598064c736dd0c712d329afcfaa0ccc1921a1"));
})

it("Should get ERC721 contract", async () => {
const erc721Parser = new ERC721Parser();

const erc721_Contract_Address_CryptoFighters = "0x87d598064c736dd0c712d329afcfaa0ccc1921a1";
const erc721Contract_CF = await erc721Parser.getERC721Contract(erc721_Contract_Address_CryptoFighters);

expect(erc721Contract_CF).to.have.property("name").eql("CryptoFighters");
expect(erc721Contract_CF).to.have.property("symbol").eql("CF");
expect(erc721Contract_CF).to.have.property("totalSupply").a("string");
expect(erc721Contract_CF).to.have.property("implementsERC721").eql(true);

const erc20_Contract_Address_Crypterium = "0x80A7E048F37A50500351C204Cb407766fA3baE7f";
const erc20Contract_CRPT = await erc721Parser.getERC721Contract(erc20_Contract_Address_Crypterium);

expect(erc20Contract_CRPT).is.undefined;
})
})

0 comments on commit 91c4651

Please sign in to comment.