Skip to content
Open
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
15 changes: 15 additions & 0 deletions packages/snap/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ClientRequestMethod,
} from './handlers/clientRequest';
import { ComputeFeeHandler } from './handlers/clientRequest/computeFee';
import { GetAccountAssetInfoHandler } from './handlers/clientRequest/getAccountAssetInfo';
import { OnAddressInputHandler } from './handlers/clientRequest/onAddressInput';
import { OnAmountInputHandler } from './handlers/clientRequest/onAmountInput';
import { SignAndSendTransactionHandler } from './handlers/clientRequest/signAndSendTransaction';
Expand All @@ -30,6 +31,7 @@ import {
SignTransactionHandler,
} from './handlers/keyring';
import { AccountService, AccountsRepository } from './services/account';
import { AccountAssetInfoService } from './services/account-asset-info';
import {
AssetMetadataRepository,
AssetMetadataService,
Expand Down Expand Up @@ -98,6 +100,13 @@ const onChainAccountService = new OnChainAccountService({
assetMetadataService,
});

const accountAssetInfoService = new AccountAssetInfoService({
logger,
accountService,
onChainAccountService,
assetMetadataService,
});

const transactionService = new TransactionService({
logger,
transactionRepository,
Expand Down Expand Up @@ -244,11 +253,17 @@ const computeFeeHandler = new ComputeFeeHandler({
transactionService,
});

const getAccountAssetInfoHandler = new GetAccountAssetInfoHandler({
logger,
accountAssetInfoService,
});

const clientRequestMethodHandlers: Record<
ClientRequestMethod,
IClientRequestHandler
> = {
[ClientRequestMethod.ChangeTrustOpt]: changeTrustOptHandler,
[ClientRequestMethod.GetAccountAssetInfo]: getAccountAssetInfoHandler,
[ClientRequestMethod.OnAddressInput]: onAddressInputHandler,
[ClientRequestMethod.OnAmountInput]: onAmountInputHandler,
[ClientRequestMethod.SignAndSendTransaction]: signAndSendTransactionHandler,
Expand Down
55 changes: 55 additions & 0 deletions packages/snap/src/handlers/clientRequest/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
ClientRequestMethod,
ClientRequestMethodStruct,
JsonRpcRequestWithAccountStruct,
GetAccountAssetInfoJsonRpcRequestStruct,
GetAccountAssetInfoJsonRpcResponseStruct,
OnAddressInputJsonRpcRequestStruct,
OnAddressInputJsonRpcResponseStruct,
OnAmountInputJsonRpcRequestStruct,
Expand Down Expand Up @@ -618,3 +620,56 @@ describe('OnAmountInputJsonRpcResponseStruct', () => {
},
);
});

describe('GetAccountAssetInfoJsonRpcRequestStruct', () => {
it('accepts a valid getAccountAssetInfo JSON-RPC request', () => {
expect(() =>
assert(
{
jsonrpc: '2.0',
id: 1,
method: ClientRequestMethod.GetAccountAssetInfo,
params: {
accountId,
scope,
assets: [classicAssetId],
},
},
GetAccountAssetInfoJsonRpcRequestStruct,
),
).not.toThrow();
});

it('rejects getAccountAssetInfo when scope is missing', () => {
expect(() =>
assert(
{
jsonrpc: '2.0',
id: 1,
method: ClientRequestMethod.GetAccountAssetInfo,
params: {
accountId,
assets: [classicAssetId],
},
},
GetAccountAssetInfoJsonRpcRequestStruct,
),
).toThrow(StructError);
});
});

describe('GetAccountAssetInfoJsonRpcResponseStruct', () => {
it('accepts a valid getAccountAssetInfo JSON-RPC response', () => {
expect(() =>
assert(
{
[classicAssetId]: {
metadata: { name: 'USD Coin', symbol: 'USDC', units: [] },
extra: { limit: '1' },
},
},
GetAccountAssetInfoJsonRpcResponseStruct,
),
).not.toThrow();
});
});
48 changes: 48 additions & 0 deletions packages/snap/src/handlers/clientRequest/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
nonempty,
integer,
min,
record,
} from '@metamask/superstruct';
import type { JsonRpcRequest } from '@metamask/utils';
import { parseCaipAssetType } from '@metamask/utils';
Expand All @@ -33,6 +34,7 @@ import {
ValidStellarAmountStruct,
SwapTransactionXdrStruct,
} from '../../api';
import { AccountAssetInfoEntryStruct } from '../../services/account-asset-info';
import { isSep41Id } from '../../utils';

/**
Expand All @@ -47,6 +49,7 @@ export enum ClientRequestMethod {
ComputeFee = 'computeFee',
/** -------------------------------- Stellar Specific -------------------------------- */
ChangeTrustOpt = 'changeTrustOpt',
GetAccountAssetInfo = 'getAccountAssetInfo',
}

