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

feat : Integrate balancer-v3 #674

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
ed79ba2
update
sunspirit99 Dec 16, 2024
188895f
add new dex
sunspirit99 Dec 23, 2024
b6eaf97
tmp
sunspirit99 Dec 29, 2024
3359ec7
tmp
sunspirit99 Dec 29, 2024
4bd8d98
tmp
sunspirit99 Dec 29, 2024
30b872c
tmp
sunspirit99 Dec 30, 2024
26eb4c6
Integrate solidly-v2 (#671)
sunspirit99 Dec 30, 2024
cdd2e74
[Bebop] Support aggregate order (#672)
chrisngyn Dec 30, 2024
864cbeb
update
sunspirit99 Dec 30, 2024
fa7563f
tmp
sunspirit99 Dec 30, 2024
cd8ed28
tmp
sunspirit99 Dec 31, 2024
2c030eb
tmp
sunspirit99 Dec 31, 2024
d46fd9f
restructure
sunspirit99 Jan 1, 2025
d82d528
cleanup
sunspirit99 Jan 1, 2025
1ed6464
Merge branch 'main' into integrate-balancer-v3
sunspirit99 Jan 1, 2025
7177587
tmp
sunspirit99 Jan 1, 2025
02f0c64
tmp
sunspirit99 Jan 1, 2025
98e437c
tmp
sunspirit99 Jan 1, 2025
5870a3f
tmp
sunspirit99 Jan 1, 2025
10ae01b
tmp
sunspirit99 Jan 2, 2025
eb87205
math for weighted pool
sunspirit99 Jan 2, 2025
1899886
math for weighted pool
sunspirit99 Jan 2, 2025
3921ad3
update
sunspirit99 Jan 2, 2025
82f206c
update
sunspirit99 Jan 2, 2025
5934d94
update
sunspirit99 Jan 2, 2025
8181a7a
update unit tests
sunspirit99 Jan 2, 2025
7aa1294
update unit tests
sunspirit99 Jan 2, 2025
07cc9a1
fix data race
sunspirit99 Jan 2, 2025
140656a
fix math of stable pool
sunspirit99 Jan 2, 2025
07a0517
update
sunspirit99 Jan 2, 2025
742d399
update
sunspirit99 Jan 2, 2025
0fb97c8
cleanup
sunspirit99 Jan 2, 2025
74bf0b6
copy instead of lock
sunspirit99 Jan 3, 2025
d7fbbd0
copy instead of lock
sunspirit99 Jan 3, 2025
6751f28
update
sunspirit99 Jan 3, 2025
0d1d4da
finish
sunspirit99 Jan 3, 2025
8bab997
use v3Utils lib for optimization
sunspirit99 Jan 3, 2025
5ef0ecb
add condition
sunspirit99 Jan 3, 2025
4fd787a
cleanup
sunspirit99 Jan 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pkg/liquidity-source/balancer-v3/hooks/base_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package hooks

import (
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v3/math"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v3/shared"
"github.com/holiman/uint256"
)

type BaseHook struct{}

var _ IHook = (*BaseHook)(nil)

func NewBaseHook() *BaseHook {
return &BaseHook{}
}

func (h *BaseHook) OnBeforeSwap(shared.PoolSwapParams) (bool, error) {
return false, nil
}

func (h *BaseHook) OnAfterSwap(shared.AfterSwapParams) (bool, *uint256.Int, error) {
return false, math.ZERO, nil

}

func (h *BaseHook) OnComputeDynamicSwapFeePercentage(shared.PoolSwapParams) (bool, *uint256.Int, error) {
return false, math.ZERO, nil
}
64 changes: 64 additions & 0 deletions pkg/liquidity-source/balancer-v3/hooks/directional_fee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package hooks

import (
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v3/math"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v3/shared"
"github.com/holiman/uint256"
)

const DirectionalFeeHookType = "DirectionalFeeHook"

type directionalFeeHook struct {
BaseHook

staticSwapFeePercentage *uint256.Int
}

func NewDirectionalFeeHook(staticSwapFeePercentage *uint256.Int) *directionalFeeHook {
return &directionalFeeHook{
staticSwapFeePercentage: staticSwapFeePercentage,
}
}

func (h *directionalFeeHook) OnComputeDynamicSwapFeePercentage(param shared.PoolSwapParams) (bool, *uint256.Int, error) {
calculatedSwapFeePercentage, err := h.calculatedExpectedSwapFeePercentage(param.BalancesLiveScaled18[param.IndexIn],
param.BalancesLiveScaled18[param.IndexOut], param.AmountGivenScaled18)
if err != nil {
return false, nil, err
}

if calculatedSwapFeePercentage.Gt(h.staticSwapFeePercentage) {
return true, calculatedSwapFeePercentage, nil
}

return false, new(uint256.Int).Set(h.staticSwapFeePercentage), nil
}

func (h *directionalFeeHook) calculatedExpectedSwapFeePercentage(balanceIn, balanceOut, swapAmount *uint256.Int) (*uint256.Int, error) {
finalBalanceTokenIn, err := math.FixPoint.Add(balanceIn, swapAmount)
if err != nil {
return nil, err
}

finalBalanceTokenOut, err := math.FixPoint.Sub(balanceOut, swapAmount)
if err != nil {
return nil, err
}

if finalBalanceTokenIn.Gt(finalBalanceTokenOut) {
diff, err := math.FixPoint.Sub(finalBalanceTokenIn, finalBalanceTokenOut)
if err != nil {
return nil, err
}

totalLiquidity, err := math.FixPoint.Add(finalBalanceTokenIn, finalBalanceTokenOut)
if err != nil {
return nil, err
}

diff, err = math.FixPoint.DivDown(diff, totalLiquidity)
return diff, err
}

return math.ZERO, nil
}
3 changes: 3 additions & 0 deletions pkg/liquidity-source/balancer-v3/hooks/fee_taking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package hooks

const FeeTakingHookType = "FeeTakingHook"
25 changes: 25 additions & 0 deletions pkg/liquidity-source/balancer-v3/hooks/ihook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hooks

import (
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v3/shared"
"github.com/holiman/uint256"
)

type IHook interface {
OnBeforeSwap(param shared.PoolSwapParams) (bool, error)
OnAfterSwap(param shared.AfterSwapParams) (bool, *uint256.Int, error)
OnComputeDynamicSwapFeePercentage(param shared.PoolSwapParams) (bool, *uint256.Int, error)
}

// Define a map of supported hooks
var hooksMap = map[string]bool{
"DirectionalFee": true,
"FeeTaking": true,
"StableSurge": true,
"VeBALFeeDiscount": true,
}

func IsHookSupported(hook string) bool {
_, exists := hooksMap[hook]
return exists
}
11 changes: 11 additions & 0 deletions pkg/liquidity-source/balancer-v3/hooks/stable_surge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package hooks

const StableSurgeHookType = "StableSurgeHook"

type StableSurgeHook struct {
BaseHook
}

func NewStableSurgeHook() *StableSurgeHook {
return &StableSurgeHook{}
}
11 changes: 11 additions & 0 deletions pkg/liquidity-source/balancer-v3/hooks/ve_BAL_fee_discount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package hooks

const VeBALFeeDiscountHookType = "VeBALFeeDiscountHook"

type VeBALFeeDiscountHook struct {
BaseHook
}

func NewVeBALFeeDiscountHook() *VeBALFeeDiscountHook {
return &VeBALFeeDiscountHook{}
}
9 changes: 9 additions & 0 deletions pkg/liquidity-source/balancer-v3/math/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package math

import "github.com/holiman/uint256"

var (
ZERO = uint256.NewInt(0)
ONE = uint256.NewInt(1)
TWO = uint256.NewInt(2)
)
154 changes: 154 additions & 0 deletions pkg/liquidity-source/balancer-v3/math/fixed_point_math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package math

import (
"errors"

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

var (
ErrAddOverflow = errors.New("ADD_OVERFLOW")
ErrSubOverflow = errors.New("SUB_OVERFLOW")
ErrMulOverflow = errors.New("MUL_OVERFLOW")
ErrZeroDivision = errors.New("ZERO_DIVISION")

ONE_E18 = uint256.NewInt(1e18) // 18 decimal places
TWO_E18 = new(uint256.Int).Mul(ONE_E18, TWO)
FOUR_E18 = new(uint256.Int).Mul(TWO_E18, TWO)
MAX_POW_RELATIVE_ERROR = uint256.NewInt(10000) // 10^(-14)
)

var FixPoint *fixPoint

type fixPoint struct{}

func init() {
FixPoint = &fixPoint{}
}

func (f *fixPoint) MulDivUp(a, b, c *uint256.Int) (*uint256.Int, error) {
sunspirit99 marked this conversation as resolved.
Show resolved Hide resolved
return v3Utils.MulDivRoundingUp(a, b, c)
}

func (f *fixPoint) MulUp(a, b *uint256.Int) (*uint256.Int, error) {
return v3Utils.MulDivRoundingUp(a, b, ONE_E18)
}

func (f *fixPoint) MulDown(a, b *uint256.Int) (*uint256.Int, error) {
res, overflow := new(uint256.Int).MulDivOverflow(a, b, ONE_E18)
if overflow {
return nil, ErrMulOverflow
}

return res, nil
}

func (f *fixPoint) DivUp(a, b *uint256.Int) (*uint256.Int, error) {
if b.IsZero() {
return nil, ErrZeroDivision
}

return v3Utils.MulDivRoundingUp(a, ONE_E18, b)
}

func (f *fixPoint) DivDown(a, b *uint256.Int) (*uint256.Int, error) {
if b.IsZero() {
return nil, ErrZeroDivision
}

res, overflow := new(uint256.Int).MulDivOverflow(a, ONE_E18, b)
if overflow {
return nil, ErrMulOverflow
}

return res, nil
}

func (f *fixPoint) DivRawUp(a, b *uint256.Int) (*uint256.Int, error) {
if b.IsZero() {
return nil, ErrZeroDivision
}

// result = a == 0 ? 0 : 1 + (a - 1) / b
if a.IsZero() {
return ZERO, nil
}

delta := new(uint256.Int).Sub(a, ONE)
delta.Div(delta, b)
delta.Add(ONE, delta)

return delta, nil
}

func (f *fixPoint) PowUp(x, y *uint256.Int) (*uint256.Int, error) {
if y.Eq(ONE_E18) {
return x, nil
}

if y.Eq(TWO_E18) {
return f.MulUp(x, x)
}

if y.Eq(FOUR_E18) {
square, err := f.MulUp(x, x)
if err != nil {
return nil, err
}

return f.MulUp(square, square)
}

raw, err := Pow(x, y)
if err != nil {
return nil, err
}

var maxError *uint256.Int
maxError, err = f.MulUp(raw, MAX_POW_RELATIVE_ERROR)
if err != nil {
return nil, err
}

maxError, err = f.Add(maxError, ONE)
if err != nil {
return nil, err
}

return f.Add(raw, maxError)
}

func (f *fixPoint) Complement(x *uint256.Int) *uint256.Int {
// result = (x < ONE) ? (ONE - x) : 0
result := new(uint256.Int).Set(ZERO)
if x.Lt(ONE_E18) {
result.Sub(ONE_E18, x)
}

return result
}

func (f *fixPoint) Add(a *uint256.Int, b *uint256.Int) (*uint256.Int, error) {
c, overflow := new(uint256.Int).AddOverflow(a, b)
if overflow {
return nil, ErrAddOverflow
}
return c, nil
}

func (f *fixPoint) Sub(a *uint256.Int, b *uint256.Int) (*uint256.Int, error) {
c, overflow := new(uint256.Int).SubOverflow(a, b)
if overflow {
return nil, ErrSubOverflow
}
return c, nil
}

func (f *fixPoint) Mul(a *uint256.Int, b *uint256.Int) (*uint256.Int, error) {
c, overflow := new(uint256.Int).MulOverflow(a, b)
if overflow {
return nil, ErrMulOverflow
}
return c, nil
}
Loading
Loading