Skip to content

Commit 4060fef

Browse files
authored
ft: integrate uniswap-v4-deli (#1142)
1 parent df2d457 commit 4060fef

File tree

23 files changed

+454
-59
lines changed

23 files changed

+454
-59
lines changed

pkg/liquidity-source/uniswap/v4/hooks.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ type BeforeSwapParams struct {
2121
}
2222

2323
type BeforeSwapResult struct {
24-
DeltaSpecific *big.Int
25-
DeltaUnSpecific *big.Int
26-
SwapFee FeeAmount
27-
Gas int64
28-
SwapInfo any
24+
DeltaSpecified *big.Int
25+
DeltaUnspecified *big.Int
26+
SwapFee FeeAmount
27+
Gas int64
28+
SwapInfo any
2929
}
3030

3131
func ValidateBeforeSwapResult(result *BeforeSwapResult) error {
3232
if result == nil {
3333
return errors.New("before swap result is nil")
3434
}
3535

36-
if result.DeltaSpecific == nil {
36+
if result.DeltaSpecified == nil {
3737
return errors.New("delta specified is nil")
3838
}
3939

40-
if result.DeltaUnSpecific == nil {
40+
if result.DeltaUnspecified == nil {
4141
return errors.New("delta unspecified is nil")
4242
}
4343

@@ -177,8 +177,8 @@ func (h *BaseHook) Track(context.Context, *HookParam) (string, error) {
177177

178178
func (h *BaseHook) BeforeSwap(_ *BeforeSwapParams) (*BeforeSwapResult, error) {
179179
return &BeforeSwapResult{
180-
DeltaSpecific: bignumber.ZeroBI,
181-
DeltaUnSpecific: bignumber.ZeroBI,
180+
DeltaSpecified: bignumber.ZeroBI,
181+
DeltaUnspecified: bignumber.ZeroBI,
182182
}, nil
183183
}
184184

pkg/liquidity-source/uniswap/v4/hooks/aegis/hook.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ func (h *Hook) BeforeSwap(swapHookParams *uniswapv4.BeforeSwapParams) (*uniswapv
142142
hookFeeAmt.Mul(hookFeeAmt, h.protocolFee).Div(hookFeeAmt, FeeMax)
143143
}
144144
return &uniswapv4.BeforeSwapResult{
145-
SwapFee: h.swapFee,
146-
DeltaSpecific: hookFeeAmt,
147-
DeltaUnSpecific: bignumber.ZeroBI,
145+
SwapFee: h.swapFee,
146+
DeltaSpecified: hookFeeAmt,
147+
DeltaUnspecified: bignumber.ZeroBI,
148148
}, nil
149149
}
150150

pkg/liquidity-source/uniswap/v4/hooks/angstrom/hook.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"math/big"
77

88
"github.com/KyberNetwork/ethrpc"
9+
"github.com/ethereum/go-ethereum/common"
10+
"github.com/samber/lo"
11+
912
uniswapv4 "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/uniswap/v4"
1013
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
1114
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject"
12-
"github.com/ethereum/go-ethereum/common"
13-
"github.com/samber/lo"
1415
)
1516

1617
type Hook struct {
@@ -88,9 +89,9 @@ func (h *Hook) Track(ctx context.Context, param *uniswapv4.HookParam) (string, e
8889

8990
func (h *Hook) BeforeSwap(swapHookParams *uniswapv4.BeforeSwapParams) (*uniswapv4.BeforeSwapResult, error) {
9091
return &uniswapv4.BeforeSwapResult{
91-
DeltaSpecific: bignumber.ZeroBI,
92-
DeltaUnSpecific: bignumber.ZeroBI,
93-
SwapFee: uniswapv4.FeeAmount(h.extra.UnlockedFee.Uint64()),
92+
DeltaSpecified: bignumber.ZeroBI,
93+
DeltaUnspecified: bignumber.ZeroBI,
94+
SwapFee: uniswapv4.FeeAmount(h.extra.UnlockedFee.Uint64()),
9495
}, nil
9596
}
9697

pkg/liquidity-source/uniswap/v4/hooks/bunni-v2/hook.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,9 @@ func (h *Hook) BeforeSwap(params *uniswapv4.BeforeSwapParams) (*uniswapv4.Before
255255
if (params.ZeroForOne && currentActiveBalance1.IsZero()) ||
256256
(!params.ZeroForOne && currentActiveBalance0.IsZero()) {
257257
return &uniswapv4.BeforeSwapResult{
258-
DeltaSpecific: params.AmountSpecified,
259-
DeltaUnSpecific: bignumber.ZeroBI,
260-
SwapInfo: swapInfo,
258+
DeltaSpecified: params.AmountSpecified,
259+
DeltaUnspecified: bignumber.ZeroBI,
260+
SwapInfo: swapInfo,
261261
}, nil
262262
}
263263

@@ -459,10 +459,10 @@ func (h *Hook) BeforeSwap(params *uniswapv4.BeforeSwapParams) (*uniswapv4.Before
459459
Sub(outputAmount, curatorFeeAmount)
460460

461461
actualInputAmount := u256.Max(amountSpecified, inputAmount)
462-
result.DeltaSpecific = actualInputAmount.ToBig()
462+
result.DeltaSpecified = actualInputAmount.ToBig()
463463

464464
outputAmountInt := outputAmount.ToBig()
465-
result.DeltaUnSpecific = outputAmountInt.Neg(outputAmountInt)
465+
result.DeltaUnspecified = outputAmountInt.Neg(outputAmountInt)
466466

467467
hookHandleSwapInputAmount.Set(inputAmount)
468468
hookHandleSwapOutputAmount.Add(outputAmount, hookFeesAmount)
@@ -502,10 +502,10 @@ func (h *Hook) BeforeSwap(params *uniswapv4.BeforeSwapParams) (*uniswapv4.Before
502502
inputAmount.Add(inputAmount, swapFeeAmount).
503503
Add(inputAmount, hookFeesAmount).
504504
Add(inputAmount, curatorFeeAmount)
505-
result.DeltaUnSpecific = inputAmount.ToBig()
505+
result.DeltaUnspecified = inputAmount.ToBig()
506506

507507
actualOutputAmount := u256.Min(amountSpecified, outputAmount).ToBig()
508-
result.DeltaSpecific = actualOutputAmount.Neg(actualOutputAmount)
508+
result.DeltaSpecified = actualOutputAmount.Neg(actualOutputAmount)
509509

510510
hookHandleSwapOutputAmount.Set(outputAmount)
511511
hookHandleSwapInputAmount.Sub(inputAmount, hookFeesAmount)

pkg/liquidity-source/uniswap/v4/hooks/clanker/dynamic_fee_hook.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,9 @@ func (h *DynamicFeeHook) BeforeSwap(params *uniswapv4.BeforeSwapParams) (*uniswa
336336

337337
if params.ExactIn && !swappingForClanker || !params.ExactIn && swappingForClanker {
338338
return &uniswapv4.BeforeSwapResult{
339-
DeltaSpecific: bignumber.ZeroBI,
340-
DeltaUnSpecific: bignumber.ZeroBI,
341-
SwapFee: swapFee,
339+
DeltaSpecified: bignumber.ZeroBI,
340+
DeltaUnspecified: bignumber.ZeroBI,
341+
SwapFee: swapFee,
342342
}, nil
343343
}
344344

@@ -358,9 +358,9 @@ func (h *DynamicFeeHook) BeforeSwap(params *uniswapv4.BeforeSwapParams) (*uniswa
358358
fee.Div(&fee, bignumber.BONE)
359359

360360
return &uniswapv4.BeforeSwapResult{
361-
DeltaSpecific: &fee,
362-
DeltaUnSpecific: bignumber.ZeroBI,
363-
SwapFee: swapFee,
361+
DeltaSpecified: &fee,
362+
DeltaUnspecified: bignumber.ZeroBI,
363+
SwapFee: swapFee,
364364
}, nil
365365
}
366366

pkg/liquidity-source/uniswap/v4/hooks/clanker/static_fee_hook.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ func (h *StaticFeeHook) BeforeSwap(params *uniswapv4.BeforeSwapParams) (*uniswap
134134

135135
if params.ExactIn && !swappingForClanker || !params.ExactIn && swappingForClanker {
136136
return &uniswapv4.BeforeSwapResult{
137-
DeltaSpecific: bignumber.ZeroBI,
138-
DeltaUnSpecific: bignumber.ZeroBI,
139-
SwapFee: h.clankerFee,
137+
DeltaSpecified: bignumber.ZeroBI,
138+
DeltaUnspecified: bignumber.ZeroBI,
139+
SwapFee: h.clankerFee,
140140
}, nil
141141
}
142142

@@ -153,9 +153,9 @@ func (h *StaticFeeHook) BeforeSwap(params *uniswapv4.BeforeSwapParams) (*uniswap
153153
fee.Div(&fee, bignumber.BONE)
154154

155155
return &uniswapv4.BeforeSwapResult{
156-
DeltaSpecific: &fee,
157-
DeltaUnSpecific: bignumber.ZeroBI,
158-
SwapFee: h.pairedFee,
156+
DeltaSpecified: &fee,
157+
DeltaUnspecified: bignumber.ZeroBI,
158+
SwapFee: h.pairedFee,
159159
}, nil
160160
}
161161

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package deli
2+
3+
import (
4+
"bytes"
5+
6+
"github.com/ethereum/go-ethereum/accounts/abi"
7+
)
8+
9+
var (
10+
constantProductABI abi.ABI
11+
)
12+
13+
func init() {
14+
builder := []struct {
15+
ABI *abi.ABI
16+
data []byte
17+
}{
18+
{&constantProductABI, constantProductABIJson},
19+
}
20+
21+
for _, b := range builder {
22+
var err error
23+
*b.ABI, err = abi.JSON(bytes.NewReader(b.data))
24+
if err != nil {
25+
panic(err)
26+
}
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
{
3+
"inputs": [
4+
{
5+
"internalType": "PoolId",
6+
"name": "poolId",
7+
"type": "bytes32"
8+
}
9+
],
10+
"name": "getReserves",
11+
"outputs": [
12+
{
13+
"internalType": "uint128",
14+
"type": "uint128"
15+
},
16+
{
17+
"internalType": "uint128",
18+
"type": "uint128"
19+
}
20+
],
21+
"stateMutability": "view",
22+
"type": "function"
23+
}
24+
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package deli
2+
3+
import (
4+
"math/big"
5+
6+
"github.com/ethereum/go-ethereum/common"
7+
"github.com/holiman/uint256"
8+
)
9+
10+
var (
11+
HookAddresses = []common.Address{
12+
common.HexToAddress("0xC384B99A6e5cD1a800B2d83aB71BaB7bD712b0cc"), // DeliHook
13+
}
14+
ConstantProductAddresses = []common.Address{
15+
common.HexToAddress("0x00C9DA9AbC5303219ead3Cf0307b5A8A7644BaC8"), // DeliHookConstantProduct
16+
}
17+
)
18+
19+
const wBLT = "0x4e74d4db6c0726ccded4656d0bce448876bb4c7a"
20+
21+
var (
22+
FeeDenom = big.NewInt(1e6)
23+
UFeeDenom = uint256.NewInt(1e6)
24+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package deli
2+
3+
import _ "embed"
4+
5+
//go:embed abis/DeliHookConstantProduct.json
6+
var constantProductABIJson []byte

0 commit comments

Comments
 (0)