Skip to content

Commit

Permalink
Merge branch 'refactor-gas-fee-controller-tests' into extract-gas-fee…
Browse files Browse the repository at this point in the history
…-recommendations
  • Loading branch information
mcmire committed Nov 1, 2021
2 parents ae5b0dd + d87b8c4 commit f103867
Show file tree
Hide file tree
Showing 21 changed files with 1,268 additions and 325 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ module.exports = {
'no-param-reassign': 'off',
radix: 'off',
'require-atomic-updates': 'off',
'jsdoc/match-description': [
'error',
{ matchDescription: '^[A-Z`\\d_][\\s\\S]*[.?!`>)}]$' },
],
},
settings: {
'import/resolver': {
Expand Down
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [18.0.0]
### Added
- **BREAKING**: ERC1155 support ([#615](https://github.com/MetaMask/controllers/pull/615))
- `CollectiblesController` requires `getOwnerOf`, `balanceOfERC1155Collectible` and `uriERC1155Collectible` properties in the constructor which are methods from `AssetsContractController`.
- Add support for custom networks by querying the blockchain as default and add support for IPFS metadata URIs ([#616](https://github.com/MetaMask/controllers/pull/616))

### Changed
- Bump @metamask/contract-metadata from 1.29.0 to 1.30.0 ([#607](https://github.com/MetaMask/controllers/pull/607))

## [17.0.0]
### Added
- Add client id header to GasFeeController ([#597](https://github.com/MetaMask/controllers/pull/597))
Expand Down Expand Up @@ -393,7 +402,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
- Remove shapeshift controller (#209)

[Unreleased]: https://github.com/MetaMask/controllers/compare/v17.0.0...HEAD
[Unreleased]: https://github.com/MetaMask/controllers/compare/v18.0.0...HEAD
[18.0.0]: https://github.com/MetaMask/controllers/compare/v17.0.0...v18.0.0
[17.0.0]: https://github.com/MetaMask/controllers/compare/v16.0.0...v17.0.0
[16.0.0]: https://github.com/MetaMask/controllers/compare/v15.1.0...v16.0.0
[15.1.0]: https://github.com/MetaMask/controllers/compare/v15.0.2...v15.1.0
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metamask/controllers",
"version": "17.0.0",
"version": "18.0.0",
"description": "Collection of platform-agnostic modules for creating secure data models for cryptocurrency wallets",
"keywords": [
"MetaMask",
Expand Down Expand Up @@ -56,6 +56,7 @@
"ethjs-unit": "^0.1.6",
"ethjs-util": "^0.1.6",
"human-standard-collectible-abi": "^1.0.2",
"human-standard-multi-collectible-abi": "^1.0.2",
"human-standard-token-abi": "^2.0.0",
"immer": "^9.0.6",
"isomorphic-fetch": "^3.0.0",
Expand Down
18 changes: 18 additions & 0 deletions src/ComposableController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ describe('ComposableController', () => {
getCollectibleTokenURI: assetContractController.getCollectibleTokenURI.bind(
assetContractController,
),
getOwnerOf: assetContractController.getOwnerOf.bind(
assetContractController,
),
balanceOfERC1155Collectible: assetContractController.balanceOfERC1155Collectible.bind(
assetContractController,
),
uriERC1155Collectible: assetContractController.uriERC1155Collectible.bind(
assetContractController,
),
});
const tokensController = new TokensController({
onPreferencesStateChange: (listener) =>
Expand Down Expand Up @@ -178,6 +187,15 @@ describe('ComposableController', () => {
getCollectibleTokenURI: assetContractController.getCollectibleTokenURI.bind(
assetContractController,
),
getOwnerOf: assetContractController.getOwnerOf.bind(
assetContractController,
),
balanceOfERC1155Collectible: assetContractController.balanceOfERC1155Collectible.bind(
assetContractController,
),
uriERC1155Collectible: assetContractController.uriERC1155Collectible.bind(
assetContractController,
),
});
const tokensController = new TokensController({
onPreferencesStateChange: (listener) =>
Expand Down
120 changes: 73 additions & 47 deletions src/assets/AssetsContractController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import { AssetsContractController } from './AssetsContractController';
const MAINNET_PROVIDER = new HttpProvider(
'https://mainnet.infura.io/v3/341eacb578dd44a1a049cbc5f6fd4035',
);
const GODSADDRESS = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab';
const CKADDRESS = '0x06012c8cf97BEaD5deAe237070F9587f8E7A266d';
const SAI_ADDRESS = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359';

const ERC20_UNI_ADDRESS = '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984';
const ERC20_DAI_ADDRESS = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359';
const ERC721_GODS_ADDRESS = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab';
const ERC1155_ADDRESS = '0x495f947276749ce646f68ac8c248420045cb7b5e';
const ERC1155_ID =
'40815311521795738946686668571398122012172359753720345430028676522525371400193';

const TEST_ACCOUNT_PUBLIC_ADDRESS =
'0x5a3CA5cD63807Ce5e4d7841AB32Ce6B6d9BbBa2D';

describe('AssetsContractController', () => {
let assetsContract: AssetsContractController;
Expand All @@ -27,86 +34,105 @@ describe('AssetsContractController', () => {
);
});

it('should determine if contract supports interface correctly', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const CKSupportsEnumerable = await assetsContract.contractSupportsEnumerableInterface(
CKADDRESS,
);
const GODSSupportsEnumerable = await assetsContract.contractSupportsEnumerableInterface(
GODSADDRESS,
);
expect(CKSupportsEnumerable).toBe(false);
expect(GODSSupportsEnumerable).toBe(true);
});

it('should get balance of contract correctly', async () => {
it('should get balance of ERC-20 token contract correctly', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const CKBalance = await assetsContract.getBalanceOf(
CKADDRESS,
'0xb1690c08e213a35ed9bab7b318de14420fb57d8c',
const UNIBalance = await assetsContract.getBalanceOf(
ERC20_UNI_ADDRESS,
TEST_ACCOUNT_PUBLIC_ADDRESS,
);
const CKNoBalance = await assetsContract.getBalanceOf(
CKADDRESS,
'0xb1690c08e213a35ed9bab7b318de14420fb57d81',
const UNINoBalance = await assetsContract.getBalanceOf(
ERC20_UNI_ADDRESS,
'0x202637dAAEfbd7f131f90338a4A6c69F6Cd5CE91',
);
expect(CKBalance.toNumber()).not.toStrictEqual(0);
expect(CKNoBalance.toNumber()).toStrictEqual(0);
expect(UNIBalance.toNumber()).not.toStrictEqual(0);
expect(UNINoBalance.toNumber()).toStrictEqual(0);
});

it('should get collectible tokenId correctly', async () => {
it('should get ERC-721 collectible tokenId correctly', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const tokenId = await assetsContract.getCollectibleTokenId(
GODSADDRESS,
ERC721_GODS_ADDRESS,
'0x9a90bd8d1149a88b42a99cf62215ad955d6f498a',
0,
);
expect(tokenId).not.toStrictEqual(0);
});

it('should get collectible tokenURI correctly', async () => {
it('should get ERC-721 collectible tokenURI correctly', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const tokenId = await assetsContract.getCollectibleTokenURI(GODSADDRESS, 0);
const tokenId = await assetsContract.getCollectibleTokenURI(
ERC721_GODS_ADDRESS,
'0',
);
expect(tokenId).toStrictEqual('https://api.godsunchained.com/card/0');
});

it('should return empty string as URI when address given is not an NFT', async () => {
it('should throw an error when address given is not an ERC-721 collectible', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const tokenId = await assetsContract.getCollectibleTokenURI(
'0x0000000000000000000000000000000000000000',
0,
);
expect(tokenId).toStrictEqual('');
const result = async () => {
await assetsContract.getCollectibleTokenURI(
'0x0000000000000000000000000000000000000000',
'0',
);
};

const error = 'Contract does not support ERC721 metadata interface.';
await expect(result).rejects.toThrow(error);
});

it('should get collectible name', async () => {
it('should get ERC-721 collectible name', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const name = await assetsContract.getAssetName(GODSADDRESS);
const name = await assetsContract.getAssetName(ERC721_GODS_ADDRESS);
expect(name).toStrictEqual('Gods Unchained');
});

it('should get collectible symbol', async () => {
it('should get ERC-721 collectible symbol', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const symbol = await assetsContract.getAssetSymbol(GODSADDRESS);
const symbol = await assetsContract.getAssetSymbol(ERC721_GODS_ADDRESS);
expect(symbol).toStrictEqual('GODS');
});

it('should get token decimals', async () => {
it('should get ERC-20 token decimals', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const symbol = await assetsContract.getTokenDecimals(SAI_ADDRESS);
const symbol = await assetsContract.getTokenDecimals(ERC20_DAI_ADDRESS);
expect(Number(symbol)).toStrictEqual(18);
});

it('should get collectible ownership', async () => {
it('should get ERC-721 collectible ownership', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const tokenId = await assetsContract.getOwnerOf(GODSADDRESS, 148332);
const tokenId = await assetsContract.getOwnerOf(
ERC721_GODS_ADDRESS,
'148332',
);
expect(tokenId).not.toStrictEqual('');
});

it('should get balances in a single call', async () => {
it('should get balance of ERC-20 token in a single call', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const balances = await assetsContract.getBalancesInSingleCall(
ERC20_DAI_ADDRESS,
[ERC20_DAI_ADDRESS],
);
expect(balances[ERC20_DAI_ADDRESS]).not.toStrictEqual(0);
});

it('should get the balance of a ERC-1155 collectible for a given address', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const balances = await assetsContract.getBalancesInSingleCall(SAI_ADDRESS, [
SAI_ADDRESS,
]);
expect(balances[SAI_ADDRESS]).not.toStrictEqual(0);
const balance = await assetsContract.balanceOfERC1155Collectible(
TEST_ACCOUNT_PUBLIC_ADDRESS,
ERC1155_ADDRESS,
ERC1155_ID,
);
expect(Number(balance)).toBeGreaterThan(0);
});

it('should get the URI of a ERC-1155 collectible', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const expectedUri = `https://api.opensea.io/api/v1/metadata/${ERC1155_ADDRESS}/0x{id}`;
const uri = await assetsContract.uriERC1155Collectible(
ERC1155_ADDRESS,
ERC1155_ID,
);
expect(uri.toLowerCase()).toStrictEqual(expectedUri);
});
});

0 comments on commit f103867

Please sign in to comment.