Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ linkStyle default opacity:0.5
money_account_balance_service --> controller_utils;
money_account_balance_service --> messenger;
money_account_balance_service --> network_controller;
money_account_balance_service --> remote_feature_flag_controller;
money_account_controller --> accounts_controller;
money_account_controller --> base_controller;
money_account_controller --> keyring_controller;
Expand Down
9 changes: 9 additions & 0 deletions packages/money-account-balance-service/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `VaultConfigNotAvailableError` and `VaultConfigValidationError` error classes for typed consumer error handling ([#8742](https://github.com/MetaMask/core/pull/8742))
- Add `LENS_ABI` constant for the Arctic Architecture Lens contract ([#8742](https://github.com/MetaMask/core/pull/8742))

### Changed

- **BREAKING:** `MoneyAccountBalanceService` no longer accepts vault config via constructor. Vault config is now read from `RemoteFeatureFlagController` state. Add `@metamask/remote-feature-flag-controller` as a dependency and permit `RemoteFeatureFlagController:getState`action and `RemoteFeatureFlagController:stateChange` event on the service's messenger. Service methods throw `VaultConfigNotAvailableError` until a valid config is available. ([#8742](https://github.com/MetaMask/core/pull/8742))
- **BREAKING:** `VaultConfig` fields have changed — `vaultAddress` → `boringVault`, `vaultChainId` → `chainId`; `underlyingTokenAddress` and `underlyingTokenDecimals` removed; `lensAddress` and `tellerAddress` added ([#8742](https://github.com/MetaMask/core/pull/8742))
- **BREAKING:** `MusdEquivalentValueResponse` shape has changed — `musdSHFvdBalance`, `exchangeRate`, and `musdEquivalentValue` replaced by a single `balanceOfInAssets` field ([#8742](https://github.com/MetaMask/core/pull/8742))
- Monad (`0x8f`) added to `VEDA_API_NETWORK_NAMES` ([#8742](https://github.com/MetaMask/core/pull/8742))
- Bump `@metamask/messenger` from `^1.1.1` to `^1.2.0` ([#8632](https://github.com/MetaMask/core/pull/8632))
- Bump `@metamask/network-controller` from `^30.0.1` to `^30.1.0` ([#8636](https://github.com/MetaMask/core/pull/8636))

Expand Down
1 change: 1 addition & 0 deletions packages/money-account-balance-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@metamask/messenger": "^1.2.0",
"@metamask/metamask-eth-abis": "^3.1.1",
"@metamask/network-controller": "^30.1.0",
"@metamask/remote-feature-flag-controller": "^4.2.0",
"@metamask/superstruct": "^3.1.0",
"@metamask/utils": "^11.9.0"
},
Expand Down
53 changes: 48 additions & 5 deletions packages/money-account-balance-service/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@ import { Hex } from '@metamask/utils';

export const VEDA_PERFORMANCE_API_BASE_URL = 'https://api.sevenseas.capital';

/**
* The key under which vault config is stored in
* `RemoteFeatureFlagController` state's `remoteFeatureFlags` map.
*/
export const VAULT_CONFIG_FEATURE_FLAG_KEY = 'moneyAccountVaultConfig';

export const VEDA_API_NETWORK_NAMES: Record<Hex, string> = {
'0xa4b1': 'arbitrum',
'0x8f': 'monad',
};

export const DEFAULT_VEDA_API_NETWORK_NAME = VEDA_API_NETWORK_NAMES['0xa4b1'];

/**
* Minimal ABI for the Veda Accountant's `getRate()` function (selector 0x679aefce).
* Returns the exchange rate between vault shares (musdSHFvd) and the
* underlying asset (mUSD) as a uint256.
* Minimal ABI for the Veda Accountant contract. Covers:
* - base (0x5001f3b5) — the underlying ERC20 base asset address
* - getRate (0x679aefce) — exchange rate between vault shares and the
* underlying asset (mUSD) as a uint256
*/
export const ACCOUNTANT_ABI = [
{
inputs: [],
name: 'base',
outputs: [{ internalType: 'contract ERC20', name: '', type: 'address' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'getRate',
Expand All @@ -22,3 +35,33 @@ export const ACCOUNTANT_ABI = [
type: 'function',
},
] as const;

/**
* Minimal ABI for the Arctic Architecture Lens contract.
* Covers:
* - balanceOf (0xf7888aec) — shares held by an account in a BoringVault
* - balanceOfInAssets (0x789fd871) — share balance denominated in underlying assets
*/
export const LENS_ABI = [
{
inputs: [
{ name: 'account', type: 'address' },
{ name: 'boringVault', type: 'address' },
],
name: 'balanceOf',
outputs: [{ name: 'shares', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{ name: 'account', type: 'address' },
{ name: 'boringVault', type: 'address' },
{ name: 'accountant', type: 'address' },
],
name: 'balanceOfInAssets',
outputs: [{ name: 'assets', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
] as const;
31 changes: 31 additions & 0 deletions packages/money-account-balance-service/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,34 @@ export class VedaResponseValidationError extends Error {
this.name = 'VedaResponseValidationError';
}
}

/**
* Thrown when a public method is called but vault config has not yet been
* loaded from RemoteFeatureFlagController, or the flag key is absent.
* This is a transient condition — the service will recover once flags are
* fetched and a valid config arrives.
*/
export class VaultConfigNotAvailableError extends Error {
constructor() {
super(
'MoneyAccountBalanceService: vault config is not available. ' +
'RemoteFeatureFlagController may not have fetched flags yet.',
);
this.name = 'VaultConfigNotAvailableError';
}
}

/**
* Thrown when the vault config flag value is present but fails superstruct
* validation. This surfaces to error monitoring service (e.g. Sentry) via the messenger's captureException
* handler.
*/
export class VaultConfigValidationError extends Error {
constructor(message?: string) {
super(
message ??
'MoneyAccountBalanceService: vault config from remote feature flags is malformed.',
);
this.name = 'VaultConfigValidationError';
}
}
5 changes: 5 additions & 0 deletions packages/money-account-balance-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ export type {
MusdEquivalentValueResponse,
NormalizedVaultApyResponse,
} from './response.types';
export {
VaultConfigNotAvailableError,
VaultConfigValidationError,
} from './errors';
export type { VaultConfig } from './types';
7 changes: 7 additions & 0 deletions packages/money-account-balance-service/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createProjectLogger, createModuleLogger } from '@metamask/utils';

export const projectLogger = createProjectLogger(
'money-account-balance-service',
);

export { createModuleLogger };
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { MoneyAccountBalanceService } from './money-account-balance-service
*
* @param accountAddress - The Money account's Ethereum address.
* @returns The mUSD balance as a raw uint256 string.
* @throws {@link VaultConfigNotAvailableError} if vault config has not been loaded.
*/
export type MoneyAccountBalanceServiceGetMusdBalanceAction = {
type: `MoneyAccountBalanceService:getMusdBalance`;
Expand All @@ -22,6 +23,7 @@ export type MoneyAccountBalanceServiceGetMusdBalanceAction = {
*
* @param accountAddress - The Money account's Ethereum address.
* @returns The musdSHFvd balance as a raw uint256 string.
* @throws {@link VaultConfigNotAvailableError} if vault config has not been loaded.
*/
export type MoneyAccountBalanceServiceGetMusdSHFvdBalanceAction = {
type: `MoneyAccountBalanceService:getMusdSHFvdBalance`;
Expand All @@ -36,6 +38,7 @@ export type MoneyAccountBalanceServiceGetMusdSHFvdBalanceAction = {
* @param options - The options for the query.
* @param options.staleTime - The stale time for the query. Defaults to 30 seconds.
* @returns The exchange rate as a raw uint256 string.
* @throws {@link VaultConfigNotAvailableError} if vault config has not been loaded.
*/
export type MoneyAccountBalanceServiceGetExchangeRateAction = {
type: `MoneyAccountBalanceService:getExchangeRate`;
Expand All @@ -51,6 +54,7 @@ export type MoneyAccountBalanceServiceGetExchangeRateAction = {
* @param accountAddress - The Money account's Ethereum address.
* @returns The musdSHFvd balance, exchange rate, and computed
* mUSD-equivalent value as raw uint256 strings.
* @throws {@link VaultConfigNotAvailableError} if vault config has not been loaded.
*/
export type MoneyAccountBalanceServiceGetMusdEquivalentValueAction = {
type: `MoneyAccountBalanceService:getMusdEquivalentValue`;
Expand All @@ -61,6 +65,7 @@ export type MoneyAccountBalanceServiceGetMusdEquivalentValueAction = {
* Fetches the vault's APY and fee breakdown from the Veda performance REST API.
*
* @returns The normalized vault APY response.
* @throws {@link VaultConfigNotAvailableError} if vault config has not been loaded.
*/
export type MoneyAccountBalanceServiceGetVaultApyAction = {
type: `MoneyAccountBalanceService:getVaultApy`;
Expand Down
Loading
Loading