Skip to content

Commit 2ee397f

Browse files
authored
migration (DefiLlama#295)
* migration * remove balancer pools from exclusion list * bug fix serverless * remove timestamp from median, update for tests * update get distinct id controller * update distinctID controller, remove dep * move confirm into scripts * testing url * remove Pg suffix from lambda names * change to existing api host * add comments * revert service name, bucketsg * put pack fantom rpc
1 parent 021f41c commit 2ee397f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1975
-1831
lines changed

.github/workflows/master.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
env:
2323
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
2424
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
25+
DATABASE_URL: ${{ secrets.DATABASE_URL }}
2526
INFURA_CONNECTION: ${{ secrets.INFURA_CONNECTION }}
2627
ALCHEMY_CONNECTION_POLYGON: ${{ secrets.ALCHEMY_CONNECTION_POLYGON }}
2728
ALCHEMY_CONNECTION_ARBITRUM: ${{ secrets.ALCHEMY_CONNECTION_ARBITRUM }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ ccImages
88
.idea/
99
*output.json
1010
*.csv
11+
*.sql
12+
scripts/*.json

beforeTests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ module.exports = async function () {
3535
global.uniquePoolIdentifiersDB = new Set(
3636
(
3737
await axios.get(
38-
'https://1rwmj4tky9.execute-api.eu-central-1.amazonaws.com/simplePools'
38+
'https://1rwmj4tky9.execute-api.eu-central-1.amazonaws.com/distinctID'
3939
)
40-
).data.data
40+
).data
4141
.filter((p) => p.project !== global.apy[0].project)
4242
.map((p) => p.pool)
4343
);

env.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ module.exports = {
1919
.readdirSync('./src/adaptors')
2020
.filter((el) => !el.includes('js') && el !== '.DS_Store')
2121
),
22+
// DB
23+
DATABASE_URL: process.env.DATABASE_URL,
2224
};

migrations/1661488110733_init.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
const { PgLiteral } = require('node-pg-migrate');
2+
3+
exports.up = (pgm) => {
4+
// ----- ADD UUID EXTENSION
5+
pgm.createExtension('uuid-ossp', {
6+
ifNotExists: true,
7+
});
8+
// ----- CREATE TABLES
9+
// --- config
10+
// table with static/semi-static information and consists of 1 row per unique pool.
11+
// operations on this table: insert for new pools, update for existing pools
12+
pgm.createTable('config', {
13+
config_id: {
14+
type: 'uuid', // uuid is created in the application
15+
primaryKey: true,
16+
},
17+
updated_at: {
18+
type: 'timestamptz',
19+
notNull: true,
20+
default: pgm.func('current_timestamp'),
21+
},
22+
pool: { type: 'text', notNull: true, unique: true },
23+
project: { type: 'text', notNull: true },
24+
chain: { type: 'text', notNull: true },
25+
symbol: { type: 'text', notNull: true },
26+
poolMeta: 'text',
27+
underlyingTokens: { type: 'text[]' },
28+
rewardTokens: { type: 'text[]' },
29+
url: { type: 'text', notNull: true },
30+
});
31+
32+
// --- yield
33+
// our timeseries table. insert only on hourly granularity
34+
pgm.createTable('yield', {
35+
yield_id: {
36+
type: 'uuid',
37+
default: new PgLiteral('uuid_generate_v4()'),
38+
primaryKey: true,
39+
},
40+
// configID is a FK in this table and references the PK (config_id) in config
41+
configID: {
42+
type: 'uuid',
43+
notNull: true,
44+
references: '"config"',
45+
onDelete: 'cascade',
46+
},
47+
timestamp: {
48+
type: 'timestamptz',
49+
notNull: true,
50+
},
51+
tvlUsd: { type: 'bigint', notNull: true },
52+
apy: { type: 'numeric', notNull: true },
53+
apyBase: 'numeric',
54+
apyReward: 'numeric',
55+
});
56+
57+
// --- stat
58+
// table which contains rolling statistics required to calculate ML features values
59+
// and other things we use for plotting on the /overview page
60+
pgm.createTable('stat', {
61+
stat_id: {
62+
type: 'uuid',
63+
default: new PgLiteral('uuid_generate_v4()'),
64+
primaryKey: true,
65+
},
66+
// configID is a FK in this table and references the PK (config_id) in config
67+
configID: {
68+
type: 'uuid',
69+
notNull: true,
70+
references: '"config"',
71+
unique: true,
72+
onDelete: 'cascade',
73+
},
74+
updated_at: {
75+
type: 'timestamptz',
76+
notNull: true,
77+
default: pgm.func('current_timestamp'),
78+
},
79+
count: { type: 'smallint', notNull: true },
80+
meanAPY: { type: 'numeric', notNull: true },
81+
mean2APY: { type: 'numeric', notNull: true },
82+
meanDR: { type: 'numeric', notNull: true },
83+
mean2DR: { type: 'numeric', notNull: true },
84+
productDR: { type: 'numeric', notNull: true },
85+
});
86+
87+
// --- median
88+
// median table content is used for the median chart on /overview (append only)
89+
pgm.createTable('median', {
90+
median_id: {
91+
type: 'uuid',
92+
default: new PgLiteral('uuid_generate_v4()'),
93+
primaryKey: true,
94+
},
95+
timestamp: {
96+
type: 'timestamptz',
97+
notNull: true,
98+
unique: true,
99+
},
100+
uniquePools: { type: 'integer', notNull: true },
101+
medianAPY: { type: 'numeric', notNull: true },
102+
});
103+
104+
// ----- FUNCTION
105+
// for creating the updated_at timestamp field
106+
pgm.createFunction(
107+
'update_updated_at',
108+
[], // no params
109+
// options
110+
{
111+
language: 'plpgsql',
112+
returns: 'TRIGGER',
113+
replace: true,
114+
},
115+
// function body
116+
`
117+
BEGIN
118+
NEW.updated_at = now();
119+
RETURN NEW;
120+
END
121+
`
122+
);
123+
124+
// ----- TRIGGERS;
125+
// to trigger the defined function
126+
pgm.createTrigger('config', 'update_updated_at', {
127+
when: 'BEFORE',
128+
operation: 'UPDATE',
129+
function: 'update_updated_at',
130+
level: 'ROW',
131+
});
132+
pgm.createTrigger('stat', 'update_updated_at', {
133+
when: 'BEFORE',
134+
operation: 'UPDATE',
135+
function: 'update_updated_at',
136+
level: 'ROW',
137+
});
138+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
exports.up = (pgm) => {
2+
// composite index for yield;
3+
// added after ingestion of historical data
4+
pgm.createIndex('yield', [
5+
{ name: 'configID', sort: 'ASC' },
6+
{ name: 'timestamp', sort: 'DESC' },
7+
]);
8+
};

0 commit comments

Comments
 (0)