Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
roy-dydx committed Mar 27, 2024
1 parent d92b5b2 commit 1903b06
Show file tree
Hide file tree
Showing 13 changed files with 716 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/protocol-build-and-push-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Protocol Build & Push Image to AWS ECR
on: # yamllint disable-line rule:truthy
push:
branches:
- roy/roy2
- main
- 'release/[a-z]+/v0.[0-9]+.x' # e.g. release/protocol/v0.1.x
- 'release/[a-z]+/v[0-9]+.x' # e.g. release/protocol/v1.x
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/protocol-build-and-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Protocol Build & Push Image to AWS ECR
on: # yamllint disable-line rule:truthy
push:
branches:
- roy/roy2
- main
- 'release/[a-z]+/v0.[0-9]+.x' # e.g. release/protocol/v0.1.x
- 'release/[a-z]+/v[0-9]+.x' # e.g. release/protocol/v1.x
Expand Down
2 changes: 1 addition & 1 deletion protocol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/h2non/gock v1.2.0
github.com/holiman/uint256 v1.2.4 // indirect
github.com/ory/dockertest v3.3.5+incompatible
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
Expand Down Expand Up @@ -241,7 +242,6 @@ require (
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/hdevalence/ed25519consensus v0.1.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect
github.com/huandu/skiplist v1.2.0 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
Expand Down
4 changes: 2 additions & 2 deletions protocol/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -932,8 +932,8 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/holiman/bloomfilter/v2 v2.0.3 h1:73e0e/V0tCydx14a0SCYS/EWCxgwLZ18CZcZKVu0fao=
github.com/holiman/bloomfilter/v2 v2.0.3/go.mod h1:zpoh+gs7qcpqrHr3dB55AMiJwo0iURXE7ZOP9L9hSkA=
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c h1:DZfsyhDK1hnSS5lH8l+JggqzEleHteTYfutAiVlSUM8=
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw=
github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU=
github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/go-assert v1.1.5 h1:fjemmA7sSfYHJD7CUqs9qTwwfdNAx7/j2/ZlHXzNB3c=
github.com/huandu/go-assert v1.1.5/go.mod h1:yOLvuqZwmcHIC5rIzrBhT7D3Q9c3GFnd0JrPVhn/06U=
Expand Down
7 changes: 7 additions & 0 deletions protocol/lib/big_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"math"
"math/big"

"github.com/holiman/uint256"
)

// BigMulPow10 returns the result of `val * 10^exponent`, in *big.Rat.
Expand Down Expand Up @@ -101,6 +103,11 @@ func BigRatMulPpm(input *big.Rat, ppm uint32) *big.Rat {
)
}

func MulPpmUint256(input *uint256.Int, ppm uint32) *uint256.Int {
result := new(uint256.Int).Mul(input, uint256.NewInt(uint64(ppm)))
return result.Div(result, uint256.NewInt(1_000_000))
}

// bigGenericClamp is a helper function for BigRatClamp and BigIntClamp
// takes an input, upper bound, and lower bound. It returns the result
// bounded within the upper and lower bound, inclusive.
Expand Down
32 changes: 32 additions & 0 deletions protocol/lib/quantums.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package lib

import (
"math/big"

"github.com/holiman/uint256"
)

// BaseToQuoteQuantums converts an amount denoted in base quantums, to an equivalent amount denoted in quote
Expand Down Expand Up @@ -37,6 +39,20 @@ func BaseToQuoteQuantums(
)
}

func BaseToQuoteQuantumsUint256(
baseQuantums *uint256.Int,
baseCurrencyAtomicResolution int32,
priceValue uint64,
priceExponent int32,
) (bigNotional *uint256.Int) {
return multiplyByPriceUint256(
baseQuantums,
baseCurrencyAtomicResolution,
priceValue,
priceExponent,
)
}

// QuoteToBaseQuantums converts an amount denoted in quote quantums, to an equivalent amount denoted in base
// quantums. To determine the equivalent amount, an oracle price is used.
//
Expand Down Expand Up @@ -117,6 +133,22 @@ func multiplyByPrice(
)
}

func multiplyByPriceUint256(
value *uint256.Int,
baseCurrencyAtomicResolution int32,
priceValue uint64,
priceExponent int32,
) (result *uint256.Int) {
result = uint256.NewInt(priceValue)
result.Mul(result, value)

// TODO: bad cast
power := uint256.NewInt(uint64(priceExponent + baseCurrencyAtomicResolution - QuoteCurrencyAtomicResolution))
power.Exp(uint256.NewInt(10), power)

return result.Mul(result, power)
}

