Skip to content

Commit

Permalink
Use vault table rather than placeholder config flags to fetch vaults.…
Browse files Browse the repository at this point in the history
… (backport #2364) (#2367)

Co-authored-by: vincentwschau <[email protected]>
  • Loading branch information
mergify[bot] and vincentwschau authored Sep 26, 2024
1 parent a47c327 commit 9875783
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand Down Expand Up @@ -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',
Expand All @@ -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]],
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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',
Expand All @@ -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]],
Expand Down Expand Up @@ -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]],
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
5 changes: 0 additions & 5 deletions indexer/services/comlink/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),

Expand Down
41 changes: 21 additions & 20 deletions indexer/services/comlink/src/controllers/api/v4/vault-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
BlockFromDatabase,
FundingIndexUpdatesTable,
PnlTickInterval,
VaultTable,
VaultFromDatabase,
} from '@dydxprotocol-indexer/postgres';
import Big from 'big.js';
import express from 'express';
Expand Down Expand Up @@ -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,
}
Expand All @@ -69,7 +69,7 @@ class VaultController extends Controller {
async getMegavaultHistoricalPnl(
@Query() resolution?: PnlTickInterval,
): Promise<MegavaultHistoricalPnlResponse> {
const vaultSubaccounts: VaultMapping = getVaultSubaccountsFromConfig();
const vaultSubaccounts: VaultMapping = await getVaultMapping();
const [
vaultPnlTicks,
vaultPositions,
Expand All @@ -79,7 +79,7 @@ class VaultController extends Controller {
Map<string, VaultPosition>,
BlockFromDatabase,
] = await Promise.all([
getVaultSubaccountPnlTicks(resolution),
getVaultSubaccountPnlTicks(vaultSubaccounts, resolution),
getVaultPositions(vaultSubaccounts),
BlockTable.getLatest(),
]);
Expand Down Expand Up @@ -111,7 +111,7 @@ class VaultController extends Controller {
async getVaultsHistoricalPnl(
@Query() resolution?: PnlTickInterval,
): Promise<VaultsHistoricalPnlResponse> {
const vaultSubaccounts: VaultMapping = getVaultSubaccountsFromConfig();
const vaultSubaccounts: VaultMapping = await getVaultMapping();
const [
vaultPnlTicks,
vaultPositions,
Expand All @@ -121,7 +121,7 @@ class VaultController extends Controller {
Map<string, VaultPosition>,
BlockFromDatabase,
] = await Promise.all([
getVaultSubaccountPnlTicks(resolution),
getVaultSubaccountPnlTicks(vaultSubaccounts, resolution),
getVaultPositions(vaultSubaccounts),
BlockTable.getLatest(),
]);
Expand Down Expand Up @@ -163,7 +163,7 @@ class VaultController extends Controller {

@Get('/megavault/positions')
async getMegavaultPositions(): Promise<MegavaultPositionResponse> {
const vaultSubaccounts: VaultMapping = getVaultSubaccountsFromConfig();
const vaultSubaccounts: VaultMapping = await getVaultMapping();

const vaultPositions: Map<string, VaultPosition> = await getVaultPositions(vaultSubaccounts);

Expand Down Expand Up @@ -286,9 +286,10 @@ router.get(
});

async function getVaultSubaccountPnlTicks(
vaultSubaccounts: VaultMapping,
resolution?: PnlTickInterval,
): Promise<PnlTicksFromDatabase[]> {
const vaultSubaccountIds: string[] = _.keys(getVaultSubaccountsFromConfig());
const vaultSubaccountIds: string[] = _.keys(vaultSubaccounts);
if (vaultSubaccountIds.length === 0) {
return [];
}
Expand Down Expand Up @@ -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<VaultMapping> {
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;
}),
);
}

Expand Down

0 comments on commit 9875783

Please sign in to comment.