-
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: add AssetPosition entity and track realized p&l
Fixes #185
- Loading branch information
Showing
4 changed files
with
100 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { AssetPosition } from '../../types/models/AssetPosition' | ||
import { RAY } from '../../config' | ||
import { nToBigInt, bnToBn } from '@polkadot/util' | ||
|
||
export class AssetPositionService extends AssetPosition { | ||
static init(assetId: string, hash: string, timestamp: Date, quantity: bigint, price: bigint) { | ||
logger.info(`Initialising new assetPosition with Id ${chainId}`) | ||
const id = `${assetId}-${hash}` | ||
return new this(id, assetId, timestamp, quantity, price) | ||
} | ||
|
||
static buy(assetId: string, hash: string, timestamp: Date, quantity: bigint, price: bigint) { | ||
return this.init(assetId, hash, timestamp, quantity, price).save() | ||
} | ||
|
||
static async sell(assetId: string, sellingQuantity: bigint, sellingPrice: bigint) { | ||
const positions = await this.getByAssetId(assetId) | ||
positions.sort((a, b) => b.timestamp.valueOf() - a.timestamp.valueOf()) | ||
|
||
const sellPositions: [assetPosition: AssetPosition, sellQuantity: bigint][] = [] | ||
let remainingQuantity = sellingQuantity | ||
while (remainingQuantity > BigInt(0)) { | ||
const currentPosition = positions.pop() | ||
if (!currentPosition) throw new Error(`No positions to sell for asset ${assetId}`) | ||
if (remainingQuantity > currentPosition.holdingQuantity) { | ||
const soldQuantity = currentPosition.holdingQuantity | ||
currentPosition['holdingQuantity'] = BigInt(0) | ||
sellPositions.push([currentPosition, soldQuantity]) | ||
remainingQuantity -= soldQuantity | ||
} else { | ||
const soldQuantity = remainingQuantity | ||
currentPosition['holdingQuantity'] -= soldQuantity | ||
sellPositions.push([currentPosition, soldQuantity]) | ||
remainingQuantity -= soldQuantity | ||
} | ||
} | ||
|
||
const profitFromSale = nToBigInt(bnToBn(sellingPrice).mul(bnToBn(sellingQuantity)).div(RAY)) | ||
const costOfBuy = sellPositions.reduce<bigint>( | ||
(totalCost, line) => totalCost + nToBigInt(bnToBn(line[0].purchasePrice).mul(bnToBn(line[1]).div(RAY))), | ||
BigInt(0) | ||
) | ||
|
||
const dbUpdates = sellPositions.map((line) => | ||
line[0].holdingQuantity > BigInt(0) ? line[0].save() : this.remove(line[0].id) | ||
) | ||
|
||
await Promise.all(dbUpdates) | ||
return profitFromSale - costOfBuy | ||
} | ||
} |
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