-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into affiliates-comlink-affiliate-info
- Loading branch information
Showing
180 changed files
with
4,446 additions
and
921 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
indexer/packages/postgres/__tests__/stores/vault-table.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as VaultTable from '../../src/stores/vault-table'; | ||
import { | ||
clearData, | ||
migrate, | ||
teardown, | ||
} from '../../src/helpers/db-helpers'; | ||
import { defaultVault, defaultAddress } from '../helpers/constants'; | ||
import { VaultFromDatabase, VaultStatus } from '../../src/types'; | ||
|
||
describe('Vault store', () => { | ||
beforeAll(async () => { | ||
await migrate(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await clearData(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await teardown(); | ||
}); | ||
|
||
it('Successfully creates a vault', async () => { | ||
await VaultTable.create(defaultVault); | ||
}); | ||
|
||
it('Successfully finds all vaults', async () => { | ||
await Promise.all([ | ||
VaultTable.create(defaultVault), | ||
VaultTable.create({ | ||
...defaultVault, | ||
address: defaultAddress, | ||
clobPairId: '1', | ||
}), | ||
]); | ||
|
||
const vaults: VaultFromDatabase[] = await VaultTable.findAll( | ||
{}, | ||
[], | ||
{ readReplica: true }, | ||
); | ||
|
||
expect(vaults.length).toEqual(2); | ||
expect(vaults[0]).toEqual(expect.objectContaining(defaultVault)); | ||
expect(vaults[1]).toEqual(expect.objectContaining({ | ||
...defaultVault, | ||
address: defaultAddress, | ||
clobPairId: '1', | ||
})); | ||
}); | ||
|
||
it('Succesfully upserts a vault', async () => { | ||
await VaultTable.create(defaultVault); | ||
|
||
let vaults: VaultFromDatabase[] = await VaultTable.findAll( | ||
{}, | ||
[], | ||
{ readReplica: true }, | ||
); | ||
|
||
expect(vaults.length).toEqual(1); | ||
expect(vaults[0]).toEqual(expect.objectContaining(defaultVault)); | ||
|
||
await VaultTable.upsert({ | ||
...defaultVault, | ||
status: VaultStatus.CLOSE_ONLY, | ||
}); | ||
|
||
vaults = await VaultTable.findAll( | ||
{}, | ||
[], | ||
{ readReplica: true }, | ||
); | ||
|
||
expect(vaults.length).toEqual(1); | ||
expect(vaults[0]).toEqual(expect.objectContaining({ | ||
...defaultVault, | ||
status: VaultStatus.CLOSE_ONLY, | ||
})); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...packages/postgres/src/db/migrations/migration_files/20240912180829_create_vaults_table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as Knex from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return knex.schema.createTable('vaults', (table) => { | ||
table.string('address').primary().notNullable(); // address of vault | ||
table.bigInteger('clobPairId').notNullable(); // clob pair id for vault | ||
table.enum('status', [ | ||
'DEACTIVATED', | ||
'STAND_BY', | ||
'QUOTING', | ||
'CLOSE_ONLY', | ||
]).notNullable(); // quoting status of vault | ||
table.timestamp('createdAt').notNullable(); | ||
table.timestamp('updatedAt').notNullable(); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema.dropTable('vaults'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { IntegerPattern } from '../lib/validators'; | ||
import { IsoString, VaultStatus } from '../types'; | ||
import BaseModel from './base-model'; | ||
|
||
export default class VaultModel extends BaseModel { | ||
|
||
static get tableName() { | ||
return 'vaults'; | ||
} | ||
|
||
static get idColumn() { | ||
return ['address']; | ||
} | ||
|
||
static get jsonSchema() { | ||
return { | ||
type: 'object', | ||
required: [ | ||
'address', | ||
'clobPairId', | ||
'status', | ||
'createdAt', | ||
'updatedAt', | ||
], | ||
properties: { | ||
address: { type: 'string' }, | ||
clobPairId: { type: 'string', pattern: IntegerPattern }, | ||
status: { type: 'string' }, | ||
createdAt: { type: 'string', format: 'date-time' }, | ||
updatedAt: { type: 'string', format: 'date-time' }, | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* A mapping from column name to JSON conversion expected. | ||
* See getSqlConversionForDydxModelTypes for valid conversions. | ||
* | ||
* TODO(IND-239): Ensure that jsonSchema() / sqlToJsonConversions() / model fields match. | ||
*/ | ||
static get sqlToJsonConversions() { | ||
return { | ||
address: 'string', | ||
clobPairId: 'string', | ||
status: 'string', | ||
createdAt: 'date-time', | ||
updatedAt: 'date-time', | ||
}; | ||
} | ||
|
||
address!: string; | ||
|
||
clobPairId!: string; | ||
|
||
status!: VaultStatus; | ||
|
||
createdAt!: IsoString; | ||
|
||
updatedAt!: IsoString; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { QueryBuilder } from 'objection'; | ||
|
||
import { DEFAULT_POSTGRES_OPTIONS } from '../constants'; | ||
import { | ||
verifyAllRequiredFields, | ||
setupBaseQuery, | ||
} from '../helpers/stores-helpers'; | ||
import Transaction from '../helpers/transaction'; | ||
import VaultModel from '../models/vault-model'; | ||
import { | ||
QueryConfig, | ||
VaultQueryConfig, | ||
VaultColumns, | ||
Options, | ||
Ordering, | ||
QueryableField, | ||
VaultFromDatabase, | ||
VaultCreateObject, | ||
} from '../types'; | ||
|
||
export async function findAll( | ||
{ | ||
address, | ||
clobPairId, | ||
status, | ||
limit, | ||
}: VaultQueryConfig, | ||
requiredFields: QueryableField[], | ||
options: Options = DEFAULT_POSTGRES_OPTIONS, | ||
): Promise<VaultFromDatabase[]> { | ||
verifyAllRequiredFields( | ||
{ | ||
address, | ||
clobPairId, | ||
status, | ||
limit, | ||
} as QueryConfig, | ||
requiredFields, | ||
); | ||
|
||
let baseQuery: QueryBuilder<VaultModel> = setupBaseQuery<VaultModel>( | ||
VaultModel, | ||
options, | ||
); | ||
|
||
if (address) { | ||
baseQuery = baseQuery.whereIn(VaultColumns.address, address); | ||
} | ||
|
||
if (clobPairId) { | ||
baseQuery = baseQuery.whereIn(VaultColumns.clobPairId, clobPairId); | ||
} | ||
|
||
if (status) { | ||
baseQuery = baseQuery.whereIn(VaultColumns.status, status); | ||
} | ||
|
||
if (options.orderBy !== undefined) { | ||
for (const [column, order] of options.orderBy) { | ||
baseQuery = baseQuery.orderBy( | ||
column, | ||
order, | ||
); | ||
} | ||
} else { | ||
baseQuery = baseQuery.orderBy( | ||
VaultColumns.clobPairId, | ||
Ordering.ASC, | ||
); | ||
} | ||
|
||
if (limit) { | ||
baseQuery = baseQuery.limit(limit); | ||
} | ||
|
||
return baseQuery.returning('*'); | ||
} | ||
|
||
export async function create( | ||
vaultToCreate: VaultCreateObject, | ||
options: Options = { txId: undefined }, | ||
): Promise<VaultFromDatabase> { | ||
return VaultModel.query( | ||
Transaction.get(options.txId), | ||
).insert({ | ||
...vaultToCreate, | ||
}); | ||
} | ||
|
||
export async function upsert( | ||
vaultToUpsert: VaultCreateObject, | ||
options: Options = { txId: undefined }, | ||
): Promise<VaultFromDatabase> { | ||
const vaults: VaultModel[] = await VaultModel.query( | ||
Transaction.get(options.txId), | ||
).upsert({ | ||
...vaultToUpsert, | ||
}).returning('*'); | ||
return vaults[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { IsoString } from './utility-types'; | ||
|
||
export interface VaultCreateObject { | ||
address: string, | ||
clobPairId: string, | ||
status: VaultStatus, | ||
createdAt: IsoString, | ||
updatedAt: IsoString, | ||
} | ||
|
||
export enum VaultStatus { | ||
DEACTIVATED = 'DEACTIVATED', | ||
STAND_BY = 'STAND_BY', | ||
QUOTING = 'QUOTING', | ||
CLOSE_ONLY = 'CLOSE_ONLY', | ||
} | ||
|
||
export enum VaultColumns { | ||
address = 'address', | ||
clobPairId = 'clobPairId', | ||
status = 'status', | ||
createdAt = 'createdAt', | ||
updatedAt = 'updatedAt', | ||
} |
Oops, something went wrong.