Skip to content

Commit

Permalink
enhancing method
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Dec 23, 2021
1 parent 43f085d commit 5ac4d99
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/assets/AssetsContractController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,34 @@ export class AssetsContractController extends BaseController<
);
}

async getTokenStandard(address: string) {
async getTokenStandardAndDetails(address: string, tokenId?: string) {
// ERC721
try {
const erc721Contract = this.web3.eth.contract(abiERC721).at(address);
const isERC721 = await this.erc721Standard.contractSupportsBase721Interface(
erc721Contract,
);
return isERC721 && ERC721;
const supportsMetadata = await this.erc721Standard.contractSupportsMetadataInterface(
erc721Contract,
);
let tokenURI, symbol, name;
if (supportsMetadata && tokenId) {
tokenURI = this.erc721Standard.getCollectibleTokenURI(
erc721Contract,
tokenId,
);
symbol = this.erc721Standard.getAssetSymbol(erc721Contract);
name = this.erc721Standard.getAssetName(erc721Contract);
}

if (isERC721) {
return {
standard: ERC721,
tokenURI,
symbol,
name,
};
}
} catch {
// Ignore
}
Expand All @@ -160,22 +180,35 @@ export class AssetsContractController extends BaseController<
const isERC1155 = await this.erc1155Standard.contractSupportsBase1155Interface(
erc1155Contract,
);
return isERC1155 && ERC1155;
let tokenURI;
if (tokenId) {
tokenURI = await this.erc1155Standard.uri(erc1155Contract, tokenId);
}

if (isERC1155) {
return {
standard: ERC1155,
tokenURI,
};
}
} catch {
// Ignore
}

// ERC20
try {
const erc20Contract = this.web3.eth.contract(abiERC20).at(address);
await erc20Contract.decimals();
const decimals = await erc20Contract.decimals();
await erc20Contract.totalSupply();
return ERC20;
return {
standard: ERC20,
decimals,
};
} catch {
// Ignore
}

return null;
throw new Error('Unable to determine contract standard');
}

/**
Expand Down

0 comments on commit 5ac4d99

Please sign in to comment.