Skip to content

Commit

Permalink
fluid: add center price for verify reserves (#691)
Browse files Browse the repository at this point in the history
* feat(fluid): add center price for verify reserves

* refactor: increase code reuse; use simpler methods; better init

* feat(fluid-dex): update dex reserves resolver in tests

---------

Co-authored-by: Phu Ngo <[email protected]>
  • Loading branch information
bergben and NgoKimPhu authored Jan 12, 2025
1 parent 16c8216 commit 726ac44
Show file tree
Hide file tree
Showing 9 changed files with 560 additions and 406 deletions.
30 changes: 30 additions & 0 deletions pkg/liquidity-source/fluid/dex-t1/abis/dexReservesResolver.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
{ "internalType": "address", "name": "token0", "type": "address" },
{ "internalType": "address", "name": "token1", "type": "address" },
{ "internalType": "uint256", "name": "fee", "type": "uint256" },
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down Expand Up @@ -295,6 +300,11 @@
{ "internalType": "address", "name": "token0", "type": "address" },
{ "internalType": "address", "name": "token1", "type": "address" },
{ "internalType": "uint256", "name": "fee", "type": "uint256" },
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down Expand Up @@ -976,6 +986,11 @@
{ "internalType": "address", "name": "token0", "type": "address" },
{ "internalType": "address", "name": "token1", "type": "address" },
{ "internalType": "uint256", "name": "fee", "type": "uint256" },
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down Expand Up @@ -1156,6 +1171,11 @@
{ "internalType": "address", "name": "token0", "type": "address" },
{ "internalType": "address", "name": "token1", "type": "address" },
{ "internalType": "uint256", "name": "fee", "type": "uint256" },
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down Expand Up @@ -1348,6 +1368,11 @@
{ "internalType": "address", "name": "token0", "type": "address" },
{ "internalType": "address", "name": "token1", "type": "address" },
{ "internalType": "uint256", "name": "fee", "type": "uint256" },
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down Expand Up @@ -1528,6 +1553,11 @@
{ "internalType": "address", "name": "token0", "type": "address" },
{ "internalType": "address", "name": "token1", "type": "address" },
{ "internalType": "uint256", "name": "fee", "type": "uint256" },
{
"internalType": "uint256",
"name": "centerPrice",
"type": "uint256"
},
{
"components": [
{
Expand Down
34 changes: 17 additions & 17 deletions pkg/liquidity-source/fluid/dex-t1/constant.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
package dexT1

import "math/big"
import (
"math/big"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
)

const (
DexType = "fluid-dex-t1"
)

const (
// DexReservesResolver methods
const ( // DexReservesResolver methods
DRRMethodGetAllPoolsReservesAdjusted = "getAllPoolsReservesAdjusted"
DRRMethodGetPoolReservesAdjusted = "getPoolReservesAdjusted"

// ERC20 Token methods
// TokenMethodDecimals - ERC20 Token methods
TokenMethodDecimals = "decimals"

// StorageRead methods
// SRMethodReadFromStorage - StorageRead methods
SRMethodReadFromStorage = "readFromStorage"
)

const (
String1e18 = "1000000000000000000"
String1e27 = "1000000000000000000000000000"

DexAmountsDecimals = 12

FeePercentPrecision int64 = 1e4
Fee100PercentPrecision int64 = 1e6
FeePercentPrecision float64 = 1e4
)

MaxPriceDiff int64 = 5 // 5%
var (
MaxPriceDiff = big.NewInt(5) // 5%
MinSwapLiquidity = big.NewInt(2e4) // on-chain we use 1e4 but use extra buffer to avoid reverts
Fee100PercentPrecision = big.NewInt(1e6)

MinSwapLiquidity int64 = 6667 // on-chain we use 1e4 but use extra buffer for potential price diff using pool price vs center price at the check
bI100 = big.NewInt(100)
bI1e18 = bignumber.TenPowInt(18)
bI1e27 = bignumber.TenPowInt(27)
)

var bI1e18, _ = new(big.Int).SetString(String1e18, 10) // 1e18
var bI1e27, _ = new(big.Int).SetString(String1e27, 10) // 1e27
var bI10 = new(big.Int).SetInt64(10)
var bI100 = new(big.Int).SetInt64(100)
3 changes: 2 additions & 1 deletion pkg/liquidity-source/fluid/dex-t1/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
CollateralReserves: curPool.CollateralReserves,
DebtReserves: curPool.DebtReserves,
DexLimits: curPool.Limits,
CenterPrice: curPool.CenterPrice,
}

extraBytes, err := json.Marshal(extra)
Expand Down Expand Up @@ -124,7 +125,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Decimals: token1Decimals,
},
},
SwapFee: float64(curPool.Fee.Int64()) / float64(FeePercentPrecision),
SwapFee: float64(curPool.Fee.Int64()) / FeePercentPrecision,
Extra: string(extraBytes),
StaticExtra: string(staticExtraBytes),
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/liquidity-source/fluid/dex-t1/pool_list_updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dexT1

import (
"context"
"math/big"
"os"
"strings"
"testing"
Expand All @@ -29,7 +28,7 @@ func TestPoolListUpdater(t *testing.T) {
err error

config = Config{
DexReservesResolver: "0x45f4ad57e300da55c33dea579a40fcee000d7b94",
DexReservesResolver: "0xb387f9C2092cF7c4943F97842887eBff7AE96EB3",
}
)

Expand Down Expand Up @@ -83,16 +82,17 @@ func TestPoolListUpdater(t *testing.T) {
require.NotEqual(t, "0", pools[0].Reserves[0], "Reserve should not be zero")
require.NotEqual(t, "0", pools[0].Reserves[1], "Reserve should not be zero")

require.True(t, extra.CollateralReserves.Token0RealReserves.Cmp(big.NewInt(0)) > 0)
require.True(t, extra.CollateralReserves.Token1RealReserves.Cmp(big.NewInt(0)) > 0)
require.True(t, extra.CollateralReserves.Token0ImaginaryReserves.Cmp(big.NewInt(0)) > 0)
require.True(t, extra.CollateralReserves.Token1ImaginaryReserves.Cmp(big.NewInt(0)) > 0)
require.True(t, extra.DebtReserves.Token0Debt.Cmp(big.NewInt(0)) > 0)
require.True(t, extra.DebtReserves.Token1Debt.Cmp(big.NewInt(0)) > 0)
require.True(t, extra.DebtReserves.Token0RealReserves.Cmp(big.NewInt(0)) > 0)
require.True(t, extra.DebtReserves.Token1RealReserves.Cmp(big.NewInt(0)) > 0)
require.True(t, extra.DebtReserves.Token0ImaginaryReserves.Cmp(big.NewInt(0)) > 0)
require.True(t, extra.DebtReserves.Token1ImaginaryReserves.Cmp(big.NewInt(0)) > 0)
require.True(t, extra.CollateralReserves.Token0RealReserves.Sign() > 0)
require.True(t, extra.CollateralReserves.Token1RealReserves.Sign() > 0)
require.True(t, extra.CollateralReserves.Token0ImaginaryReserves.Sign() > 0)
require.True(t, extra.CollateralReserves.Token1ImaginaryReserves.Sign() > 0)
require.True(t, extra.DebtReserves.Token0Debt.Sign() > 0)
require.True(t, extra.DebtReserves.Token1Debt.Sign() > 0)
require.True(t, extra.DebtReserves.Token0RealReserves.Sign() > 0)
require.True(t, extra.DebtReserves.Token1RealReserves.Sign() > 0)
require.True(t, extra.DebtReserves.Token0ImaginaryReserves.Sign() > 0)
require.True(t, extra.DebtReserves.Token1ImaginaryReserves.Sign() > 0)
require.True(t, extra.CenterPrice.Sign() > 0)

// Log all pools
// for i, pool := range pools {
Expand Down
Loading

0 comments on commit 726ac44

Please sign in to comment.