Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove a couple of uint256 <> decimal conversion #3696

Merged
merged 1 commit into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions integration/features/opening-auction-uncross.feature
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ Feature: Set up a market, with an opening auction, then uncross the book
Then the traders should have the following margin levels:
| trader | market id | maintenance | search | initial | release |
| trader1 | ETH/DEC19 | 25200 | 27720 | 30240 | 65520 |
| trader2 | ETH/DEC19 | 23899 | 26289 | 28679 | 57458 |
| trader2 | ETH/DEC19 | 23900 | 26290 | 28680 | 57460 |
# values before uint stuff
#| trader1 | ETH/DEC19 | 25201 | 27721 | 30241 | 65521 |
#| trader2 | ETH/DEC19 | 23899 | 26289 | 28679 | 57458 |
Then the traders should have the following account balances:
| trader | asset | market id | margin | general |
| trader1 | BTC | ETH/DEC19 | 30240 | 99969760 |
| trader2 | BTC | ETH/DEC19 | 28679 | 99971321 |
| trader2 | BTC | ETH/DEC19 | 28680 | 99971320 |
# values before uint
#| trader1 | BTC | ETH/DEC19 | 30241 | 99969759 |
When the traders withdraw the following assets:
| trader | asset | amount |
| trader1 | BTC | 99969760 |
| trader2 | BTC | 99971321 |
| trader2 | BTC | 99971320 |
Then the traders should have the following account balances:
| trader | asset | market id | margin | general |
| trader1 | BTC | ETH/DEC19 | 30240 | 0 |
| trader2 | BTC | ETH/DEC19 | 28679 | 0 |
| trader2 | BTC | ETH/DEC19 | 28680 | 0 |
# values before uint
#| trader1 | BTC | ETH/DEC19 | 30241 | 0 |
Then the opening auction period ends for market "ETH/DEC19"
Expand All @@ -71,10 +71,10 @@ Feature: Set up a market, with an opening auction, then uncross the book
| trader2 | t2-s-3 | STATUS_FILLED |
And the following transfers should happen:
| from | to | from account | to account | market id | amount | asset |
| trader2 | trader2 | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_GENERAL | ETH/DEC19 | 9479 | BTC |
| trader2 | trader2 | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_GENERAL | ETH/DEC19 | 9480 | BTC |
Then the traders should have the following account balances:
| trader | asset | market id | margin | general |
| trader2 | BTC | ETH/DEC19 | 19200 | 9479 |
| trader2 | BTC | ETH/DEC19 | 19200 | 9480 |
| trader1 | BTC | ETH/DEC19 | 30240 | 0 |
# values before uint
#| trader1 | BTC | ETH/DEC19 | 30241 | 0 |
44 changes: 24 additions & 20 deletions risk/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ type marginChange struct {
// Engine is the risk engine
type Engine struct {
Config
marginCalculator *types.MarginCalculator
log *logging.Logger
cfgMu sync.Mutex
model Model
factors *types.RiskResult
waiting bool
ob Orderbook
as AuctionState
broker Broker
marginCalculator *types.MarginCalculator
scalingFactorsUint *scalingFactorsUint
log *logging.Logger
cfgMu sync.Mutex
model Model
factors *types.RiskResult
waiting bool
ob Orderbook
as AuctionState
broker Broker

currTime int64
mktID string
Expand All @@ -78,18 +79,20 @@ func NewEngine(
log = log.Named(namedLogger)
log.SetLevel(config.Level.Get())

sfUint := scalingFactorsUintFromDecimals(marginCalculator.ScalingFactors)
return &Engine{
log: log,
Config: config,
marginCalculator: marginCalculator,
factors: initialFactors,
model: model,
waiting: false,
ob: ob,
as: as,
broker: broker,
currTime: initialTime,
mktID: mktID,
log: log,
Config: config,
marginCalculator: marginCalculator,
scalingFactorsUint: sfUint,
factors: initialFactors,
model: model,
waiting: false,
ob: ob,
as: as,
broker: broker,
currTime: initialTime,
mktID: mktID,
}
}

Expand All @@ -99,6 +102,7 @@ func (e *Engine) OnMarginScalingFactorsUpdate(sf *types.ScalingFactors) error {
}

e.marginCalculator.ScalingFactors = sf
e.scalingFactorsUint = scalingFactorsUintFromDecimals(sf)
return nil
}

Expand Down
60 changes: 38 additions & 22 deletions risk/margins_calculation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,45 @@ import (
"code.vegaprotocol.io/vega/types/num"
)

func newMarginLevels(maintenance num.Decimal, scalingFactors *types.ScalingFactors) *types.MarginLevels {
maintenance = maintenance.Ceil()
mUint, _ := num.UintFromDecimal(maintenance)
sl, _ := num.UintFromDecimal(maintenance.Mul(scalingFactors.SearchLevel))
im, _ := num.UintFromDecimal(maintenance.Mul(scalingFactors.InitialMargin))
cr, _ := num.UintFromDecimal(maintenance.Mul(scalingFactors.CollateralRelease))
var (
exp = num.Zero().Exp(num.NewUint(10), num.NewUint(5))
expDec = num.DecimalFromUint(exp)
)

type scalingFactorsUint struct {
search *num.Uint
initial *num.Uint
release *num.Uint
}

func scalingFactorsUintFromDecimals(sf *types.ScalingFactors) *scalingFactorsUint {
search, _ := num.UintFromDecimal(sf.SearchLevel.Mul(expDec))
initial, _ := num.UintFromDecimal(sf.InitialMargin.Mul(expDec))
release, _ := num.UintFromDecimal(sf.CollateralRelease.Mul(expDec))

return &scalingFactorsUint{
search: search,
initial: initial,
release: release,
}
}

func newMarginLevels(maintenance num.Decimal, scalingFactors *scalingFactorsUint) *types.MarginLevels {
umaintenance, _ := num.UintFromDecimal(maintenance.Ceil())
return &types.MarginLevels{
MaintenanceMargin: mUint,
SearchLevel: sl,
InitialMargin: im,
CollateralReleaseLevel: cr,
MaintenanceMargin: umaintenance,
SearchLevel: num.Zero().Div(num.Zero().Mul(scalingFactors.search, umaintenance), exp),
InitialMargin: num.Zero().Div(num.Zero().Mul(scalingFactors.initial, umaintenance), exp),
CollateralReleaseLevel: num.Zero().Div(num.Zero().Mul(scalingFactors.release, umaintenance), exp),
}
}

func addMarginLevels(ml *types.MarginLevels, maintenance num.Decimal, scalingFactors *types.ScalingFactors) {
mtl, _ := num.UintFromDecimal(maintenance)
sl, _ := num.UintFromDecimal(maintenance.Mul(scalingFactors.SearchLevel))
im, _ := num.UintFromDecimal(maintenance.Mul(scalingFactors.InitialMargin))
cr, _ := num.UintFromDecimal(maintenance.Mul(scalingFactors.CollateralRelease))
func addMarginLevels(ml *types.MarginLevels, maintenance num.Decimal, scalingFactors *scalingFactorsUint) {
mtl, _ := num.UintFromDecimal(maintenance.Ceil())
ml.MaintenanceMargin.AddSum(mtl)
ml.SearchLevel.AddSum(sl)
ml.InitialMargin.AddSum(im)
ml.CollateralReleaseLevel.AddSum(cr)
ml.SearchLevel.AddSum(num.Zero().Div(num.Zero().Mul(scalingFactors.search, mtl), exp))
ml.InitialMargin.AddSum(num.Zero().Div(num.Zero().Mul(scalingFactors.initial, mtl), exp))
ml.CollateralReleaseLevel.AddSum(num.Zero().Div(num.Zero().Mul(scalingFactors.release, mtl), exp))
}

func (e *Engine) calculateAuctionMargins(m events.Margin, markPrice *num.Uint, rf types.RiskFactor) *types.MarginLevels {
Expand All @@ -49,9 +65,9 @@ func (e *Engine) calculateAuctionMargins(m events.Margin, markPrice *num.Uint, r
}
// add buy/sell order margins to the margin requirements
if lMargin.GreaterThan(sMargin) {
addMarginLevels(ml, lMargin, e.marginCalculator.ScalingFactors)
addMarginLevels(ml, lMargin, e.scalingFactorsUint)
} else {
addMarginLevels(ml, sMargin, e.marginCalculator.ScalingFactors)
addMarginLevels(ml, sMargin, e.scalingFactorsUint)
}
// this is a bit of a hack, perhaps, but it keeps the remaining flow in the core simple:
// artificially increase the release level so we never release the margin balance during auction
Expand Down Expand Up @@ -158,10 +174,10 @@ func (e *Engine) calculateMargins(m events.Margin, markPrice *num.Uint, rf types

// the greatest liability is the most positive number
if marginMaintenanceLng.GreaterThan(marginMaintenanceSht) && marginMaintenanceLng.GreaterThan(zeroD) {
return newMarginLevels(marginMaintenanceLng, e.marginCalculator.ScalingFactors)
return newMarginLevels(marginMaintenanceLng, e.scalingFactorsUint)
}
if marginMaintenanceSht.GreaterThan(zeroD) {
return newMarginLevels(marginMaintenanceSht, e.marginCalculator.ScalingFactors)
return newMarginLevels(marginMaintenanceSht, e.scalingFactorsUint)
}

return &types.MarginLevels{
Expand Down