-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_liquidation_table.ts
58 lines (48 loc) · 1.53 KB
/
create_liquidation_table.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { Client } from "https://deno.land/x/[email protected]/mod.ts";
import { load } from "https://deno.land/[email protected]/dotenv/mod.ts";
const env = await load();
const KEYS = [
`PG_USERNAME`,
`PG_PASSWORD`,
`PG_DATABASE`,
`PG_PORT`,
`PG_HOSTNAME`,
] as const;
type A = typeof KEYS[number];
type Environment = {
[key in A]: string;
};
const ENV = KEYS.reduce((acc, key) => {
if (!env[key]) {
throw new Error(`Environment variable ${key} is missing`);
}
acc[key] = env[key];
return acc;
}, {} as Environment);
const client = new Client({
user: ENV.PG_USERNAME,
database: ENV.PG_DATABASE,
hostname: ENV.PG_HOSTNAME,
port: ENV.PG_PORT,
password: ENV.PG_PASSWORD,
});
await client.connect();
await client.queryArray(`
CREATE TABLE IF NOT EXISTS liquidations (
block_timestamp bigint NOT NULL,
transaction_hash varchar(64) NOT NULL,
liquidated_user_address varchar(64) NOT NULL,
collateral_token varchar(10) NOT NULL,
collateral_token_amount decimal NOT NULL,
-- collateral_token_value
debt_token varchar(10) NOT NULL,
debt_token_amount decimal NOT NULL,
-- debt_token_value
_cursor bigint -- REQUIRED: Apibara requires the target table to have a _cursor bigint column. This column is used to track at which block a row is inserted to handle chain reorganizations.
);
`);
await client.queryArray(`
CREATE INDEX IF NOT EXISTS liquidations_block_timestamp_index ON liquidations (block_timestamp);
`);
await client.end();
console.log("Table liquidations created successfully");