-
Notifications
You must be signed in to change notification settings - Fork 11
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/Flippers-for-collection-activity #35
Closed
daiagi
wants to merge
12
commits into
kodadot:main
from
daiagi:feat/Flippers-for-collection-activity
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
807fef3
flipper schema
daiagi ee35e88
flips handler
daiagi 2eea515
use it in buy events
daiagi 601f88e
latestFlipTimestamp => timestamp
daiagi 69832f9
block number first
daiagi b9d8030
merge main into feat/Flippers-for-collection-activity
daiagi 7632ef8
merge
daiagi b3c2ac0
merge
daiagi 11c5b38
implment code review
daiagi f825286
Merge branch 'main' of https://github.com/kodadot/stick into feat/Fli…
daiagi abdd382
Merge branch 'main' of https://github.com/kodadot/stick into feat/Fli…
daiagi b482ef3
new migrtion
daiagi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module.exports = class Data1694688464308 { | ||
name = 'Data1694688464308' | ||
|
||
async up(db) { | ||
await db.query(`CREATE TABLE "flip_event" ("id" character varying NOT NULL, "sold_price" numeric, "sold_to" text, "sell_timestamp" TIMESTAMP WITH TIME ZONE, "bought_price" numeric NOT NULL, "profit" numeric, "flipper_id" character varying, "nft_id" character varying, CONSTRAINT "PK_fa045c959eb49fd330b7130cabb" PRIMARY KEY ("id"))`) | ||
await db.query(`CREATE INDEX "IDX_b440835d296fabbade4a08f56d" ON "flip_event" ("flipper_id") `) | ||
await db.query(`CREATE INDEX "IDX_0896c2680b0a6fbff1e68d3ada" ON "flip_event" ("nft_id") `) | ||
await db.query(`CREATE TABLE "flipper_entity" ("id" character varying NOT NULL, "address" text NOT NULL, "owned" integer NOT NULL, "total_bought" numeric NOT NULL, "total_sold" numeric NOT NULL, "best_flip" numeric NOT NULL, "timestamp" TIMESTAMP WITH TIME ZONE NOT NULL, "collection_id" character varying, CONSTRAINT "PK_1715ba6adaecb7e138d4716e73e" PRIMARY KEY ("id"))`) | ||
await db.query(`CREATE INDEX "IDX_caf50ab81db0d2aa16265da559" ON "flipper_entity" ("address") `) | ||
await db.query(`CREATE INDEX "IDX_020450854a403a64ea22a1487f" ON "flipper_entity" ("collection_id") `) | ||
await db.query(`ALTER TABLE "flip_event" ADD CONSTRAINT "FK_b440835d296fabbade4a08f56db" FOREIGN KEY ("flipper_id") REFERENCES "flipper_entity"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`) | ||
await db.query(`ALTER TABLE "flip_event" ADD CONSTRAINT "FK_0896c2680b0a6fbff1e68d3adaa" FOREIGN KEY ("nft_id") REFERENCES "nft_entity"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`) | ||
await db.query(`ALTER TABLE "flipper_entity" ADD CONSTRAINT "FK_020450854a403a64ea22a1487f2" FOREIGN KEY ("collection_id") REFERENCES "collection_entity"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`) | ||
} | ||
|
||
async down(db) { | ||
await db.query(`DROP TABLE "flip_event"`) | ||
await db.query(`DROP INDEX "public"."IDX_b440835d296fabbade4a08f56d"`) | ||
await db.query(`DROP INDEX "public"."IDX_0896c2680b0a6fbff1e68d3ada"`) | ||
await db.query(`DROP TABLE "flipper_entity"`) | ||
await db.query(`DROP INDEX "public"."IDX_caf50ab81db0d2aa16265da559"`) | ||
await db.query(`DROP INDEX "public"."IDX_020450854a403a64ea22a1487f"`) | ||
await db.query(`ALTER TABLE "flip_event" DROP CONSTRAINT "FK_b440835d296fabbade4a08f56db"`) | ||
await db.query(`ALTER TABLE "flip_event" DROP CONSTRAINT "FK_0896c2680b0a6fbff1e68d3adaa"`) | ||
await db.query(`ALTER TABLE "flipper_entity" DROP CONSTRAINT "FK_020450854a403a64ea22a1487f2"`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { EntityManager } from 'typeorm' | ||
import { create, findByRawQuery, findWhere, getOptional } from '@kodadot1/metasquid/entity' | ||
import { FlipperEntity, FlipEvent, CollectionEntity, NFTEntity } from '../../model' | ||
import { CallWith } from '../utils/types' | ||
import { BuyTokenEvent } from '../nfts/types' | ||
import { pending, warn } from '../utils/logger' | ||
|
||
const OPERATION = 'BUY => FlipperEventHandler' as any | ||
|
||
type HandleFlipParams = { | ||
event: CallWith<BuyTokenEvent> | ||
nft: NFTEntity | ||
} | ||
|
||
function getFlipperId(address: string, collectionId: string): string { | ||
return `${address}-${collectionId}` | ||
} | ||
|
||
function getFlipEventId(address: string, collectionId: string, blockNumber: string): string { | ||
return `${blockNumber}-${address}-${collectionId}` | ||
} | ||
|
||
async function getOrCreateFlipper( | ||
store: EntityManager, | ||
address: string, | ||
collection: CollectionEntity, | ||
timestamp: Date | ||
): Promise<FlipperEntity> { | ||
const id = getFlipperId(address, collection.id) | ||
let flipper = await getOptional<FlipperEntity>(store, FlipperEntity, id) | ||
|
||
if (!flipper) { | ||
flipper = create(FlipperEntity, id, { | ||
address, | ||
collection, | ||
owned: 0, | ||
totalBought: BigInt(0), | ||
totalSold: BigInt(0), | ||
bestFlip: BigInt(0), | ||
timestamp, | ||
}) | ||
await store.save(flipper) | ||
} | ||
|
||
return flipper | ||
} | ||
|
||
async function handleNewOwner( | ||
store: EntityManager, | ||
event: CallWith<BuyTokenEvent>, | ||
nft: NFTEntity | ||
): Promise<FlipperEntity> { | ||
const { caller: newOwner, collectionId, timestamp, price, blockNumber } = event | ||
const flipper = await getOrCreateFlipper(store, newOwner, nft.collection, timestamp) | ||
|
||
const flipEventId = getFlipEventId(newOwner, collectionId, blockNumber) | ||
const flipEvent = create(FlipEvent, flipEventId, { | ||
flipper, | ||
nft, | ||
boughtPrice: price, | ||
}) | ||
|
||
await store.save(flipEvent) | ||
await store.update( | ||
FlipperEntity, | ||
{ id: flipper.id }, | ||
{ | ||
owned: flipper.owned + 1, | ||
totalBought: BigInt(flipper.totalBought) + BigInt(price || 0), | ||
timestamp, | ||
} | ||
) | ||
|
||
return flipper | ||
} | ||
|
||
async function handlePreviousOwner( | ||
store: EntityManager, | ||
event: CallWith<BuyTokenEvent>, | ||
nft: NFTEntity | ||
): Promise<void> { | ||
const { caller: newOwner, currentOwner: previousOwner, timestamp, price } = event | ||
|
||
const previousFlipperId = getFlipperId(previousOwner, nft.collection.id) | ||
let previousFlipper = await getOptional<FlipperEntity>(store, FlipperEntity, previousFlipperId) | ||
|
||
if (!previousFlipper) { | ||
pending( | ||
OPERATION, | ||
`No previous flipper found for the previous owner (${previousOwner}). No previous flip to handle.` | ||
) | ||
return | ||
} | ||
|
||
const previousBuyEvents = await findWhere<FlipEvent>(store, FlipEvent, { nft: { id: nft.id }, flipper: { id: previousFlipper.id } }) | ||
|
||
|
||
if (!previousBuyEvents || previousBuyEvents.length === 0) { | ||
warn(OPERATION, `No previous flips found for the previous owner (${previousOwner}). No previous flip to handle.`) | ||
return | ||
} | ||
|
||
|
||
const previousBuyEvent = previousBuyEvents[0] // there should be only 1 | ||
pending(OPERATION, `Previous flip event: ${previousBuyEvent}`) | ||
|
||
|
||
|
||
const soldPrice = BigInt(price || 0) | ||
const profit = soldPrice - previousBuyEvent.boughtPrice | ||
const profitPercentage = | ||
previousBuyEvent.boughtPrice !== BigInt(0) ? (Number(profit) / Number(previousBuyEvent.boughtPrice)) * 100 : 0 | ||
|
||
await store.update( | ||
FlipEvent, | ||
{ id: previousBuyEvent.id }, | ||
{ | ||
soldPrice, | ||
soldTo: newOwner, | ||
sellTimestamp: timestamp, | ||
profit, | ||
} | ||
) | ||
await store.update( | ||
FlipperEntity, | ||
{ id: previousFlipper.id }, | ||
{ | ||
owned: previousFlipper.owned - 1, | ||
totalSold: BigInt(previousFlipper.totalSold) + soldPrice, | ||
timestamp, | ||
bestFlip: | ||
previousFlipper.bestFlip > BigInt(profitPercentage) ? previousFlipper.bestFlip : BigInt(profitPercentage), | ||
} | ||
) | ||
} | ||
|
||
export async function handleBuy(store: EntityManager, params: HandleFlipParams): Promise<void> { | ||
await handleNewOwner(store, params.event, params.nft) | ||
await handlePreviousOwner(store, params.event, params.nft) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, ManyToOne as ManyToOne_, Index as Index_} from "typeorm" | ||
import * as marshal from "./marshal" | ||
import {FlipperEntity} from "./flipperEntity.model" | ||
import {NFTEntity} from "./nftEntity.model" | ||
|
||
@Entity_() | ||
export class FlipEvent { | ||
constructor(props?: Partial<FlipEvent>) { | ||
Object.assign(this, props) | ||
} | ||
|
||
@PrimaryColumn_() | ||
id!: string | ||
|
||
@Index_() | ||
@ManyToOne_(() => FlipperEntity, {nullable: true}) | ||
flipper!: FlipperEntity | ||
|
||
@Index_() | ||
@ManyToOne_(() => NFTEntity, {nullable: true}) | ||
nft!: NFTEntity | ||
|
||
@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: true}) | ||
soldPrice!: bigint | undefined | null | ||
|
||
@Column_("text", {nullable: true}) | ||
soldTo!: string | undefined | null | ||
|
||
@Column_("timestamp with time zone", {nullable: true}) | ||
sellTimestamp!: Date | undefined | null | ||
|
||
@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false}) | ||
boughtPrice!: bigint | ||
|
||
@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: true}) | ||
profit!: bigint | undefined | null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, Index as Index_, ManyToOne as ManyToOne_, OneToMany as OneToMany_} from "typeorm" | ||
import * as marshal from "./marshal" | ||
import {CollectionEntity} from "./collectionEntity.model" | ||
import {FlipEvent} from "./flipEvent.model" | ||
|
||
@Entity_() | ||
export class FlipperEntity { | ||
constructor(props?: Partial<FlipperEntity>) { | ||
Object.assign(this, props) | ||
} | ||
|
||
@PrimaryColumn_() | ||
id!: string | ||
|
||
@Index_() | ||
@Column_("text", {nullable: false}) | ||
address!: string | ||
|
||
@Index_() | ||
@ManyToOne_(() => CollectionEntity, {nullable: true}) | ||
collection!: CollectionEntity | ||
|
||
@OneToMany_(() => FlipEvent, e => e.flipper) | ||
flips!: FlipEvent[] | ||
|
||
@Column_("int4", {nullable: false}) | ||
owned!: number | ||
|
||
@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false}) | ||
totalBought!: bigint | ||
|
||
@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false}) | ||
totalSold!: bigint | ||
|
||
@Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false}) | ||
bestFlip!: bigint | ||
|
||
@Column_("timestamp with time zone", {nullable: false}) | ||
timestamp!: Date | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.