Skip to content

Commit

Permalink
delegation: fixes get rewards totals route
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrap committed Nov 8, 2023
1 parent c380af8 commit 87f0944
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion services/delegation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"rlp": "^3.0.0",
"rxjs": "^7.5.5",
"swagger-ui-express": "^4.3.0",
"typeorm": "^0.2.45",
"typeorm": "^0.2.34",
"web3": "^1.7.1",
"yargs": "^17.3.1"
},
Expand Down
2 changes: 0 additions & 2 deletions services/delegation/src/modules/reward/reward.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import { TotalReward, RewardRepository } from './reward.repository';
import { RewardService } from './reward.service';
import { Epoch, getEpochs } from './helper/epoch';
import { RewardQueryDto } from './dto/reward-query.dto';
import { Inject } from '@nestjs/common';

@ApiTags('rewards')
@Controller('rewards')
export class RewardsController {
constructor(
@Inject('RewardRepository')
private rewardRepository: RewardRepository,
private rewardService: RewardService,
) {}
Expand Down
26 changes: 17 additions & 9 deletions services/delegation/src/modules/reward/reward.repository.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { EntityRepository, Raw, Repository } from 'typeorm';
import { Repository } from 'typeorm';
import { RewardQueryDto } from './dto/reward-query.dto';
import { Reward } from './reward.entity';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

export interface TotalReward {
address: string;
amount: number;
}

@EntityRepository(Reward)
export class RewardRepository extends Repository<Reward> {
@Injectable()
export class RewardRepository {
constructor(
@InjectRepository(Reward)
private readonly usersRepository: Repository<Reward>,
) {}
async groupByAddress(query: RewardQueryDto): Promise<TotalReward[]> {
const { type, epoch, user, address } = query;

let rewardsQuery = this.createQueryBuilder()
let rewardsQuery = this.usersRepository
.createQueryBuilder()
.select(['SUM(value) as amount', 'LOWER(user_address) as address'])
.groupBy('address')
.where('user_address IS NOT NULL')
Expand All @@ -29,11 +36,12 @@ export class RewardRepository extends Repository<Reward> {
}

if (address) {
rewardsQuery = rewardsQuery.andWhere({
userAddress: Raw((alias) => `LOWER(${alias}) LIKE LOWER(:address)`, {
address,
}),
});
rewardsQuery = rewardsQuery.andWhere(
`LOWER(user_Address) LIKE LOWER(:address)`,
{
address: `%${address}%`,
},
);
}

return rewardsQuery.getRawMany();
Expand Down
5 changes: 2 additions & 3 deletions services/delegation/src/modules/reward/reward.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';
import moment from 'moment';
import * as ethers from 'ethers';
import { Raw } from 'typeorm';
import { Raw, Repository } from 'typeorm';
import { Interval } from '@flatten-js/interval-tree';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
Expand All @@ -21,7 +21,6 @@ import { Delegation } from '../delegation/delegation.entity';
import { Node } from '../node/node.entity';
import { NodeCommission } from '../node/node-commission.entity';
import { Reward } from './reward.entity';
import { RewardRepository } from './reward.repository';
import { RewardType } from './reward-type.enum';

@Injectable()
Expand All @@ -31,7 +30,7 @@ export class RewardService {
private stakingDataService: StakingDataService,
private delegationDataService: DelegationDataService,
@InjectRepository(Reward)
private rewardRepository: RewardRepository,
private rewardRepository: Repository<Reward>,
private config: ConfigService,
private userService: UserService,
) {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22541,7 +22541,7 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==

typeorm@^0.2.34, typeorm@^0.2.45:
typeorm@^0.2.34:
version "0.2.45"
resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.45.tgz#e5bbb3af822dc4646bad96cfa48cd22fa4687cea"
integrity sha512-c0rCO8VMJ3ER7JQ73xfk0zDnVv0WDjpsP6Q1m6CVKul7DB9iVdWLRjPzc8v2eaeBuomsbZ2+gTaYr8k1gm3bYA==
Expand Down

0 comments on commit 87f0944

Please sign in to comment.