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

Bump to 5.0.0-alpha.14 and bugfix #1

Merged
merged 3 commits into from
Dec 10, 2021
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
29 changes: 29 additions & 0 deletions db/migrations/1639147106950-Initial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { MigrationInterface, QueryRunner } = require("typeorm");

module.exports = class Initial1639147106950 {
name = 'Initial1639147106950'

async up(queryRunner) {
await queryRunner.query(`CREATE TABLE "remark_entity" ("id" character varying NOT NULL, "value" text NOT NULL, "caller" text NOT NULL, "block_number" text NOT NULL, "interaction" text, CONSTRAINT "PK_47fe6cc1a357ef713548bfb9c9c" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "failed_entity" ("id" character varying NOT NULL, "value" text NOT NULL, "reason" text NOT NULL, "interaction" text, CONSTRAINT "PK_df628d98d9ba1555d905ee20d6b" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "emote" ("id" character varying NOT NULL, "caller" text NOT NULL, "value" text NOT NULL, "nft_id" character varying NOT NULL, CONSTRAINT "PK_c08d432f6b22ef550be511163ac" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE INDEX "IDX_463234b85d428ddde1bce27182" ON "emote" ("nft_id") `);
await queryRunner.query(`CREATE TABLE "nft_entity" ("name" text, "instance" text, "transferable" integer, "issuer" text, "sn" text, "id" character varying NOT NULL, "metadata" text, "current_owner" text, "price" numeric, "burned" boolean, "block_number" numeric, "events" jsonb, "collection_id" character varying NOT NULL, CONSTRAINT "PK_ed09c6a38c0f0a867d5a7b63f0d" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE INDEX "IDX_4b98bf4d630de0037475b9bbb7" ON "nft_entity" ("collection_id") `);
await queryRunner.query(`CREATE TABLE "collection_entity" ("version" text, "name" text, "max" integer, "issuer" text, "symbol" text, "id" character varying NOT NULL, "metadata" text, "current_owner" text, "events" jsonb, "block_number" numeric, CONSTRAINT "PK_5d44e140c4fcb3d961f9e83405f" PRIMARY KEY ("id"))`);
await queryRunner.query(`ALTER TABLE "emote" ADD CONSTRAINT "FK_463234b85d428ddde1bce271829" FOREIGN KEY ("nft_id") REFERENCES "nft_entity"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "nft_entity" ADD CONSTRAINT "FK_4b98bf4d630de0037475b9bbb7a" FOREIGN KEY ("collection_id") REFERENCES "collection_entity"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

async down(queryRunner) {
await queryRunner.query(`ALTER TABLE "nft_entity" DROP CONSTRAINT "FK_4b98bf4d630de0037475b9bbb7a"`);
await queryRunner.query(`ALTER TABLE "emote" DROP CONSTRAINT "FK_463234b85d428ddde1bce271829"`);
await queryRunner.query(`DROP TABLE "collection_entity"`);
await queryRunner.query(`DROP INDEX "public"."IDX_4b98bf4d630de0037475b9bbb7"`);
await queryRunner.query(`DROP TABLE "nft_entity"`);
await queryRunner.query(`DROP INDEX "public"."IDX_463234b85d428ddde1bce27182"`);
await queryRunner.query(`DROP TABLE "emote"`);
await queryRunner.query(`DROP TABLE "failed_entity"`);
await queryRunner.query(`DROP TABLE "remark_entity"`);
}
}
588 changes: 415 additions & 173 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
"dependencies": {
"@polkadot/types": "^6.5.1",
"@subsquid/openreader": "^0.2.0",
"@subsquid/hydra-common": "5.0.0-alpha.5",
"@subsquid/hydra-processor": "5.0.0-alpha.5",
"@subsquid/hydra-common": "5.0.0-alpha.14",
"@subsquid/hydra-processor": "5.0.0-alpha.14",
"inflected": "^2.1.0",
"class-validator": "^0.13.1",
"type-graphql": "^1.1.1",
"typeorm": "0.2.38"
},
"devDependencies": {
"@subsquid/hydra-cli": "5.0.0-alpha.5",
"@subsquid/hydra-typegen": "5.0.0-alpha.5",
"@subsquid/hydra-cli": "5.0.0-alpha.14",
"@subsquid/hydra-typegen": "5.0.0-alpha.14",
"@types/inflected": "^1.1.29",
"@types/pg": "^8.6.1",
"typescript": "~4.4.2"
Expand Down
109 changes: 54 additions & 55 deletions src/mappings/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DatabaseManager, EventContext, StoreContext, ExtrinsicContext } from '@subsquid/hydra-common'
import { CollectionEntity, NFTEntity } from '../generated/model'
import { Balances } from '../types'

import { getRemarksFrom, RemarkResult } from './utils';
import { Collection, eventFrom, getNftId, NFT, RmrkEvent, RmrkInteraction } from './utils/types';
Expand Down Expand Up @@ -56,60 +55,60 @@ export async function handleRemark(extrinsic: ExtrinsicContext & StoreContext):



export async function balancesTransfer({
store,
event,
block,
extrinsic,
}: EventContext & StoreContext): Promise<void> {

const [from, to, value] = new Balances.TransferEvent(event).params
const tip = extrinsic?.tip || 0n

const fromAcc = await getOrCreate(store, Account, from.toHex())
fromAcc.wallet = from.toHuman()
fromAcc.balance = fromAcc.balance || 0n
fromAcc.balance -= value.toBigInt()
fromAcc.balance -= tip
await store.save(fromAcc)

const toAcc = await getOrCreate(store, Account, to.toHex())
toAcc.wallet = to.toHuman()
toAcc.balance = toAcc.balance || 0n
toAcc.balance += value.toBigInt()
await store.save(toAcc)

const hbFrom = new HistoricalBalance()
hbFrom.account = fromAcc;
hbFrom.balance = fromAcc.balance;
hbFrom.timestamp = new Date(block.timestamp)
await store.save(hbFrom)

const hbTo = new HistoricalBalance()
hbTo.account = toAcc;
hbTo.balance = toAcc.balance;
hbTo.timestamp = new Date(block.timestamp)
await store.save(hbTo)
}


async function getOrCreate<T extends {id: string}>(
store: DatabaseManager,
entityConstructor: EntityConstructor<T>,
id: string
): Promise<T> {

let e = await store.get(entityConstructor, {
where: { id },
})

if (e == null) {
e = new entityConstructor()
e.id = id
}

return e
}
// export async function balancesTransfer({
// store,
// event,
// block,
// extrinsic,
// }: EventContext & StoreContext): Promise<void> {

// const [from, to, value] = new Balances.TransferEvent(event).params
// const tip = extrinsic?.tip || 0n

// const fromAcc = await getOrCreate(store, Account, from.toHex())
// fromAcc.wallet = from.toHuman()
// fromAcc.balance = fromAcc.balance || 0n
// fromAcc.balance -= value.toBigInt()
// fromAcc.balance -= tip
// await store.save(fromAcc)

// const toAcc = await getOrCreate(store, Account, to.toHex())
// toAcc.wallet = to.toHuman()
// toAcc.balance = toAcc.balance || 0n
// toAcc.balance += value.toBigInt()
// await store.save(toAcc)

// const hbFrom = new HistoricalBalance()
// hbFrom.account = fromAcc;
// hbFrom.balance = fromAcc.balance;
// hbFrom.timestamp = new Date(block.timestamp)
// await store.save(hbFrom)

// const hbTo = new HistoricalBalance()
// hbTo.account = toAcc;
// hbTo.balance = toAcc.balance;
// hbTo.timestamp = new Date(block.timestamp)
// await store.save(hbTo)
// }


// async function getOrCreate<T extends {id: string}>(
// store: DatabaseManager,
// entityConstructor: EntityConstructor<T>,
// id: string
// ): Promise<T> {

// let e = await store.get(entityConstructor, {
// where: { id },
// })

// if (e == null) {
// e = new entityConstructor()
// e.id = id
// }

// return e
// }


type EntityConstructor<T> = {
Expand Down
6 changes: 3 additions & 3 deletions src/mappings/utils/consolidator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RmrkInteraction } from './types'
import { CollectionEntity, NFTEntity } from '../../types'
import { CollectionEntity, NFTEntity } from '../../generated/model'
import { ExtraCall } from './extract'
// import { decodeAddress } from '@polkadot/util-crypto'
type Entity = CollectionEntity | NFTEntity
Expand All @@ -9,7 +9,7 @@ export function exists<T>(entity: T | undefined): boolean {
}

export function isBurned(nft: NFTEntity) {
return nft.burned
return nft.burned ?? false
}

export function isTransferable(nft: NFTEntity) {
Expand Down Expand Up @@ -60,7 +60,7 @@ export function isPositiveOrElseError(entity: BigInt | number, excludeZero?: boo


const isBalanceTransfer = ({section, method}: ExtraCall) => section === 'balances' && method === 'transfer'
const canBuy = (nft: NFTEntity) => (call: ExtraCall) => isBalanceTransfer(call) && isOwner(nft, call.args[0]) && BigInt(call.args[1]) >= BigInt(nft.price)
const canBuy = (nft: NFTEntity) => (call: ExtraCall) => isBalanceTransfer(call) && isOwner(nft, call.args[0]) && BigInt(call.args[1]) >= BigInt(nft.price ?? 0)

export function isBuyLegalOrElseError(entity: NFTEntity, extraCalls: ExtraCall[]) {
const result = extraCalls.some(canBuy(entity))
Expand Down
9 changes: 4 additions & 5 deletions src/mappings/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CollectionEntity, NFTEntity } from '../../types'
import { Event } from '../../types'
import { CollectionEntity, NFTEntity, Event } from '../../generated/model'
import { RemarkResult } from './extract'

export enum RmrkEvent {
Expand All @@ -18,13 +17,13 @@ export const getNftId = (nft: any, blocknumber?: string | number): string => {
}

export function eventFrom(interaction: RmrkEvent, { blockNumber, caller, timestamp }: RemarkResult, meta: string): Event {
return {
return new Event({}, {
interaction,
blockNumber,
caller,
timestamp,
timestamp: (+timestamp),
meta
}
})
}

export interface RmrkInteraction {
Expand Down
6 changes: 1 addition & 5 deletions src/server-extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {Field, ObjectType, Query, Resolver} from "type-graphql"
import type {EntityManager} from "typeorm"
import {HistoricalBalance} from "./generated/model"


@ObjectType()
export class Hello {
Expand All @@ -22,8 +20,6 @@ export class HelloResolver {

@Query(() => Hello)
async hello(): Promise<Hello> {
const tx = await this.tx()
let count = await tx.getRepository(HistoricalBalance).count()
return new Hello(`Hello, we've seen ${count} transfers!`)
return new Hello(`Hey, this is you custom API extension`)
}
}
18 changes: 18 additions & 0 deletions src/types/balances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {create} from './_registry'
import {AccountId32} from '@polkadot/types/interfaces'
import {u128} from '@polkadot/types'
import {SubstrateEvent} from '@subsquid/hydra-common'

export namespace Balances {
/**
* Transfer succeeded.
*/
export class TransferEvent {
constructor(private event: SubstrateEvent) {}

get params(): [AccountId32, AccountId32, u128] {
return [create('AccountId32', this.event.params[0].value), create('AccountId32', this.event.params[1].value), create('u128', this.event.params[2].value)]
}
}

}