Skip to content

Commit

Permalink
chore: re-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
albertchon committed Mar 13, 2023
1 parent d9862a9 commit 42cab64
Show file tree
Hide file tree
Showing 15 changed files with 1,310 additions and 588 deletions.
4 changes: 4 additions & 0 deletions chain/exchange/types/common_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ func GetSubaccountIDOrDeriveFromNonce(sender sdk.AccAddress, subaccountId string
return GetNonceDerivedSubaccountID(sender, subaccountId)
}

if !IsHexHash(subaccountId) {
return common.Hash{}, sdkerrors.Wrap(ErrBadSubaccountID, subaccountId)
}

return common.HexToHash(subaccountId), nil
}

Expand Down
832 changes: 690 additions & 142 deletions chain/exchange/types/events.pb.go

Large diffs are not rendered by default.

21 changes: 15 additions & 6 deletions chain/exchange/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (o *DerivativeOrder) ValidateBasic(senderAddr sdk.AccAddress, hasBinaryPric
}

if o.IsConditional() && (o.TriggerPrice == nil || o.TriggerPrice.LT(MinDerivativeOrderPrice)) { /*||
!o.IsConditional() && o.TriggerPrice != nil */ // commented out this check since FE is sending to us 0.0 trigger price for all orders
!o.IsConditional() && o.TriggerPrice != nil */// commented out this check since FE is sending to us 0.0 trigger price for all orders
return sdkerrors.Wrapf(ErrInvalidTriggerPrice, "Mismatch between triggerPrice: %v and orderType: %v, or triggerPrice is incorrect", o.TriggerPrice, o.OrderType)
}

Expand Down Expand Up @@ -1246,17 +1246,26 @@ func (msg *MsgExternalTransfer) ValidateBasic() error {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}

