From c3a6026a6275b4c959b585a9f2a747c14888ec25 Mon Sep 17 00:00:00 2001 From: reyraa Date: Sat, 28 Oct 2023 11:12:04 +0200 Subject: [PATCH] Validte badge claim command --- .../modules/anchor/commands/create_command.ts | 7 ++-- .../modules/anchor/commands/vote_command.ts | 4 +-- src/app/modules/anchor/method.ts | 3 +- src/app/modules/anchor/utils.ts | 5 --- .../modules/badge/commands/claim_command.ts | 20 +++++++++++ src/app/utils.ts | 34 +++++-------------- 6 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/app/modules/anchor/commands/create_command.ts b/src/app/modules/anchor/commands/create_command.ts index 9a83a80..4b0983f 100644 --- a/src/app/modules/anchor/commands/create_command.ts +++ b/src/app/modules/anchor/commands/create_command.ts @@ -12,7 +12,8 @@ import { AnchorAccountStore } from '../stores/anchorAccount'; import { AnchorCreated } from '../events/anchorCreated'; import { CreateCommandParams, Anchor, AnchorAccount } from '../types'; import { createCommandParamsSchema } from '../schemas'; -import { getCreatedAt, getAnchorID } from '../utils'; +import { getAnchorID } from '../utils'; +import { getCreatedAt } from '../../../utils'; import { VOTE_RATE_LIMIT } from '../constants'; import { BadgeMethod } from '../../badge/method'; import { Badges } from '../../badge/types'; @@ -50,7 +51,7 @@ export class CreateCommand extends BaseCommand { if (IDS.length >= VOTE_RATE_LIMIT) { const anchor = await anchorStore.get(context, IDS[0]); - if (anchor.createdAt === getCreatedAt(Math.floor(new Date().getTime()))) { + if (anchor.createdAt === getCreatedAt(new Date())) { throw new Error( `You have exceeded the ${VOTE_RATE_LIMIT} anchor submissions daily limit.`, ); @@ -73,7 +74,7 @@ export class CreateCommand extends BaseCommand { // Create anchor ID const anchorID = getAnchorID(context.params); - const createdAt = getCreatedAt(new Date().getTime()); + const createdAt = getCreatedAt(new Date()); // Create anchor object const anchor: Anchor = { ...params, diff --git a/src/app/modules/anchor/commands/vote_command.ts b/src/app/modules/anchor/commands/vote_command.ts index 7941e24..1958094 100644 --- a/src/app/modules/anchor/commands/vote_command.ts +++ b/src/app/modules/anchor/commands/vote_command.ts @@ -15,7 +15,7 @@ import { AnchorAccountStore } from '../stores/anchorAccount'; import { VoteCommandParams, AnchorAccount, Anchor } from '../types'; import { voteCommandParamsSchema } from '../schemas'; import { CONTRIBUTION_FEE, VOTE_RATE_LIMIT } from '../constants'; -import { getCreatedAt } from '../utils'; +import { getCreatedAt } from '../../../utils'; import { TREASURY_ADDRESS } from '../../../constants'; import { BadgeMethod } from '../../badge/method'; import { AnchorStatsStore } from '../stores/anchorStats'; @@ -62,7 +62,7 @@ export class VoteCommand extends BaseCommand { if (IDS.length >= VOTE_RATE_LIMIT) { const thresholdAnchor = await anchorStore.get(context, IDS[0]); - if (thresholdAnchor.createdAt === getCreatedAt(Math.floor(new Date().getTime()))) { + if (thresholdAnchor.createdAt === getCreatedAt(new Date())) { throw new Error(`You have exceeded the ${VOTE_RATE_LIMIT} vote submissions daily limit.`); } } diff --git a/src/app/modules/anchor/method.ts b/src/app/modules/anchor/method.ts index b24539f..53d9052 100644 --- a/src/app/modules/anchor/method.ts +++ b/src/app/modules/anchor/method.ts @@ -23,7 +23,8 @@ export class AnchorMethod extends BaseMethod { // Get VotesCount by date public async getVoteCounts(context: MethodContext, date: string): Promise { const anchorStatsStore = this.stores.get(AnchorStatsStore); - return getVoteCounts(context, date, anchorStatsStore); + const response = await getVoteCounts(context, date, anchorStatsStore); + return response; } } diff --git a/src/app/modules/anchor/utils.ts b/src/app/modules/anchor/utils.ts index 36d9282..29bda96 100644 --- a/src/app/modules/anchor/utils.ts +++ b/src/app/modules/anchor/utils.ts @@ -2,8 +2,3 @@ import { CreateCommandParams } from './types'; export const getAnchorID = (params: CreateCommandParams): Buffer => Buffer.concat([Buffer.from(params.spotifyId, 'utf8')]); - -export const getCreatedAt = (timestamp: number): string => { - const date = new Date(timestamp); - return date.toISOString().substring(0, 10); -}; diff --git a/src/app/modules/badge/commands/claim_command.ts b/src/app/modules/badge/commands/claim_command.ts index 1860c07..24f0825 100644 --- a/src/app/modules/badge/commands/claim_command.ts +++ b/src/app/modules/badge/commands/claim_command.ts @@ -42,6 +42,26 @@ export class ClaimCommand extends BaseCommand { error: new Error('You are not authorized to claim this badge.'), }; } + // Only claim unclaimed badges + if (badgeNFT.claimed) { + return { + status: VerifyStatus.FAIL, + error: new Error('This badge has already been claimed.'), + }; + } + + // Only claim badges of 3 dys ago + const awardDate = new Date(badgeNFT.awardDate); + + const threeDaysAgo = new Date(); + threeDaysAgo.setDate(threeDaysAgo.getDate() - 3); + + if (awardDate > threeDaysAgo) { + return { + status: VerifyStatus.FAIL, + error: new Error('This badge cannot be claimed yet.'), + }; + } return { status: VerifyStatus.OK }; } diff --git a/src/app/utils.ts b/src/app/utils.ts index 64d11f8..73a20c1 100644 --- a/src/app/utils.ts +++ b/src/app/utils.ts @@ -1,25 +1,9 @@ -// import { createHash } from 'crypto'; -// import { ed } from '@liskhq/lisk-cryptography'; -// import { codec } from '@liskhq/lisk-codec'; -// import { Transaction } from './types'; -// import { baseTransactionSchema } from './schemas'; - -// // export const getEntityID = (transaction: Transaction): Buffer => { -// // const txBytes = codec.encode(baseTransactionSchema, transaction); -// // return createHash('md5').update(txBytes).digest(); -// // }; - -// // export const verifyHash = (signature: Buffer, message: Buffer, publicKey: Buffer) => { -// // let isCorrect = false; - -// // try { -// // isCorrect = ed.verifyMessageWithPublicKey({ -// // message: message.toString('hex'), -// // publicKey, -// // signature, -// // }); -// // } catch (e) { -// // isCorrect = false; -// // } -// // return isCorrect; -// // }; +export const getCreatedAt = (value: number|Date): string => { + let date: Date; + if (typeof value === 'number') { + date = new Date(value); + } else { + date = value; + } + return date.toISOString().substring(0, 10); +};