Skip to content

Commit

Permalink
Adding more data
Browse files Browse the repository at this point in the history
	deleted:    db/migrations/1693282513188-Data.js
	new file:   db/migrations/1693492520799-Data.js
	modified:   schema.graphql
	modified:   src/main.ts
	new file:   src/model/generated/blockStats.model.ts
	modified:   src/model/generated/index.ts
	new file:   src/model/generated/subject.model.ts
	deleted:    src/model/generated/volumeByBlock.model.ts
  • Loading branch information
abernatskiy committed Aug 31, 2023
1 parent 6da95b8 commit 254c459
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 56 deletions.
23 changes: 0 additions & 23 deletions db/migrations/1693282513188-Data.js

This file was deleted.

29 changes: 29 additions & 0 deletions db/migrations/1693492520799-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = class Data1693492520799 {
name = 'Data1693492520799'

async up(db) {
await db.query(`CREATE TABLE "trade" ("id" character varying NOT NULL, "block" integer NOT NULL, "trader" text NOT NULL, "subject" text NOT NULL, "is_buy" boolean NOT NULL, "share_amount" numeric NOT NULL, "eth_amount" numeric NOT NULL, "protocol_eth_amount" numeric NOT NULL, "subject_eth_amount" numeric NOT NULL, "supply" numeric NOT NULL, "txn_hash" text NOT NULL, CONSTRAINT "PK_d4097908741dc408f8274ebdc53" PRIMARY KEY ("id"))`)
await db.query(`CREATE INDEX "IDX_c4d177ef22a72a103adb206ab7" ON "trade" ("block") `)
await db.query(`CREATE INDEX "IDX_8782e523050ad117640677be17" ON "trade" ("trader") `)
await db.query(`CREATE INDEX "IDX_9e00c3c01798dce17b45421f76" ON "trade" ("subject") `)
await db.query(`CREATE INDEX "IDX_d2201e8e0ddcb79dc9df0b7380" ON "trade" ("txn_hash") `)
await db.query(`CREATE TABLE "subject" ("id" character varying NOT NULL, "address" text NOT NULL, "first_trade_block" integer NOT NULL, "supply" numeric NOT NULL, "total_trades" integer NOT NULL, "total_eth_volume" numeric NOT NULL, "total_eth_buy_volume" numeric NOT NULL, "total_eth_sell_volume" numeric NOT NULL, "total_share_volume" numeric NOT NULL, "total_share_buy_volume" numeric NOT NULL, "total_share_sell_volume" numeric NOT NULL, "subject_eth_total" numeric NOT NULL, CONSTRAINT "PK_12eee115462e38d62e5455fc054" PRIMARY KEY ("id"))`)
await db.query(`CREATE INDEX "IDX_24a18e0dc5d06d8b516d99688f" ON "subject" ("address") `)
await db.query(`CREATE INDEX "IDX_36e946cd07407f62c658f2669d" ON "subject" ("first_trade_block") `)
await db.query(`CREATE TABLE "block_stats" ("id" character varying NOT NULL, "block" integer NOT NULL, "trades" integer NOT NULL, "num_involved_subjects" integer NOT NULL, "eth_volume" numeric NOT NULL, "eth_buy_volume" numeric NOT NULL, "eth_sell_volume" numeric NOT NULL, "share_volume" numeric NOT NULL, "share_buy_volume" numeric NOT NULL, "share_sell_volume" numeric NOT NULL, "subject_eth_total" numeric NOT NULL, "protocol_eth_total" numeric NOT NULL, CONSTRAINT "PK_2e5ab1b4d3ee438a1ce2b7cbba3" PRIMARY KEY ("id"))`)
await db.query(`CREATE INDEX "IDX_f16be6bd07f58fbf10d88d91cf" ON "block_stats" ("block") `)
}

async down(db) {
await db.query(`DROP TABLE "trade"`)
await db.query(`DROP INDEX "public"."IDX_c4d177ef22a72a103adb206ab7"`)
await db.query(`DROP INDEX "public"."IDX_8782e523050ad117640677be17"`)
await db.query(`DROP INDEX "public"."IDX_9e00c3c01798dce17b45421f76"`)
await db.query(`DROP INDEX "public"."IDX_d2201e8e0ddcb79dc9df0b7380"`)
await db.query(`DROP TABLE "subject"`)
await db.query(`DROP INDEX "public"."IDX_24a18e0dc5d06d8b516d99688f"`)
await db.query(`DROP INDEX "public"."IDX_36e946cd07407f62c658f2669d"`)
await db.query(`DROP TABLE "block_stats"`)
await db.query(`DROP INDEX "public"."IDX_f16be6bd07f58fbf10d88d91cf"`)
}
}
30 changes: 27 additions & 3 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,32 @@ type Trade @entity {
txnHash: String! @index
}

