Skip to content

Commit

Permalink
fix unicryt
Browse files Browse the repository at this point in the history
  • Loading branch information
dtmkeng committed Jul 26, 2023
1 parent 950864b commit 6c5f5ee
Showing 1 changed file with 25 additions and 34 deletions.
59 changes: 25 additions & 34 deletions fees/unicrypt.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Chain } from "@defillama/sdk/build/general";
import { Adapter, FetchResultFees } from "../adapters/types";
import { CHAIN } from "../helpers/chains";
import { getBlock } from "../helpers/getBlock";
import { getPrices } from "../utils/prices";
import { queryFlipside } from "../helpers/flipsidecrypto";
import postgres from "postgres";

interface IFee {
contract_address: string;
volume: number;
}

interface ILog {
data: string;
contract_address: string;
}
type TPrice = {
[s: string]: {
price: number;
Expand All @@ -25,50 +28,37 @@ const tokens: IToken = {

const fetch = (chain: Chain) => {
return async (timestamp: number): Promise<FetchResultFees> => {

const fromTimestamp = timestamp - 60 * 60 * 24
const toTimestamp = timestamp
const now = new Date(timestamp * 1e3)
const dayAgo = new Date(now.getTime() - 1000 * 60 * 60 * 24)
const sql = postgres(process.env.INDEXA_DB!);
try {

const startblock = (await getBlock(fromTimestamp, chain, {}));
const endblock = (await getBlock(toTimestamp, chain, {}));


const query =`
const transfer_logs: ILog[] = await sql`
SELECT
data,
contract_address,
tx_hash
from
${chain}.core.fact_event_logs
encode(data, 'hex') AS data,
encode(contract_address, 'hex') as contract_address
FROM
ethereum.event_logs
WHERE
BLOCK_NUMBER > ${startblock} AND BLOCK_NUMBER < ${endblock}
and contract_address in (${tokens[chain].map((a: string) => `'${a.toLowerCase()}'`).join(',')})
and topics[0] = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
and topics[2] = '0x00000000000000000000000004bda42de3bc32abb00df46004204424d4cf8287'
block_number > 15454990
AND contract_address in ('\\xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', '\\xdac17f958d2ee523a2206206994597c13d831ec7')
AND topic_0 = '\\xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
AND topic_2 = '\\x00000000000000000000000004bda42de3bc32abb00df46004204424d4cf8287'
AND block_time BETWEEN ${dayAgo.toISOString()} AND ${now.toISOString()};
`;
// 0xcdd1b25d - replay
// 0x3593564c - ex


const logs: [string, string][] = (await queryFlipside(query))

const log = logs.map(([data, contract_address]: [string, string]) => {
const volume = Number(data)
const log = transfer_logs.map((a: ILog) => {
const volume = Number('0x'+a.data)
return {
volume: volume,
contract_address: contract_address,
contract_address: '0x'+a.contract_address,
} as IFee
});

const coins = [...new Set(log.map((e: IFee) => `${chain}:${e.contract_address}`.toLowerCase()))]
const coins_split = [];
for(let i = 0; i <= coins.length; i+=100) {
coins_split.push(coins.slice(i, i + 100))
}
const prices_result: any = (await Promise.all(coins_split.map((a: string[]) => getPrices(a, timestamp)))).flat().flat().flat();
const prices: TPrice = Object.assign({}, {});
prices_result.map((a: any) => Object.assign(prices, a))
const prices: any = (await getPrices(coins, timestamp))

const amounts = log.map((p: IFee) => {
const price = prices[`${chain}:${p.contract_address}`.toLowerCase()]?.price || 0;
Expand All @@ -80,7 +70,7 @@ const fetch = (chain: Chain) => {
.filter((e: number) => e < 100_000_000)
.reduce((a: number, b: number) => a+b, 0);
const dailyFees = volume;

await sql.end({ timeout: 3 })
return {
timestamp: timestamp,
dailyFees: dailyFees.toString(),
Expand All @@ -89,6 +79,7 @@ const fetch = (chain: Chain) => {
} as FetchResultFees

} catch (error) {
await sql.end({ timeout: 3 })
throw error
}
}
Expand All @@ -98,7 +89,7 @@ const adapter: Adapter = {
adapter: {
[CHAIN.ETHEREUM]: {
fetch: fetch(CHAIN.ETHEREUM),
start: async () => 1672531200,
start: async () => 1661990400,
}
},

Expand Down

0 comments on commit 6c5f5ee

Please sign in to comment.