Skip to content

Commit

Permalink
fix: add spec version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
hieronx committed Jun 11, 2024
1 parent 98b6a7f commit 9983b93
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/mappings/handlers/loansHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ async function _handleLoanCreated(event: SubstrateEvent<LoanCreatedEvent>) {
export const handleLoanBorrowed = errorHandler(_handleLoanBorrowed)
async function _handleLoanBorrowed(event: SubstrateEvent<LoanBorrowedEvent>): Promise<void> {
const [poolId, loanId, borrowAmount] = event.event.data
const specVersion = api.runtimeVersion.specVersion.toNumber()

const pool = await PoolService.getById(poolId.toString())
if (!pool) throw missingPool
Expand Down Expand Up @@ -144,7 +145,7 @@ async function _handleLoanBorrowed(event: SubstrateEvent<LoanBorrowedEvent>): Pr
await asset.borrow(amount)

if (borrowAmount.isExternal) {
await asset.updateCurrentPrice(borrowAmount.asExternal.settlementPrice.toBigInt())
if (specVersion < 1100) await asset.updateCurrentPrice(borrowAmount.asExternal.settlementPrice.toBigInt())
await asset.increaseQuantity(borrowAmount.asExternal.quantity.toBigInt())
await AssetPositionService.buy(
asset.id,
Expand Down Expand Up @@ -175,6 +176,7 @@ async function _handleLoanBorrowed(event: SubstrateEvent<LoanBorrowedEvent>): Pr
export const handleLoanRepaid = errorHandler(_handleLoanRepaid)
async function _handleLoanRepaid(event: SubstrateEvent<LoanRepaidEvent>) {
const [poolId, loanId, { principal, interest, unscheduled }] = event.event.data
const specVersion = api.runtimeVersion.specVersion.toNumber()

const pool = await PoolService.getById(poolId.toString())
if (!pool) throw missingPool
Expand Down Expand Up @@ -219,7 +221,7 @@ async function _handleLoanRepaid(event: SubstrateEvent<LoanRepaidEvent>) {
let realizedProfitFifo: bigint
if (principal.isExternal) {
const { quantity, settlementPrice } = principal.asExternal
await asset.updateCurrentPrice(settlementPrice.toBigInt())
if (specVersion < 1100) await asset.updateCurrentPrice(settlementPrice.toBigInt())

await asset.decreaseQuantity(quantity.toBigInt())
realizedProfitFifo = await AssetPositionService.sellFifo(
Expand Down Expand Up @@ -300,7 +302,8 @@ async function _handleLoanDebtTransferred(event: SubstrateEvent<LoanDebtTransfer
if (!pool) throw missingPool

const repaidPrincipalAmount = AssetService.extractPrincipalAmount(_repaidAmount.principal)
const repaidInterestAmount = specVersion < 1029 ? BigInt(0) : _repaidAmount.interest.toBigInt()
// Interest amount until spec version 1100 is off
const repaidInterestAmount = specVersion < 1100 ? BigInt(0) : _repaidAmount.interest.toBigInt()
const repaidUnscheduledAmount = _repaidAmount.unscheduled.toBigInt()
const repaidAmount = repaidPrincipalAmount + repaidInterestAmount + repaidUnscheduledAmount

Expand Down Expand Up @@ -337,7 +340,7 @@ async function _handleLoanDebtTransferred(event: SubstrateEvent<LoanDebtTransfer
let realizedProfitFifo: bigint
if (_repaidAmount.principal.isExternal) {
const { quantity, settlementPrice } = _repaidAmount.principal.asExternal
await fromAsset.updateCurrentPrice(settlementPrice.toBigInt())
if (specVersion < 1100) await fromAsset.updateCurrentPrice(settlementPrice.toBigInt())
await fromAsset.decreaseQuantity(quantity.toBigInt())
realizedProfitFifo = await AssetPositionService.sellFifo(
fromAsset.id,
Expand Down Expand Up @@ -380,7 +383,7 @@ async function _handleLoanDebtTransferred(event: SubstrateEvent<LoanDebtTransfer
await toAsset.borrow(borrowPrincipalAmount)
if (_borrowAmount.isExternal) {
const { quantity, settlementPrice } = _borrowAmount.asExternal
await toAsset.updateCurrentPrice(settlementPrice.toBigInt())
if (specVersion < 1100) await toAsset.updateCurrentPrice(settlementPrice.toBigInt())
await toAsset.increaseQuantity(quantity.toBigInt())
await AssetPositionService.buy(
toAsset.id,
Expand Down

0 comments on commit 9983b93

Please sign in to comment.