type VolumeByBlock @entity {
id: ID!
type Subject @entity {
id: ID! # same as address
address: String! @index
firstTradeBlock: Int! @index
supply: BigInt!
totalTrades: Int!
totalEthVolume: BigInt!
totalEthBuyVolume: BigInt!
totalEthSellVolume: BigInt!
totalShareVolume: BigInt!
totalShareBuyVolume: BigInt!
totalShareSellVolume: BigInt!
subjectEthTotal: BigInt!
}

type BlockStats @entity {
id: ID! # `${block}`
block: Int! @index
totalEthAmount: BigInt!
trades: Int!
numInvolvedSubjects: Int!
ethVolume: BigInt!
ethBuyVolume: BigInt!
ethSellVolume: BigInt!
shareVolume: BigInt!
shareBuyVolume: BigInt!
shareSellVolume: BigInt!
subjectEthTotal: BigInt!
protocolEthTotal: BigInt! # should be equal to the subject fees total
}
75 changes: 65 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import {TypeormDatabase} from '@subsquid/typeorm-store'
import {Trade, VolumeByBlock} from './model'
import {In} from 'typeorm'

import {Trade, Subject, BlockStats} from './model'
import {processor, FRIENDTECH_CONTRACT} from './processor'
import * as friendTechAbi from './abi/friendTechAbi'

processor.run(new TypeormDatabase({supportHotBlocks: true}), async (ctx) => {
const trades: Trade[] = []
const volumes: VolumeByBlock[] = []
const blockStats: BlockStats[] = []

for (let block of ctx.blocks) {
let totalEthAmount = 0n
let involvedSubjects: Set<string> = new Set()
let currentBlockStats: BlockStats = new BlockStats({
id: `${block.header.height}`,
block: block.header.height,
trades: 0,
ethVolume: 0n,
ethBuyVolume: 0n,
ethSellVolume: 0n,
shareVolume: 0n,
shareBuyVolume: 0n,
shareSellVolume: 0n,
subjectEthTotal: 0n,
protocolEthTotal: 0n
})

for (let log of block.logs) {
if (log.address === FRIENDTECH_CONTRACT &&
Expand Down Expand Up @@ -39,17 +54,57 @@ processor.run(new TypeormDatabase({supportHotBlocks: true}), async (ctx) => {
txnHash: log.transactionHash,
}))

totalEthAmount += ethAmount
involvedSubjects.add(subject)
currentBlockStats.trades += 1
currentBlockStats.ethVolume += ethAmount
currentBlockStats.ethBuyVolume += isBuy ? ethAmount : 0n
currentBlockStats.ethSellVolume += isBuy ? 0n : ethAmount
currentBlockStats.shareVolume += shareAmount
currentBlockStats.shareBuyVolume += isBuy ? shareAmount : 0n
currentBlockStats.shareSellVolume += isBuy ? 0n : shareAmount
currentBlockStats.subjectEthTotal += subjectEthAmount
currentBlockStats.protocolEthTotal += protocolEthAmount
}
}

volumes.push(new VolumeByBlock({
id: `${block.header.height}`,
block: block.header.height,
totalEthAmount
}))
currentBlockStats.numInvolvedSubjects = involvedSubjects.size
blockStats.push(currentBlockStats)
}

const subjectIds = new Set(trades.map(t => t.subject))
const subjects: Map<string, Subject> = await ctx.store.findBy(Subject, {address: In([...subjectIds])}).then(subjs => new Map(subjs.map(s => [s.address, s])))
for (let trade of trades) {
if (subjects.has(trade.subject)) {
const tradeSubject = subjects.get(trade.subject)!
tradeSubject.supply = trade.supply
tradeSubject.totalTrades += 1
tradeSubject.totalEthVolume += trade.ethAmount,
tradeSubject.totalEthBuyVolume += trade.isBuy ? trade.ethAmount : 0n,
tradeSubject.totalEthSellVolume += trade.isBuy ? 0n : trade.ethAmount,
tradeSubject.totalShareVolume += trade.shareAmount,
tradeSubject.totalShareBuyVolume += trade.isBuy ? trade.shareAmount : 0n,
tradeSubject.totalShareSellVolume += trade.isBuy ? 0n : trade.shareAmount,
tradeSubject.subjectEthTotal += trade.subjectEthAmount
}
else {
subjects.set(trade.subject, new Subject({
id: trade.subject,
address: trade.subject,
firstTradeBlock: trade.block,
supply: trade.supply,
totalTrades: 1,
totalEthVolume: trade.ethAmount,
totalEthBuyVolume: trade.isBuy ? trade.ethAmount : 0n,
totalEthSellVolume: trade.isBuy ? 0n : trade.ethAmount,
totalShareVolume: trade.shareAmount,
totalShareBuyVolume: trade.isBuy ? trade.shareAmount : 0n,
totalShareSellVolume: trade.isBuy ? 0n : trade.shareAmount,
subjectEthTotal: trade.subjectEthAmount
}))
}
}

await ctx.store.upsert(trades)
await ctx.store.upsert(volumes)
await ctx.store.upsert([...subjects.values()])
await ctx.store.upsert(blockStats)
})
46 changes: 46 additions & 0 deletions src/model/generated/blockStats.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, Index as Index_} from "typeorm"
import * as marshal from "./marshal"