// FundingRateToIndex converts funding rate (in ppm) to FundingIndex given the oracle price.
//
// To get funding index from funding rate, we know that:
Expand Down
68 changes: 68 additions & 0 deletions protocol/x/assets/keeper/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
"github.com/holiman/uint256"
)

func (k Keeper) CreateAsset(
Expand Down Expand Up @@ -218,6 +219,42 @@ func (k Keeper) GetNetCollateral(
return big.NewInt(0), types.ErrNotImplementedMargin
}

// GetNetCollateral returns the net collateral that a given position (quantums)
// for a given assetId contributes to an account.
func (k Keeper) GetNetCollateralUint256(
ctx sdk.Context,
id uint32,
quantums *uint256.Int,
) (
netCollateralQuoteQuantums *uint256.Int,
err error,
) {
if id == types.AssetUsdc.Id {
return new(uint256.Int).Set(quantums), nil
}

// Get asset
_, exists := k.GetAsset(ctx, id)
if !exists {
return uint256.NewInt(0), errorsmod.Wrap(types.ErrAssetDoesNotExist, lib.UintToString(id))
}

// Balance is zero.
if quantums.BitLen() == 0 {
return uint256.NewInt(0), nil
}

// Balance is positive.
// TODO(DEC-581): add multi-collateral support.
if quantums.Sign() == 1 {
return uint256.NewInt(0), types.ErrNotImplementedMulticollateral
}

// Balance is negative.
// TODO(DEC-582): add margin-trading support.
return uint256.NewInt(0), types.ErrNotImplementedMargin
}

// GetMarginRequirements returns the initial and maintenance margin-
// requirements for a given position size for a given assetId.
func (k Keeper) GetMarginRequirements(
Expand Down Expand Up @@ -251,6 +288,37 @@ func (k Keeper) GetMarginRequirements(
return big.NewInt(0), big.NewInt(0), types.ErrNotImplementedMargin
}

func (k Keeper) GetMarginRequirementsUint256(
ctx sdk.Context,
id uint32,
bigQuantums *uint256.Int,
) (
bigInitialMarginQuoteQuantums *uint256.Int,
bigMaintenanceMarginQuoteQuantums *uint256.Int,
err error,
) {
// QuoteBalance does not contribute to any margin requirements.
if id == types.AssetUsdc.Id {
return uint256.NewInt(0), uint256.NewInt(0), nil
}

// Get asset
_, exists := k.GetAsset(ctx, id)
if !exists {
return uint256.NewInt(0), uint256.NewInt(0), errorsmod.Wrap(
types.ErrAssetDoesNotExist, lib.UintToString(id))
}

// Balance is zero or positive.
if bigQuantums.Sign() >= 0 {
return uint256.NewInt(0), uint256.NewInt(0), nil
}

// Balance is negative.
// TODO(DEC-582): margin-trading
return uint256.NewInt(0), uint256.NewInt(0), types.ErrNotImplementedMargin
}

// ConvertAssetToCoin converts the given `assetId` and `quantums` used in `x/asset`,
// to an `sdk.Coin` in correspoding `denom` and `amount` used in `x/bank`.
// Also outputs `convertedQuantums` which has the equal value as converted `sdk.Coin`.
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/keeper/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ func (k Keeper) AddOrderToOrderbookCollatCheck(
metrics.Latency,
)

success, successPerSubaccountUpdate, err := k.subaccountsKeeper.CanUpdateSubaccounts(
success, successPerSubaccountUpdate, err := k.subaccountsKeeper.CanUpdateSubaccountsUint256(
ctx,
updates,
satypes.Match,
Expand Down
9 changes: 9 additions & 0 deletions protocol/x/clob/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ type SubaccountsKeeper interface {
successPerUpdate []satypes.UpdateResult,
err error,
)
CanUpdateSubaccountsUint256(
ctx sdk.Context,
updates []satypes.Update,
updateType satypes.UpdateType,
) (
success bool,
successPerUpdate []satypes.UpdateResult,
err error,
)
GetNetCollateralAndMarginRequirements(
ctx sdk.Context,
update satypes.Update,
Expand Down
Loading

0 comments on commit 1903b06

Please sign in to comment.