Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
fix: decompose the routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dalechyn committed Jan 4, 2023
1 parent 7a0bf10 commit d3375ac
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 98 deletions.
123 changes: 118 additions & 5 deletions src/transactions/transactions.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,128 @@
import { Controller, Get, Param, Query } from '@nestjs/common'
import {
Controller,
Get,
Post,
Param,
Query,
Body,
HttpCode
} from '@nestjs/common'
import {
NFTPortRetrieveContractsOwnedByAnAccount,
NFTPortTransaction
} from '../nftport/nftport.responses.interface'
import { TransactionsService } from './transactions.service'

@Controller('transactions')
export class TransactionsController {
constructor(private readonly appService: TransactionsService) {}
constructor(private readonly service: TransactionsService) {}

@Get(':address')
@Get('/transactions/:address')
getTransactions(
@Param('address') address: string,
@Query() options: Record<string, boolean>
@Query('continuation') continuation: string
) {
return this.appService.getTransactions(address.toLowerCase(), options)
return this.service.getTransactions(address.toLowerCase(), continuation)
}

@Get('/owned-contracts/:address')
getOwnedContracts(@Param('address') address: string) {
return this.service.getOwnedContracts(address.toLowerCase())
}

@Post('/biggest-sale/:address')
@HttpCode(200)
getBiggestSale(
@Body('transactions') transactions: NFTPortTransaction[],
@Param('address') address: string
) {
return this.service.getBiggestNFTSale(transactions, address.toLowerCase())
}

@Post('/biggest-purchase/:address')
@HttpCode(200)
getBiggestPurchase(
@Body('transactions') transactions: NFTPortTransaction[],
@Param('address') address: string
) {
return this.service.getBiggestNFTPurchase(
transactions,
address.toLowerCase()
)
}

@Post('/total-spent-on-mint/:address')
@HttpCode(200)
getTotalSpentOnMint(
@Body('transactions') transactions: NFTPortTransaction[],
@Param('address') address: string
) {
return this.service.getTotalSpentOnMintInETH(
transactions,
address.toLowerCase()
)
}

@Post('/total-minted/:address')
@HttpCode(200)
getTotalMinted(
@Body('transactions') transactions: NFTPortTransaction[],
@Param('address') address: string
) {
return this.service.getTotalNFTsMinted(transactions, address.toLowerCase())
}

@Post('/total-bought/:address')
@HttpCode(200)
getTotalBought(
@Body('transactions') transactions: NFTPortTransaction[],
@Param('address') address: string
) {
return this.service.getNFTsBoughtQuantity(
transactions,
address.toLowerCase()
)
}

@Post('/total-sold/:address')
@HttpCode(200)
getTotalSold(
@Body('transactions') transactions: NFTPortTransaction[],
@Param('address') address: string
) {
return this.service.getNFTsSoldQuantity(transactions, address.toLowerCase())
}

@Post('/total-bought-in-eth/:address')
@HttpCode(200)
getTotalBoughtInETH(
@Body('transactions') transactions: NFTPortTransaction[],
@Param('address') address: string
) {
return this.service.getTotalBoughtInETH(transactions, address.toLowerCase())
}

@Post('/total-sold-in-eth/:address')
@HttpCode(200)
getTotalSoldInETH(
@Body('transactions') transactions: NFTPortTransaction[],
@Param('address') address: string
) {
return this.service.getTotalSoldInETH(transactions, address.toLowerCase())
}

@Post('/average-hold-time/:address')
@HttpCode(200)
getAverageHoldTime(@Body('transactions') transactions: NFTPortTransaction[]) {
return this.service.getAverageHoldTime(transactions)
}

@Post('/bluechips/:address')
@HttpCode(200)
getBluechips(
@Body('contracts')
ownedContracts: NFTPortRetrieveContractsOwnedByAnAccount[]
) {
return this.service.getBluechips(ownedContracts)
}
}
141 changes: 48 additions & 93 deletions src/transactions/transactions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class TransactionsService {
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache
) {}

