This section provides a deep understanding of how the accounting of collateral is done in the BookKeeper contract.
Where is a position's collateral recorded?
Each position's collateral amount and debt are recorded in the BookKeeper contract. In the BookKeeper contract, positions are the double mapping that keeps track of each position's lockedCollateral and debtShare.
position.lockedCollateralrepresents the amount of collateral locked in a specific position. It serves as the primary reference to determine the amount of collateral a particular position holds. The value of position.lockedCollateral increases when a position is opened and decreases when it is closed. During the position opening flow, the value of CollateralToken variable increase temporarily, and later gets transferred to position.lockedCollateral.
Similarly, the position value.lockedCollateral is temporarily transferred to the CollateralToken variable during the position closing flow. Then, when the collateral gets withdrawn from CollateralTokenAdapter, the temporarily increased number on CollateralToken decreases as much as withdrawn.
Three functions are directly involved in lockedCollateral accounting.