Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nfa not financial advice #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 23 additions & 0 deletions src/routes/nfa/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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) {
const client = await fastify.pg.connect()
try {
const { rows } = await client.query(NFA_QUERY, [])
return rows
} finally {
client.release()
}
})
}

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
`