-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* effectiveLeverage and migration fixes * fixing variable name * fixes after merge with main
- Loading branch information
Showing
23 changed files
with
443 additions
and
342 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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,31 @@ | ||
package keeper | ||
|
||
import ( | ||
"cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/elys-network/elys/x/perpetual/types" | ||
) | ||
|
||
// GetEffectiveLeverage = custody / (custody - liabilities), has to be dimensionless | ||
func (k Keeper) GetEffectiveLeverage(ctx sdk.Context, mtp types.MTP) (math.LegacyDec, error) { | ||
|
||
// using swaps will have fees but this is just user facing value for a query | ||
tradingAssetPrice, err := k.GetAssetPrice(ctx, mtp.TradingAsset) | ||
if err != nil { | ||
return math.LegacyDec{}, err | ||
} | ||
|
||
if mtp.Position == types.Position_LONG { | ||
// custody is trading asset, liabilities are in usdc | ||
custodyInLiabilitiesAsset := mtp.Custody.ToLegacyDec().Mul(tradingAssetPrice) | ||
denominator := custodyInLiabilitiesAsset.Sub(mtp.Liabilities.ToLegacyDec()) | ||
effectiveLeverage := custodyInLiabilitiesAsset.Quo(denominator) | ||
return effectiveLeverage, nil | ||
} else { | ||
// custody is usdc, liabilities are in trading asset | ||
liabilitiesInCustodyAsset := mtp.Liabilities.ToLegacyDec().Mul(tradingAssetPrice) | ||
denominator := mtp.Custody.ToLegacyDec().Sub(liabilitiesInCustodyAsset) | ||
effectiveLeverage := mtp.Custody.ToLegacyDec().Quo(denominator) | ||
return effectiveLeverage, nil | ||
} | ||
} |
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,73 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"cosmossdk.io/math" | ||
ptypes "github.com/elys-network/elys/x/parameter/types" | ||
"github.com/elys-network/elys/x/perpetual/types" | ||
) | ||
|
||
func (suite *PerpetualKeeperTestSuite) TestGetEffectiveLeverage() { | ||
var mtp types.MTP | ||
mtp.TradingAsset = ptypes.ATOM | ||
testCases := []struct { | ||
name string | ||
expectErrMsg string | ||
result math.LegacyDec | ||
prerequisiteFunction func() | ||
}{ | ||
{ | ||
"price not set", | ||
"asset price uatom not found", | ||
math.LegacyDec{}, | ||
func() { | ||
}, | ||
}, | ||
{ | ||
"LONG", | ||
"", | ||
math.LegacyMustNewDecFromStr("1.176470588235294118"), | ||
func() { | ||
suite.SetupCoinPrices() | ||
mtp.Position = types.Position_LONG | ||
mtp.Custody = math.NewInt(100) | ||
mtp.Liabilities = math.NewInt(75) | ||
}, | ||
}, | ||
{ | ||
"SHORT", | ||
"", | ||
math.LegacyMustNewDecFromStr("2.666666666666666667"), | ||
func() { | ||
suite.SetupCoinPrices() | ||
mtp.Position = types.Position_SHORT | ||
mtp.Custody = math.NewInt(600) | ||
mtp.Liabilities = math.NewInt(75) | ||
}, | ||
}, | ||
{ | ||
"SHORT, bot should liquidate before leverage goes negative", | ||
"", | ||
math.LegacyMustNewDecFromStr("-0.363636363636363636"), | ||
func() { | ||
suite.SetupCoinPrices() | ||
mtp.Position = types.Position_SHORT | ||
mtp.Custody = math.NewInt(100) | ||
mtp.Liabilities = math.NewInt(75) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(tc.name, func() { | ||
tc.prerequisiteFunction() | ||
effectiveLeverage, err := suite.app.PerpetualKeeper.GetEffectiveLeverage(suite.ctx, mtp) | ||
suite.Require().Equal(tc.result, effectiveLeverage) | ||
if tc.expectErrMsg != "" { | ||
suite.Require().Error(err) | ||
suite.Require().Contains(err.Error(), tc.expectErrMsg) | ||
} else { | ||
suite.Require().NoError(err) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.