This repository has been archived by the owner on May 7, 2024. It is now read-only.
forked from centrifuge/api
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 74 index loanentity data types (#86) * feat: create loanService * refactor: improved service getters * feat: increase outstandingDebt on borrowings * feat: basics for handle loan prices * feat: save priced loans * feat: activate loan when priced * feat: handle writeoffs * feat: handle loan closed and write offs * fix: loan.repay() should increase totalRepaid * fix: invest execution decimals and avoid empty transactions (#87) * 82 add prices to all investortransaction types (#88) * chore: update chain data * feat: extend InvestorTransaction decoration and refactor * feat: use rpc prices from trancheTokenPrices() when investor transactions are processed (#89) * feat: improved loans and borrower transactions (#90) * 63 convert outflows to dai (#91) * feat: add currency value for redeem fields * fix: currency conversion should support null prices
- Loading branch information
Showing
13 changed files
with
683 additions
and
460 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
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
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,13 +1,134 @@ | ||
import { SubstrateEvent } from '@subql/types' | ||
import { LoanBorrowedEvent } from '../../helpers/types' | ||
import { | ||
LoanBorrowedRepaidEvent, | ||
LoanCreatedClosedEvent, | ||
LoanPricedEvent, | ||
LoanWrittenOffEvent, | ||
} from '../../helpers/types' | ||
import { errorHandler } from '../../helpers/errorHandler' | ||
import { PoolService } from '../services/poolService' | ||
import { LoanService } from '../services/loanService' | ||
import { BorrowerTransactionService } from '../services/borrowerTransactionService' | ||
|
||
export const handleBorrowings = errorHandler(_handleBorrowings) | ||
async function _handleBorrowings(event: SubstrateEvent<LoanBorrowedEvent>): Promise<void> { | ||
const [poolId, , amount] = event.event.data | ||
logger.info(`Pool: ${poolId.toString()} borrowed ${amount.toString()}`) | ||
const poolService = await PoolService.getById(poolId.toString()) | ||
await poolService.increaseTotalBorrowings(amount.toBigInt()) | ||
await poolService.save() | ||
export const handleLoanCreated = errorHandler(_handleLoanCreated) | ||
async function _handleLoanCreated(event: SubstrateEvent<LoanCreatedClosedEvent>) { | ||
const [poolId, loanId] = event.event.data | ||
logger.info(`Loan created event for pool: ${poolId.toString()} loan: ${loanId.toString()}`) | ||
const pool = await PoolService.getById(poolId.toString()) | ||
|
||
const loan = await LoanService.init(poolId.toString(), loanId.toString(), event.block.timestamp) | ||
await loan.save() | ||
|
||
const bt = await BorrowerTransactionService.created({ | ||
poolId: poolId.toString(), | ||
loanId: loanId.toString(), | ||
address: event.extrinsic.extrinsic.signer.toString(), | ||
epochNumber: pool.pool.currentEpoch, | ||
hash: event.extrinsic.extrinsic.hash.toString(), | ||
timestamp: event.block.timestamp, | ||
}) | ||
await bt.save() | ||
} | ||
|
||
export const handleLoanBorrowed = errorHandler(_handleLoanBorrowed) | ||
async function _handleLoanBorrowed(event: SubstrateEvent<LoanBorrowedRepaidEvent>): Promise<void> { | ||
const [poolId, loanId, amount] = event.event.data | ||
logger.info(`Loan borrowed event for pool: ${poolId.toString()} amount: ${amount.toString()}`) | ||
const pool = await PoolService.getById(poolId.toString()) | ||
|
||
// Update loan amount | ||
const loan = await LoanService.getById(poolId.toString(), loanId.toString()) | ||
await loan.borrow(amount.toBigInt()) | ||
await loan.save() | ||
|
||
const bt = await BorrowerTransactionService.borrowed({ | ||
poolId: poolId.toString(), | ||
loanId: loanId.toString(), | ||
address: event.extrinsic.extrinsic.signer.toString(), | ||
epochNumber: pool.pool.currentEpoch, | ||
hash: event.extrinsic.extrinsic.hash.toString(), | ||
timestamp: event.block.timestamp, | ||
amount: amount.toBigInt(), | ||
}) | ||
await bt.save() | ||
|
||
// Update poolState info | ||
await pool.increaseTotalBorrowings(amount.toBigInt()) | ||
await pool.save() | ||
} | ||
|
||
export const handleLoanPriced = errorHandler(_handleLoanPriced) | ||
async function _handleLoanPriced(event: SubstrateEvent<LoanPricedEvent>) { | ||
const [poolId, loanId, interestRatePerSec, loanType] = event.event.data | ||
logger.info(`Loan priced event for pool: ${poolId.toString()} loan: ${loanId.toString()}`) | ||
const pool = await PoolService.getById(poolId.toString()) | ||
|
||
const loan = await LoanService.getById(poolId.toString(), loanId.toString()) | ||
await loan.activate() | ||
await loan.updateInterestRate(interestRatePerSec.toBigInt()) | ||
await loan.updateLoanType(loanType.type, loanType.inner.toJSON()) | ||
await loan.save() | ||
|
||
const bt = await BorrowerTransactionService.priced({ | ||
poolId: poolId.toString(), | ||
loanId: loanId.toString(), | ||
address: event.extrinsic.extrinsic.signer.toString(), | ||
epochNumber: pool.pool.currentEpoch, | ||
hash: event.extrinsic.extrinsic.hash.toString(), | ||
timestamp: event.block.timestamp, | ||
}) | ||
await bt.save() | ||
} | ||
|
||
export const handleLoanRepaid = errorHandler(_handleLoanRepaid) | ||
async function _handleLoanRepaid(event: SubstrateEvent<LoanBorrowedRepaidEvent>) { | ||
const [poolId, loanId, amount] = event.event.data | ||
logger.info(`Loan borrowed event for pool: ${poolId.toString()} amount: ${amount.toString()}`) | ||
const pool = await PoolService.getById(poolId.toString()) | ||
|
||
const loan = await LoanService.getById(poolId.toString(), loanId.toString()) | ||
await loan.repay(amount.toBigInt()) | ||
await loan.save() | ||
|
||
const bt = await BorrowerTransactionService.repaid({ | ||
poolId: poolId.toString(), | ||
loanId: loanId.toString(), | ||
address: event.extrinsic.extrinsic.signer.toString(), | ||
epochNumber: pool.pool.currentEpoch, | ||
hash: event.extrinsic.extrinsic.hash.toString(), | ||
timestamp: event.block.timestamp, | ||
amount: amount.toBigInt(), | ||
}) | ||
await bt.save() | ||
} | ||
|
||
export const handleLoanWrittenOff = errorHandler(_handleLoanWrittenOff) | ||
async function _handleLoanWrittenOff(event: SubstrateEvent<LoanWrittenOffEvent>) { | ||
const [poolId, loanId, percentage, penaltyInterestRatePerSec, writeOffGroupIndex] = event.event.data | ||
logger.info(`Loan writtenoff event for pool: ${poolId.toString()} loanId: ${loanId.toString()}`) | ||
const loan = await LoanService.getById(poolId.toString(), loanId.toString()) | ||
const writeOffIndex = writeOffGroupIndex.isSome ? writeOffGroupIndex.unwrap().toNumber() : null | ||
await loan.writeOff(percentage.toBigInt(), penaltyInterestRatePerSec.toBigInt(), writeOffIndex) | ||
await loan.save() | ||
} | ||
|
||
export const handleLoanClosed = errorHandler(_handleLoanClosed) | ||
async function _handleLoanClosed(event: SubstrateEvent<LoanCreatedClosedEvent>) { | ||
const [poolId, loanId] = event.event.data | ||
logger.info(`Loan closed event for pool: ${poolId.toString()} loanId: ${loanId.toString()}`) | ||
const pool = await PoolService.getById(poolId.toString()) | ||
|
||
const loan = await LoanService.getById(poolId.toString(), loanId.toString()) | ||
await loan.close() | ||
await loan.save() | ||
|
||
const bt = await BorrowerTransactionService.repaid({ | ||
poolId: poolId.toString(), | ||
loanId: loanId.toString(), | ||
address: event.extrinsic.extrinsic.signer.toString(), | ||
epochNumber: pool.pool.currentEpoch, | ||
hash: event.extrinsic.extrinsic.hash.toString(), | ||
timestamp: event.block.timestamp, | ||
}) | ||
await bt.save() | ||
} |
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
Oops, something went wrong.