Skip to content

Commit

Permalink
Add GET /winnerLiquidityProvisions
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemKolodko committed Dec 1, 2024
1 parent 4657517 commit 903e965
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {AddCommentDto, GetCommentsDto} from "./dto/comment.dto";
import {AppService} from "./app.service";
import {GetTokenBalancesDto, GetTokenBurnsDto, GetTokensDto, GetTokenWinnersDto} from "./dto/token.dto";
import {GetCandlesDto, GetTradesDto} from "./dto/trade.dto";
import {AddUserDto} from "./dto/user.dto";
import {UserService} from "./user/user.service";
import {FileInterceptor} from "@nestjs/platform-express";
import {GcloudService} from "./gcloud/gcloud.service";
Expand All @@ -26,6 +25,7 @@ import {AddTokenMetadataDto} from "./dto/metadata.dto";
import {AuthGuard} from "./common/auth.guard";
import {plainToInstance} from "class-transformer";
import {JwtUserAccount} from "./entities/user-account.entity";
import {GetWinnerLiquidityProvisionsDto} from "./dto/winner.liquidity.dto";

@SkipThrottle()
@ApiTags('app')
Expand Down Expand Up @@ -102,6 +102,11 @@ export class AppController {
return this.appService.getTokenBurns(dto)
}

@Get('/winnerLiquidityProvisions')
getWinnerLiquidityProvisions(@Query() dto: GetWinnerLiquidityProvisionsDto) {
return this.appService.getWinnerLiquidityProvisions(dto)
}

@Post('/uploadImage')
@UseGuards(AuthGuard)
@ApiBearerAuth()
Expand Down
19 changes: 18 additions & 1 deletion src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {Injectable, Logger} from '@nestjs/common';
import {DataSource, EntityManager, MoreThan} from "typeorm";
import {Comment, Token, TokenBalance, TokenBurn, TokenWinner, Trade, UserAccount} from "./entities";
import {Comment, LiquidityProvision, Token, TokenBalance, TokenBurn, TokenWinner, Trade, UserAccount} from "./entities";
import {AddCommentDto, GetCommentsDto} from "./dto/comment.dto";
import {GetTokenBalancesDto, GetTokenBurnsDto, GetTokensDto, GetTokenWinnersDto} from "./dto/token.dto";
import {GetCandlesDto, GetTradesDto} from "./dto/trade.dto";
import {UserService} from "./user/user.service";
import {Candle} from "./types";
import {GetWinnerLiquidityProvisionsDto} from "./dto/winner.liquidity.dto";

@Injectable()
export class AppService {
Expand Down Expand Up @@ -114,6 +115,22 @@ export class AppService {
})
}

async getWinnerLiquidityProvisions(dto: GetWinnerLiquidityProvisionsDto){
return await this.dataSource.manager.find(LiquidityProvision, {
where: {
token: {
address: dto.tokenAddress
},
sender: dto.sender
},
take: dto.limit,
skip: dto.offset,
order: {
timestamp: 'desc'
}
})
}

