Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ node_modules
.turbo
/generated/prisma

# Payout artifacts
/payouts/

# Yarn (Berry)
.yarn/*
!.yarn/patches
Expand Down
20 changes: 16 additions & 4 deletions apps/swap-service/src/swaps/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,15 @@ const resolveActualFeeUsd = (swap: Swap): number | null => {
return bnOrZero(amount).div(bnOrZero(10).pow(precision)).times(priceUsd).toNumber()
}

export const calculateFeeForSwap = (swap: Swap): { feeUsd: number; volumeUsd: number; verifiedBps: number } | null => {
export const calculateFeeForSwap = (
swap: Swap,
): {
feeUsd: number
volumeUsd: number
verifiedBps: number
actualFeeUsd: number | null
impliedFeeUsd: number | null
} | null => {
const verifiedBps = swap.affiliateVerificationDetails?.affiliateBps
if (!verifiedBps) {
logger.warn(`Verified swap ${swap.swapId} missing affiliate bps in verification details, skipping`)
Expand All @@ -175,14 +183,18 @@ export const calculateFeeForSwap = (swap: Swap): { feeUsd: number; volumeUsd: nu
)

const actualFeeUsd = resolveActualFeeUsd(swap)
const impliedFeeUsd =
sellAmountUsd === null ? null : bnOrZero(sellAmountUsd).times(verifiedBps).div(BPS_DENOMINATOR).toNumber()

if (actualFeeUsd === null && sellAmountUsd === null) {
// Prefer the on-chain collected fee; fall back to the bps-implied fee.
const feeUsd = actualFeeUsd ?? impliedFeeUsd

if (feeUsd === null) {
logger.warn(`Unable to calculate fee for swap ${swap.swapId}, skipping`)
return null
}

const feeUsd = actualFeeUsd ?? bnOrZero(sellAmountUsd).times(verifiedBps).div(BPS_DENOMINATOR).toNumber()
const volumeUsd = sellAmountUsd ?? bnOrZero(actualFeeUsd).times(BPS_DENOMINATOR).div(verifiedBps).toNumber()

return { feeUsd, volumeUsd, verifiedBps }
return { feeUsd, volumeUsd, verifiedBps, actualFeeUsd, impliedFeeUsd }
}
21 changes: 3 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"db:migrate:status": "prisma migrate status",
"db:migrate:create": "prisma migrate dev --create-only --name",
"db:studio": "prisma studio",
"referral-rewards": "ts-node scripts/referral-rewards.ts"
"referral-rewards": "ts-node scripts/referral-rewards.ts",
"affiliate-payouts": "ts-node --transpile-only scripts/affiliate-payouts/affiliate-payouts.ts",
"affiliate-payouts:test": "jest --config scripts/jest.config.ts"
},
"dependencies": {
"@bitcoinerlab/secp256k1": "^1.1.1",
Expand Down Expand Up @@ -94,22 +96,5 @@
"resolutions": {
"google-protobuf": "3.15.7"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"packageManager": "yarn@4.7.0"
}
Loading
Loading