-
Notifications
You must be signed in to change notification settings - Fork 44
/
update_open_price.go
54 lines (46 loc) · 1.74 KB
/
update_open_price.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package keeper
import (
"fmt"
"cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
"github.com/elys-network/elys/x/perpetual/types"
)
func (k Keeper) UpdateOpenPrice(ctx sdk.Context, mtp *types.MTP, ammPool ammtypes.Pool, baseCurrency string) error {
collateralAmountInBaseCurrency := mtp.Collateral
if mtp.CollateralAsset != baseCurrency {
C, err := k.EstimateSwap(ctx, sdk.NewCoin(mtp.CollateralAsset, mtp.Collateral), baseCurrency, ammPool)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error estimating swap: %s %s", mtp.CollateralAsset, mtp.Collateral))
}
collateralAmountInBaseCurrency = C
}
liabilitiesAmountInBaseCurrency := mtp.Liabilities
if mtp.LiabilitiesAsset != baseCurrency {
L, err := k.EstimateSwap(ctx, sdk.NewCoin(mtp.LiabilitiesAsset, mtp.Liabilities), baseCurrency, ammPool)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error estimating swap: %s %s", mtp.LiabilitiesAsset, mtp.Liabilities))
}
liabilitiesAmountInBaseCurrency = L
}
custodyAmountInTradingAsset := mtp.Custody
if mtp.CustodyAsset != mtp.TradingAsset {
C, err := k.EstimateSwap(ctx, sdk.NewCoin(mtp.CustodyAsset, mtp.Custody), mtp.TradingAsset, ammPool)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error estimating swap: %s %s", mtp.CustodyAsset, mtp.Custody))
}
custodyAmountInTradingAsset = C
}
// open price = (collateral + liabilities) / custody
mtp.OpenPrice = math.LegacyNewDecFromBigInt(
collateralAmountInBaseCurrency.Add(liabilitiesAmountInBaseCurrency).BigInt(),
).Quo(
math.LegacyNewDecFromBigInt(custodyAmountInTradingAsset.BigInt()),
)
err := k.SetMTP(ctx, mtp)
if err != nil {
return err
}
return nil
}