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

get dependencies for wombat #738

Merged
merged 4 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions pkg/source/pool/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type IPoolsListUpdater interface {
GetNewPools(ctx context.Context, metadataBytes []byte) ([]entity.Pool, []byte, error)
}

type IPoolsListUpdaterWithDependencies interface {
GetDependencies(ctx context.Context, p entity.Pool) ([]string, bool, error)
}

type GetNewPoolStateParams struct {
Logs []types.Log
}
Expand All @@ -38,6 +42,10 @@ type IPoolTracker interface {
GetNewPoolState(ctx context.Context, p entity.Pool, params GetNewPoolStateParams) (entity.Pool, error)
}

type IPoolTrackerWithDependencies interface {
GetDependencies(ctx context.Context, p entity.Pool) ([]string, bool, error)
}

type IPoolSimulator interface {
// CalcAmountOut amountOut, fee, gas
// the required params is TokenAmountIn and TokenOut.
Expand Down
26 changes: 20 additions & 6 deletions pkg/source/wombat/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
"github.com/goccy/go-json"
"github.com/samber/lo"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
Expand Down Expand Up @@ -192,12 +193,13 @@ func (d *PoolTracker) getNewPoolState(
}

extraByte, err := json.Marshal(Extra{
Paused: paused,
HaircutRate: haircutRate,
AmpFactor: ampFactor,
StartCovRatio: startCovRatio,
EndCovRatio: endcovRatio,
AssetMap: assetMap,
Paused: paused,
HaircutRate: haircutRate,
AmpFactor: ampFactor,
StartCovRatio: startCovRatio,
EndCovRatio: endcovRatio,
AssetMap: assetMap,
DependenciesStored: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a common struct to be embedded? Use short but unique json tag

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why do we need this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for saving dependencies to global for current pool running(default false).
Once it updated to true, it will skip that saving part

})
if err != nil {
logger.WithFields(logger.Fields{
Expand All @@ -220,3 +222,15 @@ func (d *PoolTracker) getNewPoolState(

return p, nil
}

func (d *PoolTracker) GetDependencies(ctx context.Context, p entity.Pool) ([]string, bool, error) {
var extra Extra
err := json.Unmarshal([]byte(p.Extra), &extra)
if err != nil {
return nil, false, err
}

return lo.MapToSlice(extra.AssetMap, func(_ string, asset Asset) string {
return asset.Address
}), extra.DependenciesStored, nil
}
24 changes: 24 additions & 0 deletions pkg/source/wombat/pools_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/KyberNetwork/ethrpc"
"github.com/KyberNetwork/logger"
"github.com/goccy/go-json"
"github.com/samber/lo"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
poollist "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool/list"
Expand Down Expand Up @@ -111,13 +112,24 @@ func (d *PoolsListUpdater) getNewPoolFromSubgraph(ctx context.Context, lastCreat
continue
}

extraByte, err := json.Marshal(Extra{
AssetMap: lo.SliceToMap(p.Assets, func(item AssetSubgraph) (string, Asset) {
return item.ID, Asset{Address: item.ID}
}),
DependenciesStored: true,
})
if err != nil {
return nil, lastCreateTime, err
}

var newPool = entity.Pool{
Address: p.ID,
Exchange: d.config.DexID,
Type: poolType,
Timestamp: time.Now().Unix(),
Reserves: reserves,
Tokens: tokens,
Extra: string(extraByte),
}

pools = append(pools, newPool)
Expand Down Expand Up @@ -221,3 +233,15 @@ func (d *PoolsListUpdater) classifyPoolType(ctx context.Context, p *SubgraphPool

return PoolTypeWombatMain, nil
}

func (d *PoolsListUpdater) GetDependencies(ctx context.Context, p entity.Pool) ([]string, bool, error) {
var extra Extra
err := json.Unmarshal([]byte(p.Extra), &extra)
if err != nil {
return nil, false, err
}

return lo.MapToSlice(extra.AssetMap, func(_ string, asset Asset) string {
return asset.Address
}), extra.DependenciesStored, nil
}
13 changes: 7 additions & 6 deletions pkg/source/wombat/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package wombat
import "math/big"

type Extra struct {
Paused bool `json:"paused"`
HaircutRate *big.Int `json:"haircutRate"`
AmpFactor *big.Int `json:"ampFactor"`
StartCovRatio *big.Int `json:"startCovRatio"`
EndCovRatio *big.Int `json:"endCovRatio"`
AssetMap map[string]Asset `json:"assetMap"`
Paused bool `json:"paused"`
HaircutRate *big.Int `json:"haircutRate"`
AmpFactor *big.Int `json:"ampFactor"`
StartCovRatio *big.Int `json:"startCovRatio"`
EndCovRatio *big.Int `json:"endCovRatio"`
AssetMap map[string]Asset `json:"assetMap"`
DependenciesStored bool `json:"ds"`
}

type Asset struct {
Expand Down