-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19a79fd
commit c5011e1
Showing
5 changed files
with
156 additions
and
118 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,47 @@ | ||
package funding | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/dydxprotocol/v4-chain/protocol/lib" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types" | ||
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" | ||
) | ||
|
||
// GetFundingIndexDelta returns `fundingIndexDelta` which represents the change of the funding index | ||
// given the funding rate, the time since the last funding tick, and the oracle price. The index delta | ||
// is in parts-per-million (PPM) and is calculated as follows: | ||
// | ||
// indexDelta = | ||
// fundingRatePpm * | ||
// (time / realizationPeriod) * | ||
// quoteQuantumsPerBaseQuantum | ||
// | ||
// Any multiplication is done before division to avoid precision loss. | ||
func GetFundingIndexDelta( | ||
perp types.Perpetual, | ||
marketPrice pricestypes.MarketPrice, | ||
big8hrFundingRatePpm *big.Int, | ||
timeSinceLastFunding uint32, | ||
) (fundingIndexDelta *big.Int) { | ||
// Get pro-rated funding rate adjusted by time delta. | ||
result := new(big.Int).SetUint64(uint64(timeSinceLastFunding)) | ||
|
||
// Multiply by the time-delta numerator upfront. | ||
result.Mul(result, big8hrFundingRatePpm) | ||
|
||
// Multiply by the price of the asset. | ||
result = lib.BaseToQuoteQuantums( | ||
result, | ||
perp.Params.AtomicResolution, | ||
marketPrice.Price, | ||
marketPrice.Exponent, | ||
) | ||
|
||
// Divide by the time-delta denominator. | ||
// Use truncated division (towards zero) instead of Euclidean division. | ||
// TODO(DEC-1536): Make the 8-hour funding rate period configurable. | ||
result.Quo(result, big.NewInt(60*60*8)) | ||
|
||
return result | ||
} |
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,89 @@ | ||
package funding_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
perptest "github.com/dydxprotocol/v4-chain/protocol/testutil/perpetuals" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/funding" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types" | ||
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" | ||
) | ||
|
||
func TestGetFundingIndexDelta(t *testing.T) { | ||
testCases := map[string]struct { | ||
perp types.Perpetual | ||
marketPrice pricestypes.MarketPrice | ||
big8hrFundingRatePpm *big.Int | ||
timeSinceLastFunding uint32 | ||
expected *big.Int | ||
}{ | ||
"Positive Funding Rate (rounds towards zero)": { | ||
perp: *perptest.GeneratePerpetual( | ||
perptest.WithAtomicResolution(-12), | ||
), | ||
marketPrice: pricestypes.MarketPrice{ | ||
Id: 0, | ||
Exponent: 0, | ||
Price: 1_000, | ||
}, | ||
big8hrFundingRatePpm: big.NewInt(1_001_999), | ||
timeSinceLastFunding: 8 * 60 * 60, | ||
expected: big.NewInt(1_001), | ||
}, | ||
"Negative Funding Rate (rounds towards zero)": { | ||
perp: *perptest.GeneratePerpetual( | ||
perptest.WithAtomicResolution(-12), | ||
), | ||
marketPrice: pricestypes.MarketPrice{ | ||
Id: 0, | ||
Exponent: 0, | ||
Price: 1_000, | ||
}, | ||
big8hrFundingRatePpm: big.NewInt(-1_001_999), | ||
timeSinceLastFunding: 8 * 60 * 60, | ||
expected: big.NewInt(-1_001), | ||
}, | ||
"Varied parameters (1)": { | ||
perp: *perptest.GeneratePerpetual( | ||
perptest.WithAtomicResolution(-4), | ||
), | ||
marketPrice: pricestypes.MarketPrice{ | ||
Id: 0, | ||
Exponent: 2, | ||
Price: 1_000, | ||
}, | ||
big8hrFundingRatePpm: big.NewInt(-1_001_999), | ||
timeSinceLastFunding: 8 * 60 * 60 / 2, | ||
expected: big.NewInt(-5_009_995_000_000), | ||
}, | ||
"Varied parameters (2)": { | ||
perp: *perptest.GeneratePerpetual( | ||
perptest.WithAtomicResolution(0), | ||
), | ||
marketPrice: pricestypes.MarketPrice{ | ||
Id: 0, | ||
Exponent: -6, | ||
Price: 1_000, | ||
}, | ||
big8hrFundingRatePpm: big.NewInt(-1_001_999), | ||
timeSinceLastFunding: 8 * 60 * 60 / 8, | ||
expected: big.NewInt(-125_249_875), | ||
}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
result := funding.GetFundingIndexDelta( | ||
tc.perp, | ||
tc.marketPrice, | ||
tc.big8hrFundingRatePpm, | ||
tc.timeSinceLastFunding, | ||
) | ||
|
||
require.Equal(t, tc.expected, result) | ||
}) | ||
} | ||
} |
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