Skip to content

Commit

Permalink
Validte badge claim command
Browse files Browse the repository at this point in the history
  • Loading branch information
reyraa committed Oct 28, 2023
1 parent 4b870e1 commit c3a6026
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
7 changes: 4 additions & 3 deletions src/app/modules/anchor/commands/create_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.`,
);
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/anchor/commands/vote_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.`);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/modules/anchor/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export class AnchorMethod extends BaseMethod {
// Get VotesCount by date
public async getVoteCounts(context: MethodContext, date: string): Promise<number> {
const anchorStatsStore = this.stores.get(AnchorStatsStore);
return getVoteCounts(context, date, anchorStatsStore);
const response = await getVoteCounts(context, date, anchorStatsStore);
return response;
}
}

5 changes: 0 additions & 5 deletions src/app/modules/anchor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
20 changes: 20 additions & 0 deletions src/app/modules/badge/commands/claim_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

Expand Down
34 changes: 9 additions & 25 deletions src/app/utils.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(transaction: Transaction<T>): 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);
};

0 comments on commit c3a6026

Please sign in to comment.