Skip to content

Commit 3b33b17

Browse files
committed
Add assets-contract actions as allowed actions to downstream controllers
1 parent 2016beb commit 3b33b17

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed

packages/assets-controllers/src/NftController.ts

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ import BN from 'bn.js';
4444
import { v4 as random } from 'uuid';
4545

4646
import type {
47-
AssetsContractControllerGetERC1155StandardAction,
48-
AssetsContractControllerGetERC20StandardAction,
49-
AssetsContractControllerGetERC721StandardAction,
47+
AssetsContractControllerGetERC1155BalanceOfAction,
48+
AssetsContractControllerGetERC1155TokenURIAction,
49+
AssetsContractControllerGetERC721AssetNameAction,
50+
AssetsContractControllerGetERC721AssetSymbolAction,
51+
AssetsContractControllerGetERC721OwnerOfAction,
52+
AssetsContractControllerGetERC721TokenURIAction,
5053
} from './AssetsContractController';
5154
import { compareNftMetadata, getFormattedIpfsUrl } from './assetsUtil';
5255
import { Source } from './constants';
@@ -227,9 +230,12 @@ export type AllowedActions =
227230
| AccountsControllerGetAccountAction
228231
| AccountsControllerGetSelectedAccountAction
229232
| NetworkControllerGetNetworkClientByIdAction
230-
| AssetsContractControllerGetERC20StandardAction
231-
| AssetsContractControllerGetERC721StandardAction
232-
| AssetsContractControllerGetERC1155StandardAction;
233+
| AssetsContractControllerGetERC721AssetNameAction
234+
| AssetsContractControllerGetERC721AssetSymbolAction
235+
| AssetsContractControllerGetERC721TokenURIAction
236+
| AssetsContractControllerGetERC721OwnerOfAction
237+
| AssetsContractControllerGetERC1155BalanceOfAction
238+
| AssetsContractControllerGetERC1155TokenURIAction;
233239

234240
export type AllowedEvents =
235241
| PreferencesControllerStateChangeEvent
@@ -657,19 +663,25 @@ export class NftController extends BaseController<
657663
): Promise<[string, string]> {
658664
// try ERC721 uri
659665
try {
660-
const uri = await this.messagingSystem
661-
.call('AssetsContractController:getERC721Standard', networkClientId)
662-
.getTokenURI(contractAddress, tokenId);
666+
const uri = await this.messagingSystem.call(
667+
'AssetsContractController:getERC721TokenURI',
668+
contractAddress,
669+
tokenId,
670+
networkClientId,
671+
);
663672
return [uri, ERC721];
664673
} catch {
665674
// Ignore error
666675
}
667676

668677
// try ERC1155 uri
669678
try {
670-
const tokenURI = await this.messagingSystem
671-
.call('AssetsContractController:getERC1155Standard', networkClientId)
672-
.getTokenURI(contractAddress, tokenId);
679+
const tokenURI = await this.messagingSystem.call(
680+
'AssetsContractController:getERC1155TokenURI',
681+
contractAddress,
682+
tokenId,
683+
networkClientId,
684+
);
673685

674686
/**
675687
* According to EIP1155 the URI value allows for ID substitution
@@ -750,12 +762,16 @@ export class NftController extends BaseController<
750762
Pick<ApiNftContract, 'collection'>
751763
> {
752764
const [name, symbol] = await Promise.all([
753-
this.messagingSystem
754-
.call('AssetsContractController:getERC721Standard', networkClientId)
755-
.getAssetName(contractAddress),
756-
this.messagingSystem
757-
.call('AssetsContractController:getERC721Standard', networkClientId)
758-
.getAssetSymbol(contractAddress),
765+
this.messagingSystem.call(
766+
'AssetsContractController:getERC721AssetName',
767+
contractAddress,
768+
networkClientId,
769+
),
770+
this.messagingSystem.call(
771+
'AssetsContractController:getERC721AssetSymbol',
772+
contractAddress,
773+
networkClientId,
774+
),
759775
]);
760776

761777
return {
@@ -1329,9 +1345,12 @@ export class NftController extends BaseController<
13291345
): Promise<boolean> {
13301346
// Checks the ownership for ERC-721.
13311347
try {
1332-
const owner = await this.messagingSystem
1333-
.call('AssetsContractController:getERC721Standard', networkClientId)
1334-
.getOwnerOf(nftAddress, tokenId);
1348+
const owner = await this.messagingSystem.call(
1349+
'AssetsContractController:getERC721OwnerOf',
1350+
nftAddress,
1351+
tokenId,
1352+
networkClientId,
1353+
);
13351354
return ownerAddress.toLowerCase() === owner.toLowerCase();
13361355
// eslint-disable-next-line no-empty
13371356
} catch {
@@ -1340,9 +1359,13 @@ export class NftController extends BaseController<
13401359

13411360
// Checks the ownership for ERC-1155.
13421361
try {
1343-
const balance = await this.messagingSystem
1344-
.call('AssetsContractController:getERC1155Standard', networkClientId)
1345-
.getBalanceOf(ownerAddress, nftAddress, tokenId);
1362+
const balance = await this.messagingSystem.call(
1363+
'AssetsContractController:getERC1155BalanceOf',
1364+
ownerAddress,
1365+
nftAddress,
1366+
tokenId,
1367+
networkClientId,
1368+
);
13461369
return !balance.isZero();
13471370
// eslint-disable-next-line no-empty
13481371
} catch {

packages/assets-controllers/src/TokenBalancesController.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
import { BaseController } from '@metamask/base-controller';
88
import { safelyExecute, toHex } from '@metamask/controller-utils';
99

10-
import type { AssetsContractControllerGetERC20StandardAction } from './AssetsContractController';
10+
import type { AssetsContractControllerGetERC20BalanceOfAction } from './AssetsContractController';
1111
import type { Token } from './TokenRatesController';
1212
import type { TokensControllerStateChangeEvent } from './TokensController';
1313

@@ -56,8 +56,8 @@ export type TokenBalancesControllerActions =
5656

5757
export type AllowedActions =
5858
| AccountsControllerGetSelectedAccountAction
59-
| AssetsContractControllerGetERC20StandardAction;
60-
59+
| AssetsContractControllerGetERC20BalanceOfAction;
60+
6161
export type TokenBalancesControllerStateChangeEvent =
6262
ControllerStateChangeEvent<
6363
typeof controllerName,
@@ -203,9 +203,11 @@ export class TokenBalancesController extends BaseController<
203203
for (const token of this.#tokens) {
204204
const { address } = token;
205205
try {
206-
const balance = await this.messagingSystem
207-
.call('AssetsContractController:getERC20Standard')
208-
.getBalanceOf(address, selectedInternalAccount.address);
206+
const balance = await this.messagingSystem.call(
207+
'AssetsContractController:getERC20BalanceOf',
208+
address,
209+
selectedInternalAccount.address,
210+
);
209211
newContractBalances[address] = toHex(balance);
210212
token.hasBalanceError = false;
211213
} catch (error) {

0 commit comments

Comments
 (0)