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

40 owner of #49

Merged
merged 7 commits into from
Apr 12, 2022
Merged
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
11 changes: 11 additions & 0 deletions db/migrations/1649244965659-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = class Data1649244965659 {
name = 'Data1649244965659'

async up(db) {
await db.query(`ALTER TABLE "event" ADD "current_owner" text NOT NULL`)
}

async down(db) {
await db.query(`ALTER TABLE "event" DROP COLUMN "current_owner"`)
}
}
11 changes: 7 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ reset:
npx sqd db:migrate

migrate:
@npx sqd db:migrate
npx sqd db:migrate

update NAME:
npx sqd db:create-migration "{{NAME}}"
update-db:
npx sqd db:create-migration Data

test:
npm run test:unit
Expand All @@ -60,4 +60,7 @@ kill TAG:
npx sqd squid:kill "rubick@{{TAG}}"

update-deps:
npx npm-check-updates -u
npx taze

exec:
docker exec -it rubick-db-1 psql -U postgres -d squid
3 changes: 3 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ interface EventType {
blockNumber: BigInt
timestamp: DateTime!
caller: String!
currentOwner: String # currentOwner
interaction: Interaction!
meta: String!
}
Expand All @@ -80,6 +81,7 @@ type Event implements EventType @entity {
blockNumber: BigInt
timestamp: DateTime!
caller: String!
currentOwner: String! # currentOwner
interaction: Interaction!
meta: String!
nft: NFTEntity!
Expand All @@ -104,6 +106,7 @@ enum Interaction {
MINT
MINTNFT
LIST
UNLIST
BUY
SEND
CONSUME
Expand Down
12 changes: 7 additions & 5 deletions src/mappings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,14 @@ async function send(remark: RemarkResult, { store }: Context) {
)
validateInteraction(nft, interaction)
isOwnerOrElseError(nft, remark.caller)

const originalOwner = nft.currentOwner ?? undefined
nft.currentOwner = interaction.metadata
nft.price = BigInt(0)
nft.updatedAt = remark.timestamp

logger.success(`[SEND] ${nft.id} to ${interaction.metadata}`)
await store.save(nft)
await createEvent(nft, RmrkEvent.SEND, remark, interaction.metadata || '', store)
await createEvent(nft, RmrkEvent.SEND, remark, interaction.metadata || '', store, originalOwner)
} catch (e) {
logError(e, (e) =>
logger.error(`[SEND] ${e.message} ${JSON.stringify(interaction)}`)
Expand All @@ -237,13 +237,14 @@ async function buy(remark: RemarkResult, { store }: Context) {
isPositiveOrElseError(nft.price, true)
isBuyLegalOrElseError(nft, remark.extra || [])
const originalPrice = nft.price
const originalOwner = nft.currentOwner ?? undefined
nft.currentOwner = remark.caller
nft.price = BigInt(0)
nft.updatedAt = remark.timestamp

logger.success(`[BUY] ${nft.id} from ${remark.caller}`)
await store.save(nft)
await createEvent(nft, RmrkEvent.BUY, remark, String(originalPrice), store)
await createEvent(nft, RmrkEvent.BUY, remark, String(originalPrice), store, originalOwner)
} catch (e) {
logError(e, (e) =>
logger.error(`[BUY] ${e.message} ${JSON.stringify(interaction)}`)
Expand Down Expand Up @@ -299,7 +300,8 @@ async function list(remark: RemarkResult, { store }: Context) {

logger.success(`[LIST] ${nft.id} from ${remark.caller}`)
await store.save(nft)
await createEvent(nft, RmrkEvent.LIST, remark, String(price), store)
const event = nft.price === 0n ? RmrkEvent.UNLIST : RmrkEvent.LIST
await createEvent(nft, event, remark, String(price), store)
} catch (e) {
logError(e, (e) =>
logger.warn(`[LIST] ${e.message} ${JSON.stringify(interaction)}`)
Expand Down Expand Up @@ -413,7 +415,7 @@ async function handleMetadata(
}


async function createEvent(final: NFTEntity, interaction: RmrkEvent, remark: RemarkResult, meta: string, store: Store) {
async function createEvent(final: NFTEntity, interaction: RmrkEvent, remark: RemarkResult, meta: string, store: Store, currentOwner?: string) {
try {
const newEventId = eventId(final.id, interaction)
const event = create<Event>(Event, newEventId, eventFrom(interaction, remark, meta))
Expand Down
4 changes: 3 additions & 1 deletion src/mappings/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ export function collectionEventFrom(interaction: RmrkEvent.MINT | RmrkEvent.CHAN
})
}

export function eventFrom(interaction: RmrkEvent, { blockNumber, caller, timestamp }: RemarkResult, meta: string): IEvent {
export function eventFrom(interaction: RmrkEvent, { blockNumber, caller, timestamp }: RemarkResult, meta: string, currentOwner?: string): IEvent {
return {
interaction,
blockNumber: BigInt(blockNumber),
caller,
currentOwner: currentOwner || caller,
timestamp,
meta
}
Expand All @@ -46,6 +47,7 @@ export interface IEvent {
interaction: RmrkEvent;
blockNumber: bigint,
caller: string,
currentOwner: string,
timestamp: Date,
meta: string;
}
Expand Down
1 change: 1 addition & 0 deletions src/model/generated/_interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum Interaction {
MINT = "MINT",
MINTNFT = "MINTNFT",
LIST = "LIST",
UNLIST = "UNLIST",
BUY = "BUY",
SEND = "SEND",
CONSUME = "CONSUME",
Expand Down
3 changes: 3 additions & 0 deletions src/model/generated/event.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export class Event {
@Column_("text", {nullable: false})
caller!: string

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

@Column_("varchar", {length: 12, nullable: false})
interaction!: Interaction

Expand Down