export enum MultiChainSendErrorCodes {
Expand Down Expand Up @@ -136,6 +139,37 @@ export const ChangeTrustOptJsonRpcResponseStruct = object({
transactionId: optional(StellarTransactionHashStruct),
});

const GetAccountAssetInfoParamsStruct = object({
accountId: UuidStruct,
scope: KnownCaip2ChainIdStruct,
assets: array(
union([
KnownCaip19Sep41AssetStruct,
KnownCaip19ClassicAssetStruct,
KnownCaip19Slip44IdStruct,
]),
),
});

/**
* Validation struct for the getAccountAssetInfo JSON-RPC request.
*/
export const GetAccountAssetInfoJsonRpcRequestStruct = assign(
JsonRpcRequestStruct,
object({
method: literal(ClientRequestMethod.GetAccountAssetInfo),
params: GetAccountAssetInfoParamsStruct,
}),
);

/**
* Validation struct for the getAccountAssetInfo JSON-RPC response.
*/
export const GetAccountAssetInfoJsonRpcResponseStruct = record(
string(),
AccountAssetInfoEntryStruct,
);

/**
* Validation struct for the sendTransaction JSON-RPC request.
*/
Expand Down Expand Up @@ -283,6 +317,20 @@ export type ChangeTrustOptJsonRpcResponse = Infer<
typeof ChangeTrustOptJsonRpcResponseStruct
>;

/**
* Type for the getAccountAssetInfo JSON-RPC request.
*/
export type GetAccountAssetInfoJsonRpcRequest = Infer<
typeof GetAccountAssetInfoJsonRpcRequestStruct
>;

/**
* Type for the getAccountAssetInfo JSON-RPC response.
*/
export type GetAccountAssetInfoJsonRpcResponse = Infer<
typeof GetAccountAssetInfoJsonRpcResponseStruct
>;

/**
* Type for the onAddressInput JSON-RPC request.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ describe('ChangeTrustOptHandler', () => {
txId: '7d4b0c5ef7498b223f45a10f461060fb64f53eb13caf18e8dc7de95a8cf9c0e1',
scope,
accountIds: [account.id],
trustlineVerification: {
assetId,
action: ChangeTrustOptAction.Add,
},
});
});

Expand Down Expand Up @@ -362,6 +366,10 @@ describe('ChangeTrustOptHandler', () => {
txId: '7d4b0c5ef7498b223f45a10f461060fb64f53eb13caf18e8dc7de95a8cf9c0e1',
scope,
accountIds: [account.id],
trustlineVerification: {
assetId,
action: ChangeTrustOptAction.Delete,
},
});
});

Expand Down
4 changes: 4 additions & 0 deletions packages/snap/src/handlers/clientRequest/changeTrustOpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ export class ChangeTrustOptHandler extends BaseClientRequestHandler<
txId: transactionId,
scope,
accountIds: [account.id],
trustlineVerification: {
assetId,
action,
},
});

return {
Expand Down
Loading
Loading