Skip to content
Merged
4 changes: 2 additions & 2 deletions packages/keyring-api/src/api/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { object, UuidStruct } from '@metamask/keyring-utils';
import { AccountIdStruct, object } from '@metamask/keyring-utils';
import type { Infer } from '@metamask/superstruct';
import {
nonempty,
Expand Down Expand Up @@ -56,7 +56,7 @@ export const KeyringAccountStruct = object({
/**
* Account ID (UUIDv4).
*/
id: UuidStruct,
id: AccountIdStruct,

/**
* Account type.
Expand Down
25 changes: 16 additions & 9 deletions packages/keyring-api/src/api/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ import {
isPlainObject,
} from '@metamask/utils';

/**
* Fungible asset amount struct.
*/
export const FungibleAssetAmountStruct = object({
/**
* Asset unit.
*/
unit: string(),

/**
* Asset amount.
*/
amount: StringNumberStruct,
});

/**
* Fungible asset struct.
*/
Expand All @@ -25,15 +40,7 @@ export const FungibleAssetStruct = object({
*/
type: CaipAssetTypeStruct,

/**
* Asset unit.
*/
unit: string(),

/**
* Asset amount.
*/
amount: StringNumberStruct,
...FungibleAssetAmountStruct.schema,
});

/**
Expand Down
147 changes: 143 additions & 4 deletions packages/keyring-api/src/events.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { exactOptional, object, UuidStruct } from '@metamask/keyring-utils';
import { boolean, literal, string } from '@metamask/superstruct';
import { JsonStruct } from '@metamask/utils';
import {
exactOptional,
object,
UuidStruct,
AccountIdStruct,
} from '@metamask/keyring-utils';
import type { Infer } from '@metamask/superstruct';
import { array, boolean, literal, record, string } from '@metamask/superstruct';
import { CaipAssetTypeStruct, JsonStruct } from '@metamask/utils';

import { KeyringAccountStruct } from './api';
import {
CaipAssetTypeOrIdStruct,
FungibleAssetAmountStruct,
KeyringAccountStruct,
TransactionStruct,
} from './api';

/**
* Supported keyring events.
Expand All @@ -16,6 +27,11 @@ export enum KeyringEvent {
// Request events
RequestApproved = 'notify:requestApproved',
RequestRejected = 'notify:requestRejected',

// Assets related events
AccountBalancesUpdated = 'notify:accountBalancesUpdated',
AccountAssetListUpdated = 'notify:accountAssetListUpdated',
AccountTransactionsUpdated = 'notify:accountTransactionsUpdated',
}

export const AccountCreatedEventStruct = object({
Expand Down Expand Up @@ -87,3 +103,126 @@ export const RequestRejectedEventStruct = object({
id: UuidStruct,
}),
});

// Assets related events:
// -----------------------------------------------------------------------------------------------

export const AccountBalancesUpdatedEventStruct = object({
method: literal(`${KeyringEvent.AccountBalancesUpdated}`),
params: object({
/**
* Balances updates of accounts owned by the Snap.
*/
balances: record(
/**
* Account ID.
*/
AccountIdStruct,

/**
* Mapping of each owned assets and their respective balances for that account.
*/
record(
/**
* Asset type (CAIP-19).
*/
CaipAssetTypeStruct,

/**
* Balance information for a given asset.
*/
FungibleAssetAmountStruct,
),
),
}),
});

/**
* Event emitted when the balances of an account are updated.
*
* Only changes are reported.
*
* The Snap can choose to emit this event for multiple accounts at once.
*/
export type AccountBalancesUpdatedEvent = Infer<
typeof AccountBalancesUpdatedEventStruct
>;
export type AccountBalancesUpdatedEventPayload =
AccountBalancesUpdatedEvent['params'];

export const AccountTransactionsUpdatedEventStruct = object({
method: literal(`${KeyringEvent.AccountTransactionsUpdated}`),
params: object({
/**
* Transactions updates of accounts owned by the Snap.
*/
transactions: record(
/**
* Account ID.
*/
AccountIdStruct,

/**
* List of updated transactions for that account.
*/
array(TransactionStruct),
),
}),
});

/**
* Event emitted when the transactions of an account are updated (added or
* changed).
*
* Only changes are reported.
*
* The Snap can choose to emit this event for multiple accounts at once.
*/
export type AccountTransactionsUpdatedEvent = Infer<
typeof AccountTransactionsUpdatedEventStruct
>;
export type AccountTransactionsUpdatedEventPayload =
AccountTransactionsUpdatedEvent['params'];

export const AccountAssetListUpdatedEventStruct = object({
method: literal(`${KeyringEvent.AccountAssetListUpdated}`),
params: object({
/**
* Asset list update of accounts owned by the Snap.
*/
assets: record(
/**
* Account ID.
*/
AccountIdStruct,

/**
* Asset list changes for that account.
*/
object({
/**
* New assets detected.
*/
added: array(CaipAssetTypeOrIdStruct),

/**
* Assets no longer available on that account.
*/
removed: array(CaipAssetTypeOrIdStruct),
}),
),
}),
});

/**
* Event emitted when the assets of an account are updated.
*
* Only changes are reported.
*
* The Snap can choose to emit this event for multiple accounts at once.
*/
export type AccountAssetListUpdatedEvent = Infer<
typeof AccountAssetListUpdatedEventStruct
>;
export type AccountAssetListUpdatedEventPayload =
AccountAssetListUpdatedEvent['params'];
Loading
Loading