Skip to content

Commit

Permalink
feat: nfa not financial advice
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Mar 12, 2024
1 parent d1b7770 commit 2bfdc87
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 2 deletions.
162 changes: 161 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
"@fastify/cors": "^8.2.1",
"@fastify/env": "^4.2.0",
"@fastify/http-proxy": "^9.0.0",
"@fastify/postgres": "^5.2.2",
"@fastify/sensible": "^5.0.0",
"fastify": "^4.0.0",
"fastify-cli": "^5.7.1",
"fastify-plugin": "^4.0.0"
"fastify-plugin": "^4.0.0",
"pg": "^8.11.3"
},
"devDependencies": {
"@types/jest": "^29.5.0",
Expand Down
15 changes: 15 additions & 0 deletions src/plugins/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ const schema = {
PROXY_UPSTREAM: {
type: "string",
},
NFA_DB_HOST: {
type: "string",
},
NFA_DB_PORT: {
type: "string",
},
NFA_DB_LOGIN: {
type: "string",
},
NFA_DB_PASS: {
type: "string",
},
NFA_DB_NAME: {
type: "string",
},
},
};

Expand Down
29 changes: 29 additions & 0 deletions src/routes/nfa/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { FastifyPluginAsync } from 'fastify'
import fastifyPostgres from '@fastify/postgres'
import {NFA_QUERY} from './query'

const nfa: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
const {NFA_DB_HOST, NFA_DB_PORT, NFA_DB_LOGIN, NFA_DB_PASS, NFA_DB_NAME} = fastify.config

fastify.register(fastifyPostgres, {
connectionString: `postgres://${NFA_DB_LOGIN}:${NFA_DB_PASS}@${NFA_DB_HOST}:${NFA_DB_PORT}/${NFA_DB_NAME}`
})

fastify.get('/', async function (request, reply) {
fastify.pg.connect(onConnect)

function onConnect(err: Error, client: any, release: any) {
if (err) return reply.send(err)

client.query(
NFA_QUERY, [],
function onResult(err: Error, result: any) {
release()
reply.send(err || result)
}
)
}
})
}

export default nfa;
118 changes: 118 additions & 0 deletions src/routes/nfa/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
export const NFA_QUERY = `
with total_users as (select count(distinct encode(owner, 'hex')) as cnt_users,
count(*) as cnt_orders
from orders
where creation_timestamp >= now() - interval '1' hour)
-- SHOW THE LARGEST NUMBER OUT OF TWO
-- if users buy usdc/dai or usdt then retunr next row
-------------------------
-- most purchased token in the past hour by orders
-- example output: 36% of the orders in the past hour were to buy ETH
, top_buy_orders as (select 'buy order' as message,
case
when encode(buy_token, 'hex') = 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' or
encode(buy_token, 'hex') = 'c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
then 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
else encode(buy_token, 'hex') end
as token,
cnt_orders as group_col,
count(*) as order_col,
count(*) / cast(cnt_orders as float) as percent,
RANK() OVER (ORDER BY count(*) desc) rank_number
from orders o,
total_users
where creation_timestamp >= now() - interval '1' hour
and encode(buy_token, 'hex') not in ('a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
'6b175474e89094c44da98b954eedeac495271d0f',
'dac17f958d2ee523a2206206994597c13d831ec7')
group by 1, 2, 3
order by 4 desc
limit 5
)
-- most users (x %) prefered x token
-- example output: 22% of all users in the past hour bought ETH
, top_buy_traders as (
select
'buy users' as message, case when encode(buy_token, 'hex')='eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' or
encode(buy_token, 'hex')='c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' then 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
else encode(buy_token, 'hex') end
as token, cnt_users as group_col, count (distinct encode(owner, 'hex')) as order_col, count (distinct encode(owner, 'hex'))/ cast (cnt_users as float) as percent, RANK() OVER (ORDER BY count (distinct encode(owner, 'hex')) desc) rank_number
from orders o, total_users
where creation_timestamp >= now() - interval '1' hour
and encode(buy_token
, 'hex') not in ('a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
, '6b175474e89094c44da98b954eedeac495271d0f'
, 'dac17f958d2ee523a2206206994597c13d831ec7')
group by 1, 2, 3
order by 4 desc
limit 5
)
-- most sold token in the past hour
, top_sell_orders as (
select
'sell order' as message, case when encode(sell_token, 'hex')='eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' or
encode(sell_token, 'hex')='c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' then 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
else encode(sell_token, 'hex') end
as token, cnt_orders as group_col, count (*) as order_col, count (*)/ cast (cnt_orders as float) as percent, RANK() OVER (ORDER BY count (*) desc) rank_number
from orders o, total_users
where creation_timestamp >= now() - interval '1' hour
and encode(sell_token
, 'hex') not in ('a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
, '6b175474e89094c44da98b954eedeac495271d0f'
, 'dac17f958d2ee523a2206206994597c13d831ec7')
group by 1, 2, 3
order by 4 desc
limit 5
)
-- most users (x %) prefered x token
-- example output: 22% of all users in the past hour bought ETH
, top_sell_traders as (
select
'sell users' as message, case when encode(sell_token, 'hex')='eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' or
encode(sell_token, 'hex')='c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' then 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
else encode(sell_token, 'hex') end
as token, cnt_users as group_col, count (distinct encode(owner, 'hex')) as order_col, count (distinct encode(owner, 'hex'))/ cast (cnt_users as float) as percent, RANK() OVER (ORDER BY count (distinct encode(owner, 'hex')) desc) rank_number
from orders o, total_users
where creation_timestamp >= now() - interval '1' hour
and encode(sell_token
, 'hex') not in ('a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
, '6b175474e89094c44da98b954eedeac495271d0f'
, 'dac17f958d2ee523a2206206994597c13d831ec7')
group by 1, 2, 3
order by 4 desc
limit 5
)
select message, token, percent, rank_number
from top_buy_orders
union all
select message, token, percent, rank_number
from top_buy_traders
union all
select message, token, percent, rank_number
from top_sell_orders
union all
select message, token, percent, rank_number
from top_sell_traders
order by 1, 3 desc
`

0 comments on commit 2bfdc87

Please sign in to comment.