private async getNFTsBought(
public async getNFTsBought(
transactions: NFTPortTransaction[],
address: string
) {
Expand All @@ -30,7 +30,18 @@ export class TransactionsService {
.filter(({ buyer_address }) => buyer_address === address)
}

private async getNFTsSold(
public async getNFTsBoughtQuantity(
transactions: NFTPortTransaction[],
address: string
) {
const boughtNFTs = await this.getNFTsBought(transactions, address)
return boughtNFTs.reduce(
(total, current) => total + (current.quantity ?? 0),
0
)
}

public async getNFTsSold(
transactions: NFTPortTransaction[],
address: string
) {
Expand All @@ -39,6 +50,17 @@ export class TransactionsService {
.filter(({ seller_address }) => seller_address === address)
}

public async getNFTsSoldQuantity(
transactions: NFTPortTransaction[],
address: string
) {
const soldNFTs = await this.getNFTsSold(transactions, address)
return soldNFTs.reduce(
(total, current) => total + (current.quantity ?? 0),
0
)
}

private async getNFTsMinted(
transactions: NFTPortTransaction[],
address: string
Expand All @@ -51,7 +73,7 @@ export class TransactionsService {
)
}

private async getTotalSpentOnMintInETH(
public async getTotalSpentOnMintInETH(
transactions: NFTPortTransaction[],
address: string
) {
Expand All @@ -75,15 +97,15 @@ export class TransactionsService {
return parseFloat(formatEther(totalSpent))
}

private async getTotalNFTsMinted(
public async getTotalNFTsMinted(
transactions: NFTPortTransaction[],
address: string
) {
const minted = await this.getNFTsMinted(transactions, address)
return minted.reduce((total, current) => total + current.quantity, 0)
}

private async getBiggestNFTSale(
public async getBiggestNFTSale(
transactions: NFTPortTransaction[],
address: string
) {
Expand All @@ -98,7 +120,7 @@ export class TransactionsService {
}, soldInETH[0]?.price_details.price ?? 0)
}

private async getBiggestNFTPurchase(
public async getBiggestNFTPurchase(
transactions: NFTPortTransaction[],
address: string
) {
Expand All @@ -113,7 +135,7 @@ export class TransactionsService {
}, boughtInETH[0]?.price_details.price ?? 0)
}

private async getTotalSoldInETH(
public async getTotalSoldInETH(
transactions: NFTPortTransaction[],
address: string
) {
Expand All @@ -127,7 +149,7 @@ export class TransactionsService {
}, 0)
}

private async getTotalBoughtInETH(
public async getTotalBoughtInETH(
transactions: NFTPortTransaction[],
address: string
) {
Expand All @@ -141,7 +163,7 @@ export class TransactionsService {
}, 0)
}

private async getCoolHoldings(
public async getBluechips(
ownedContracts: NFTPortRetrieveContractsOwnedByAnAccount[]
) {
const ownedContractsStatistics = await Promise.all(
Expand All @@ -150,6 +172,12 @@ export class TransactionsService {
)
)

console.log(
ownedContractsStatistics.filter(
(data) => data && data.floor_price >= 1 && data.thirty_day_volume >= 30
)
)

return ownedContractsStatistics.filter(
(data) => data && data.floor_price >= 1 && data.thirty_day_volume >= 30
).length
Expand All @@ -160,7 +188,7 @@ export class TransactionsService {
else return key
}

private async getAverageHoldTime(transactions: NFTPortTransaction[]) {
public async getAverageHoldTime(transactions: NFTPortTransaction[]) {
const transfers = transactions.filter(NFTPortTransactionGuard.isTransfer)

const richTranfers = new Array<NFTPortTransactionTransfer>()
Expand Down Expand Up @@ -206,91 +234,18 @@ export class TransactionsService {
}
}

public async getOwnedContracts(address: string): Promise<any> {
return this.nftportService.getContractsOwnedByAnAccount('ethereum', address)
}

async getTransactions(
address: string,
options: {
biggestSale?: boolean
biggestPurchase?: boolean
totalBought?: boolean
totalSold?: boolean
totalBoughtInETH?: boolean
totalSoldInETH?: boolean
totalSpentOnMint?: boolean
totalNFTsMinted?: boolean
bluechips?: boolean
avgHoldTime?: boolean
continuationTransactions?: string
continuationContracts?: string
}
continuationTransactions?: string
): Promise<any> {
if (Object.values(options).every((v) => !v)) return {}
let result = {}
const { transactions, continuation: continuationTransactions } =
await this.nftportService.getTransactionsByAddress(
'ethereum',
address,
options.continuationTransactions
)

const { contracts, continuation: continuationContracts } =
await this.nftportService.getContractsOwnedByAnAccount(
'ethereum',
address,
options.continuationContracts
)

const biggestSale =
options.biggestSale &&
(await this.getBiggestNFTSale(transactions, address))
if (biggestSale) result = { ...result, biggestSale }

const biggestPurchase =
options.biggestPurchase &&
(await this.getBiggestNFTPurchase(transactions, address))
if (biggestPurchase) result = { ...result, biggestPurchase }

const totalBought =
options.totalBought &&
(await this.getNFTsBought(transactions, address)).length
if (totalBought) result = { ...result, totalBought }

const totalSold =
options.totalSold &&
(await this.getNFTsSold(transactions, address)).length
if (totalSold) result = { ...result, totalSold }

const totalBoughtInETH =
options.totalBoughtInETH &&
(await this.getTotalBoughtInETH(transactions, address))
if (totalBoughtInETH) result = { ...result, totalBoughtInETH }

const totalSoldInETH =
options.totalSoldInETH &&
(await this.getTotalSoldInETH(transactions, address))
if (totalSoldInETH) result = { ...result, totalSoldInETH }

const totalSpentOnMint =
options.totalSpentOnMint &&
(await this.getTotalSpentOnMintInETH(transactions, address))
if (totalSpentOnMint) result = { ...result, totalSpentOnMint }

const totalNFTsMinted =
options.totalNFTsMinted &&
(await this.getTotalNFTsMinted(transactions, address))
if (totalNFTsMinted) result = { ...result, totalNFTsMinted }

const bluechips =
options.bluechips && (await this.getCoolHoldings(contracts))
if (bluechips) result = { ...result, bluechips }

const avgHoldTime =
options.avgHoldTime && (await this.getAverageHoldTime(transactions))
if (avgHoldTime) result = { ...result, ...avgHoldTime }

if (continuationContracts) result = { ...result, continuationContracts }
if (continuationTransactions)
result = { ...result, continuationTransactions }

return result
return await this.nftportService.getTransactionsByAddress(
'ethereum',
address,
continuationTransactions
)
}
}

0 comments on commit d3375ac

Please sign in to comment.