Skip to content

Commit

Permalink
[assets-contract] Adjust downstream tests to use messenger actions in…
Browse files Browse the repository at this point in the history
…stead of class method callbacks
  • Loading branch information
MajorLift committed Jun 26, 2024
1 parent 7c0ebc6 commit 5da97d5
Show file tree
Hide file tree
Showing 4 changed files with 502 additions and 422 deletions.
123 changes: 86 additions & 37 deletions packages/assets-controllers/src/AssetsContractController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import assert from 'assert';
import { mockNetwork } from '../../../tests/mock-network';
import { buildInfuraNetworkClientConfiguration } from '../../network-controller/tests/helpers';
import type {
AssetsContractControllerActions,
AssetsContractControllerEvents,
AllowedActions as AssetsContractAllowedActions,
AllowedEvents as AssetsContractAllowedEvents,
} from './AssetsContractController';
Expand Down Expand Up @@ -61,7 +63,9 @@ async function setupAssetContractControllers({
useNetworkControllerProvider,
infuraProjectId = '341eacb578dd44a1a049cbc5f6fd4035',
}: {
options?: Partial<ConstructorParameters<typeof AssetsContractController>[0]>;
options?: Partial<
Omit<ConstructorParameters<typeof AssetsContractController>[0], 'messenger'>
>;
useNetworkControllerProvider?: boolean;
infuraProjectId?: string;
} = {}) {
Expand All @@ -75,8 +79,12 @@ async function setupAssetContractControllers({
let provider: Provider;

const controllerMessenger = new ControllerMessenger<
NetworkControllerActions | AssetsContractAllowedActions,
NetworkControllerEvents | AssetsContractAllowedEvents
| NetworkControllerActions
| AssetsContractControllerActions
| AssetsContractAllowedActions,
| NetworkControllerEvents
| AssetsContractControllerEvents
| AssetsContractAllowedEvents
>();
const networkMessenger = controllerMessenger.getRestricted({
name: 'NetworkController',
Expand Down Expand Up @@ -227,7 +235,8 @@ describe('AssetsContractController', () => {
const { assetsContract, messenger } = await setupAssetContractControllers();
assetsContract.provider = undefined;
await expect(
assetsContract.getERC20BalanceOf(
messenger.call(
`AssetsContractController:getERC20BalanceOf`,
ERC20_UNI_ADDRESS,
TEST_ACCOUNT_PUBLIC_ADDRESS,
),
Expand All @@ -239,7 +248,10 @@ describe('AssetsContractController', () => {
const { assetsContract, messenger } = await setupAssetContractControllers();
assetsContract.provider = undefined;
await expect(
assetsContract.getERC20TokenDecimals(ERC20_UNI_ADDRESS),
messenger.call(
`AssetsContractController:getERC20TokenDecimals`,
ERC20_UNI_ADDRESS,
),
).rejects.toThrow(MISSING_PROVIDER_ERROR);
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
});
Expand Down Expand Up @@ -285,11 +297,13 @@ describe('AssetsContractController', () => {
},
],
});
const UNIBalance = await assetsContract.getERC20BalanceOf(
const UNIBalance = await messenger.call(
`AssetsContractController:getERC20BalanceOf`,
ERC20_UNI_ADDRESS,
TEST_ACCOUNT_PUBLIC_ADDRESS,
);
const UNINoBalance = await assetsContract.getERC20BalanceOf(
const UNINoBalance = await messenger.call(
`AssetsContractController:getERC20BalanceOf`,
ERC20_UNI_ADDRESS,
'0x202637dAAEfbd7f131f90338a4A6c69F6Cd5CE91',
);
Expand Down Expand Up @@ -323,7 +337,8 @@ describe('AssetsContractController', () => {
},
],
});
const tokenId = await assetsContract.getERC721NftTokenId(
const tokenId = await messenger.call(
`AssetsContractController:getERC721NftTokenId`,
ERC721_GODS_ADDRESS,
'0x9a90bd8d1149a88b42a99cf62215ad955d6f498a',
0,
Expand All @@ -336,7 +351,8 @@ describe('AssetsContractController', () => {
const { assetsContract, messenger } = await setupAssetContractControllers();
assetsContract.provider = undefined;
await expect(
assetsContract.getTokenStandardAndDetails(
messenger.call(
`AssetsContractController:getTokenStandardAndDetails`,
ERC20_UNI_ADDRESS,
TEST_ACCOUNT_PUBLIC_ADDRESS,
),
Expand All @@ -350,7 +366,8 @@ describe('AssetsContractController', () => {
assetsContract.provider = provider;
const error = 'Unable to determine contract standard';
await expect(
assetsContract.getTokenStandardAndDetails(
messenger.call(
`AssetsContractController:getTokenStandardAndDetails`,
'BaDeRc20AdDrEsS',
TEST_ACCOUNT_PUBLIC_ADDRESS,
),
Expand Down Expand Up @@ -415,7 +432,8 @@ describe('AssetsContractController', () => {
},
],
});
const standardAndDetails = await assetsContract.getTokenStandardAndDetails(
const standardAndDetails = await messenger.call(
`AssetsContractController:getTokenStandardAndDetails`,
ERC721_GODS_ADDRESS,
TEST_ACCOUNT_PUBLIC_ADDRESS,
);
Expand Down Expand Up @@ -496,7 +514,8 @@ describe('AssetsContractController', () => {
},
],
});
const standardAndDetails = await assetsContract.getTokenStandardAndDetails(
const standardAndDetails = await messenger.call(
`AssetsContractController:getTokenStandardAndDetails`,
ERC1155_ADDRESS,
TEST_ACCOUNT_PUBLIC_ADDRESS,
);
Expand Down Expand Up @@ -597,7 +616,8 @@ describe('AssetsContractController', () => {
},
],
});
const standardAndDetails = await assetsContract.getTokenStandardAndDetails(
const standardAndDetails = await messenger.call(
`AssetsContractController:getTokenStandardAndDetails`,
ERC20_UNI_ADDRESS,
TEST_ACCOUNT_PUBLIC_ADDRESS,
);
Expand Down Expand Up @@ -646,7 +666,8 @@ describe('AssetsContractController', () => {
},
],
});
const tokenId = await assetsContract.getERC721TokenURI(
const tokenId = await messenger.call(
`AssetsContractController:getERC721TokenURI`,
ERC721_GODS_ADDRESS,
'0',
);
Expand Down Expand Up @@ -699,7 +720,8 @@ describe('AssetsContractController', () => {
},
],
});
const uri = await assetsContract.getERC721TokenURI(
const uri = await messenger.call(
`AssetsContractController:getERC721TokenURI`,
'0x0000000000000000000000000000000000000000',
'0',
);
Expand Down Expand Up @@ -737,7 +759,10 @@ describe('AssetsContractController', () => {
},
],
});
const name = await assetsContract.getERC721AssetName(ERC721_GODS_ADDRESS);
const name = await messenger.call(
`AssetsContractController:getERC721AssetName`,
ERC721_GODS_ADDRESS,
);
expect(name).toBe('Gods Unchained');
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
});
Expand Down Expand Up @@ -767,17 +792,21 @@ describe('AssetsContractController', () => {
},
],
});
const symbol = await assetsContract.getERC721AssetSymbol(
const symbol = await messenger.call(
`AssetsContractController:getERC721AssetSymbol`,
ERC721_GODS_ADDRESS,
);
expect(symbol).toBe('GODS');
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
});

it('should throw missing provider error when getting ERC-721 NFT symbol when missing provider', async () => {
const { assetsContract, messenger } = await setupAssetContractControllers();
const { messenger } = await setupAssetContractControllers();
await expect(
assetsContract.getERC721AssetSymbol(ERC721_GODS_ADDRESS),
messenger.call(
`AssetsContractController:getERC721AssetSymbol`,
ERC721_GODS_ADDRESS,
),
).rejects.toThrow(MISSING_PROVIDER_ERROR);
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
});
Expand Down Expand Up @@ -807,7 +836,8 @@ describe('AssetsContractController', () => {
},
],
});
const decimals = await assetsContract.getERC20TokenDecimals(
const decimals = await messenger.call(
`AssetsContractController:getERC20TokenDecimals`,
ERC20_SAI_ADDRESS,
);
expect(Number(decimals)).toBe(18);
Expand Down Expand Up @@ -840,7 +870,10 @@ describe('AssetsContractController', () => {
],
});

const name = await assetsContract.getERC20TokenName(ERC20_DAI_ADDRESS);
const name = await messenger.call(
`AssetsContractController:getERC20TokenName`,
ERC20_DAI_ADDRESS,
);

expect(name).toBe('Dai Stablecoin');
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
Expand Down Expand Up @@ -871,7 +904,8 @@ describe('AssetsContractController', () => {
},
],
});
const tokenId = await assetsContract.getERC721OwnerOf(
const tokenId = await messenger.call(
`AssetsContractController:getERC721OwnerOf`,
ERC721_GODS_ADDRESS,
'148332',
);
Expand All @@ -880,9 +914,13 @@ describe('AssetsContractController', () => {
});

it('should throw missing provider error when getting ERC-721 NFT ownership', async () => {
const { assetsContract, messenger } = await setupAssetContractControllers();
const { messenger } = await setupAssetContractControllers();
await expect(
assetsContract.getERC721OwnerOf(ERC721_GODS_ADDRESS, '148332'),
messenger.call(
`AssetsContractController:getERC721OwnerOf`,
ERC721_GODS_ADDRESS,
'148332',
),
).rejects.toThrow(MISSING_PROVIDER_ERROR);
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
});
Expand Down Expand Up @@ -912,7 +950,8 @@ describe('AssetsContractController', () => {
},
],
});
const balances = await assetsContract.getBalancesInSingleCall(
const balances = await messenger.call(
`AssetsContractController:getBalancesInSingleCall`,
ERC20_SAI_ADDRESS,
[ERC20_SAI_ADDRESS],
);
Expand Down Expand Up @@ -988,7 +1027,7 @@ describe('AssetsContractController', () => {
},
],
});
const { assetsContract, network, provider } =
const { assetsContract, messenger, network, provider } =
await setupAssetContractControllers({
options: {
chainId: ChainId.mainnet,
Expand All @@ -998,7 +1037,8 @@ describe('AssetsContractController', () => {
});
assetsContract.provider = provider;

const balancesOnMainnet = await assetsContract.getBalancesInSingleCall(
const balancesOnMainnet = await messenger.call(
`AssetsContractController:getBalancesInSingleCall`,
ERC20_SAI_ADDRESS,
[ERC20_SAI_ADDRESS],
);
Expand All @@ -1008,9 +1048,11 @@ describe('AssetsContractController', () => {

await network.setActiveNetwork(InfuraNetworkType['linea-mainnet']);

const balancesOnLineaMainnet = await assetsContract.getBalancesInSingleCall(
const balancesOnLineaMainnet = await messenger.call(
`AssetsContractController:getBalancesInSingleCall`,
ERC20_SAI_ADDRESS,
[ERC20_SAI_ADDRESS],
InfuraNetworkType['linea-mainnet'],
);
expect(balancesOnLineaMainnet).toStrictEqual({
[ERC20_SAI_ADDRESS]: BigNumber.from('0xa0155d09733ed8ef4c4'),
Expand Down Expand Up @@ -1078,15 +1120,17 @@ describe('AssetsContractController', () => {
],
});

const balances = await assetsContract.getBalancesInSingleCall(
const balances = await messenger.call(
`AssetsContractController:getBalancesInSingleCall`,
ERC20_SAI_ADDRESS,
[ERC20_SAI_ADDRESS],
);
expect(balances[ERC20_SAI_ADDRESS]).toBeDefined();

await network.setActiveNetwork(NetworkType.sepolia);

const noBalances = await assetsContract.getBalancesInSingleCall(
const noBalances = await messenger.call(
`AssetsContractController:getBalancesInSingleCall`,
ERC20_SAI_ADDRESS,
[ERC20_SAI_ADDRESS],
);
Expand All @@ -1098,7 +1142,8 @@ describe('AssetsContractController', () => {
const { assetsContract, messenger } = await setupAssetContractControllers();
assetsContract.provider = undefined;
await expect(
assetsContract.transferSingleERC1155(
messenger.call(
`AssetsContractController:transferSingleERC1155`,
ERC1155_ADDRESS,
TEST_ACCOUNT_PUBLIC_ADDRESS,
TEST_ACCOUNT_PUBLIC_ADDRESS,
Expand Down Expand Up @@ -1135,7 +1180,8 @@ describe('AssetsContractController', () => {
],
});
await expect(
assetsContract.transferSingleERC1155(
messenger.call(
`AssetsContractController:transferSingleERC1155`,
ERC1155_ADDRESS,
'0x0',
TEST_ACCOUNT_PUBLIC_ADDRESS,
Expand Down Expand Up @@ -1171,19 +1217,21 @@ describe('AssetsContractController', () => {
},
],
});
const balance = await assetsContract.getERC1155BalanceOf(
const balance = await messenger.call(
`AssetsContractController:getERC1155BalanceOf`,
TEST_ACCOUNT_PUBLIC_ADDRESS,
ERC1155_ADDRESS,
ERC1155_ID,
);
expect(Number(balance)).toBeGreaterThan(0);
expect(balance.isZero() || balance.isNeg()).toBe(false);
messenger.clearEventSubscriptions('NetworkController:networkDidChange');
});

it('should throw missing provider error when getting the balance of a ERC-1155 NFT when missing provider', async () => {
const { assetsContract, messenger } = await setupAssetContractControllers();
const { messenger } = await setupAssetContractControllers();
await expect(
assetsContract.getERC1155BalanceOf(
messenger.call(
`AssetsContractController:getERC1155BalanceOf`,
TEST_ACCOUNT_PUBLIC_ADDRESS,
ERC1155_ADDRESS,
ERC1155_ID,
Expand Down Expand Up @@ -1218,7 +1266,8 @@ describe('AssetsContractController', () => {
],
});
const expectedUri = `https://api.opensea.io/api/v1/metadata/${ERC1155_ADDRESS}/0x{id}`;
const uri = await assetsContract.getERC1155TokenURI(
const uri = await messenger.call(
`AssetsContractController:getERC1155TokenURI`,
ERC1155_ADDRESS,
ERC1155_ID,
);
Expand Down
Loading

0 comments on commit 5da97d5

Please sign in to comment.