Skip to content

Commit

Permalink
chore: sell to sellFifo
Browse files Browse the repository at this point in the history
  • Loading branch information
filo87 committed Jun 6, 2024
1 parent 8928812 commit 61cc614
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/mappings/handlers/loansHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ async function _handleLoanRepaid(event: SubstrateEvent<LoanRepaidEvent>) {
if (principal.isExternal) {
const { quantity, settlementPrice } = principal.asExternal
await asset.decreaseQuantity(quantity.toBigInt())
realizedProfit = await AssetPositionService.sell(asset.id, quantity.toBigInt(), settlementPrice.toBigInt())
realizedProfit = await AssetPositionService.sellFifo(asset.id, quantity.toBigInt(), settlementPrice.toBigInt())
}

const at = await AssetTransactionService.repaid({ ...assetTransactionBaseData, realizedProfit })
Expand Down Expand Up @@ -328,7 +328,11 @@ async function _handleLoanDebtTransferred(event: SubstrateEvent<LoanDebtTransfer
if (_repaidAmount.principal.isExternal) {
const { quantity, settlementPrice } = _repaidAmount.principal.asExternal
await fromAsset.decreaseQuantity(quantity.toBigInt())
realizedProfit = await AssetPositionService.sell(fromAsset.id, quantity.toBigInt(), settlementPrice.toBigInt())
realizedProfit = await AssetPositionService.sellFifo(
fromAsset.id,
quantity.toBigInt(),
settlementPrice.toBigInt()
)
}
await fromAsset.updateIpfsAssetName()
await fromAsset.save()
Expand Down Expand Up @@ -442,7 +446,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.sell(toAsset.id, quantity, toAsset.currentPrice)
const realizedProfit = await AssetPositionService.sellFifo(toAsset.id, quantity, toAsset.currentPrice)

// principal repayment transaction
const principalRepayment = await AssetTransactionService.repaid({
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/services/assetPositionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class AssetPositionService extends AssetPosition {
}
}

static async sell(assetId: string, sellingQuantity: bigint, sellingPrice: bigint) {
static async sellFifo(assetId: string, sellingQuantity: bigint, sellingPrice: bigint) {
logger.info(
`Selling positions for ${assetId} ` +
`sellingQuantity: ${sellingQuantity.toString(10)} sellingPrice: ${sellingPrice.toString(10)}`
Expand Down

0 comments on commit 61cc614

Please sign in to comment.