async getCandles(dto: GetCandlesDto){
const query = this.dataSource.getRepository(Trade)
.createQueryBuilder('trades')
Expand Down
32 changes: 32 additions & 0 deletions src/dto/winner.liquidity.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {ApiProperty} from "@nestjs/swagger";
import {Transform, Type} from "class-transformer";
import {IsOptional, IsString} from "class-validator";

export class GetWinnerLiquidityProvisionsDto {
@ApiProperty({ type: String, required: true })
@Transform((address) => address ? address.value.trim().toLowerCase() : address)
@Type(() => String)
@IsString()
tokenAddress: string;

@ApiProperty({ type: String, required: false })
@Transform((address) => address ? address.value.trim().toLowerCase() : address)
@Type(() => String)
@IsString()
@IsOptional()
sender?: string;

@ApiProperty({ type: Number, required: false, default: '100' })
// @Transform((limit) => limit.value.toNumber())
@Type(() => String)
@IsString()
@IsOptional()
limit: number;

@ApiProperty({ type: Number, required: false, default: '0' })
// @Transform((offset) => offset.value.toNumber())
@Type(() => String)
@IsString()
@IsOptional()
offset: number;
}
12 changes: 6 additions & 6 deletions src/indexer/indexer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export class IndexerService {
this.logger.log(`BurnTokenAndMintWinner: senderAddress=${senderAddress}, tokenAddress=${tokenAddress}, winnerTokenAddress=${winnerTokenAddress}, burnedAmount=${burnedAmount}, receivedETH=${receivedETH}, mintedAmount=${mintedAmount}, txnHash=${txnHash}`);
}

private async processLiquidityProvisionEvent(event: EventLog, transactionalEntityManager: EntityManager) {
private async processWinnerLiquidityEvent(event: EventLog, transactionalEntityManager: EntityManager) {
const txnHash = event.transactionHash.toLowerCase()
const values = event.returnValues
const tokenAddress = (values['tokenAddress'] as string).toLowerCase()
Expand Down Expand Up @@ -417,7 +417,7 @@ export class IndexerService {
fromBlock, toBlock, topics: [ this.web3.utils.sha3('SetWinner(address,uint256,uint256)')],
}) as EventLog[];

const liquidityProvisionEvents = await this.tokenFactoryContract.getPastEvents('allEvents', {
const winnerLiquidityEvents = await this.tokenFactoryContract.getPastEvents('allEvents', {
fromBlock, toBlock, topics: [ this.web3.utils.sha3('WinnerLiquidityAdded(address,address,address,address,uint256,uint128,uint256,uint256,uint256)')],
}) as EventLog[];

Expand Down Expand Up @@ -464,7 +464,7 @@ export class IndexerService {
.concat(...sellEvents.map(data => ({ type: 'sell', data })))
.concat(...setWinnerEvents.map(data => ({ type: 'set_winner', data })))
.concat(...burnAndSetWinnerEvents.map(data => ({ type: 'burn_token_and_set_winner', data })))
.concat(...liquidityProvisionEvents.map(data => ({ type: 'liquidity_provision', data })))
.concat(...winnerLiquidityEvents.map(data => ({ type: 'winner_liquidity', data })))
.concat(...newCompetitionEvents.map(data => ({ type: 'new_competition', data })))
.sort((a, b) => {
const blockNumberDiff = Number(a.data.blockNumber) - Number(b.data.blockNumber)
Expand Down Expand Up @@ -498,8 +498,8 @@ export class IndexerService {
await this.processBurnTokenAndSetWinnerEvent(data, transactionalEntityManager)
break;
}
case 'liquidity_provision': {
await this.processLiquidityProvisionEvent(data, transactionalEntityManager)
case 'winner_liquidity': {
await this.processWinnerLiquidityEvent(data, transactionalEntityManager)
break;
}
case 'new_competition': {
Expand All @@ -510,7 +510,7 @@ export class IndexerService {
}
})

this.logger.log(`[${fromBlock}-${toBlock}] (${((toBlock - fromBlock + 1))} blocks), new tokens=${tokenCreatedEvents.length}, trade=${[...buyEvents, ...sellEvents].length} (buy=${buyEvents.length}, sell=${sellEvents.length}), SetWinner=${setWinnerEvents.length}, WinnerLiquidityAdded=${liquidityProvisionEvents.length}, NewCompetitionStarted=${newCompetitionEvents.length}`)
this.logger.log(`[${fromBlock}-${toBlock}] (${((toBlock - fromBlock + 1))} blocks), new tokens=${tokenCreatedEvents.length}, trade=${[...buyEvents, ...sellEvents].length} (buy=${buyEvents.length}, sell=${sellEvents.length}), SetWinner=${setWinnerEvents.length}, WinnerLiquidityAdded=${winnerLiquidityEvents.length}, NewCompetitionStarted=${newCompetitionEvents.length}`)
} else {
// Wait for blockchain
toBlock = fromBlockParam - 1
Expand Down

0 comments on commit 903e965

Please sign in to comment.