Skip to content

Commit

Permalink
clean up isERC721 flag tests + add conditional block to enhance our c…
Browse files Browse the repository at this point in the history
…heck of contract-maps in _detectERC721
  • Loading branch information
adonesky1 committed Jul 20, 2021
1 parent 133af84 commit c7ee5eb
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 66 deletions.
218 changes: 152 additions & 66 deletions src/assets/TokensController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,90 +295,176 @@ describe('TokensController', () => {
});

describe('isERC721 flag', function () {
it('should add isERC721 = true to token object in state when token is collectible and in our contract-metadata repo', async function () {
const supportsInterfaceStub = sinon.stub().returns(Promise.resolve(true));
await sinon
.stub(tokensController, '_createEthersContract')
.callsFake(() =>
Promise.resolve({ supportsInterface: supportsInterfaceStub }),
describe('updateTokenType method', function () {
it('should add isERC721 = true to token object already in state when token is collectible and in our contract-metadata repo', async function () {
const contractAddresses = Object.keys(contractMaps);
const erc721ContractAddresses = contractAddresses.filter(
(contractAddress) => contractMaps[contractAddress].erc721 === true,
);
const contractAddresses = Object.keys(contractMaps);
const erc721ContractAddresses = contractAddresses.filter(
(contractAddress) => contractMaps[contractAddress].erc721 === true,
);
const address = erc721ContractAddresses[0];
const { symbol, decimals } = contractMaps[address];
tokensController.update({
tokens: [{ address, symbol, decimals }],
const address = erc721ContractAddresses[0];
const { symbol, decimals } = contractMaps[address];
tokensController.update({
tokens: [{ address, symbol, decimals }],
});
const result = await tokensController.updateTokenType(address);
expect(result.isERC721).toBe(true);
});

it('should add isERC721 = false to token object already in state when token is not a collectible and is in our contract-metadata repo', async function () {
const contractAddresses = Object.keys(contractMaps);
const erc20ContractAddresses = contractAddresses.filter(
(contractAddress) => contractMaps[contractAddress].erc20 === true,
);
const address = erc20ContractAddresses[0];
const { symbol, decimals } = contractMaps[address];
tokensController.update({
tokens: [{ address, symbol, decimals }],
});
const result = await tokensController.updateTokenType(address);
expect(result.isERC721).toBe(false);
});

it('should add isERC721 = true to token object already in state when token is collectible and is not in our contract-metadata repo', async function () {
const supportsInterfaceStub = sinon
.stub()
.returns(Promise.resolve(true));
await sinon
.stub(tokensController, '_createEthersContract')
.callsFake(() =>
Promise.resolve({ supportsInterface: supportsInterfaceStub }),
);
const tokenAddress = '0xda5584cc586d07c7141aa427224a4bd58e64af7d';
tokensController.update({
tokens: [
{
address: tokenAddress,
symbol: 'TESTNFT',
decimals: 0,
},
],
});

const result = await tokensController.updateTokenType(tokenAddress);

expect(result.isERC721).toBe(true);
});

it('should add isERC721 = false to token object already in state when token is not a collectible and not in our contract-metadata repo', async function () {
const supportsInterfaceStub = sinon
.stub()
.returns(Promise.resolve(false));
await sinon
.stub(tokensController, '_createEthersContract')
.callsFake(() =>
Promise.resolve({ supportsInterface: supportsInterfaceStub }),
);
const tokenAddress = '0xda5584cc586d07c7141aa427224a4bd58e64af7d';
tokensController.update({
tokens: [
{
address: tokenAddress,
symbol: 'TESTNFT',
decimals: 0,
},
],
});

const result = await tokensController.updateTokenType(tokenAddress);

expect(result.isERC721).toBe(false);
});
const result = await tokensController.updateTokenType(address);
expect(result.isERC721).toBe(true);
});

it('should add isERC721 = true to token object in state when token is collectible and not in our contract-metadata repo', async function () {
const supportsInterfaceStub = sinon.stub().returns(Promise.resolve(true));
await sinon
.stub(tokensController, '_createEthersContract')
.callsFake(() =>
Promise.resolve({ supportsInterface: supportsInterfaceStub }),
describe('addToken method', function () {
it('should add isERC721 = true when token is a collectible and is in our contract-metadata repo', async function () {
const contractAddresses = Object.keys(contractMaps);
const erc721ContractAddresses = contractAddresses.filter(
(contractAddress) => contractMaps[contractAddress].erc721 === true,
);
const tokenAddress = '0xda5584cc586d07c7141aa427224a4bd58e64af7d';
tokensController.update({
tokens: [
const address = erc721ContractAddresses[0];
const { symbol, decimals } = contractMaps[address];
await tokensController.addToken(address, symbol, decimals);

expect(tokensController.state.tokens).toStrictEqual([
{
address: tokenAddress,
symbol: 'TESTNFT',
decimals: 0,
address: address,
symbol: symbol,
isERC721: true,
image: undefined,
decimals: decimals,
},
],
]);
});

const result = await tokensController.updateTokenType(tokenAddress);
it('should add isERC721 = true when the token is a collectible but not in our contract-metadata repo', async function () {
const supportsInterfaceStub = sinon
.stub()
.returns(Promise.resolve(true));
await sinon
.stub(tokensController, '_createEthersContract')
.callsFake(() =>
Promise.resolve({ supportsInterface: supportsInterfaceStub }),
);

expect(result.isERC721).toBe(true);
});
const tokenAddress = '0xDA5584Cc586d07c7141aA427224A4Bd58E64aF7D';

it('should return true when token is in our contract-metadata repo', async function () {
const supportsInterfaceStub = sinon.stub().returns(Promise.resolve(true));
await sinon
.stub(tokensController, '_createEthersContract')
.callsFake(() =>
Promise.resolve({ supportsInterface: supportsInterfaceStub }),
);
const tokenAddress = '0x06012c8cf97BEaD5deAe237070F9587f8E7A266d';
await tokensController.addToken(tokenAddress, 'REST', 4);

const result = await tokensController._detectIsERC721(tokenAddress);
expect(result).toBe(true);
});
expect(tokensController.state.tokens).toStrictEqual([
{
address: tokenAddress,
symbol: 'REST',
isERC721: true,
image: undefined,
decimals: 4,
},
]);
});

it('should return true when the token is not in our contract-metadata repo but tokenContract.supportsInterface returns true', async function () {
const supportsInterfaceStub = sinon.stub().returns(Promise.resolve(true));
await sinon
.stub(tokensController, '_createEthersContract')
.callsFake(() =>
Promise.resolve({ supportsInterface: supportsInterfaceStub }),
);
it('should add isERC721 = false to token object already in state when token is not a collectible and in our contract-metadata repo', async function () {
const contractAddresses = Object.keys(contractMaps);
const erc20ContractAddresses = contractAddresses.filter(
(contractAddress) => contractMaps[contractAddress].erc20 === true,
);
const address = erc20ContractAddresses[0];
const { symbol, decimals } = contractMaps[address];

const tokenAddress = '0xda5584cc586d07c7141aa427224a4bd58e64af7d';
await tokensController.addToken(address, symbol, decimals);

const result = await tokensController._detectIsERC721(tokenAddress);
expect(tokensController.state.tokens).toStrictEqual([
{
address: address,
symbol: symbol,
isERC721: false,
image: undefined,
decimals: decimals,
},
]);
});

expect(result).toBe(true);
});
it('should add isERC721 = false when the token is not a collectible and not in our contract-metadata repo', async function () {
const supportsInterfaceStub = sinon
.stub()
.returns(Promise.resolve(false));
await sinon
.stub(tokensController, '_createEthersContract')
.callsFake(() =>
Promise.resolve({ supportsInterface: supportsInterfaceStub }),
);
const tokenAddress = '0xDA5584Cc586d07c7141aA427224A4Bd58E64aF7D';

it('should return false when the token is not in our contract-metadata repo and tokenContract.supportsInterface returns false', async function () {
const supportsInterfaceStub = sinon
.stub()
.returns(Promise.resolve(false));
await sinon
.stub(tokensController, '_createEthersContract')
.callsFake(() =>
Promise.resolve({ supportsInterface: supportsInterfaceStub }),
);
const tokenAddress = '0xda5584cc586d07c7141aa427224a4bd58e64af7d';
await tokensController.addToken(tokenAddress, 'LEST', 5);

const result = await tokensController._detectIsERC721(tokenAddress);
expect(result).toBe(false);
expect(tokensController.state.tokens).toStrictEqual([
{
address: tokenAddress,
symbol: 'LEST',
isERC721: false,
image: undefined,
decimals: 5,
},
]);
});
});
});

Expand Down
3 changes: 3 additions & 0 deletions src/assets/TokensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@ export class TokensController extends BaseController<
// to check against the contract
if (contractsMap[checksumAddress]?.erc721 === true) {
return Promise.resolve(true);
} else if (contractsMap[checksumAddress]?.erc20 === true){
return Promise.resolve(false);
}

const tokenContract = await this._createEthersContract(
tokenAddress,
abiERC721,
Expand Down

0 comments on commit c7ee5eb

Please sign in to comment.