diff --git a/indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts b/indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts index ff03a401f6..488435f8e7 100644 --- a/indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts +++ b/indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts @@ -12,17 +12,15 @@ import { AssetPositionTable, FundingIndexUpdatesTable, PnlTicksFromDatabase, + VaultTable, } from '@dydxprotocol-indexer/postgres'; import { RequestMethod, VaultHistoricalPnl } from '../../../../src/types'; import request from 'supertest'; import { getFixedRepresentation, sendRequest } from '../../../helpers/helpers'; -import config from '../../../../src/config'; import { DateTime } from 'luxon'; import Big from 'big.js'; describe('vault-controller#V4', () => { - const experimentVaultsPrevVal: string = config.EXPERIMENT_VAULTS; - const experimentVaultMarketsPrevVal: string = config.EXPERIMENT_VAULT_MARKETS; const latestBlockHeight: string = '25'; const currentBlockHeight: string = '7'; const twoHourBlockHeight: string = '5'; @@ -45,8 +43,6 @@ describe('vault-controller#V4', () => { describe('GET /v1', () => { beforeEach(async () => { - config.EXPERIMENT_VAULTS = testConstants.defaultPnlTick.subaccountId; - config.EXPERIMENT_VAULT_MARKETS = testConstants.defaultPerpetualMarket.clobPairId; await testMocks.seedData(); await perpetualMarketRefresher.updatePerpetualMarkets(); await liquidityTierRefresher.updateLiquidityTiers(); @@ -96,15 +92,10 @@ describe('vault-controller#V4', () => { }); afterEach(async () => { - config.EXPERIMENT_VAULTS = experimentVaultsPrevVal; - config.EXPERIMENT_VAULT_MARKETS = experimentVaultMarketsPrevVal; await dbHelpers.clearData(); }); it('Get /megavault/historicalPnl with no vault subaccounts', async () => { - config.EXPERIMENT_VAULTS = ''; - config.EXPERIMENT_VAULT_MARKETS = ''; - const response: request.Response = await sendRequest({ type: RequestMethod.GET, path: '/v4/vault/v1/megavault/historicalPnl', @@ -122,6 +113,11 @@ describe('vault-controller#V4', () => { queryParam: string, expectedTicksIndex: number[], ) => { + await VaultTable.create({ + ...testConstants.defaultVault, + address: testConstants.defaultSubaccount.address, + clobPairId: testConstants.defaultPerpetualMarket.clobPairId, + }); const createdPnlTicks: PnlTicksFromDatabase[] = await createPnlTicks(); const finalTick: PnlTicksFromDatabase = { ...createdPnlTicks[expectedTicksIndex[expectedTicksIndex.length - 1]], @@ -155,14 +151,18 @@ describe('vault-controller#V4', () => { queryParam: string, expectedTicksIndex: number[], ) => { - config.EXPERIMENT_VAULTS = [ - testConstants.defaultPnlTick.subaccountId, - testConstants.vaultSubaccountId, - ].join(','); - config.EXPERIMENT_VAULT_MARKETS = [ - testConstants.defaultPerpetualMarket.clobPairId, - testConstants.defaultPerpetualMarket2.clobPairId, - ].join(','); + await Promise.all([ + VaultTable.create({ + ...testConstants.defaultVault, + address: testConstants.defaultAddress, + clobPairId: testConstants.defaultPerpetualMarket.clobPairId, + }), + VaultTable.create({ + ...testConstants.defaultVault, + address: testConstants.vaultAddress, + clobPairId: testConstants.defaultPerpetualMarket2.clobPairId, + }), + ]); const createdPnlTicks: PnlTicksFromDatabase[] = await createPnlTicks(); const response: request.Response = await sendRequest({ @@ -199,9 +199,6 @@ describe('vault-controller#V4', () => { }); it('Get /vaults/historicalPnl with no vault subaccounts', async () => { - config.EXPERIMENT_VAULTS = ''; - config.EXPERIMENT_VAULT_MARKETS = ''; - const response: request.Response = await sendRequest({ type: RequestMethod.GET, path: '/v4/vault/v1/vaults/historicalPnl', @@ -219,6 +216,11 @@ describe('vault-controller#V4', () => { queryParam: string, expectedTicksIndex: number[], ) => { + await VaultTable.create({ + ...testConstants.defaultVault, + address: testConstants.defaultAddress, + clobPairId: testConstants.defaultPerpetualMarket.clobPairId, + }); const createdPnlTicks: PnlTicksFromDatabase[] = await createPnlTicks(); const finalTick: PnlTicksFromDatabase = { ...createdPnlTicks[expectedTicksIndex[expectedTicksIndex.length - 1]], @@ -255,15 +257,18 @@ describe('vault-controller#V4', () => { expectedTicksIndex1: number[], expectedTicksIndex2: number[], ) => { - config.EXPERIMENT_VAULTS = [ - testConstants.defaultPnlTick.subaccountId, - testConstants.vaultSubaccountId, - ].join(','); - config.EXPERIMENT_VAULT_MARKETS = [ - testConstants.defaultPerpetualMarket.clobPairId, - testConstants.defaultPerpetualMarket2.clobPairId, - ].join(','); - + await Promise.all([ + VaultTable.create({ + ...testConstants.defaultVault, + address: testConstants.defaultAddress, + clobPairId: testConstants.defaultPerpetualMarket.clobPairId, + }), + VaultTable.create({ + ...testConstants.defaultVault, + address: testConstants.vaultAddress, + clobPairId: testConstants.defaultPerpetualMarket2.clobPairId, + }), + ]); const createdPnlTicks: PnlTicksFromDatabase[] = await createPnlTicks(); const finalTick1: PnlTicksFromDatabase = { ...createdPnlTicks[expectedTicksIndex1[expectedTicksIndex1.length - 1]], @@ -312,9 +317,6 @@ describe('vault-controller#V4', () => { }); it('Get /megavault/positions with no vault subaccount', async () => { - config.EXPERIMENT_VAULTS = ''; - config.EXPERIMENT_VAULT_MARKETS = ''; - const response: request.Response = await sendRequest({ type: RequestMethod.GET, path: '/v4/vault/v1/megavault/positions', @@ -326,6 +328,11 @@ describe('vault-controller#V4', () => { }); it('Get /megavault/positions with 1 vault subaccount', async () => { + await VaultTable.create({ + ...testConstants.defaultVault, + address: testConstants.defaultAddress, + clobPairId: testConstants.defaultPerpetualMarket.clobPairId, + }); const response: request.Response = await sendRequest({ type: RequestMethod.GET, path: '/v4/vault/v1/megavault/positions', @@ -374,15 +381,18 @@ describe('vault-controller#V4', () => { }); it('Get /megavault/positions with 2 vault subaccount, 1 with no perpetual', async () => { - config.EXPERIMENT_VAULTS = [ - testConstants.defaultPnlTick.subaccountId, - testConstants.vaultSubaccountId, - ].join(','); - config.EXPERIMENT_VAULT_MARKETS = [ - testConstants.defaultPerpetualMarket.clobPairId, - testConstants.defaultPerpetualMarket2.clobPairId, - ].join(','); - + await Promise.all([ + VaultTable.create({ + ...testConstants.defaultVault, + address: testConstants.defaultAddress, + clobPairId: testConstants.defaultPerpetualMarket.clobPairId, + }), + VaultTable.create({ + ...testConstants.defaultVault, + address: testConstants.vaultAddress, + clobPairId: testConstants.defaultPerpetualMarket2.clobPairId, + }), + ]); const response: request.Response = await sendRequest({ type: RequestMethod.GET, path: '/v4/vault/v1/megavault/positions', diff --git a/indexer/services/comlink/src/config.ts b/indexer/services/comlink/src/config.ts index 9591c64eb0..43d2c81222 100644 --- a/indexer/services/comlink/src/config.ts +++ b/indexer/services/comlink/src/config.ts @@ -56,11 +56,6 @@ export const configSchema = { // Expose setting compliance status, only set to true in dev/staging. EXPOSE_SET_COMPLIANCE_ENDPOINT: parseBoolean({ default: false }), - // TODO(TRA-570): Placeholder data for vaults and matching set of markets for each vault until - // vaults table is added. - EXPERIMENT_VAULTS: parseString({ default: '' }), - EXPERIMENT_VAULT_MARKETS: parseString({ default: '' }), - // Affiliates config VOLUME_ELIGIBILITY_THRESHOLD: parseInteger({ default: 10_000 }), diff --git a/indexer/services/comlink/src/controllers/api/v4/vault-controller.ts b/indexer/services/comlink/src/controllers/api/v4/vault-controller.ts index 830980955d..e377f40c9b 100644 --- a/indexer/services/comlink/src/controllers/api/v4/vault-controller.ts +++ b/indexer/services/comlink/src/controllers/api/v4/vault-controller.ts @@ -22,6 +22,8 @@ import { BlockFromDatabase, FundingIndexUpdatesTable, PnlTickInterval, + VaultTable, + VaultFromDatabase, } from '@dydxprotocol-indexer/postgres'; import Big from 'big.js'; import express from 'express'; @@ -57,8 +59,6 @@ import { const router: express.Router = express.Router(); const controllerName: string = 'vault-controller'; -// TODO(TRA-570): Placeholder interface for mapping of vault subaccounts to tickers until vaults -// table is added. interface VaultMapping { [subaccountId: string]: string, } @@ -69,7 +69,7 @@ class VaultController extends Controller { async getMegavaultHistoricalPnl( @Query() resolution?: PnlTickInterval, ): Promise { - const vaultSubaccounts: VaultMapping = getVaultSubaccountsFromConfig(); + const vaultSubaccounts: VaultMapping = await getVaultMapping(); const [ vaultPnlTicks, vaultPositions, @@ -79,7 +79,7 @@ class VaultController extends Controller { Map, BlockFromDatabase, ] = await Promise.all([ - getVaultSubaccountPnlTicks(resolution), + getVaultSubaccountPnlTicks(vaultSubaccounts, resolution), getVaultPositions(vaultSubaccounts), BlockTable.getLatest(), ]); @@ -111,7 +111,7 @@ class VaultController extends Controller { async getVaultsHistoricalPnl( @Query() resolution?: PnlTickInterval, ): Promise { - const vaultSubaccounts: VaultMapping = getVaultSubaccountsFromConfig(); + const vaultSubaccounts: VaultMapping = await getVaultMapping(); const [ vaultPnlTicks, vaultPositions, @@ -121,7 +121,7 @@ class VaultController extends Controller { Map, BlockFromDatabase, ] = await Promise.all([ - getVaultSubaccountPnlTicks(resolution), + getVaultSubaccountPnlTicks(vaultSubaccounts, resolution), getVaultPositions(vaultSubaccounts), BlockTable.getLatest(), ]); @@ -163,7 +163,7 @@ class VaultController extends Controller { @Get('/megavault/positions') async getMegavaultPositions(): Promise { - const vaultSubaccounts: VaultMapping = getVaultSubaccountsFromConfig(); + const vaultSubaccounts: VaultMapping = await getVaultMapping(); const vaultPositions: Map = await getVaultPositions(vaultSubaccounts); @@ -286,9 +286,10 @@ router.get( }); async function getVaultSubaccountPnlTicks( + vaultSubaccounts: VaultMapping, resolution?: PnlTickInterval, ): Promise { - const vaultSubaccountIds: string[] = _.keys(getVaultSubaccountsFromConfig()); + const vaultSubaccountIds: string[] = _.keys(vaultSubaccounts); if (vaultSubaccountIds.length === 0) { return []; } @@ -454,19 +455,19 @@ function getPnlTicksWithCurrentTick( return pnlTicks.concat([currentTick]); } -// TODO(TRA-570): Placeholder for getting vault subaccount ids until vault table is added. -function getVaultSubaccountsFromConfig(): VaultMapping { - if (config.EXPERIMENT_VAULTS === '' && config.EXPERIMENT_VAULT_MARKETS === '') { - return {}; - } - const vaultSubaccountIds: string[] = config.EXPERIMENT_VAULTS.split(','); - const vaultClobPairIds: string[] = config.EXPERIMENT_VAULT_MARKETS.split(','); - if (vaultSubaccountIds.length !== vaultClobPairIds.length) { - throw new Error('Expected number of vaults to match number of markets'); - } +async function getVaultMapping(): Promise { + const vaults: VaultFromDatabase[] = await VaultTable.findAll( + {}, + [], + {}, + ); return _.zipObject( - vaultSubaccountIds, - vaultClobPairIds, + vaults.map((vault: VaultFromDatabase): string => { + return SubaccountTable.uuid(vault.address, 0); + }), + vaults.map((vault: VaultFromDatabase): string => { + return vault.clobPairId; + }), ); }