From 434129b39c79cdbd3198b4bf3b0c197041302f42 Mon Sep 17 00:00:00 2001 From: Chris Malec Date: Thu, 27 Jun 2024 09:24:28 -0700 Subject: [PATCH] LM-168-Rename-liquidityrebalancer-package-and-interface (#1093) ## Motivation ## Solution --- .../ocr2/plugins/liquiditymanager/factory.go | 10 ++++---- .../liquiditymanager/mocks/rebalancer_mock.go | 23 ++++++++++--------- .../plugins/liquiditymanager/models/config.go | 6 +++++ .../ocr2/plugins/liquiditymanager/plugin.go | 10 ++++---- .../plugins/liquiditymanager/plugin_test.go | 8 +++---- .../common.go | 2 +- .../minimum_balance.go | 2 +- .../minimum_balance_test.go | 2 +- .../pingpong.go | 2 +- .../pingpong_test.go | 2 +- .../rebalancer.go | 6 ++--- .../target_balance_with_minimum.go | 2 +- .../target_balance_with_minimum_test.go | 2 +- 13 files changed, 42 insertions(+), 35 deletions(-) rename core/services/ocr2/plugins/liquiditymanager/{liquidityrebalancer => rebalalgo}/common.go (99%) rename core/services/ocr2/plugins/liquiditymanager/{liquidityrebalancer => rebalalgo}/minimum_balance.go (99%) rename core/services/ocr2/plugins/liquiditymanager/{liquidityrebalancer => rebalalgo}/minimum_balance_test.go (99%) rename core/services/ocr2/plugins/liquiditymanager/{liquidityrebalancer => rebalalgo}/pingpong.go (99%) rename core/services/ocr2/plugins/liquiditymanager/{liquidityrebalancer => rebalalgo}/pingpong_test.go (99%) rename core/services/ocr2/plugins/liquiditymanager/{liquidityrebalancer => rebalalgo}/rebalancer.go (85%) rename core/services/ocr2/plugins/liquiditymanager/{liquidityrebalancer => rebalalgo}/target_balance_with_minimum.go (99%) rename core/services/ocr2/plugins/liquiditymanager/{liquidityrebalancer => rebalalgo}/target_balance_with_minimum_test.go (99%) diff --git a/core/services/ocr2/plugins/liquiditymanager/factory.go b/core/services/ocr2/plugins/liquiditymanager/factory.go index f44052b431..13e7509c2f 100644 --- a/core/services/ocr2/plugins/liquiditymanager/factory.go +++ b/core/services/ocr2/plugins/liquiditymanager/factory.go @@ -11,8 +11,8 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/bridge" liquiditymanager "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/chain/evm" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/discoverer" - "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/models" + "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/rebalalgo" ) const ( @@ -60,14 +60,14 @@ func NewPluginFactory( }, nil } -func (p PluginFactory) buildRebalancer() (liquidityrebalancer.Rebalancer, error) { +func (p PluginFactory) buildRebalancer() (rebalalgo.RebalancingAlgo, error) { switch p.config.RebalancerConfig.Type { case models.RebalancerTypePingPong: - return liquidityrebalancer.NewPingPong(), nil + return rebalalgo.NewPingPong(), nil case models.RebalancerTypeMinLiquidity: - return liquidityrebalancer.NewMinLiquidityRebalancer(p.lggr), nil + return rebalalgo.NewMinLiquidityRebalancer(p.lggr), nil case models.RebalancerTypeTargetAndMin: - return liquidityrebalancer.NewTargetMinBalancer(p.lggr), nil + return rebalalgo.NewTargetMinBalancer(p.lggr), nil default: return nil, fmt.Errorf("invalid rebalancer type %s", p.config.RebalancerConfig.Type) } diff --git a/core/services/ocr2/plugins/liquiditymanager/mocks/rebalancer_mock.go b/core/services/ocr2/plugins/liquiditymanager/mocks/rebalancer_mock.go index 82a7fc95a1..31a011710a 100644 --- a/core/services/ocr2/plugins/liquiditymanager/mocks/rebalancer_mock.go +++ b/core/services/ocr2/plugins/liquiditymanager/mocks/rebalancer_mock.go @@ -4,19 +4,20 @@ package mocks import ( graph "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/graph" - liquidityrebalancer "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer" mock "github.com/stretchr/testify/mock" models "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/models" + + rebalalgo "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/rebalalgo" ) -// Rebalancer is an autogenerated mock type for the Rebalancer type -type Rebalancer struct { +// RebalancingAlgo is an autogenerated mock type for the RebalancingAlgo type +type RebalancingAlgo struct { mock.Mock } // ComputeTransfersToBalance provides a mock function with given fields: g, unexecuted -func (_m *Rebalancer) ComputeTransfersToBalance(g graph.Graph, unexecuted []liquidityrebalancer.UnexecutedTransfer) ([]models.ProposedTransfer, error) { +func (_m *RebalancingAlgo) ComputeTransfersToBalance(g graph.Graph, unexecuted []rebalalgo.UnexecutedTransfer) ([]models.ProposedTransfer, error) { ret := _m.Called(g, unexecuted) if len(ret) == 0 { @@ -25,10 +26,10 @@ func (_m *Rebalancer) ComputeTransfersToBalance(g graph.Graph, unexecuted []liqu var r0 []models.ProposedTransfer var r1 error - if rf, ok := ret.Get(0).(func(graph.Graph, []liquidityrebalancer.UnexecutedTransfer) ([]models.ProposedTransfer, error)); ok { + if rf, ok := ret.Get(0).(func(graph.Graph, []rebalalgo.UnexecutedTransfer) ([]models.ProposedTransfer, error)); ok { return rf(g, unexecuted) } - if rf, ok := ret.Get(0).(func(graph.Graph, []liquidityrebalancer.UnexecutedTransfer) []models.ProposedTransfer); ok { + if rf, ok := ret.Get(0).(func(graph.Graph, []rebalalgo.UnexecutedTransfer) []models.ProposedTransfer); ok { r0 = rf(g, unexecuted) } else { if ret.Get(0) != nil { @@ -36,7 +37,7 @@ func (_m *Rebalancer) ComputeTransfersToBalance(g graph.Graph, unexecuted []liqu } } - if rf, ok := ret.Get(1).(func(graph.Graph, []liquidityrebalancer.UnexecutedTransfer) error); ok { + if rf, ok := ret.Get(1).(func(graph.Graph, []rebalalgo.UnexecutedTransfer) error); ok { r1 = rf(g, unexecuted) } else { r1 = ret.Error(1) @@ -45,13 +46,13 @@ func (_m *Rebalancer) ComputeTransfersToBalance(g graph.Graph, unexecuted []liqu return r0, r1 } -// NewRebalancer creates a new instance of Rebalancer. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// NewRebalancingAlgo creates a new instance of RebalancingAlgo. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. // The first argument is typically a *testing.T value. -func NewRebalancer(t interface { +func NewRebalancingAlgo(t interface { mock.TestingT Cleanup(func()) -}) *Rebalancer { - mock := &Rebalancer{} +}) *RebalancingAlgo { + mock := &RebalancingAlgo{} mock.Mock.Test(t) t.Cleanup(func() { mock.AssertExpectations(t) }) diff --git a/core/services/ocr2/plugins/liquiditymanager/models/config.go b/core/services/ocr2/plugins/liquiditymanager/models/config.go index d7ab10662f..cf3249e4d1 100644 --- a/core/services/ocr2/plugins/liquiditymanager/models/config.go +++ b/core/services/ocr2/plugins/liquiditymanager/models/config.go @@ -3,6 +3,7 @@ package models import ( "errors" "fmt" + "math/big" "slices" ) @@ -19,6 +20,11 @@ type RebalancerConfig struct { Type string `json:"type"` } +type NetworkTarget struct { + Network NetworkSelector `json:"network,string"` + Target *big.Int `json:"target"` +} + func ValidateRebalancerConfig(config RebalancerConfig) error { if config.Type == "" { return errors.New("rebalancerType must be provided") diff --git a/core/services/ocr2/plugins/liquiditymanager/plugin.go b/core/services/ocr2/plugins/liquiditymanager/plugin.go index 2ebb0afeb3..e5b0d37ec3 100644 --- a/core/services/ocr2/plugins/liquiditymanager/plugin.go +++ b/core/services/ocr2/plugins/liquiditymanager/plugin.go @@ -20,8 +20,8 @@ import ( "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/discoverer" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/graph" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/inflight" - "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/models" + "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/rebalalgo" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/rebalcalc" ) @@ -33,7 +33,7 @@ type Plugin struct { bridgeFactory bridge.Factory mu sync.RWMutex liquidityGraph graph.Graph - liquidityRebalancer liquidityrebalancer.Rebalancer + liquidityRebalancer rebalalgo.RebalancingAlgo inflight inflight.Container lggr logger.Logger reportCodec evmliquiditymanager.OnchainReportCodec @@ -47,7 +47,7 @@ func NewPlugin( liquidityManagerFactory evmliquiditymanager.Factory, discoverer discoverer.Discoverer, bridgeFactory bridge.Factory, - liquidityRebalancer liquidityrebalancer.Rebalancer, + liquidityRebalancer rebalalgo.RebalancingAlgo, reportCodec evmliquiditymanager.OnchainReportCodec, lggr logger.Logger, ) *Plugin { @@ -217,8 +217,8 @@ func combinedUnexecutedTransfers( pendingTransfers []models.PendingTransfer, resolvedTransfersQuorum []models.Transfer, inflightTransfers []models.Transfer, -) []liquidityrebalancer.UnexecutedTransfer { - unexecuted := make([]liquidityrebalancer.UnexecutedTransfer, 0, len(resolvedTransfersQuorum)+len(inflightTransfers)+len(pendingTransfers)) +) []rebalalgo.UnexecutedTransfer { + unexecuted := make([]rebalalgo.UnexecutedTransfer, 0, len(resolvedTransfersQuorum)+len(inflightTransfers)+len(pendingTransfers)) for _, resolvedTransfer := range resolvedTransfersQuorum { unexecuted = append(unexecuted, resolvedTransfer) } diff --git a/core/services/ocr2/plugins/liquiditymanager/plugin_test.go b/core/services/ocr2/plugins/liquiditymanager/plugin_test.go index cb377fdfa6..cb3af9d08f 100644 --- a/core/services/ocr2/plugins/liquiditymanager/plugin_test.go +++ b/core/services/ocr2/plugins/liquiditymanager/plugin_test.go @@ -30,9 +30,9 @@ import ( liquiditymanagermocks "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/chain/evm/mocks" discoverermocks "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/discoverer/mocks" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/graph" - "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/mocks" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/models" + "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/rebalalgo" "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/liquiditymanager/testhelpers" ) @@ -1563,7 +1563,7 @@ func newNode(t *testing.T, lggr logger.Logger, f int) node { // discovererMock.On("Discover", mock.Anything).Return(g, nil).Maybe() discovererFactory.On("NewDiscoverer", mock.Anything, mock.Anything).Return(discovererMock, nil).Maybe() bridgeFactory := bridgemocks.NewFactory(t) - rebalancerAlg := liquidityrebalancer.NewPingPong() + rebalancerAlg := rebalalgo.NewPingPong() node1 := NewPlugin( f, @@ -1603,7 +1603,7 @@ type pluginWithMocks struct { lmFactory *mocks.Factory discovererFactory *discoverermocks.Factory bridgeFactory *bridgemocks.Factory - rebalancerAlg *liquidityrebalancer.PingPong + rebalancerAlg *rebalalgo.PingPong } func newPluginWithMocksAndDefaults(t *testing.T) *pluginWithMocks { @@ -1625,7 +1625,7 @@ func newPluginWithMocks( discovererMock := discoverermocks.NewDiscoverer(t) discovererFactory.On("NewDiscoverer", mock.Anything, mock.Anything).Return(discovererMock, nil).Maybe() bridgeFactory := bridgemocks.NewFactory(t) - rebalancerAlg := liquidityrebalancer.NewPingPong() + rebalancerAlg := rebalalgo.NewPingPong() return &pluginWithMocks{ plugin: NewPlugin( f, diff --git a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/common.go b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/common.go similarity index 99% rename from core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/common.go rename to core/services/ocr2/plugins/liquiditymanager/rebalalgo/common.go index 3165d59384..34add8a14f 100644 --- a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/common.go +++ b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/common.go @@ -1,4 +1,4 @@ -package liquidityrebalancer +package rebalalgo import ( "fmt" diff --git a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/minimum_balance.go b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/minimum_balance.go similarity index 99% rename from core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/minimum_balance.go rename to core/services/ocr2/plugins/liquiditymanager/rebalalgo/minimum_balance.go index 143b073e4f..60de4141a3 100644 --- a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/minimum_balance.go +++ b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/minimum_balance.go @@ -1,4 +1,4 @@ -package liquidityrebalancer +package rebalalgo import ( "fmt" diff --git a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/minimum_balance_test.go b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/minimum_balance_test.go similarity index 99% rename from core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/minimum_balance_test.go rename to core/services/ocr2/plugins/liquiditymanager/rebalalgo/minimum_balance_test.go index 491b6e7370..05c9716f88 100644 --- a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/minimum_balance_test.go +++ b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/minimum_balance_test.go @@ -1,4 +1,4 @@ -package liquidityrebalancer +package rebalalgo import ( "math/big" diff --git a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/pingpong.go b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/pingpong.go similarity index 99% rename from core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/pingpong.go rename to core/services/ocr2/plugins/liquiditymanager/rebalalgo/pingpong.go index 0dec82241a..97084b1ec9 100644 --- a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/pingpong.go +++ b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/pingpong.go @@ -1,4 +1,4 @@ -package liquidityrebalancer +package rebalalgo import ( "fmt" diff --git a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/pingpong_test.go b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/pingpong_test.go similarity index 99% rename from core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/pingpong_test.go rename to core/services/ocr2/plugins/liquiditymanager/rebalalgo/pingpong_test.go index edbaf7b038..7a9c36094e 100644 --- a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/pingpong_test.go +++ b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/pingpong_test.go @@ -1,4 +1,4 @@ -package liquidityrebalancer +package rebalalgo import ( "math/big" diff --git a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/rebalancer.go b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/rebalancer.go similarity index 85% rename from core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/rebalancer.go rename to core/services/ocr2/plugins/liquiditymanager/rebalalgo/rebalancer.go index 64f4112fe5..4b479a423f 100644 --- a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/rebalancer.go +++ b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/rebalancer.go @@ -1,4 +1,4 @@ -package liquidityrebalancer +package rebalalgo import ( "math/big" @@ -17,8 +17,8 @@ type UnexecutedTransfer interface { TransferStatus() models.TransferStatus } -//go:generate mockery --quiet --name Rebalancer --output ../mocks --filename rebalancer_mock.go --case=underscore -type Rebalancer interface { +//go:generate mockery --quiet --name RebalancingAlgo --output ../mocks --filename rebalancer_mock.go --case=underscore +type RebalancingAlgo interface { // ComputeTransfersToBalance computes the transfers needed to balance the // liquidity across the provided graph. The rebalancer will also take into account // currently unexecuted transfers to avoid proposing transfers that would be diff --git a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/target_balance_with_minimum.go b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/target_balance_with_minimum.go similarity index 99% rename from core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/target_balance_with_minimum.go rename to core/services/ocr2/plugins/liquiditymanager/rebalalgo/target_balance_with_minimum.go index 326de9fcc2..c479b0f657 100644 --- a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/target_balance_with_minimum.go +++ b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/target_balance_with_minimum.go @@ -1,4 +1,4 @@ -package liquidityrebalancer +package rebalalgo import ( "fmt" diff --git a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/target_balance_with_minimum_test.go b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/target_balance_with_minimum_test.go similarity index 99% rename from core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/target_balance_with_minimum_test.go rename to core/services/ocr2/plugins/liquiditymanager/rebalalgo/target_balance_with_minimum_test.go index 35e90b94ac..150fad455b 100644 --- a/core/services/ocr2/plugins/liquiditymanager/liquidityrebalancer/target_balance_with_minimum_test.go +++ b/core/services/ocr2/plugins/liquiditymanager/rebalalgo/target_balance_with_minimum_test.go @@ -1,4 +1,4 @@ -package liquidityrebalancer +package rebalalgo import ( "math/big"