Function: settleFundingPayment(Position storage self, int256 cumulativeFunding)
This internal function calculates the funding payment owed or credited to the user since their last funding update, adjusts their margin accordingly, and updates the funding snapshot in the position to the current global funding index. Function execution will be skipped if the position.lastCumulativeFunding == cumulativeFunding
. The fundingPayment
is calculated as the position’s amount multiplied by the difference between the current cumulative premium funding rate and the position’s last cumulative funding snapshot: fundingPayment = position.amount × (cumulativePremiumFunding - position.lastCumulativeFunding)
If
position.isLong
,position.amount
is considered as positive.If
!position.isLong
,position.amount
is considered as negative.If
fundingPayment
is positive, it will be subtracted from the margin (meaning the user pays funding).If
fundingPayment
is negative, it will be added to the margin (meaning the user receives funding).
The position.lastCumulativeFunding
will be updated to the current cumulativeFunding
.
Branches and code coverage
Intended branches
If
fundingPayment
is positive and the user is long, margin is reduced.If
fundingPayment
is negative and the user is short, margin is reduced.If
fundingPayment
is positive and the user is short, margin is increased.If
fundingPayment
is negative and the user is long, margin is increased.lastCumulativeFunding == cumulativeFunding
.For a small amount or small
marginDelta
, the result is calculated correctly.