Skip to content

Commit

Permalink
BREAKING: ERC1155 support (#615)
Browse files Browse the repository at this point in the history
Enhancement to support ERC1155 collectible standard by adding the smart contract ABI.
  • Loading branch information
Gustavo Antunes authored and MajorLift committed Oct 11, 2023
1 parent dfe5c6d commit 1c31ad7
Show file tree
Hide file tree
Showing 16 changed files with 947 additions and 255 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
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
108 changes: 65 additions & 43 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,101 @@ 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 return empty string as URI when address given is not an ERC-721 collectible', async () => {
assetsContract.configure({ provider: MAINNET_PROVIDER });
const tokenId = await assetsContract.getCollectibleTokenURI(
'0x0000000000000000000000000000000000000000',
0,
'0',
);
expect(tokenId).toStrictEqual('');
});

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);
});
});
Loading

0 comments on commit 1c31ad7

Please sign in to comment.