Skip to content

Commit 0f8b920

Browse files
committed
make use of getTokenStandardAndDetails method exposed on assetsContractController to determine how to represent the contract being interacted with in token contract method calls
1 parent 1e494f3 commit 0f8b920

18 files changed

+738
-336
lines changed

app/_locales/en/messages.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/scripts/metamask-controller.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,6 @@ export default class MetamaskController extends EventEmitter {
12671267
appStateController,
12681268
collectiblesController,
12691269
collectibleDetectionController,
1270-
assetsContractController,
12711270
currencyRateController,
12721271
detectTokensController,
12731272
ensController,
@@ -1422,9 +1421,7 @@ export default class MetamaskController extends EventEmitter {
14221421
),
14231422

14241423
// AssetsContractController
1425-
getTokenStandardAndDetails: assetsContractController.getTokenStandardAndDetails.bind(
1426-
assetsContractController,
1427-
),
1424+
getTokenStandardAndDetails: this.getTokenStandardAndDetails.bind(this),
14281425

14291426
// CollectiblesController
14301427
addCollectible: collectiblesController.addCollectible.bind(
@@ -1747,6 +1744,19 @@ export default class MetamaskController extends EventEmitter {
17471744
};
17481745
}
17491746

1747+
async getTokenStandardAndDetails(address, userAddress, tokenId) {
1748+
const details = await this.assetsContractController.getTokenStandardAndDetails(
1749+
address,
1750+
userAddress,
1751+
tokenId,
1752+
);
1753+
return {
1754+
...details,
1755+
decimals: details?.decimals?.toString(10),
1756+
balance: details?.balance?.toString(10),
1757+
};
1758+
}
1759+
17501760
//=============================================================================
17511761
// VAULT / KEYRING RELATED METHODS
17521762
//=============================================================================

test/e2e/metamask-ui.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,11 @@ describe('MetaMask', function () {
343343
// Continue to next screen
344344
await driver.delay(largeDelayMs);
345345
await driver.clickElement({ text: 'Next', tag: 'button' });
346-
await driver.delay(regularDelayMs);
346+
await driver.delay(largeDelayMs);
347347
});
348348

349349
it('displays the token transfer data', async function () {
350+
await driver.delay(largeDelayMs);
350351
await driver.clickElement({ text: 'Hex', tag: 'button' });
351352
await driver.delay(regularDelayMs);
352353

@@ -449,6 +450,7 @@ describe('MetaMask', function () {
449450
});
450451

451452
it('customizes gas', async function () {
453+
await driver.delay(largeDelayMs);
452454
await driver.clickElement({ text: 'Edit', tag: 'button' });
453455
await driver.delay(largeDelayMs);
454456
await driver.clickElement(

ui/helpers/utils/token-util.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import {
44
conversionUtil,
55
multiplyCurrencies,
66
} from '../../../shared/modules/conversion.utils';
7+
import { getTokenStandardAndDetails } from '../../store/actions';
8+
import { ERC1155, ERC721 } from '../constants/common';
79
import * as util from './util';
810
import { formatCurrency } from './confirm-tx.util';
11+
import { getTransactionData } from './transactions.util';
912

1013
const DEFAULT_SYMBOL = '';
1114

@@ -212,3 +215,48 @@ export function getTokenFiatAmount(
212215
}
213216
return result;
214217
}
218+
219+
export async function getAssetDetails(
220+
tokenAddress,
221+
currentUserAddress,
222+
transactionData,
223+
existingCollectibles,
224+
) {
225+
const tokenData = getTransactionData(transactionData);
226+
if (!tokenData) {
227+
throw new Error('Unable to detect valid token data');
228+
}
229+
230+
const tokenId = getTokenValueParam(tokenData);
231+
let tokenDetails;
232+
try {
233+
tokenDetails = await getTokenStandardAndDetails(
234+
tokenAddress,
235+
currentUserAddress,
236+
tokenId,
237+
);
238+
} catch (error) {
239+
log.warn(error);
240+
return {};
241+
}
242+
243+
if (tokenDetails?.standard) {
244+
const { standard } = tokenDetails;
245+
if (standard === ERC721 || standard === ERC1155) {
246+
const existingCollectible = existingCollectibles.find(({ address }) =>
247+
util.isEqualCaseInsensitive(tokenAddress, address),
248+
);
249+
250+
if (existingCollectible) {
251+
return {
252+
...existingCollectible,
253+
standard,
254+
};
255+
}
256+
}
257+
// else if not a collectible already in state or standard === ERC20 just return tokenDetails as it contains all required data
258+
return tokenDetails;
259+
}
260+
261+
return {};
262+
}

ui/hooks/useAssetDetails.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { useState, useEffect } from 'react';
2+
import { useSelector, useDispatch } from 'react-redux';
3+
import { getCollectibles, getTokens } from '../ducks/metamask/metamask';
4+
import { ERC1155, ERC721, ERC20 } from '../helpers/constants/common';
5+
import {
6+
calcTokenAmount,
7+
getAssetDetails,
8+
getTokenAddressParam,
9+
getTokenValueParam,
10+
} from '../helpers/utils/token-util';
11+
import { getTransactionData } from '../helpers/utils/transactions.util';
12+
import { getTokenList } from '../selectors';
13+
import { hideLoadingIndication, showLoadingIndication } from '../store/actions';
14+
import { usePrevious } from './usePrevious';
15+
16+
export function useAssetDetails(tokenAddress, userAddress, transactionData) {
17+
const dispatch = useDispatch();
18+
19+
// state selectors
20+
const tokens = useSelector(getTokens);
21+
const collectibles = useSelector(getCollectibles);
22+
const tokenList = useSelector(getTokenList);
23+
24+
// in-hook state
25+
const [currentAsset, setCurrentAsset] = useState(null);
26+
27+
// previous state checkers
28+
const prevTokenAddress = usePrevious(tokenAddress);
29+
const prevUserAddress = usePrevious(userAddress);
30+
const prevTransactionData = usePrevious(transactionData);
31+
32+
useEffect(() => {
33+
async function getAndSetAssetDetails() {
34+
dispatch(showLoadingIndication());
35+
const assetDetails = await getAssetDetails(
36+
tokenAddress,
37+
userAddress,
38+
transactionData,
39+
collectibles,
40+
tokens,
41+
tokenList,
42+
);
43+
setCurrentAsset(assetDetails);
44+
dispatch(hideLoadingIndication());
45+
}
46+
if (
47+
tokenAddress !== prevTokenAddress ||
48+
userAddress !== prevUserAddress ||
49+
transactionData !== prevTransactionData
50+
) {
51+
getAndSetAssetDetails();
52+
}
53+
}, [
54+
dispatch,
55+
prevTokenAddress,
56+
prevTransactionData,
57+
prevUserAddress,
58+
tokenAddress,
59+
userAddress,
60+
transactionData,
61+
collectibles,
62+
tokens,
63+
tokenList,
64+
]);
65+
66+
let assetStandard,
67+
assetName,
68+
assetAddress,
69+
tokenSymbol,
70+
decimals,
71+
tokenImage,
72+
userBalance,
73+
tokenValue,
74+
toAddress,
75+
tokenAmount,
76+
tokenId;
77+
78+
if (currentAsset) {
79+
const {
80+
standard,
81+
symbol,
82+
image,
83+
name,
84+
balance,
85+
decimals: currentAssetDecimals,
86+
} = currentAsset;
87+
const tokenData = getTransactionData(transactionData);
88+
assetStandard = standard;
89+
assetAddress = tokenAddress;
90+
tokenSymbol = symbol;
91+
tokenImage = image;
92+
toAddress = getTokenAddressParam(tokenData);
93+
if (assetStandard === ERC721 || assetStandard === ERC1155) {
94+
assetName = name;
95+
tokenId = getTokenValueParam(tokenData);
96+
}
97+
if (assetStandard === ERC20) {
98+
userBalance = balance;
99+
decimals = Number(currentAssetDecimals?.toString(10));
100+
tokenAmount =
101+
tokenData &&
102+
calcTokenAmount(getTokenValueParam(tokenData), decimals).toString(10);
103+
}
104+
}
105+
return {
106+
assetStandard,
107+
assetName,
108+
assetAddress,
109+
userBalance,
110+
tokenSymbol,
111+
decimals,
112+
tokenImage,
113+
tokenValue,
114+
toAddress,
115+
tokenAmount,
116+
tokenId,
117+
};
118+
}

0 commit comments

Comments
 (0)