Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

math.gno

2.83 Kb · 81 lines
 1package limit
 2
 3import (
 4	"math"
 5
 6	u256 "gno.land/p/gnoswap/uint256/v1"
 7)
 8
 9// Read-only constants: never pass them to a mutating u256 method.
10var (
11	maxInt64U256 = u256.NewUint(uint64(math.MaxInt64))
12)
13
14// Every product below has int64 or bps operands, so it fits in 140 bits and
15// never relies on truncating uint256 overflow.
16
17// CumulativeExecutionFee returns floor(grossOutput * executionFeeRate / BpsDenominator):
18// the total execution fee owed on an order's cumulative pool output.
19// The execution fee of one fill is the increase of this value, so splitting a fill
20// never lowers the total execution fee.
21func CumulativeExecutionFee(grossOutput int64, executionFeeRate int64) (int64, error) {
22	if grossOutput < 0 {
23		return 0, ErrInsufficientOutput
24	}
25
26	if !isValidExecutionFeeRate(executionFeeRate) {
27		return 0, ErrInvalidExecutionFeeRate
28	}
29
30	return toInt64(u256.MulDiv(
31		u256.NewUint(uint64(grossOutput)),
32		u256.NewUint(uint64(executionFeeRate)),
33		u256.NewUint(uint64(BpsDenominator)),
34	))
35}
36
37// proportionalMinimumOutput returns
38// ceil(takingAmount * fillInput * (BpsDenominator-slippageBps) / (makingAmount * BpsDenominator)):
39// the owner-net output a fill of fillInput must deliver, rounded up so the
40// order's slippage-adjusted ratio is never undercut.
41// Callers validate makingAmount > 0, takingAmount > 0, 0 <= fillInput <= makingAmount
42// and 0 <= slippageBps <= BpsDenominator.
43func proportionalMinimumOutput(makingAmount, takingAmount, slippageBps, fillInput int64) (int64, error) {
44	numerator := u256.Zero().Mul(u256.NewUint(uint64(takingAmount)), u256.NewUint(uint64(fillInput)))
45	numerator.Mul(numerator, u256.NewUint(uint64(BpsDenominator-slippageBps)))
46
47	denominator := u256.Zero().Mul(u256.NewUint(uint64(makingAmount)), u256.NewUint(uint64(BpsDenominator)))
48
49	return toInt64(u256.DivRoundingUp(numerator, denominator))
50}
51
52// requiredGrossOutput returns the least cumulative gross output whose
53// owner-net share reaches requiredNet. With
54// net(G) = G-floor(G*f/D) = ceil(G*(D-f)/D),
55// it is floor((requiredNet-1)*D/(D-f))+1.
56// requiredNet must be positive and executionFeeRate valid. The result may exceed
57// MaxInt64, in which case it returns ErrAmountOverflow.
58func requiredGrossOutput(requiredNet *u256.Uint, executionFeeRate int64) (int64, error) {
59	numerator := u256.Zero().Sub(requiredNet, u256.One())
60	required := u256.MulDiv(
61		numerator,
62		u256.NewUint(uint64(BpsDenominator)),
63		u256.NewUint(uint64(BpsDenominator-executionFeeRate)),
64	)
65	required.Add(required, u256.One())
66
67	return toInt64(required)
68}
69
70func isValidExecutionFeeRate(executionFeeRate int64) bool {
71	return executionFeeRate >= 0 && executionFeeRate <= MaxExecutionFeeRate
72}
73
74// toInt64 converts a non-negative u256 to int64, or returns ErrAmountOverflow.
75func toInt64(v *u256.Uint) (int64, error) {
76	if v.Gt(maxInt64U256) {
77		return 0, ErrAmountOverflow
78	}
79
80	return int64(v.Uint64()), nil
81}