Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create NonStandardFallbackClass #838

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/assets/AssetsContractController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { NetworkState } from '../network/NetworkController';
import { ERC721Standard } from './Standards/CollectibleStandards/ERC721/ERC721Standard';
import { ERC1155Standard } from './Standards/CollectibleStandards/ERC1155/ERC1155Standard';
import { ERC20Standard } from './Standards/ERC20Standard';
import { NonStandardFallback } from './Standards/NonStandardFallBack';

/**
* Check if token detection is enabled for certain networks
Expand Down Expand Up @@ -67,6 +68,8 @@ export class AssetsContractController extends BaseController<

private erc20Standard?: ERC20Standard;

private nonStandardFallback?: NonStandardFallback;

/**
* Name of this controller used during composition
*/
Expand Down Expand Up @@ -129,6 +132,11 @@ export class AssetsContractController extends BaseController<
this.erc721Standard = new ERC721Standard(this.web3);
this.erc1155Standard = new ERC1155Standard(this.web3);
this.erc20Standard = new ERC20Standard(this.web3);

this.nonStandardFallback = new NonStandardFallback({
erc721Standard: this.erc721Standard,
erc20Standard: this.erc20Standard,
});
}

get provider() {
Expand Down Expand Up @@ -211,7 +219,8 @@ export class AssetsContractController extends BaseController<
if (
this.erc721Standard === undefined ||
this.erc1155Standard === undefined ||
this.erc20Standard === undefined
this.erc20Standard === undefined ||
this.nonStandardFallback === undefined
) {
throw new Error(MISSING_PROVIDER_ERROR);
}
Expand Down Expand Up @@ -253,7 +262,12 @@ export class AssetsContractController extends BaseController<
// Ignore
}

throw new Error('Unable to determine contract standard');
return await this.nonStandardFallback.getDetails(
tokenAddress,
ipfsGateway,
userAddress,
tokenId,
);
}

/**
Expand Down
52 changes: 37 additions & 15 deletions src/assets/Standards/CollectibleStandards/ERC721/ERC721Standard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,38 @@ export class ERC721Standard {
});
};

getTokenURIAndImage = async (
address: string,
ipfsGateway: string,
tokenId: string,
) => {
let tokenURI, image;

try {
tokenURI = await this.getTokenURI(address, tokenId);
if (tokenURI.startsWith('ipfs://')) {
tokenURI = getFormattedIpfsUrl(ipfsGateway, tokenURI, true);
}
} catch {
// ignore
}

if (tokenURI) {
try {
const response = await timeoutFetch(tokenURI);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may potentially leak users IP. Needs to be an issue created if there isn't already.

const object = await response.json();
image = object?.image;
if (image?.startsWith('ipfs://')) {
image = getFormattedIpfsUrl(ipfsGateway, image, true);
}
} catch {
// ignore
}
}

return { tokenURI, image };
};

/**
* Query if a contract implements an interface.
*
Expand Down Expand Up @@ -242,21 +274,11 @@ export class ERC721Standard {
}

if (tokenId) {
try {
tokenURI = await this.getTokenURI(address, tokenId);
if (tokenURI.startsWith('ipfs://')) {
tokenURI = getFormattedIpfsUrl(ipfsGateway, tokenURI, true);
}

const response = await timeoutFetch(tokenURI);
const object = await response.json();
image = object?.image;
if (image?.startsWith('ipfs://')) {
image = getFormattedIpfsUrl(ipfsGateway, image, true);
}
} catch {
// ignore
}
({ tokenURI, image } = await this.getTokenURIAndImage(
address,
ipfsGateway,
tokenId,
));
}

return {
Expand Down
91 changes: 91 additions & 0 deletions src/assets/Standards/NonStandardFallBack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { BN } from 'ethereumjs-util';
import { UNKNOWN_STANDARD } from '../../constants';
import { ERC721Standard } from './CollectibleStandards/ERC721/ERC721Standard';
import { ERC20Standard } from './ERC20Standard';

export class NonStandardFallback {
private erc20Standard: ERC20Standard;

private erc721Standard: ERC721Standard;

constructor({
erc20Standard,
erc721Standard,
}: {
erc20Standard: ERC20Standard;
erc721Standard: ERC721Standard;
}) {
this.erc20Standard = erc20Standard;
this.erc721Standard = erc721Standard;
}

/**
* Query for useful values if a contract does not fully implement one of the three major token interfaces we support (ERC20, ERC721, ERC1155).
*
* @param address - Asset contract address.
* @param ipfsGateway - The user's preferred IPFS gateway.
* @param userAddress - The public address for the currently active user's account.
* @param tokenId - tokenId of a given token in the contract.
* @returns Promise resolving an object containing the standard, decimals, symbol and balance of the given contract/userAddress pair.
*/
async getDetails(
address: string,
ipfsGateway: string,
userAddress?: string,
tokenId?: string,
): Promise<{
standard: string;
symbol: string | undefined;
decimals: string | undefined;
balance: BN | undefined;
name: string | undefined;
tokenURI: string | undefined;
image: string | undefined;
}> {
let decimals, symbol, balance, name, image, tokenURI;

try {
decimals = await this.erc20Standard.getTokenDecimals(address);
} catch {
// ignore
}

try {
decimals = await this.erc20Standard.getTokenSymbol(address);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

symbol

} catch {
// ignore
}

if (userAddress) {
try {
balance = await this.erc20Standard.getBalanceOf(address, userAddress);
} catch {
// ignore
}
}

try {
name = await this.erc721Standard.getAssetName(address);
} catch {
// ignore
}

if (tokenId) {
({ tokenURI, image } = await this.erc721Standard.getTokenURIAndImage(
address,
ipfsGateway,
tokenId,
));
}

return {
standard: UNKNOWN_STANDARD,
decimals,
symbol,
balance,
name,
tokenURI,
image,
};
}
}
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const RINKEBY_CHAIN_ID = '4';
export const ERC721 = 'ERC721';
export const ERC1155 = 'ERC1155';
export const ERC20 = 'ERC20';
export const UNKNOWN_STANDARD = 'UNKNOWN_STANDARD';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The important thing is how we handle this in the extension imo.


// TOKEN INTERFACE IDS
export const ERC721_INTERFACE_ID = '0x80ac58cd';
Expand Down