generated from well-known-components/template-server
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new dapps database connection (#118)
- Loading branch information
Showing
15 changed files
with
185 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
HTTP_SERVER_HOST=localhost | ||
|
||
FAVORITES_PG_COMPONENT_PSQL_CONNECTION_STRING=postgres://testuser:testpassword@localhost:5432/marketplace | ||
FAVORITES_PG_COMPONENT_PSQL_CONNECTION_STRING=postgres://marketplace_admin:marketplace_password@localhost:5432/marketplace | ||
FAVORITES_PG_COMPONENT_PSQL_SCHEMA=favorites | ||
|
||
DAPPS_PG_COMPONENT_PSQL_CONNECTION_STRING=postgres://dapps_admin:dapps_password@localhost:5433/dapps_test | ||
|
||
SNAPSHOT_URL=https://score.snapshot.org/ | ||
SNAPSHOT_NETWORK=11155111 | ||
SNAPSHOT_SPACE=daotest.dcl.eth | ||
SNAPSHOT_SPACE=daotest.dcl.eth |
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,23 @@ | ||
import { DBTrade } from '../../ports/trades' | ||
import { HTTPResponse, HandlerContextWithPath, StatusCode } from '../../types' | ||
|
||
export async function getTradesHandler( | ||
context: Pick<HandlerContextWithPath<'trades', '/v1/trades'>, 'components'> | ||
): Promise<HTTPResponse<{ data: DBTrade[]; count: number }>> { | ||
const { | ||
components: { trades } | ||
} = context | ||
|
||
const { data, count } = await trades.getTrades() | ||
|
||
return { | ||
status: StatusCode.OK, | ||
body: { | ||
ok: true, | ||
data: { | ||
data, | ||
count | ||
} | ||
} | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
src/migrations/dapps/1718991797670_trades-and-asset-trades-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,66 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate' | ||
|
||
export const SCHEMA = 'marketplace' | ||
|
||
export const TRADES_TABLE = `${SCHEMA}.trades` | ||
export const TRADE_ASSETS_TABLE = `${SCHEMA}.trade_assets` | ||
export const TRADE_TYPE = `${SCHEMA}.trade_type` | ||
export const ASSET_DIRECTION_TYPE = `${SCHEMA}.asset_direction_type` | ||
export const shorthands: ColumnDefinitions | undefined = undefined | ||
|
||
export async function up(pgm: MigrationBuilder): Promise<void> { | ||
pgm.createType(TRADE_TYPE, ['bid', 'public_order']) | ||
|
||
pgm.createTable(TRADES_TABLE, { | ||
id: { | ||
type: 'uuid', | ||
notNull: true, | ||
primaryKey: true, | ||
unique: true, | ||
default: pgm.func('public.uuid_generate_v4()') | ||
}, | ||
network: { type: 'text', notNull: true }, | ||
chain_id: { type: 'integer', notNull: true }, | ||
signature: { type: 'text', notNull: true, unique: true }, | ||
checks: { type: 'jsonb', notNull: true }, | ||
signer: { type: 'varchar(42)', notNull: true }, | ||
type: { type: `"${TRADE_TYPE}"`, notNull: true }, | ||
expires_at: { type: 'timestamp', notNull: true }, | ||
effective_since: { type: 'timestamp', notNull: true }, | ||
created_at: { type: 'timestamp', notNull: true, default: pgm.func('now()') } | ||
}) | ||
|
||
pgm.createType(ASSET_DIRECTION_TYPE, ['sent', 'received']) | ||
|
||
pgm.createTable(TRADE_ASSETS_TABLE, { | ||
id: { | ||
type: 'uuid', | ||
notNull: true, | ||
primaryKey: true, | ||
unique: true, | ||
default: pgm.func('public.uuid_generate_v4()') | ||
}, | ||
trade_id: { | ||
type: 'uuid', | ||
notNull: true, | ||
references: `"${TRADES_TABLE}"(id)`, | ||
onDelete: 'CASCADE' | ||
}, | ||
direction: { type: `"${ASSET_DIRECTION_TYPE}"`, notNull: true }, | ||
asset_type: { | ||
type: 'smallint' // (1: ERC20, 2: ERC721, 3: COLLECTION ITEM) | ||
}, | ||
contract_address: { type: 'varchar(42)', notNull: true }, | ||
value: { type: 'numeric(78, 0)', notNull: true }, | ||
beneficiary: { type: 'varchar(42)', notNull: true }, | ||
extra: { type: 'text', notNull: true }, | ||
created_at: { type: 'timestamp', notNull: true, default: pgm.func('now()') } | ||
}) | ||
} | ||
|
||
export async function down(pgm: MigrationBuilder): Promise<void> { | ||
pgm.dropTable(TRADES_TABLE) | ||
pgm.dropTable(TRADE_ASSETS_TABLE) | ||
pgm.dropType(ASSET_DIRECTION_TYPE) | ||
} |
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,16 @@ | ||
import SQL from 'sql-template-strings' | ||
import { AppComponents } from '../../types' | ||
import { DBTrade, ITradesComponent } from './types' | ||
|
||
export function createTradesComponent(components: Pick<AppComponents, 'dappsDatabase'>): ITradesComponent { | ||
const { dappsDatabase: pg } = components | ||
|
||
async function getTrades() { | ||
const result = await pg.query<DBTrade>(SQL`SELECT * FROM trades`) | ||
return { data: result.rows, count: result.rowCount } | ||
} | ||
|
||
return { | ||
getTrades | ||
} | ||
} |
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,2 @@ | ||
export * from './types' | ||
export * from './component' |
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,10 @@ | ||
export type ITradesComponent = { | ||
getTrades(): Promise<{ data: DBTrade[]; count: number }> | ||
} | ||
|
||
export type DBTrade = { | ||
signer: string | ||
id: string | ||
checks: Record<string, any> | ||
signature: string | ||
} |
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
File renamed without changes.
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,12 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL | ||
CREATE SCHEMA marketplace; | ||
-- CREATE THE ROLE AND ASSIGN IT TO THE PREVIOUSLY CREATED DB | ||
CREATE ROLE testuser WITH LOGIN PASSWORD 'testpassword'; | ||
GRANT ALL PRIVILEGES ON SCHEMA marketplace TO testuser; | ||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
EOSQL |