Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/solana-wallet-snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/internal-snaps.git"
},
"source": {
"shasum": "V3iOdFzSye/NKphktjgWLlyZ6npSWHr+dgR0tkVtJvU=",
"shasum": "AEtvrNFWywA98I2okgZahTBeW0PeNhyIpS5NzT9bheM=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import { InMemoryCache } from '../../../caching/InMemoryCache';
import { MOCK_NFTS_LIST_RESPONSE_MAPPED } from '../../../clients/nft-api/mocks/mockNftsListResponseMapped';
import type { NftApiClient } from '../../../clients/nft-api/NftApiClient';
import type { TokenApiClient } from '../../../clients/token-api-client/TokenApiClient';
import { Network } from '../../../constants/solana';
import {
MOCK_ASSET_ENTITY_0,
MOCK_ASSET_ENTITY_1,
MOCK_ASSET_ENTITY_2,
} from '../../../test/mocks/asset-entities';
import { MOCK_SOLANA_KEYRING_ACCOUNT_0 } from '../../../test/mocks/solana-keyring-accounts';
import { mockLogger } from '../../__mocks__/logger';
import { createMockConnection } from '../../__mocks__/mockConnection';
import { MOCK_SOLANA_RPC_GET_TOKEN_ACCOUNTS_BY_OWNER_RESPONSE } from '../../__mocks__/mockSolanaRpcResponses';
import type { AccountsService } from '../../accounts/AccountsService';
import type { ConfigProvider } from '../../config';
import type { SolanaConnection } from '../../connection';
Expand Down Expand Up @@ -115,4 +118,58 @@ describe('SnapAssetsAdapter', () => {
expect(SnapAssetsAdapter.hasChanged(asset, assetsLookup)).toBe(false);
});
});

describe('fetch', () => {
it('aggregates token accounts for the same mint', async () => {
jest
.spyOn(mockConfigProvider, 'getActiveNetworks')
.mockImplementation()
.mockResolvedValue([Network.Mainnet]);
jest
.spyOn(mockTokenApiClient, 'getTokensMetadata')
.mockImplementation()
.mockResolvedValue({});

const [firstTokenAccount] =
MOCK_SOLANA_RPC_GET_TOKEN_ACCOUNTS_BY_OWNER_RESPONSE.result.value;
if (!firstTokenAccount) {
throw new Error('Missing token account fixture');
}
const secondTokenAccount = cloneDeep(firstTokenAccount);
secondTokenAccount.pubkey =
'7Gg2Y8vCj3v5nQj5xFfC3uT7wJ9sN4mK2pL8rH6dQ1eA';
secondTokenAccount.account.data.parsed.info.tokenAmount.amount =
'1000000';
secondTokenAccount.account.data.parsed.info.tokenAmount.uiAmountString =
'1';

jest.spyOn(mockConnection, 'getRpc').mockReturnValue({
getBalance: jest.fn().mockReturnValue({
send: jest.fn().mockResolvedValue({ value: 1000000000 }),
}),
getTokenAccountsByOwner: jest
.fn()
.mockReturnValueOnce({
send: jest.fn().mockResolvedValue({
value: [firstTokenAccount, secondTokenAccount],
}),
})
.mockReturnValue({
send: jest.fn().mockResolvedValue({ value: [] }),
}),
} as unknown as ReturnType<SolanaConnection['getRpc']>);

expect(
await snapAssetsAdapter.fetch(MOCK_SOLANA_KEYRING_ACCOUNT_0),
).toStrictEqual(
expect.arrayContaining([
expect.objectContaining({
mint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
rawAmount: '124456789',
uiAmount: '124.456789',
}),
]),
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {
Address,
} from '@solana/kit';
import { address as asAddress } from '@solana/kit';
import { BigNumber } from 'bignumber.js';

import type {
AssetEntity,
Expand Down Expand Up @@ -360,26 +361,42 @@ export class SnapAssetsAdapter {
const tokensMetadata =
await this.#tokenApiClient.getTokensMetadata(assetTypes);

const tokenAssets: TokenAsset[] = tokenAccounts
.filter((tokenAccount) => tokenAccount.assetType.includes('/token:'))
.map((tokenAccount) => {
const { assetType } = tokenAccount;
const { decimals, amount, uiAmountString } =
tokenAccount.token.account.data.parsed.info.tokenAmount;
const tokenAssetsByKey = new Map<string, TokenAsset>();

return {
tokenAccounts
.filter((tokenAccount) => tokenAccount.assetType.includes('/token:'))
.forEach((tokenAccount) => {
const { assetType, scope, keyringAccount } = tokenAccount;
const { mint, tokenAmount } =
tokenAccount.token.account.data.parsed.info;
const { decimals, amount, uiAmountString } = tokenAmount;
const key = `${keyringAccount.id}:${scope}:${mint}`;
const existingAsset = tokenAssetsByKey.get(key);

if (existingAsset) {
existingAsset.rawAmount = (
BigInt(existingAsset.rawAmount) + BigInt(amount)
).toString();
existingAsset.uiAmount = new BigNumber(existingAsset.uiAmount)
.plus(uiAmountString ?? fromTokenUnits(amount, decimals))
.toFixed();
return;
}

tokenAssetsByKey.set(key, {
assetType,
keyringAccountId: tokenAccount.keyringAccount.id,
network: tokenAccount.scope,
mint: tokenAccount.token.account.data.parsed.info.mint,
pubkey: tokenAccount.token.pubkey,
keyringAccountId: keyringAccount.id,
network: scope,
mint,
symbol: tokensMetadata[assetType]?.symbol ?? 'UNKNOWN',
decimals,
rawAmount: amount,
uiAmount: uiAmountString ?? fromTokenUnits(amount, decimals),
};
});
});

const tokenAssets = [...tokenAssetsByKey.values()];

// const nftAssets = await this.#fetchNftAssets(account, tokenAccounts.filter(
// (token) => token.assetType.includes('/nft:'),
// ));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ describe('SendService', () => {
keyringAccountId: mockAccount.id,
network: Network.Mainnet,
mint: 'token-address-123',
pubkey: 'token-address-123',
symbol: 'USDC',
decimals: 6,
rawAmount: '1000000',
Expand Down Expand Up @@ -503,7 +502,6 @@ describe('SendService', () => {
keyringAccountId: mockAccount.id,
network: Network.Mainnet,
mint: 'token-address-123',
pubkey: 'token-address-123',
symbol: 'USDC',
decimals: 6,
rawAmount: '100000000000',
Expand Down Expand Up @@ -544,7 +542,6 @@ describe('SendService', () => {
keyringAccountId: mockAccount.id,
network: Network.Mainnet,
mint: 'token-address-123',
pubkey: 'token-address-123',
symbol: 'USDC',
decimals: 6,
rawAmount: '100000000000',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,6 @@ describe('KeyringAccountMonitor', () => {
keyringAccountId: account.id,
network: Network.Mainnet,
mint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
pubkey: '9wt9PfjPD3JCy5r7o4K1cTGiuTG7fq2pQhdDCdQALKjg',
symbol: 'USDC',
decimals: 6,
rawAmount: '123456789',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ export class KeyringAccountMonitor {
keyringAccountId: keyringAccount.id,
network,
mint,
pubkey,
symbol: metadata?.symbol ?? 'UNKNOWN',
decimals,
rawAmount: amount,
Expand Down
Loading
Loading