-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into 1.9.0
- Loading branch information
Showing
26 changed files
with
773 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
apps/server/prisma/migrations/20230322052857_add_unconfirmed_txn_hash/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-- AlterTable | ||
ALTER TABLE "BridgeEventTransactions" ADD COLUMN "blockHash" TEXT, | ||
ADD COLUMN "blockHeight" TEXT, | ||
ADD COLUMN "unconfirmedSendTransactionHash" TEXT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 95 additions & 79 deletions
174
apps/server/src/defichain/services/DeFiChainStatsService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,116 @@ | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
import { BadRequestException, HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||
import { Prisma } from '@prisma/client'; | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { SupportedDFCTokenSymbols } from '../../AppConfig'; | ||
import { PrismaService } from '../../PrismaService'; | ||
import { BridgedDfcToEvm, DeFiChainStats, DFCStatsDto } from '../DefichainInterface'; | ||
import { initializeEnumKeys } from '../../utils/EnumUtils'; | ||
import { BridgedDfcToEvm, BridgedDFCTokenSum, DeFiChainStats, DFCStatsDto } from '../DefichainInterface'; | ||
|
||
@Injectable() | ||
export class DeFiChainStatsService { | ||
private readonly NUMERICAL_PLACEHOLDER = '0.000000'; | ||
|
||
constructor(private prisma: PrismaService) {} | ||
|
||
async getDefiChainStats(date?: DFCStatsDto): Promise<DeFiChainStats> { | ||
const dateOnly = date ?? new Date().toISOString().slice(0, 10); | ||
const dateFrom = new Date(dateOnly as string); | ||
const today = new Date(); | ||
if (dateFrom > today) { | ||
throw new BadRequestException(`Cannot query future date.`); | ||
} | ||
dateFrom.setUTCHours(0, 0, 0, 0); // set to UTC +0 | ||
const dateTo = new Date(dateFrom); | ||
dateTo.setDate(dateFrom.getDate() + 1); | ||
try { | ||
const dateOnly = date ?? new Date().toISOString().slice(0, 10); | ||
const dateFrom = new Date(dateOnly as string); | ||
const today = new Date(); | ||
if (dateFrom > today) { | ||
throw new BadRequestException(`Cannot query future date.`); | ||
} | ||
dateFrom.setUTCHours(0, 0, 0, 0); // set to UTC +0 | ||
const dateTo = new Date(dateFrom); | ||
dateTo.setDate(dateFrom.getDate() + 1); | ||
|
||
const [totalTransactions, confirmedTransactions] = await Promise.all([ | ||
this.prisma.deFiChainAddressIndex.count({ | ||
where: { | ||
createdAt: { | ||
// new Date() creates date with current time and day and etc. | ||
gte: dateFrom.toISOString(), | ||
lt: dateTo.toISOString(), | ||
const [totalTransactions, confirmedTransactions] = await Promise.all([ | ||
this.prisma.deFiChainAddressIndex.count({ | ||
where: { | ||
createdAt: { | ||
// new Date() creates date with current time and day and etc. | ||
gte: dateFrom.toISOString(), | ||
lt: dateTo.toISOString(), | ||
}, | ||
}, | ||
}, | ||
}), | ||
}), | ||
|
||
this.prisma.deFiChainAddressIndex.findMany({ | ||
where: { | ||
claimSignature: { not: null }, | ||
address: { not: undefined }, | ||
tokenSymbol: { not: null }, | ||
claimAmount: { not: null }, | ||
createdAt: { | ||
// new Date() creates date with current time and day and etc. | ||
gte: dateFrom.toISOString(), | ||
lt: dateTo.toISOString(), | ||
this.prisma.deFiChainAddressIndex.findMany({ | ||
where: { | ||
claimSignature: { not: null }, | ||
address: { not: undefined }, | ||
tokenSymbol: { not: null }, | ||
claimAmount: { not: null }, | ||
createdAt: { | ||
// new Date() creates date with current time and day and etc. | ||
gte: dateFrom.toISOString(), | ||
lt: dateTo.toISOString(), | ||
}, | ||
}, | ||
}, | ||
select: { | ||
tokenSymbol: true, | ||
claimAmount: true, | ||
}, | ||
}), | ||
]); | ||
select: { | ||
tokenSymbol: true, | ||
claimAmount: true, | ||
}, | ||
}), | ||
]); | ||
|
||
const amountBridged = getAmountBridged(confirmedTransactions); | ||
const amountBridged = this.getAmountBridged(confirmedTransactions); | ||
|
||
return { | ||
totalTransactions, | ||
confirmedTransactions: confirmedTransactions.length, | ||
amountBridged, | ||
}; | ||
} | ||
} | ||
// Get overall total amount of tokens claimed | ||
const totalBridgedAmount = initializeEnumKeys(SupportedDFCTokenSymbols, this.NUMERICAL_PLACEHOLDER); | ||
const totalAmounts: BridgedDFCTokenSum[] = await this.prisma.$queryRaw( | ||
Prisma.sql` | ||
SELECT SUM("claimAmount"::DECIMAL) AS "totalAmount", "tokenSymbol" | ||
FROM "DeFiChainAddressIndex" WHERE "claimAmount" IS NOT NULL GROUP BY "tokenSymbol";`, | ||
); | ||
for (const total of totalAmounts) { | ||
totalBridgedAmount[total.tokenSymbol as SupportedDFCTokenSymbols] = BigNumber(total.totalAmount) | ||
.decimalPlaces(6, BigNumber.ROUND_FLOOR) | ||
.toFixed(6); | ||
} | ||
|
||
function getAmountBridged( | ||
confirmedTransactions: Array<{ | ||
tokenSymbol: string | null; | ||
claimAmount: string | null; | ||
}>, | ||
): BridgedDfcToEvm { | ||
const amountBridgedBigN: Record<SupportedDFCTokenSymbols, BigNumber> = { | ||
USDC: BigNumber(0), | ||
USDT: BigNumber(0), | ||
BTC: BigNumber(0), | ||
ETH: BigNumber(0), | ||
DFI: BigNumber(0), | ||
EUROC: BigNumber(0), | ||
}; | ||
for (const transaction of confirmedTransactions) { | ||
const { tokenSymbol, claimAmount } = transaction; | ||
amountBridgedBigN[tokenSymbol as SupportedDFCTokenSymbols] = amountBridgedBigN[ | ||
tokenSymbol as SupportedDFCTokenSymbols | ||
].plus(BigNumber(claimAmount as string)); | ||
return { | ||
totalTransactions, | ||
confirmedTransactions: confirmedTransactions.length, | ||
amountBridged, | ||
totalBridgedAmount, | ||
}; | ||
} catch (e: any) { | ||
throw new HttpException( | ||
{ | ||
status: e.code || HttpStatus.INTERNAL_SERVER_ERROR, | ||
error: `API call for DefiChain statistics was unsuccessful: ${e.message}`, | ||
}, | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
{ | ||
cause: e, | ||
}, | ||
); | ||
} | ||
} | ||
const numericalPlaceholder = '0.000000'; | ||
const amountBridgedToEVM: BridgedDfcToEvm = { | ||
USDC: numericalPlaceholder, | ||
USDT: numericalPlaceholder, | ||
BTC: numericalPlaceholder, | ||
ETH: numericalPlaceholder, | ||
DFI: numericalPlaceholder, | ||
EUROC: numericalPlaceholder, | ||
}; | ||
|
||
Object.keys(amountBridgedBigN).forEach((key) => { | ||
amountBridgedToEVM[key as SupportedDFCTokenSymbols] = amountBridgedBigN[key as SupportedDFCTokenSymbols] | ||
.decimalPlaces(6, BigNumber.ROUND_FLOOR) | ||
.toString(); | ||
}); | ||
getAmountBridged( | ||
confirmedTransactions: Array<{ | ||
tokenSymbol: string | null; | ||
claimAmount: string | null; | ||
}>, | ||
): BridgedDfcToEvm { | ||
const amountBridgedBigN = initializeEnumKeys(SupportedDFCTokenSymbols, BigNumber(0)); | ||
for (const transaction of confirmedTransactions) { | ||
const { tokenSymbol, claimAmount } = transaction; | ||
amountBridgedBigN[tokenSymbol as SupportedDFCTokenSymbols] = amountBridgedBigN[ | ||
tokenSymbol as SupportedDFCTokenSymbols | ||
].plus(BigNumber(claimAmount as string)); | ||
} | ||
const amountBridgedToEVM = initializeEnumKeys(SupportedDFCTokenSymbols, this.NUMERICAL_PLACEHOLDER); | ||
|
||
Object.keys(amountBridgedBigN).forEach((key) => { | ||
amountBridgedToEVM[key as SupportedDFCTokenSymbols] = amountBridgedBigN[key as SupportedDFCTokenSymbols] | ||
.decimalPlaces(6, BigNumber.ROUND_FLOOR) | ||
.toFixed(6); | ||
}); | ||
|
||
return amountBridgedToEVM; | ||
return amountBridgedToEVM; | ||
} | ||
} |
Oops, something went wrong.