Skip to content

Commit

Permalink
add lsd trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
slasher125 committed Jun 7, 2023
1 parent e26a9f2 commit 945ab3f
Show file tree
Hide file tree
Showing 3 changed files with 412 additions and 0 deletions.
12 changes: 12 additions & 0 deletions migrations/lsd.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE IF NOT EXISTS lsd (
timestamp timestamp NOT NULL,
name text NOT NULL,
symbol text NOT NULL,
address text NOT NULL,
type text,
"expectedRate" numeric,
"marketRate" numeric,
"ethPeg" numeric
);

CREATE INDEX IF NOT EXISTS lsd_address_timestamp_idx ON lsd (address, timestamp DESC);
57 changes: 57 additions & 0 deletions src/controllers/lsdController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const minify = require('pg-minify');

const { pgp, connect } = require('../utils/dbConnection');

const getLsd = async () => {
const conn = await connect();

const query = minify(
`
SELECT
DISTINCT ON (address) *
FROM
lsd
ORDER BY
address,
timestamp DESC
`,
{ compress: true }
);

const response = await conn.query(query);

if (!response) {
return new AppError(`Couldn't get data`, 404);
}

return response;
};

const insertLsd = async (payload) => {
const conn = await connect();

const columns = [
'timestamp',
'name',
'symbol',
'address',
{ name: 'type', def: null },
{ name: 'expectedRate', def: null },
{ name: 'marketRate', def: null },
{ name: 'ethPeg', def: null },
];
const cs = new pgp.helpers.ColumnSet(columns, { table: 'lsd' });
const query = pgp.helpers.insert(payload, cs);
const response = await conn.result(query);

if (!response) {
return new AppError(`Couldn't insert/update data`, 404);
}

return response;
};

module.exports = {
getLsd,
insertLsd,
};
Loading

0 comments on commit 945ab3f

Please sign in to comment.