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

fix/algebra integral concurrent map access #663

Merged
merged 9 commits into from
Dec 26, 2024
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/KyberNetwork/logger v0.2.1
github.com/KyberNetwork/msgpack/v5 v5.4.2
github.com/KyberNetwork/pancake-v3-sdk v0.2.0
github.com/KyberNetwork/uniswapv3-sdk-uint256 v0.5.0
github.com/KyberNetwork/uniswapv3-sdk-uint256 v0.5.3-0.20241221122441-5808be2e7503
github.com/daoleno/uniswap-sdk-core v0.1.7
github.com/daoleno/uniswapv3-sdk v0.4.0
github.com/davecgh/go-spew v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/KyberNetwork/uniswap-sdk-core v0.1.7 h1:9CfjLkFZcOxgu0US0ZB1vvdgA1sp3
github.com/KyberNetwork/uniswap-sdk-core v0.1.7/go.mod h1:DPzL8zNicstPzvX74ZeeHsiIUquZRpwviceDHQ8+UQ4=
github.com/KyberNetwork/uniswapv3-sdk v0.5.5 h1:PLkDL8YWgN00yfNGbQpOGRcKRD3eoFXOFzjvuHP9Gmo=
github.com/KyberNetwork/uniswapv3-sdk v0.5.5/go.mod h1:XFySgQXZ+dl0yRze6Rj8jloILcFP8FbXJjcyk/1RmSU=
github.com/KyberNetwork/uniswapv3-sdk-uint256 v0.5.0 h1:AkEmCEBmyYAUU+WHGrgw9uaXHteqNn66CD36FaXoUig=
github.com/KyberNetwork/uniswapv3-sdk-uint256 v0.5.0/go.mod h1:VQNKmTwlX6gyW1gzaSQjYG+xCBpqmgH0VQslYMtqzAg=
github.com/KyberNetwork/uniswapv3-sdk-uint256 v0.5.3-0.20241221122441-5808be2e7503 h1:FvNvHE1yHjLK8owNpOnVUdZNYNZ5xnXJ9uKvuWMC0NY=
github.com/KyberNetwork/uniswapv3-sdk-uint256 v0.5.3-0.20241221122441-5808be2e7503/go.mod h1:VQNKmTwlX6gyW1gzaSQjYG+xCBpqmgH0VQslYMtqzAg=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/VictoriaMetrics/fastcache v1.12.2 h1:N0y9ASrJ0F6h0QaC3o6uJb3NIZ9VKLjCM7NQbSmF7WI=
Expand Down
2 changes: 1 addition & 1 deletion pkg/liquidity-source/algebra/integral/adaptive_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
// / @notice Calculates fee based on formula:
// / baseFee + sigmoid1(volatility) + sigmoid2(volatility)
// / maximum value capped by baseFee + alpha1 + alpha2
func getFee(volatility *uint256.Int, config FeeConfiguration) uint16 {
func getFee(volatility *uint256.Int, config *DynamicFeeConfig) uint16 {
// normalize for 15 sec interval
normalizedVolatility := new(uint256.Int).Div(volatility, uFIFTEEN)

Expand Down
13 changes: 1 addition & 12 deletions pkg/liquidity-source/algebra/integral/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"math/big"
"time"

"github.com/KyberNetwork/int256"
"github.com/holiman/uint256"
)

Expand Down Expand Up @@ -59,30 +58,20 @@ var (
FEE_DENOMINATOR = uint256.NewInt(1e6)
COMMUNITY_FEE_DENOMINATOR = uint256.NewInt(1e3)

ZERO = int256.NewInt(0)
ONE = int256.NewInt(1)

MIN_INT256 = new(int256.Int).Neg(new(int256.Int).Lsh(ZERO, 255)) // -2^255
MAX_UINT256 = new(uint256.Int).Sub(new(uint256.Int).Lsh(uZERO, 256), uONE)

MAX_UINT16 = new(uint256.Int).SetUint64(1<<16 - 1)
MIN_UINT16 = new(uint256.Int).SetUint64(1)

uZERO = uint256.NewInt(0)
uONE = uint256.NewInt(1)
uTWO = uint256.NewInt(2)
uFOUR = uint256.NewInt(4)
uSIX = uint256.NewInt(6)
uEIGHT = uint256.NewInt(8)
uFIFTEEN = uint256.NewInt(15)
uSIXTEEN = uint256.NewInt(16)
uTWENTYFOUR = uint256.NewInt(24)

MIN_SQRT_RATIO = big.NewInt(4295128739)
MAX_SQRT_RATIO, _ = new(big.Int).SetString("1461446703485210103287273052203988822378723970342", 10)

Q96 = new(uint256.Int).Lsh(uONE, 96) // 1 << 96
Q128 = new(uint256.Int).Lsh(uONE, 128) // 1 << 128
Q96 = new(uint256.Int).Lsh(uONE, 96) // 1 << 96

BASE_FEE_MULTIPLIER = new(uint256.Int).Lsh(uONE, FEE_FACTOR_SHIFT) // 1 << 96
DOUBLE_FEE_MULTIPLIER = new(uint256.Int).Lsh(uONE, 2*FEE_FACTOR_SHIFT) // 1 << 2*96
Expand Down
2 changes: 2 additions & 0 deletions pkg/liquidity-source/algebra/integral/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ var (
ErrOutOfRangeOrInvalid = errors.New("value out of range or invalid")
ErrLiquiditySub = errors.New("liquidity sub error")
ErrLiquidityAdd = errors.New("liquidity add error")
ErrOverflow = errors.New("overflow")
ErrUnderflow = errors.New("underflow")
)
14 changes: 5 additions & 9 deletions pkg/liquidity-source/algebra/integral/math.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
package integral

import (
"errors"

"github.com/KyberNetwork/int256"
v3Utils "github.com/KyberNetwork/uniswapv3-sdk-uint256/utils"
"github.com/holiman/uint256"
)

func unsafeDivRoundingUp(x, y *uint256.Int) (*uint256.Int, error) {
func unsafeDivRoundingUp(x, y *uint256.Int) *uint256.Int {
if y.Sign() == 0 {
return nil, errors.New("division by zero")
panic("division by zero")
NgoKimPhu marked this conversation as resolved.
Show resolved Hide resolved
}

quotient := new(uint256.Int).Div(x, y)

remainder := new(uint256.Int).Mod(x, y)
quotient, remainder := new(uint256.Int).DivMod(x, y, new(uint256.Int))
if remainder.Sign() > 0 {
quotient.Add(quotient, uONE)
quotient.AddUint64(quotient, 1)
}

return quotient, nil
return quotient
}

func addDelta(x *uint256.Int, y *int256.Int) (*uint256.Int, error) {
Expand Down
Loading
Loading