sourceSubaccountAddress, ok := IsValidSubaccountID(msg.SourceSubaccountId)
if !ok || IsDefaultSubaccountID(common.HexToHash(msg.SourceSubaccountId)) {
if err := CheckValidSubaccountIDOrNonce(senderAddr, msg.SourceSubaccountId); err != nil {
return err
}

sourceSubaccountId, err := GetSubaccountIDOrDeriveFromNonce(senderAddr, msg.SourceSubaccountId)
if err != nil {
return sdkerrors.Wrap(ErrBadSubaccountID, msg.SourceSubaccountId)
}

_, ok = IsValidSubaccountID(msg.DestinationSubaccountId)
if IsDefaultSubaccountID(common.HexToHash(msg.SourceSubaccountId)) {
return sdkerrors.Wrap(ErrBadSubaccountID, msg.SourceSubaccountId)
}

_, ok := IsValidSubaccountID(msg.DestinationSubaccountId)
if !ok || IsDefaultSubaccountID(common.HexToHash(msg.DestinationSubaccountId)) {
return sdkerrors.Wrap(ErrBadSubaccountID, msg.DestinationSubaccountId)
}
if !bytes.Equal(sourceSubaccountAddress.Bytes(), senderAddr.Bytes()) {
return sdkerrors.Wrap(ErrBadSubaccountID, msg.Sender)

if !bytes.Equal(SubaccountIDToSdkAddress(sourceSubaccountId).Bytes(), senderAddr.Bytes()) {
return sdkerrors.Wrap(ErrBadSubaccountID, msg.DestinationSubaccountId)
}
return nil
}
Expand Down
22 changes: 22 additions & 0 deletions chain/exchange/types/positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ func (p *Position) ApplyProfitHaircutForDerivatives(deficitAmount, totalProfits,
// newEntryPrice = haircutPercentage * (settlementPrice - entryPrice) + entryPrice
newEntryPrice := deficitAmount.Mul(settlementPrice.Sub(p.EntryPrice)).Quo(totalProfits).Add(p.EntryPrice)
p.EntryPrice = newEntryPrice

// profitable position but with negative margin, we didn't account for negative margin previously,
// so we can safely add it if payout becomes negative from haircut
newPositionPayout := p.GetPayoutIfFullyClosing(settlementPrice, sdk.ZeroDec()).Payout
if newPositionPayout.IsNegative() {
p.Margin = p.Margin.Add(newPositionPayout.Abs())
}
}

func (p *Position) ApplyTotalPositionPayoutHaircut(deficitAmount, totalPayouts, settlementPrice sdk.Dec) {
p.ApplyProfitHaircutForDerivatives(deficitAmount, totalPayouts, settlementPrice)

removedMargin := p.Margin.Mul(deficitAmount).Quo(totalPayouts)
p.Margin = p.Margin.Sub(removedMargin)
}

func (p *Position) ApplyProfitHaircutForBinaryOptions(deficitAmount, totalAssets sdk.Dec, oracleScaleFactor uint32) {
Expand Down Expand Up @@ -110,6 +124,14 @@ func (p *Position) ClosePositionWithSettlePrice(settlementPrice, closingFeeRate
return payout, closeTradingFee, positionDelta
}

func (p *Position) ClosePositionWithoutPayouts() {
p.IsLong = false
p.EntryPrice = sdk.ZeroDec()
p.Quantity = sdk.ZeroDec()
p.Margin = sdk.ZeroDec()
p.CumulativeFundingEntry = sdk.ZeroDec()
}

func (p *Position) ClosePositionByRefunding(closingFeeRate sdk.Dec) (payout, closeTradingFee sdk.Dec, positionDelta *PositionDelta) {
return p.ClosePositionWithSettlePrice(p.EntryPrice, closingFeeRate)
}
Expand Down
107 changes: 101 additions & 6 deletions chain/oracle/types/band_ibc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"fmt"

bandprice "github.com/InjectiveLabs/sdk-go/bandchain/hooks/price"
bandobi "github.com/bandprotocol/bandchain-packet/obi"
bandPacket "github.com/bandprotocol/bandchain-packet/packet"
Expand All @@ -20,14 +22,107 @@ func NewOracleRequestPacketData(clientID string, calldata []byte, r *BandOracleR
}

// GetCalldata gets the Band IBC request call data based on the symbols and multiplier.
func (r *BandOracleRequest) GetCalldata() []byte {
func (r *BandOracleRequest) GetCalldata(legacyScheme bool) []byte {
if legacyScheme {
return bandobi.MustEncode(bandprice.Input{
Symbols: r.Symbols,
Multiplier: BandPriceMultiplier,
})
}

return bandobi.MustEncode(bandprice.SymbolInput{
Symbols: r.Symbols,
MinimumSourceCount: uint8(r.MinSourceCount),
})
}

func DecodeOracleInput(data []byte) (OracleInput, error) {
var (
legacyInput LegacyBandInput
newInput BandInput
err error
)

requestCallData := bandprice.Input{
Symbols: r.Symbols,
Multiplier: BandPriceMultiplier,
if err = bandobi.Decode(data, &legacyInput); err == nil {
return legacyInput, nil
}

callData := bandobi.MustEncode(requestCallData)
if err = bandobi.Decode(data, &newInput); err == nil {
return newInput, nil
}

return nil, fmt.Errorf("failed to decode oracle input: %w", err)
}

func DecodeOracleOutput(data []byte) (OracleOutput, error) {
var (
legacyOutput LegacyBandOutput
newOutput BandOutput
err error
)

if err = bandobi.Decode(data, &legacyOutput); err == nil {
return legacyOutput, nil
}

if err = bandobi.Decode(data, &newOutput); err == nil {
return newOutput, nil
}

return nil, fmt.Errorf("failed to decode oracle output: %w", err)
}

// it is assumed that the id of a symbol
// within OracleInput exists within OracleOutput

type OracleInput interface {
PriceSymbols() []string
PriceMultiplier() uint64
}

type (
LegacyBandInput bandprice.Input
BandInput bandprice.SymbolInput
)

func (in LegacyBandInput) PriceSymbols() []string {
return in.Symbols
}

func (in LegacyBandInput) PriceMultiplier() uint64 {
return in.Multiplier
}

func (in BandInput) PriceSymbols() []string {
return in.Symbols
}

func (in BandInput) PriceMultiplier() uint64 {
return BandPriceMultiplier
}

type OracleOutput interface {
Rate(id int) uint64
Valid(id int) bool
}

type (
LegacyBandOutput bandprice.Output
BandOutput bandprice.SymbolOutput
)

func (out LegacyBandOutput) Rate(id int) uint64 {
return out.Pxs[id]
}

func (out LegacyBandOutput) Valid(id int) bool {
return true
}

func (out BandOutput) Rate(id int) uint64 {
return out.Responses[id].Rate
}

return callData
func (out BandOutput) Valid(id int) bool {
return out.Responses[id].ResponseCode == 0
}
10 changes: 10 additions & 0 deletions chain/oracle/types/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,13 @@ func (s SymbolPriceTimestamps) GetTimestamp(oracleType OracleType, symbol string
func CheckPriceFeedThreshold(lastPrice, newPrice sdk.Dec) bool {
return newPrice.GT(lastPrice.Mul(sdk.NewDec(100))) || newPrice.LT(lastPrice.Quo(sdk.NewDec(100)))
}

func IsLegacySchemeOracleScript(scriptID int64, params BandIBCParams) bool {
for _, id := range params.LegacyOracleIds {
if id == scriptID {
return true
}
}

return false
}
Loading

0 comments on commit 42cab64

Please sign in to comment.