Skip to content

Commit

Permalink
refactor: naming
Browse files Browse the repository at this point in the history
  • Loading branch information
filo87 committed Jun 7, 2024
1 parent 70d83f3 commit 2362c09
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/mappings/handlers/loansHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,18 @@ async function _handleLoanRepaid(event: SubstrateEvent<LoanRepaidEvent>) {
} else {
await asset.repay(amount)

let realizedProfit: bigint
let realizedProfitFifo: bigint
if (principal.isExternal) {
const { quantity, settlementPrice } = principal.asExternal
await asset.decreaseQuantity(quantity.toBigInt())
realizedProfit = await AssetPositionService.sellFifo(asset.id, quantity.toBigInt(), settlementPrice.toBigInt())
realizedProfitFifo = await AssetPositionService.sellFifo(
asset.id,
quantity.toBigInt(),
settlementPrice.toBigInt()
)
}

const at = await AssetTransactionService.repaid({ ...assetTransactionBaseData, realizedProfit })
const at = await AssetTransactionService.repaid({ ...assetTransactionBaseData, realizedProfitFifo })
await at.save()

// Update pool info
Expand Down Expand Up @@ -324,11 +328,11 @@ async function _handleLoanDebtTransferred(event: SubstrateEvent<LoanDebtTransfer
//Track repayment
await fromAsset.activate()
await fromAsset.repay(repaidAmount)
let realizedProfit: bigint
let realizedProfitFifo: bigint
if (_repaidAmount.principal.isExternal) {
const { quantity, settlementPrice } = _repaidAmount.principal.asExternal
await fromAsset.decreaseQuantity(quantity.toBigInt())
realizedProfit = await AssetPositionService.sellFifo(
realizedProfitFifo = await AssetPositionService.sellFifo(
fromAsset.id,
quantity.toBigInt(),
settlementPrice.toBigInt()
Expand Down Expand Up @@ -357,7 +361,7 @@ async function _handleLoanDebtTransferred(event: SubstrateEvent<LoanDebtTransfer
: null,
fromAssetId: fromLoanId.toString(10),
toAssetId: toLoanId.toString(10),
realizedProfit,
realizedProfitFifo,
})
await principalRepayment.save()
}
Expand Down Expand Up @@ -446,7 +450,7 @@ async function _handleLoanDebtTransferred1024(event: SubstrateEvent<LoanDebtTran
await epoch.save()

const quantity = nToBigInt(bnToBn(amount).mul(WAD).div(bnToBn(fromAsset.currentPrice)))
const realizedProfit = await AssetPositionService.sellFifo(toAsset.id, quantity, toAsset.currentPrice)
const realizedProfitFifo = await AssetPositionService.sellFifo(toAsset.id, quantity, toAsset.currentPrice)

// principal repayment transaction
const principalRepayment = await AssetTransactionService.repaid({
Expand All @@ -457,7 +461,7 @@ async function _handleLoanDebtTransferred1024(event: SubstrateEvent<LoanDebtTran
toAssetId: toLoanId.toString(10),
settlementPrice: fromAsset.currentPrice,
quantity: nToBigInt(bnToBn(amount).mul(WAD).div(bnToBn(fromAsset.currentPrice))),
realizedProfit,
realizedProfitFifo,
})
await principalRepayment.save()
}
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/services/assetTransactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface AssetTransactionData {
readonly assetId: string
readonly fromAssetId?: string
readonly toAssetId?: string
readonly realizedProfit?: bigint
readonly realizedProfitFifo?: bigint
}

export class AssetTransactionService extends AssetTransaction {
Expand All @@ -39,7 +39,7 @@ export class AssetTransactionService extends AssetTransaction {
tx.settlementPrice = data.settlementPrice ?? null
tx.fromAssetId = data.fromAssetId ? `${data.poolId}-${data.fromAssetId}` : null
tx.toAssetId = data.toAssetId ? `${data.poolId}-${data.toAssetId}` : null
tx.realizedProfit = data.realizedProfit ?? null
tx.realizedProfitFifo = data.realizedProfitFifo ?? null
return tx
}

Expand Down

0 comments on commit 2362c09

Please sign in to comment.