Skip to content

Commit

Permalink
fix some types and setup functions
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Jul 22, 2024
1 parent 77aebfe commit 7c22996
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
37 changes: 27 additions & 10 deletions api/tests/api/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ENV } from '../../src/env';
import { PgStore } from '../../src/pg/pg-store';
import { DbRune } from '../../src/pg/types';
import { DbLedgerEntry, DbRune } from '../../src/pg/types';
import {
dropDatabase,
insertDbEntry,
insertRune,
runMigrations,
startTestApiServer,
Expand Down Expand Up @@ -30,17 +31,29 @@ describe('Etchings', () => {
});

test('displays etched rune', async () => {

// '1:0', 0, 'UNCOMMONGOODS', 'UNCOMMON•GOODS',
// '0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5', 840000, 0, '', '⧉', 1,
// '340282366920938463463374607431768211455', 840000, 1050000, 0
const ledgerEntry: DbLedgerEntry = {
rune_id: '1:1',
block_hash: 'sample_block_hash',
block_height: '1',
tx_index: 0,
tx_id: '0',
output: 0,
address: '0',
receiver_address: '0',
amount: '0',
operation: 'etching',
timestamp: 0
};
const rune: DbRune = {
id: '1:1',
name: 'Sample Rune Name',
spaced_name: 'Sample•Rune•Name',
number: 1,
block_hash: 'sample_block_hash',
block_height: '0x1a',
block_height: '10',
tx_index: 0,
tx_id: 'sample_tx_id',
divisibility: 8,
Expand All @@ -61,16 +74,20 @@ describe('Etchings', () => {
total_operations: '2000',
timestamp: Date.now(),
};
// await insertRune(db, rune);
// const runes = await fastify.inject({
// method: 'GET',
// url: '/runes/v1/etchings/',
// });
await insertRune(db, rune);
const runes = await fastify.inject({
method: 'GET',
url: '/runes/v1/etchings',
});
expect(JSON.parse(runes.body).results.not.toHaveLength(0));
expect(runes.statusCode).toBe(200);
// TODO: ????
const event_index = 0;
await insertDbEntry(db, ledgerEntry, event_index);
// console.log(runes);
const etching = 'UNCOMMON GOODS'
const response = await fastify.inject({
method: 'GET',
url: '/runes/v1/etchings/' + etching,
url: '/runes/v1/etchings/' + ledgerEntry.rune_id,
});
expect(response.statusCode).toBe(200);
});
Expand Down
46 changes: 36 additions & 10 deletions api/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IncomingMessage, Server, ServerResponse } from 'http';
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import { buildApiServer } from '../src/api/init';
import { Rune } from '../src/api/schemas';
import { DbRune } from '../src/pg/types';
import { DbLedgerEntry, DbRune } from '../src/pg/types';

export type TestFastifyServer = FastifyInstance<
Server,
Expand Down Expand Up @@ -54,9 +54,40 @@ export async function dropDatabase(db: PgStore) {
});
}

export async function insertRune(db: PgStore, payload: DbRune): Promise<void> {
export async function insertDbEntry(
db: PgStore,
payload: DbLedgerEntry,
event_index: number
): Promise<void> {
await db.sqlWriteTransaction(async sql => {
const {
rune_id,
block_hash,
block_height,
tx_index,
tx_id,
output,
address,
receiver_address,
amount,
operation,
} = payload;

await sql`
INSERT INTO ledger (
rune_id, block_hash, block_height, tx_index, tx_id, output,
address, receiver_address, amount, operation, timestamp, event_index
)
VALUES (
${rune_id}, ${block_hash}, ${block_height}, ${tx_index}, ${tx_id}, ${output}, ${address}, ${receiver_address}, ${amount}, ${operation}, 0, ${event_index}
)
`;
});
}

export async function insertRune(db: PgStore, payload: DbRune): Promise<void> {
await db.sqlWriteTransaction(async sql => {
const {
id,
name,
Expand All @@ -74,9 +105,6 @@ export async function insertRune(db: PgStore, payload: DbRune): Promise<void> {
terms_height_end,
} = payload;

// Insert a new rune into the 'runes' table
// Ensure the column names and types match your database schema

// INSERT INTO runes (
// id, number, name, spaced_name, block_hash, block_height, tx_index, tx_id, symbol, terms_amount,
// terms_cap, terms_height_start, terms_height_end, timestamp
Expand All @@ -91,11 +119,9 @@ export async function insertRune(db: PgStore, payload: DbRune): Promise<void> {
)
VALUES (
${id}, ${number}, ${sql(name)}, ${sql(spaced_name)}, ${sql(block_hash)}, ${sql(
block_height
)}, ${tx_index}, ${sql(tx_id)}, ${sql(symbol)}, ${cenotaph}, ${sql(terms_amount || '')}, ${sql(
terms_cap || ''
)}, ${terms_height_start}, ${terms_height_end}, NOW()
${id}, ${number}, ${name}, ${spaced_name}, ${block_hash}, ${block_height}, ${tx_index}, ${tx_id}, ${symbol}, ${cenotaph}, ${
terms_amount || ''
}, ${terms_cap || ''}, ${terms_height_start}, ${terms_height_end}, 0
)
`;
});
Expand Down

0 comments on commit 7c22996

Please sign in to comment.