Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
109 changes: 102 additions & 7 deletions packages/earn-controller/src/EarnController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jest.mock('@metamask/stake-sdk', () => ({
getPooledStakes: jest.fn(),
getPooledStakingEligibility: jest.fn(),
getVaultData: jest.fn(),
getVaultDailyApys: jest.fn(),
getVaultApyAverages: jest.fn(),
})),
}));

Expand Down Expand Up @@ -102,14 +104,90 @@ const mockPooledStakes = {
assets: '1000',
exitRequests: [],
};
const mockVaultData = {

const mockVaultMetadata = {
apy: '5.5',
capacity: '1000000',
feePercent: 10,
totalAssets: '500000',
vaultAddress: '0xabcd',
};

const mockPooledStakingVaultDailyApys = [
{
id: 1,
chain_id: 1,
vault_address: '0xabc',
timestamp: '2025-02-19T00:00:00.000Z',
daily_apy: '2.273150114369428540',
created_at: '2025-02-20T01:00:00.686Z',
updated_at: '2025-02-20T01:00:00.686Z',
},
{
id: 2,
chain_id: 1,
vault_address: '0xabc',
timestamp: '2025-02-18T00:00:00.000Z',
daily_apy: '2.601753752988867146',
created_at: '2025-02-19T01:00:00.460Z',
updated_at: '2025-02-19T01:00:00.460Z',
},
{
id: 3,
chain_id: 1,
vault_address: '0xabc',
timestamp: '2025-02-17T00:00:00.000Z',
daily_apy: '2.371788704658418308',
created_at: '2025-02-18T01:00:00.579Z',
updated_at: '2025-02-18T01:00:00.579Z',
},
{
id: 4,
chain_id: 1,
vault_address: '0xabc',
timestamp: '2025-02-16T00:00:00.000Z',
daily_apy: '2.037130166329167644',
created_at: '2025-02-17T01:00:00.368Z',
updated_at: '2025-02-17T01:00:00.368Z',
},
{
id: 5,
chain_id: 1,
vault_address: '0xabc',
timestamp: '2025-02-15T00:00:00.000Z',
daily_apy: '2.495509141072538330',
created_at: '2025-02-16T01:00:00.737Z',
updated_at: '2025-02-16T01:00:00.737Z',
},
{
id: 6,
chain_id: 1,
vault_address: '0xabc',
timestamp: '2025-02-14T00:00:00.000Z',
daily_apy: '2.760147959320520741',
created_at: '2025-02-15T01:00:00.521Z',
updated_at: '2025-02-15T01:00:00.521Z',
},
{
id: 7,
chain_id: 1,
vault_address: '0xabc',
timestamp: '2025-02-13T00:00:00.000Z',
daily_apy: '2.620957696005122124',
created_at: '2025-02-14T01:00:00.438Z',
updated_at: '2025-02-14T01:00:00.438Z',
},
];

const mockPooledStakingVaultApyAverages = {
oneDay: '3.047713358665092375',
oneWeek: '3.25756026351317301786',
oneMonth: '3.25616054301749304217',
threeMonths: '3.31863306662107446672',
sixMonths: '3.05557344496273894133',
oneYear: '0',
};

const setupController = ({
options = {},

Expand Down Expand Up @@ -185,7 +263,13 @@ describe('EarnController', () => {
getPooledStakingEligibility: jest.fn().mockResolvedValue({
eligible: true,
}),
getVaultData: jest.fn().mockResolvedValue(mockVaultData),
getVaultData: jest.fn().mockResolvedValue(mockVaultMetadata),
getVaultDailyApys: jest
.fn()
.mockResolvedValue(mockPooledStakingVaultDailyApys),
getVaultApyAverages: jest
.fn()
.mockResolvedValue(mockPooledStakingVaultApyAverages),
} as Partial<StakingApiService>;

StakingApiServiceMock.mockImplementation(
Expand All @@ -204,8 +288,10 @@ describe('EarnController', () => {
pooled_staking: {
pooledStakes: mockPooledStakes,
exchangeRate: '1.5',
vaultData: mockVaultData,
vaultMetadata: mockVaultMetadata,
isEligible: true,
vaultDailyApys: mockPooledStakingVaultDailyApys,
vaultApyAverages: mockPooledStakingVaultApyAverages,
},
lastUpdated: 1234567890,
};
Expand Down Expand Up @@ -288,7 +374,9 @@ describe('EarnController', () => {
expect(controller.state.pooled_staking).toStrictEqual({
pooledStakes: mockPooledStakes,
exchangeRate: '1.5',
vaultData: mockVaultData,
vaultMetadata: mockVaultMetadata,
vaultDailyApys: mockPooledStakingVaultDailyApys,
vaultApyAverages: mockPooledStakingVaultApyAverages,
isEligible: true,
});
expect(controller.state.lastUpdated).toBeDefined();
Expand Down Expand Up @@ -347,20 +435,27 @@ describe('EarnController', () => {
consoleErrorSpy.mockRestore();
});

// if no account is selected, it should not fetch stakes data but still updates vault data
// if no account is selected, it should not fetch stakes data but still update vault metadata, vault daily apys and vault apy averages.
it('does not fetch staking data if no account is selected', async () => {
const { controller } = setupController({
mockGetSelectedAccount: jest.fn(() => null),
});

expect(mockedStakingApiService.getPooledStakes).not.toHaveBeenCalled();

await controller.refreshPooledStakingData();

expect(controller.state.pooled_staking.pooledStakes).toStrictEqual(
getDefaultEarnControllerState().pooled_staking.pooledStakes,
);
expect(controller.state.pooled_staking.vaultData).toStrictEqual(
mockVaultData,
expect(controller.state.pooled_staking.vaultMetadata).toStrictEqual(
mockVaultMetadata,
);
expect(controller.state.pooled_staking.vaultDailyApys).toStrictEqual(
mockPooledStakingVaultDailyApys,
);
expect(controller.state.pooled_staking.vaultApyAverages).toStrictEqual(
mockPooledStakingVaultApyAverages,
);
expect(controller.state.pooled_staking.isEligible).toBe(false);
});
Expand Down
79 changes: 70 additions & 9 deletions packages/earn-controller/src/EarnController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ import {
type PooledStake,
type StakeSdkConfig,
type VaultData,
type VaultDailyApy,
type VaultApyAverages,
} from '@metamask/stake-sdk';

export const controllerName = 'EarnController';

export type PooledStakingState = {
pooledStakes: PooledStake;
exchangeRate: string;
vaultData: VaultData;
vaultMetadata: VaultData;
vaultDailyApys: VaultDailyApy[];
vaultApyAverages: VaultApyAverages;
isEligible: boolean;
};

Expand Down Expand Up @@ -85,6 +89,15 @@ const DEFAULT_STABLECOIN_VAULT: StablecoinVault = {
liquidity: '0',
};

const DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES: VaultApyAverages = {
oneDay: '0',
oneWeek: '0',
oneMonth: '0',
threeMonths: '0',
sixMonths: '0',
oneYear: '0',
};

/**
* Gets the default state for the EarnController.
*
Expand All @@ -100,13 +113,15 @@ export function getDefaultEarnControllerState(): EarnControllerState {
exitRequests: [],
},
exchangeRate: '1',
vaultData: {
vaultMetadata: {
apy: '0',
capacity: '0',
feePercent: 0,
totalAssets: '0',
vaultAddress: '0x0000000000000000000000000000000000000000',
},
vaultDailyApys: [],
vaultApyAverages: DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES,
isEligible: false,
},
stablecoin_lending: {
Expand Down Expand Up @@ -345,18 +360,58 @@ export class EarnController extends BaseController<
}

/**
* Refreshes vault data for the current chain.
* Updates the vault data in the controller state including APY, capacity,
* Refreshes vault metadata for the current chain.
* Updates the vault metadata in the controller state including APY, capacity,
* fee percentage, total assets, and vault address.
*
* @returns A promise that resolves when the vault data has been updated
* @returns A promise that resolves when the vault metadata has been updated
*/
async refreshVaultData(): Promise<void> {
async refreshVaultMetadata(): Promise<void> {
const chainId = this.#getCurrentChainId();
const vaultData = await this.#stakingApiService.getVaultData(chainId);
const vaultMetadata = await this.#stakingApiService.getVaultData(chainId);

this.update((state) => {
state.pooled_staking.vaultData = vaultData;
state.pooled_staking.vaultMetadata = vaultMetadata;
});
}

/**
* Refreshes pooled staking vault daily apys for the current chain.
* Updates the pooled staking vault daily apys controller state.
*
* @param days - The number of days to fetch pooled staking vault daily apys for (defaults to 30).
* @param order - The order in which to fetch pooled staking vault daily apys. Descending order fetches the latest N days (latest working backwards). Ascending order fetches the oldest N days (oldest working forwards) (defaults to 'desc').
* @returns A promise that resolves when the pooled staking vault daily apys have been updated.
*/
async refreshPooledStakingVaultDailyApys(
days = 30,
order: 'asc' | 'desc' = 'desc',
): Promise<void> {
const chainId = this.#getCurrentChainId();
const vaultDailyApys = await this.#stakingApiService.getVaultDailyApys(
chainId,
days,
order,
);

this.update((state) => {
state.pooled_staking.vaultDailyApys = vaultDailyApys;
});
}

/**
* Refreshes pooled staking vault apy averages for the current chain.
* Updates the pooled staking vault apy averages controller state.
*
* @returns A promise that resolves when the pooled staking vault apy averages have been updated.
*/
async refreshPooledStakingVaultApyAverages() {
const chainId = this.#getCurrentChainId();
const vaultApyAverages =
await this.#stakingApiService.getVaultApyAverages(chainId);

this.update((state) => {
state.pooled_staking.vaultApyAverages = vaultApyAverages;
});
}

Expand All @@ -379,7 +434,13 @@ export class EarnController extends BaseController<
this.refreshStakingEligibility().catch((error) => {
errors.push(error);
}),
this.refreshVaultData().catch((error) => {
this.refreshVaultMetadata().catch((error) => {
errors.push(error);
}),
this.refreshPooledStakingVaultDailyApys().catch((error) => {
errors.push(error);
}),
this.refreshPooledStakingVaultApyAverages().catch((error) => {
errors.push(error);
}),
]);
Expand Down
Loading