diff --git a/src/config/scriptions_cfg.ts b/src/config/scriptions_cfg.ts index 69a9c23..5d006f6 100644 --- a/src/config/scriptions_cfg.ts +++ b/src/config/scriptions_cfg.ts @@ -1,4 +1,5 @@ import { CheckIn } from "models/CheckIn" +import { utf8ToHex } from "utils/string.util" export interface IScriptionCfg { chain: number, @@ -8,15 +9,19 @@ export interface IScriptionCfg { process: (event: any) => Promise, } +const CHECKIN_DATA_STR = 'data:,{"p":"cf-20","op":"check"}' +const CHECKIN_DATA_HEX = '0x'+utf8ToHex(CHECKIN_DATA_STR) +const CHECKIN_ADDRESS = '0x50a8e60041a206acaa5f844a1104896224be6f39' + export const SCRIPTIONS_CFG: IScriptionCfg[] = [ { chain: 421614, // rpc: 'https://arbitrum-sepolia.infura.io/v3/25559ac58e714177b31ff48d507e7ac9', rpc: 'https://arb-sepolia.g.alchemy.com/v2/EKR1je8ZGia332kkemNc4mtXQuFskIq3', - fromBlock: 5063559, + fromBlock: 5624211, filter: (event: any) => { - return ( event.input === '0x646174613a2c7b2270223a2263662d3230222c226f70223a22636869636b227d' - && event.to.toLowerCase() === '0x50a8e60041a206acaa5f844a1104896224be6f39') + return ( event.input === CHECKIN_DATA_HEX + && event.to.toLowerCase() === CHECKIN_ADDRESS) }, process: async (tx: any) => { return CheckIn.saveEvent(tx) diff --git a/src/controllers/task.controllers.ts b/src/controllers/task.controllers.ts index 3262e5c..6385b63 100644 --- a/src/controllers/task.controllers.ts +++ b/src/controllers/task.controllers.ts @@ -13,7 +13,7 @@ class TaskController extends BaseController { if (!address || (!days && !limit)) { throw new ZError(10, 'params mismatch') } - let query: any = { from: address } + let query: any = { from: address.toLowerCase() } if (!limit) { if (typeof days === 'number') { let begin = getNDayAgo(days, true) @@ -31,9 +31,9 @@ class TaskController extends BaseController { } let records if (limit) { - records = await CheckIn.find(query).limit(limit) + records = await CheckIn.find(query).sort({_id: -1}).limit(limit) } else { - records = await CheckIn.find(query) + records = await CheckIn.find(query).sort({_id: -1}) } let result = [] for (let record of records) { diff --git a/src/models/CheckIn.ts b/src/models/CheckIn.ts index 01b2325..cf42e2c 100644 --- a/src/models/CheckIn.ts +++ b/src/models/CheckIn.ts @@ -1,6 +1,7 @@ import { getModelForClass, index, modelOptions, prop } from '@typegoose/typegoose' import { dbconn } from 'decorators/dbconn' import { BaseModule } from './Base' +import { formatDate, yesterday } from 'utils/date.util' @dbconn() @index({ from: 1 }, { unique: false }) @@ -24,12 +25,20 @@ export class CheckInClass extends BaseModule { public blockTime: number @prop() public dateTag: string + // 连签天数 + @prop({default: 0}) + public count: number @prop() public value: string @prop() public input: string public static async saveEvent(event: any) { + const preDay = formatDate(yesterday()); + const preDayEvent = await CheckIn.findOne({ from: event.from, dateTag: preDay }) + if (preDayEvent) { + event.count = preDayEvent.count + 1 + } return CheckIn.insertOrUpdate({ hash: event.hash }, event) } @@ -38,6 +47,7 @@ export class CheckInClass extends BaseModule { address: this.from, day: this.dateTag, time: this.blockTime, + count: this.count } } } diff --git a/src/utils/string.util.ts b/src/utils/string.util.ts index 62dba1b..d923052 100644 --- a/src/utils/string.util.ts +++ b/src/utils/string.util.ts @@ -104,3 +104,21 @@ export function string62to10(numberCode: string) { } return originNumber } + +export function hexToUtf8(hexString) { + // Remove any leading "0x" prefix and split into pairs of characters + let _hexString = hexString.replace(/^0x/, ""); + let buffer = Buffer.from(_hexString, 'hex') + return buffer.toString('utf8') +} + +export function utf8ToHex(utf8String) { + // Create a Buffer object from the UTF-8 string + const buffer = Buffer.from(utf8String, "utf8"); + + // Convert the Buffer object to a hex string + const hexString = buffer.toString("hex"); + + return hexString; +} +