@Entity_()
export class BlockStats {
constructor(props?: Partial<BlockStats>) {
Object.assign(this, props)
}

@PrimaryColumn_()
id!: string

@Index_()
@Column_("int4", {nullable: false})
block!: number

@Column_("int4", {nullable: false})
trades!: number

@Column_("int4", {nullable: false})
numInvolvedSubjects!: number

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
ethVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
ethBuyVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
ethSellVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
shareVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
shareBuyVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
shareSellVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
subjectEthTotal!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
protocolEthTotal!: bigint
}
3 changes: 2 additions & 1 deletion src/model/generated/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./trade.model"
export * from "./volumeByBlock.model"
export * from "./subject.model"
export * from "./blockStats.model"
47 changes: 47 additions & 0 deletions src/model/generated/subject.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, Index as Index_} from "typeorm"
import * as marshal from "./marshal"

@Entity_()
export class Subject {
constructor(props?: Partial<Subject>) {
Object.assign(this, props)
}

@PrimaryColumn_()
id!: string

@Index_()
@Column_("text", {nullable: false})
address!: string

@Index_()
@Column_("int4", {nullable: false})
firstTradeBlock!: number

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
supply!: bigint

@Column_("int4", {nullable: false})
totalTrades!: number

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
totalEthVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
totalEthBuyVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
totalEthSellVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
totalShareVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
totalShareBuyVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
totalShareSellVolume!: bigint

@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false})
subjectEthTotal!: bigint
}
19 changes: 0 additions & 19 deletions src/model/generated/volumeByBlock.model.ts

This file was deleted.

0 comments on commit 254c459

Please sign in to comment.