Skip to content

Commit

Permalink
Merge pull request #13 from near-daos/feature/bounty-end-time
Browse files Browse the repository at this point in the history
feature(bounty end time): added endTime field to bounty claim
  • Loading branch information
vhorin-mp authored Nov 28, 2021
2 parents e97cc61 + dde7aae commit 90749ee
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 8 deletions.
8 changes: 7 additions & 1 deletion apps/aggregator/src/bounty-aggregator/types/bounty.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import camelcaseKeys from 'camelcase-keys';
import { btoaJSON, buildBountyClaimId, buildBountyId } from '@sputnik-v2/utils';
import {
btoaJSON,
buildBountyClaimId,
buildBountyId,
calculateClaimEndTime,
} from '@sputnik-v2/utils';
import { Transaction } from '@sputnik-v2/near-indexer';
import { Dao } from '@sputnik-v2/dao';
import {
Expand All @@ -16,6 +21,7 @@ export function castBountyClaim(
) {
const claimDto = {
...claim,
endTime: calculateClaimEndTime(claim.startTime, claim.deadline),
id: buildBountyClaimId(dao.id, bounty.id, claim.startTime),
bounty: {
id: buildBountyId(dao.id, bounty.id),
Expand Down
1 change: 1 addition & 0 deletions libs/bounty/src/dto/bounty-claim.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class BountyClaimDto extends TransactionInfo {
startTime: string;
/// Deadline specified by claimer.
deadline: string;
endTime: string;
/// Completed?
completed: boolean;
}
8 changes: 2 additions & 6 deletions libs/bounty/src/entities/bounty-claim.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { AfterLoad, Column, Entity, ManyToOne, PrimaryColumn } from 'typeorm';
import { Column, Entity, ManyToOne, PrimaryColumn } from 'typeorm';
import { TransactionEntity } from '@sputnik-v2/common';

import { Bounty } from './bounty.entity';
Expand Down Expand Up @@ -30,10 +30,6 @@ export class BountyClaim extends TransactionEntity {
completed: boolean;

@ApiProperty()
@Column({ nullable: true })
endTime: string;

@AfterLoad()
getEndTime() {
this.endTime = (BigInt(this.startTime) + BigInt(this.deadline)).toString();
}
}
3 changes: 2 additions & 1 deletion libs/migrations/src/migration.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import configuration, {
} from '@sputnik-v2/config/aggregator-dao-config';
import { Token } from '@sputnik-v2/token';
import { Dao } from '@sputnik-v2/dao';
import { BountyClaim } from '@sputnik-v2/bounty';

import migrationScripts from './scripts';

Expand All @@ -19,7 +20,7 @@ import migrationScripts from './scripts';
TypeOrmModule.forRootAsync({
useClass: TypeOrmConfigService,
}),
TypeOrmModule.forFeature([Dao, Token]),
TypeOrmModule.forFeature([Dao, Token, BountyClaim]),
ConfigModule.forRoot({
isGlobal: true,
load: configuration,
Expand Down
38 changes: 38 additions & 0 deletions libs/migrations/src/scripts/bounty-claim-end-time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Dao } from '@sputnik-v2/dao';
import { calculateClaimEndTime } from '@sputnik-v2/utils';
import { BountyClaim } from '@sputnik-v2/bounty';

import { Migration } from '..';

@Injectable()
export class BountyClaimEndTimeMigration implements Migration {
private readonly logger = new Logger(BountyClaimEndTimeMigration.name);

constructor(
@InjectRepository(BountyClaim)
private readonly bountyClaimRepository: Repository<BountyClaim>,
) {}

public async migrate(): Promise<void> {
this.logger.log('Starting Bounty Claim End Time migration...');

this.logger.log('Collecting bounty claims...');
const bountyClaims = await this.bountyClaimRepository.find();

this.logger.log('Setting endTime field...');
await this.bountyClaimRepository.save(
bountyClaims.map((bountyClaim) => {
return {
...bountyClaim,
endTime: calculateClaimEndTime(
bountyClaim.startTime,
bountyClaim.deadline,
),
};
}),
);
}
}
2 changes: 2 additions & 0 deletions libs/migrations/src/scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { ProposalPurgeMigration } from './proposal-purge.migration';
import { ProposalActionsMigration } from './proposal-actions.migration';
import { TokenIdsMigration } from './token-ids.migration';
import { DaoMetadataMigration } from './dao-metadata.migration';
import { BountyClaimEndTimeMigration } from './bounty-claim-end-time';

export default [
ProposalPurgeMigration,
ProposalActionsMigration,
TokenIdsMigration,
DaoMetadataMigration,
BountyClaimEndTimeMigration,
];
2 changes: 2 additions & 0 deletions libs/sputnikdao/src/sputnik.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
buildBountyId,
buildProposalId,
buildRoleId,
calculateClaimEndTime,
} from '@sputnik-v2/utils';

import { castRolePermission, castVotePolicy } from './types';
Expand Down Expand Up @@ -195,6 +196,7 @@ export class SputnikDaoService {
.filter((claim) => bounty.id === claim.bountyId)
.map((claim) => ({
...camelcaseKeys(claim),
endTime: calculateClaimEndTime(claim.startTime, claim.deadline),
id: buildBountyClaimId(contractId, bounty.id, claim.startTime),
bounty: {
id: buildBountyId(contractId, bounty.id),
Expand Down
2 changes: 2 additions & 0 deletions libs/transaction-handler/src/types/bounty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BountyDto, BountyClaimDto } from '@sputnik-v2/bounty/dto';
import {
buildBountyClaimId,
buildBountyId,
calculateClaimEndTime,
getBlockTimestamp,
} from '@sputnik-v2/utils';

Expand Down Expand Up @@ -39,6 +40,7 @@ export function castBountyClaims({
const claim = camelcaseKeys(bountyClaim);
return {
...claim,
endTime: calculateClaimEndTime(claim.startTime, claim.deadline),
id: buildBountyClaimId(contractId, claim.bountyId, claim.startTime),
accountId,
bounty: {
Expand Down
7 changes: 7 additions & 0 deletions libs/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,10 @@ export function paginate<T>(
export const sleep = async (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

export const calculateClaimEndTime = (
startTime = '0',
deadline = '0',
): string => {
return (BigInt(startTime) + BigInt(deadline)).toString();
};

0 comments on commit 90749ee

Please sign in to comment.