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.amountis considered as positive.If
!position.isLong,position.amountis considered as negative.If
fundingPaymentis positive, it will be subtracted from the margin (meaning the user pays funding).If
fundingPaymentis 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
fundingPaymentis positive and the user is long, margin is reduced.If
fundingPaymentis negative and the user is short, margin is reduced.If
fundingPaymentis positive and the user is short, margin is increased.If
fundingPaymentis negative and the user is long, margin is increased.lastCumulativeFunding == cumulativeFunding.For a small amount or small
marginDelta, the result is calculated correctly.