每日签到增加连签标志

This commit is contained in:
CounterFire2023 2024-01-08 19:01:10 +08:00
parent ad86cb3eb8
commit 0bb8709fcd
4 changed files with 39 additions and 6 deletions

View File

@ -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<void>,
}
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)

View File

@ -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) {

View File

@ -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
}
}
}

View File

@ -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;
}