Skip to content

Commit a0296fb

Browse files
authored
ft: integrate uniswap-v4-arrakis (#1260)
1 parent 6729a51 commit a0296fb

File tree

12 files changed

+158
-9
lines changed

12 files changed

+158
-9
lines changed

pkg/liquidity-source/euler-swap/shared/constant.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"math/big"
55

66
"github.com/KyberNetwork/int256"
7-
big256 "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/big256"
87
"github.com/holiman/uint256"
8+
9+
big256 "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/big256"
910
)
1011

1112
const (

pkg/liquidity-source/euler-swap/v1/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package v1
33
import (
44
"math/big"
55

6-
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/euler-swap/shared"
76
"github.com/ethereum/go-ethereum/common"
87
"github.com/holiman/uint256"
8+
9+
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/euler-swap/shared"
910
)
1011

1112
type PoolExtra struct {

pkg/liquidity-source/euler-swap/v2/integration_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
"testing"
99

1010
"github.com/KyberNetwork/ethrpc"
11-
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/euler-swap/shared"
12-
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
13-
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
1411
"github.com/ethereum/go-ethereum/common"
1512
"github.com/stretchr/testify/assert"
1613
"github.com/stretchr/testify/require"
14+
15+
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/euler-swap/shared"
16+
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
17+
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
1718
)
1819

1920
func TestIntegration_V2_ComputeQuote(t *testing.T) {

pkg/liquidity-source/euler-swap/v2/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package v2
33
import (
44
"math/big"
55

6-
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/euler-swap/shared"
76
"github.com/ethereum/go-ethereum/common"
87
"github.com/holiman/uint256"
8+
9+
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/euler-swap/shared"
910
)
1011

1112
type StaticExtra struct {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package arrakis
2+
3+
import (
4+
"bytes"
5+
6+
"github.com/ethereum/go-ethereum/accounts/abi"
7+
)
8+
9+
var hookABI abi.ABI
10+
11+
func init() {
12+
builder := []struct {
13+
ABI *abi.ABI
14+
data []byte
15+
}{
16+
{&hookABI, hookABIJson},
17+
}
18+
19+
for _, b := range builder {
20+
var err error
21+
*b.ABI, err = abi.JSON(bytes.NewReader(b.data))
22+
if err != nil {
23+
panic(err)
24+
}
25+
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[
2+
{
3+
"inputs": [
4+
{
5+
"internalType": "PoolId",
6+
"name": "id_",
7+
"type": "bytes32"
8+
}
9+
],
10+
"name": "getFeesData",
11+
"outputs": [
12+
{
13+
"internalType": "address",
14+
"name": "module",
15+
"type": "address"
16+
},
17+
{
18+
"internalType": "uint32",
19+
"name": "zeroForOneFee",
20+
"type": "uint32"
21+
},
22+
{
23+
"internalType": "uint32",
24+
"name": "oneForZeroFee",
25+
"type": "uint32"
26+
}
27+
],
28+
"stateMutability": "view",
29+
"type": "function"
30+
}
31+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package arrakis
2+
3+
import (
4+
"github.com/ethereum/go-ethereum/common"
5+
)
6+
7+
var (
8+
HookAddresses = []common.Address{
9+
common.HexToAddress("0xF9527fB5a34aC6FBC579e4fbc3bF292Ed57d4880"),
10+
}
11+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package arrakis
2+
3+
import _ "embed"
4+
5+
//go:embed abis/ArrakisPrivateHook.json
6+
var hookABIJson []byte
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package arrakis
2+
3+
import (
4+
"context"
5+
6+
"github.com/KyberNetwork/ethrpc"
7+
"github.com/ethereum/go-ethereum/common"
8+
"github.com/ethereum/go-ethereum/common/hexutil"
9+
"github.com/goccy/go-json"
10+
"github.com/samber/lo"
11+
12+
uniswapv4 "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/uniswap/v4"
13+
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
14+
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject"
15+
)
16+
17+
type Hook struct { // scheduled
18+
uniswapv4.Hook
19+
Extra
20+
}
21+
22+
type Extra struct {
23+
FeesFrom [2]uniswapv4.FeeAmount `json:"fs"`
24+
}
25+
26+
type FeesDataRPC struct {
27+
Module common.Address
28+
ZeroForOneFee uint32
29+
OneForZeroFee uint32
30+
}
31+
32+
var _ = uniswapv4.RegisterHooksFactory(func(param *uniswapv4.HookParam) uniswapv4.Hook {
33+
var extra Extra
34+
if param.HookExtra != "" {
35+
_ = json.Unmarshal([]byte(param.HookExtra), &extra)
36+
}
37+
return &Hook{
38+
Hook: &uniswapv4.BaseHook{Exchange: valueobject.ExchangeUniswapV4Arrakis},
39+
Extra: extra,
40+
}
41+
}, HookAddresses...)
42+
43+
func (h *Hook) Track(ctx context.Context, param *uniswapv4.HookParam) (string, error) {
44+
var feesData FeesDataRPC
45+
if _, err := param.RpcClient.NewRequest().SetContext(ctx).AddCall(&ethrpc.Call{
46+
ABI: hookABI,
47+
Target: hexutil.Encode(param.HookAddress[:]),
48+
Method: "getFeesData",
49+
Params: []any{common.HexToHash(param.Pool.Address)},
50+
}, []any{&feesData}).Call(); err != nil {
51+
return "", err
52+
}
53+
54+
extraBytes, _ := json.Marshal(Extra{FeesFrom: [2]uniswapv4.FeeAmount{
55+
uniswapv4.FeeAmount(feesData.ZeroForOneFee),
56+
uniswapv4.FeeAmount(feesData.OneForZeroFee),
57+
}})
58+
return string(extraBytes), nil
59+
}
60+
61+
func (h *Hook) BeforeSwap(params *uniswapv4.BeforeSwapParams) (*uniswapv4.BeforeSwapResult, error) {
62+
return &uniswapv4.BeforeSwapResult{
63+
DeltaSpecified: bignumber.ZeroBI,
64+
DeltaUnspecified: bignumber.ZeroBI,
65+
SwapFee: h.Extra.FeesFrom[lo.Ternary(params.ZeroForOne, 0, 1)],
66+
}, nil
67+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ func (p *PoolSimulator) CalcAmountOut(param pool.CalcAmountOutParams) (swapResul
162162
return nil, ErrInvalidAmountIn
163163
}
164164

165-
if beforeSwapResult.SwapFee >= FeeMax {
165+
if !shared.IsDynamicFee(p.staticExtra.Fee) { // ignore if not dynamic fee
166+
} else if beforeSwapResult.SwapFee >= FeeMax {
166167
return nil, ErrInvalidFee
167168
} else if beforeSwapResult.SwapFee > 0 && beforeSwapResult.SwapFee != p.V3Pool.Fee {
168169
cloned := *poolSim
@@ -293,7 +294,8 @@ func (p *PoolSimulator) CalcAmountIn(param pool.CalcAmountInParams) (swapResult
293294
return nil, ErrInvalidAmountOut
294295
}
295296

296-
if beforeSwapResult.SwapFee >= FeeMax {
297+
if !shared.IsDynamicFee(p.staticExtra.Fee) { // ignore if not dynamic fee
298+
} else if beforeSwapResult.SwapFee >= FeeMax {
297299
return nil, ErrInvalidFee
298300
} else if beforeSwapResult.SwapFee > 0 && beforeSwapResult.SwapFee != p.V3Pool.Fee {
299301
cloned := *poolSim

0 commit comments

Comments
 (0)