-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from AvailSpace/master
Data indexer Avail mainnet
- Loading branch information
Showing
12 changed files
with
280 additions
and
30 deletions.
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
DB_NAME=squid_avail | ||
DB_PORT=23798 | ||
DB_NAME=squid_avail_turing | ||
DB_PORT=23799 | ||
GQL_PORT=3000 | ||
NETWORK=testnet | ||
RPC_ENDPOINT=wss://rpc-testnet.avail.tools/ws | ||
RPC_ENDPOINT="" | ||
ARCHIVE_LOOKUP_NAME_TESTNET=data-avail | ||
SUPPORT_HOT_BLOCKS=true | ||
USE_ONLY_RPC=false # If true, archive is not used and only NODE_RPC_WS will be used | ||
START_BLOCK=0 | ||
PROCESSOR_BATCH_SIZE=100 | ||
PROCESSOR_BATCH_SIZE=80 | ||
SS58_CODEC=42 |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,44 @@ | ||
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, Index as Index_, ManyToOne as ManyToOne_} from "typeorm" | ||
import * as marshal from "./marshal" | ||
import {Account} from "./account.model" | ||
import {Amount} from "./_amount" | ||
|
||
@Entity_() | ||
export class DataAvailability { | ||
constructor(props?: Partial<DataAvailability>) { | ||
Object.assign(this, props) | ||
} | ||
|
||
@PrimaryColumn_() | ||
id!: string | ||
|
||
@Column_("text", {nullable: true}) | ||
action!: string | undefined | null | ||
|
||
@Column_("text", {nullable: true}) | ||
extrinsicHash!: string | undefined | null | ||
|
||
@Index_() | ||
@Column_("timestamp with time zone", {nullable: false}) | ||
timestamp!: Date | ||
|
||
@Index_() | ||
@Column_("int4", {nullable: false}) | ||
blockNumber!: number | ||
|
||
@Index_() | ||
@ManyToOne_(() => Account, {nullable: true}) | ||
sender!: Account | undefined | null | ||
|
||
@Column_("jsonb", {transformer: {to: obj => obj == null ? undefined : obj.toJSON(), from: obj => obj == null ? undefined : new Amount(undefined, obj)}, nullable: true}) | ||
fee!: Amount | undefined | null | ||
|
||
@Column_("text", {nullable: true}) | ||
dataRaw!: string | undefined | null | ||
|
||
@Column_("text", {nullable: true}) | ||
dataValue!: string | undefined | null | ||
|
||
@Column_("bool", {nullable: true}) | ||
isJson!: boolean | 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
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,41 @@ | ||
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, Index as Index_, ManyToOne as ManyToOne_} from "typeorm" | ||
import * as marshal from "./marshal" | ||
import {Account} from "./account.model" | ||
import {Amount} from "./_amount" | ||
|
||
@Entity_() | ||
export class Remark { | ||
constructor(props?: Partial<Remark>) { | ||
Object.assign(this, props) | ||
} | ||
|
||
@PrimaryColumn_() | ||
id!: string | ||
|
||
@Column_("text", {nullable: true}) | ||
action!: string | undefined | null | ||
|
||
@Column_("text", {nullable: true}) | ||
extrinsicHash!: string | undefined | null | ||
|
||
@Index_() | ||
@Column_("timestamp with time zone", {nullable: false}) | ||
timestamp!: Date | ||
|
||
@Index_() | ||
@Column_("int4", {nullable: false}) | ||
blockNumber!: number | ||
|
||
@Index_() | ||
@ManyToOne_(() => Account, {nullable: true}) | ||
sender!: Account | undefined | null | ||
|
||
@Column_("jsonb", {transformer: {to: obj => obj == null ? undefined : obj.toJSON(), from: obj => obj == null ? undefined : new Amount(undefined, obj)}, nullable: true}) | ||
fee!: Amount | undefined | null | ||
|
||
@Column_("text", {nullable: true}) | ||
dataRaw!: string | undefined | null | ||
|
||
@Column_("text", {nullable: true}) | ||
dataValue!: string | 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,69 @@ | ||
import { Call } from "@subsquid/substrate-processor"; | ||
import { DataAvailability, Amount } from "../model"; | ||
import { Fields, ctx } from "../processor"; | ||
import { DataRawAddress, } from "../util/interfaces"; | ||
import { AccountQuerier } from "./accountHandler"; | ||
import { IHandler } from "../interfaces/interfaces"; | ||
import { Buffer } from 'buffer'; | ||
|
||
export class DataAvailabilityHandler implements IHandler { | ||
availabilityData: Map<string, DataAvailability> = new Map(); | ||
async process(call: Call<Fields>, accountInstance: AccountQuerier){ | ||
let addressHex = (call.extrinsic!.signature!.address as DataRawAddress).value; | ||
|
||
let fee = { | ||
amount: call.extrinsic!.fee || BigInt(0), | ||
symbol: "AVL", | ||
decimal: 18 | ||
} | ||
const idExist = this.availabilityData.get(call.id); | ||
const dataValue = decodeData(call.args.data); | ||
if(!idExist){ | ||
this.availabilityData.set(call.id, new DataAvailability({ | ||
id: call.id, | ||
action: call.name, | ||
extrinsicHash: call.extrinsic!.hash, | ||
timestamp: new Date(call.block.timestamp!), | ||
blockNumber: call.extrinsic!.block.height, | ||
sender: accountInstance.getAccountId(addressHex), | ||
fee: new Amount(fee), | ||
dataRaw: call.args.data, | ||
dataValue: dataValue, | ||
isJson: isJson(dataValue) ? true : false | ||
})); | ||
} | ||
} | ||
|
||
async save(){ | ||
await ctx.store.save([...this.availabilityData.values()]); | ||
} | ||
} | ||
|
||
function isJson(str:string):boolean { | ||
try { | ||
const parsed = JSON.parse(str); | ||
return typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed); | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
function decodeData(str:string):string { | ||
let strInput = str.slice(2, str.length); | ||
const bytesData:Buffer = Buffer.from(strInput, 'hex'); | ||
let res = bytesData.toString().replace(/<Buffer |>/g, ''); | ||
if (isValidHexString(res)) return res; | ||
else if (isJson(res)) return res; | ||
else if (isValidString(res)) return res; | ||
else return str; | ||
} | ||
|
||
function isValidHexString(str:string):boolean { | ||
const hexRegex = /^0x[0-9A-Fa-f]+$/; | ||
return hexRegex.test(str); | ||
} | ||
|
||
function isValidString(str: string): boolean { | ||
const regex = /^[\x20-\x7E]*$/; | ||
return regex.test(str); | ||
} |
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,39 @@ | ||
import { Call } from "@subsquid/substrate-processor"; | ||
import { Remark, Amount } from "../model"; | ||
import { Fields, ctx } from "../processor"; | ||
import { DataRawAddress, } from "../util/interfaces"; | ||
import { AccountQuerier } from "./accountHandler"; | ||
import { IHandler } from "../interfaces/interfaces"; | ||
|
||
export class RemarkHandler implements IHandler { | ||
remarkData: Map<string, Remark> = new Map(); | ||
async process(call: Call<Fields>, accountInstance: AccountQuerier){ | ||
let addressHex = (call.extrinsic!.signature!.address as DataRawAddress).value; | ||
|
||
let fee = { | ||
amount: call.extrinsic!.fee || BigInt(0), | ||
symbol: "AVL", | ||
decimal: 18 | ||
} | ||
|
||
const idExist = this.remarkData.get(call.id); | ||
|
||
if(!idExist){ | ||
this.remarkData.set(call.id, new Remark({ | ||
id: call.id, | ||
action: call.name, | ||
extrinsicHash: call.extrinsic!.hash, | ||
timestamp: new Date(call.block.timestamp!), | ||
blockNumber: call.extrinsic!.block.height, | ||
sender: accountInstance.getAccountId(addressHex), | ||
fee:new Amount(fee), | ||
dataRaw: call.args.remark, | ||
dataValue: call.args.remark, | ||
})); | ||
} | ||
} | ||
|
||
async save(){ | ||
await ctx.store.save([...this.remarkData.values()]); | ||
} | ||
} |
Oops, something went wrong.