From 7edd302ebb54a33bbc0416640518409137847e59 Mon Sep 17 00:00:00 2001 From: Phong Date: Tue, 11 Jun 2024 19:13:36 +0700 Subject: [PATCH] fix datavalue of dataavailability --- .env | 2 +- schema.graphql | 6 ++++-- src/model/generated/dataAvailability.model.ts | 5 ++++- src/model/generated/remark.model.ts | 5 ++++- src/process/dataAvailabilityHandler.ts | 17 ++++++++++++----- src/process/remarkHandler.ts | 3 ++- 6 files changed, 27 insertions(+), 11 deletions(-) diff --git a/.env b/.env index d74041a..8b42728 100644 --- a/.env +++ b/.env @@ -1,6 +1,6 @@ DB_NAME=squid_avail_turing DB_PORT=23799 -GQL_PORT=3001 +GQL_PORT=3000 NETWORK=testnet RPC_ENDPOINT=wss://turing-rpc.availspace.app ARCHIVE_LOOKUP_NAME_TESTNET=data-avail diff --git a/schema.graphql b/schema.graphql index 5e94b24..e437558 100644 --- a/schema.graphql +++ b/schema.graphql @@ -259,7 +259,8 @@ type Remark @entity { blockNumber: Int! @index sender: Account fee: Amount - rawValue: String + dataRaw: String + dataValue: String } type DataAvailability @entity { @@ -270,6 +271,7 @@ type DataAvailability @entity { blockNumber: Int! @index sender: Account fee: Amount - rawValue: String + dataRaw: String + dataValue: String isJson: Boolean } diff --git a/src/model/generated/dataAvailability.model.ts b/src/model/generated/dataAvailability.model.ts index 4b0f43e..4e850c2 100644 --- a/src/model/generated/dataAvailability.model.ts +++ b/src/model/generated/dataAvailability.model.ts @@ -34,7 +34,10 @@ export class DataAvailability { fee!: Amount | undefined | null @Column_("text", {nullable: true}) - rawValue!: string | undefined | null + dataRaw!: string | undefined | null + + @Column_("text", {nullable: true}) + dataValue!: string | undefined | null @Column_("bool", {nullable: true}) isJson!: boolean | undefined | null diff --git a/src/model/generated/remark.model.ts b/src/model/generated/remark.model.ts index 85ed6bc..a1442e3 100644 --- a/src/model/generated/remark.model.ts +++ b/src/model/generated/remark.model.ts @@ -34,5 +34,8 @@ export class Remark { fee!: Amount | undefined | null @Column_("text", {nullable: true}) - rawValue!: string | undefined | null + dataRaw!: string | undefined | null + + @Column_("text", {nullable: true}) + dataValue!: string | undefined | null } diff --git a/src/process/dataAvailabilityHandler.ts b/src/process/dataAvailabilityHandler.ts index 3989c50..085a73b 100644 --- a/src/process/dataAvailabilityHandler.ts +++ b/src/process/dataAvailabilityHandler.ts @@ -16,9 +16,8 @@ export class DataAvailabilityHandler implements IHandler { 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, @@ -28,8 +27,9 @@ export class DataAvailabilityHandler implements IHandler { blockNumber: call.extrinsic!.block.height, sender: accountInstance.getAccountId(addressHex), fee: new Amount(fee), - rawValue: await decodeData(call.args.data), - isJson: await isJson(call.args) ? true : false + dataRaw: call.args.data, + dataValue: dataValue, + isJson: isJson(dataValue) ? true : false })); } } @@ -52,7 +52,9 @@ function decodeData(str:string):string { let strInput = str.slice(2, str.length); const bytesData:Buffer = Buffer.from(strInput, 'hex'); let res = bytesData.toString().replace(//g, ''); - if(isValidHexString(res)) return res; + if (isValidHexString(res)) return res; + else if (isJson(res)) return res; + else if (isValidString(res)) return res; else return str; } @@ -60,3 +62,8 @@ 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); +} diff --git a/src/process/remarkHandler.ts b/src/process/remarkHandler.ts index e79d261..7b702f4 100644 --- a/src/process/remarkHandler.ts +++ b/src/process/remarkHandler.ts @@ -27,7 +27,8 @@ export class RemarkHandler implements IHandler { blockNumber: call.extrinsic!.block.height, sender: accountInstance.getAccountId(addressHex), fee:new Amount(fee), - rawValue: call.args.remark, + dataRaw: call.args.remark, + dataValue: call.args.remark, })); } }