Skip to content

Commit

Permalink
[IND-350] deprecate seeding from genesis (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill authored Aug 25, 2023
1 parent 42fee77 commit 06f31ac
Show file tree
Hide file tree
Showing 20 changed files with 27 additions and 3,230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
DB_PORT: 5432
NODE_ENV: test
run: |
cd packages/postgres && pnpm run migrate && pnpm run seed
cd packages/postgres && pnpm run migrate
- name: Run docker image
id: run-image
Expand Down
2 changes: 1 addition & 1 deletion indexer/Dockerfile.postgres-package.local
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ RUN pnpm i --loglevel warn --production --frozen-lockfile

WORKDIR /home/dydx/app/packages/postgres

CMD ["sh", "-c", "pnpm run migrate && pnpm run seed"]
CMD ["sh", "-c", "pnpm run migrate"]
26 changes: 1 addition & 25 deletions indexer/packages/postgres/README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
# Postgres

Postgres package holds all postgres knex migrations, seeders, postgres models and helper functions.

## Seeder for the database

There are seed files for seeding a postgres database for the Indexer using data from the
`genesis.json` for the V4 network that the Indexer will be connected to.

The seed file will generate the initial rows in the `perpetual_markets` table using `Perpetual` and
`ClobPair` objects that exist in the `genesis.json` file.

To set the `genesis.json` file to use, copy it into the path `src/db/seeds`.

The `genesis.json` file for a local V4 node can be copied from `~/.dydxprotocol/config`.

### Running the seeder

On a machine with access to the database instance for the Indexer, update `.env` with the required
environment variables for accessing the database instance. (See `.env.test` for an example of the
required values).

Then run:

```
pnpm run build && pnpm run seed
```
Postgres package holds all postgres knex migrations, postgres models and helper functions.

## Knex migration
Add a knex migration by running `pnpm run migrate:make <create_fake_table>`
Expand Down
108 changes: 2 additions & 106 deletions indexer/packages/postgres/__tests__/db/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
import {
AssetCreateObject,
MarketColumns,
MarketFromDatabase,
MarketsMap,
PerpetualPositionFromDatabase,
PositionSide,
SubaccountFromDatabase,
} from '../../src/types';
import { getMaintenanceMarginPpm, getUnrealizedPnl, getUnsettledFunding } from '../../src/db/helpers';
import {
AssetPositionCreateObjectWithId,
getAssetCreateObject,
getAssetPositionCreateObject,
getMaintenanceMarginPpm,
getUnrealizedPnl,
getUnsettledFunding,
SubaccountCreateObjectWithId,
} from '../../src/db/helpers';
import {
Asset, AssetPosition, MarketParam, MarketPrice,
} from '@dydxprotocol-indexer/v4-protos';
import { bigIntToBytes } from '@dydxprotocol-indexer/v4-proto-parser';
import {
expectAsset, expectAssetPositionCreateObject, expectMarketParamAndPrice, expectSubaccount,
} from './helpers';
import {
createdDateTime,
createdHeight,
defaultAddress,
defaultFundingIndexUpdate,
defaultPerpetualMarket,
defaultPerpetualPosition,
defaultPerpetualPositionId,
defaultSubaccount,
defaultSubaccountId,
} from '../helpers/constants';
import * as SubaccountTable from '../../src/stores/subaccount-table';
import * as PerpetualPositionTable from '../../src/stores/perpetual-position-table';
import * as MarketTable from '../../src/stores/market-table';
import { CURRENCY_DECIMAL_PRECISION, USDC_DENOM, USDC_SYMBOL } from '../../src';
import Long from 'long';
import { CURRENCY_DECIMAL_PRECISION } from '../../src';
import Big from 'big.js';
import { seedData } from '../helpers/mock-generators';
import _ from 'lodash';
Expand All @@ -50,59 +26,6 @@ describe('helpers', () => {
await clearData();
});

describe('getAssetCreateObjects', () => {
const defaultAsset: Asset = {
id: 0,
symbol: USDC_SYMBOL,
denom: USDC_DENOM,
denomExponent: -6,
atomicResolution: -2,
hasMarket: false,
marketId: 0,
longInterest: Long.fromValue(1000),
};

it('create asset object from asset proto', () => {
const assetCreateObject: AssetCreateObject = getAssetCreateObject(defaultAsset);
expectAsset(assetCreateObject, defaultAsset);
});
});

describe('getAssetPositionCreateObjects', () => {
const atomicResolution: number = 2;
const defaultAssetPosition: AssetPosition = {
assetId: 0,
quantums: bigIntToBytes(BigInt(1000)),
index: Long.fromValue(0),
};

it('create asset position object from AssetPosition proto', () => {
const assetPositionCreateObject:
AssetPositionCreateObjectWithId = getAssetPositionCreateObject(
defaultSubaccountId, defaultAssetPosition, atomicResolution);
expectAssetPositionCreateObject(
assetPositionCreateObject, defaultAssetPosition, atomicResolution);
});
});

describe('expectSubaccount', () => {
const subaccount: SubaccountFromDatabase = {
id: defaultSubaccountId,
address: defaultAddress,
subaccountNumber: 0,
updatedAt: createdDateTime.toISO(),
updatedAtHeight: createdHeight,
};
const subaccountCreateObject: SubaccountCreateObjectWithId = {
id: SubaccountTable.uuid(defaultSubaccount.address, defaultSubaccount.subaccountNumber),
...defaultSubaccount,
};

it('expect subaccount', () => {
expectSubaccount(subaccount, subaccountCreateObject);
});
});

describe('getUnsettledFunding', () => {
const position: PerpetualPositionFromDatabase = {
...defaultPerpetualPosition,
Expand Down Expand Up @@ -210,33 +133,6 @@ describe('helpers', () => {
});
});

describe('expectMarketParamAndPrice', () => {

it('expect market', () => {
const marketFromDb: MarketFromDatabase = {
id: 0,
pair: 'BTC-USD',
exponent: -5,
minPriceChangePpm: 50,
oraclePrice: '50000',
};
const marketParam: MarketParam = {
id: 0,
pair: 'BTC-USD',
exponent: -5,
exchangeConfigJson: '{exchanges:[{"exchangeName":"Binance","ticker":"BTCUSDT"},{"exchangeName":"BinanceUS","ticker":"BTCUSD"}]}',
minExchanges: 1,
minPriceChangePpm: 50,
};
const marketPrice: MarketPrice = {
id: 0,
exponent: -5,
price: Long.fromValue(5_000_000_000),
};
expectMarketParamAndPrice(marketFromDb, marketParam, marketPrice);
});
});

describe('getMaintenanceMarginPpm', () => {
it('5% initial margin, 60% maintenance fraction', () => {
expect(getMaintenanceMarginPpm(50_000, 600_000)).toEqual(30_000);
Expand Down
80 changes: 0 additions & 80 deletions indexer/packages/postgres/__tests__/db/helpers.ts

This file was deleted.

44 changes: 0 additions & 44 deletions indexer/packages/postgres/__tests__/db/seeds/seed.test.ts

This file was deleted.

6 changes: 0 additions & 6 deletions indexer/packages/postgres/knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ module.exports = {
migrations: {
directory: './src/db/migrations/migration_files/',
},
seeds: {
loadExtensions: ['.js'],
// needs to load files from build/ since src/db/seeds/helpers.ts has dependencies
// and only build directory copied in Dockerfile
directory: './build/src/db/seeds',
},
client: 'pg',
connection: {
host: process.env.DB_HOSTNAME,
Expand Down
3 changes: 1 addition & 2 deletions indexer/packages/postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"build": "rm -rf build/ && tsc",
"build:prod": "pnpm run build",
"build:watch": "pnpm run build -- --watch",
"test": "NODE_ENV=test jest --runInBand --forceExit",
"seed": "knex seed:run"
"test": "NODE_ENV=test jest --runInBand --forceExit"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 06f31ac

Please sign in to comment.