-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: track future cashflows per asset (#219)
* feat: track future cashflows per asset Fixes #196 * feat: track future cashflows Closes #196 Closes #220 * chore: record cashflows for debt transfers * fix: runtime typing
- Loading branch information
Showing
5 changed files
with
165 additions
and
87 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ExtendedCall } from '../../helpers/types' | ||
import { AssetCashflow } from '../../types/models/AssetCashflow' | ||
|
||
export class AssetCashflowService extends AssetCashflow { | ||
static init(assetId: string, timestamp: Date, principal: bigint, interest: bigint) { | ||
const id = `${assetId}-${timestamp.valueOf()}` | ||
logger.info(`Initialising new AssetCashflow with Id ${id}`) | ||
return new this(id, assetId, timestamp, principal, interest) | ||
} | ||
|
||
static async recordAssetCashflows(_assetId: string) { | ||
const specVersion = api.runtimeVersion.specVersion.toNumber() | ||
if (specVersion < 1103) return | ||
const [poolId, assetId] = _assetId.split('-') | ||
logger.info(`Recording AssetCashflows for Asset ${_assetId}`) | ||
const apiCall = api.call as ExtendedCall | ||
logger.info(`Calling runtime API loansApi.expectedCashflows(${poolId}, ${assetId})`) | ||
const response = await apiCall.loansApi.expectedCashflows(poolId, assetId) | ||
logger.info(JSON.stringify(response)) | ||
if(!response.isOk) return | ||
await this.clearAssetCashflows(_assetId) | ||
const saves = response.asOk.map((cf) => { | ||
const { when, principal, interest } = cf | ||
const timestamp = new Date(when.toNumber() * 1000) | ||
const cashflow = this.init(_assetId, timestamp, principal.toBigInt(), interest.toBigInt()) | ||
return cashflow.save() | ||
}) | ||
return Promise.all(saves) | ||
} | ||
|
||
static async clearAssetCashflows(assetId: string) { | ||
logger.info(`Clearing AssetCashflows for asset: ${assetId}`) | ||
const cashflows = await this.getByAssetId(assetId) | ||
const deletes = cashflows.map((cf) => this.remove(cf.id)) | ||
return Promise.all(deletes) | ||
} | ||
} |