@humanode-hyperlane/rebalancer 0.0.0-stage → 27.2.2-humanode.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +193 -0
- package/README.md +297 -2
- package/dist/bridges/LiFiBridge.d.ts +93 -0
- package/dist/bridges/LiFiBridge.d.ts.map +1 -0
- package/dist/bridges/LiFiBridge.js +527 -0
- package/dist/bridges/LiFiBridge.js.map +1 -0
- package/dist/bridges/LiFiBridge.test.d.ts +2 -0
- package/dist/bridges/LiFiBridge.test.d.ts.map +1 -0
- package/dist/bridges/LiFiBridge.test.js +504 -0
- package/dist/bridges/LiFiBridge.test.js.map +1 -0
- package/dist/config/RebalancerConfig.d.ts +20 -0
- package/dist/config/RebalancerConfig.d.ts.map +1 -0
- package/dist/config/RebalancerConfig.js +35 -0
- package/dist/config/RebalancerConfig.js.map +1 -0
- package/dist/config/RebalancerConfig.test.d.ts +2 -0
- package/dist/config/RebalancerConfig.test.d.ts.map +1 -0
- package/dist/config/RebalancerConfig.test.js +798 -0
- package/dist/config/RebalancerConfig.test.js.map +1 -0
- package/dist/config/types.d.ts +3008 -0
- package/dist/config/types.d.ts.map +1 -0
- package/dist/config/types.js +451 -0
- package/dist/config/types.js.map +1 -0
- package/dist/core/InventoryRebalancer.d.ts +201 -0
- package/dist/core/InventoryRebalancer.d.ts.map +1 -0
- package/dist/core/InventoryRebalancer.js +994 -0
- package/dist/core/InventoryRebalancer.js.map +1 -0
- package/dist/core/InventoryRebalancer.test.d.ts +2 -0
- package/dist/core/InventoryRebalancer.test.d.ts.map +1 -0
- package/dist/core/InventoryRebalancer.test.js +1511 -0
- package/dist/core/InventoryRebalancer.test.js.map +1 -0
- package/dist/core/Rebalancer.d.ts +37 -0
- package/dist/core/Rebalancer.d.ts.map +1 -0
- package/dist/core/Rebalancer.js +433 -0
- package/dist/core/Rebalancer.js.map +1 -0
- package/dist/core/Rebalancer.test.d.ts +2 -0
- package/dist/core/Rebalancer.test.d.ts.map +1 -0
- package/dist/core/Rebalancer.test.js +424 -0
- package/dist/core/Rebalancer.test.js.map +1 -0
- package/dist/core/RebalancerOrchestrator.d.ts +56 -0
- package/dist/core/RebalancerOrchestrator.d.ts.map +1 -0
- package/dist/core/RebalancerOrchestrator.js +154 -0
- package/dist/core/RebalancerOrchestrator.js.map +1 -0
- package/dist/core/RebalancerOrchestrator.test.d.ts +2 -0
- package/dist/core/RebalancerOrchestrator.test.d.ts.map +1 -0
- package/dist/core/RebalancerOrchestrator.test.js +721 -0
- package/dist/core/RebalancerOrchestrator.test.js.map +1 -0
- package/dist/core/RebalancerService.d.ts +127 -0
- package/dist/core/RebalancerService.d.ts.map +1 -0
- package/dist/core/RebalancerService.js +270 -0
- package/dist/core/RebalancerService.js.map +1 -0
- package/dist/core/RebalancerService.test.d.ts +2 -0
- package/dist/core/RebalancerService.test.d.ts.map +1 -0
- package/dist/core/RebalancerService.test.js +777 -0
- package/dist/core/RebalancerService.test.js.map +1 -0
- package/dist/e2e/collateral-deficit.e2e-test.d.ts +2 -0
- package/dist/e2e/collateral-deficit.e2e-test.d.ts.map +1 -0
- package/dist/e2e/collateral-deficit.e2e-test.js +227 -0
- package/dist/e2e/collateral-deficit.e2e-test.js.map +1 -0
- package/dist/e2e/composite.e2e-test.d.ts +2 -0
- package/dist/e2e/composite.e2e-test.d.ts.map +1 -0
- package/dist/e2e/composite.e2e-test.js +666 -0
- package/dist/e2e/composite.e2e-test.js.map +1 -0
- package/dist/e2e/fixtures/routes.d.ts +89 -0
- package/dist/e2e/fixtures/routes.d.ts.map +1 -0
- package/dist/e2e/fixtures/routes.js +420 -0
- package/dist/e2e/fixtures/routes.js.map +1 -0
- package/dist/e2e/harness/BaseLocalDeploymentManager.d.ts +38 -0
- package/dist/e2e/harness/BaseLocalDeploymentManager.d.ts.map +1 -0
- package/dist/e2e/harness/BaseLocalDeploymentManager.js +144 -0
- package/dist/e2e/harness/BaseLocalDeploymentManager.js.map +1 -0
- package/dist/e2e/harness/BridgeSetup.d.ts +11 -0
- package/dist/e2e/harness/BridgeSetup.d.ts.map +1 -0
- package/dist/e2e/harness/BridgeSetup.js +50 -0
- package/dist/e2e/harness/BridgeSetup.js.map +1 -0
- package/dist/e2e/harness/Erc20InventoryLocalDeploymentManager.d.ts +13 -0
- package/dist/e2e/harness/Erc20InventoryLocalDeploymentManager.d.ts.map +1 -0
- package/dist/e2e/harness/Erc20InventoryLocalDeploymentManager.js +105 -0
- package/dist/e2e/harness/Erc20InventoryLocalDeploymentManager.js.map +1 -0
- package/dist/e2e/harness/Erc20LocalDeploymentManager.d.ts +11 -0
- package/dist/e2e/harness/Erc20LocalDeploymentManager.d.ts.map +1 -0
- package/dist/e2e/harness/Erc20LocalDeploymentManager.js +105 -0
- package/dist/e2e/harness/Erc20LocalDeploymentManager.js.map +1 -0
- package/dist/e2e/harness/ForkIndexer.d.ts +23 -0
- package/dist/e2e/harness/ForkIndexer.d.ts.map +1 -0
- package/dist/e2e/harness/ForkIndexer.js +127 -0
- package/dist/e2e/harness/ForkIndexer.js.map +1 -0
- package/dist/e2e/harness/ForkIndexer.test.d.ts +2 -0
- package/dist/e2e/harness/ForkIndexer.test.d.ts.map +1 -0
- package/dist/e2e/harness/ForkIndexer.test.js +257 -0
- package/dist/e2e/harness/ForkIndexer.test.js.map +1 -0
- package/dist/e2e/harness/MixedLocalDeploymentManager.d.ts +21 -0
- package/dist/e2e/harness/MixedLocalDeploymentManager.d.ts.map +1 -0
- package/dist/e2e/harness/MixedLocalDeploymentManager.js +115 -0
- package/dist/e2e/harness/MixedLocalDeploymentManager.js.map +1 -0
- package/dist/e2e/harness/MockExplorerClient.d.ts +23 -0
- package/dist/e2e/harness/MockExplorerClient.d.ts.map +1 -0
- package/dist/e2e/harness/MockExplorerClient.js +57 -0
- package/dist/e2e/harness/MockExplorerClient.js.map +1 -0
- package/dist/e2e/harness/MockExternalBridge.d.ts +32 -0
- package/dist/e2e/harness/MockExternalBridge.d.ts.map +1 -0
- package/dist/e2e/harness/MockExternalBridge.js +247 -0
- package/dist/e2e/harness/MockExternalBridge.js.map +1 -0
- package/dist/e2e/harness/NativeLocalDeploymentManager.d.ts +13 -0
- package/dist/e2e/harness/NativeLocalDeploymentManager.d.ts.map +1 -0
- package/dist/e2e/harness/NativeLocalDeploymentManager.js +91 -0
- package/dist/e2e/harness/NativeLocalDeploymentManager.js.map +1 -0
- package/dist/e2e/harness/SnapshotHelper.d.ts +13 -0
- package/dist/e2e/harness/SnapshotHelper.d.ts.map +1 -0
- package/dist/e2e/harness/SnapshotHelper.js +36 -0
- package/dist/e2e/harness/SnapshotHelper.js.map +1 -0
- package/dist/e2e/harness/TestHelpers.d.ts +25 -0
- package/dist/e2e/harness/TestHelpers.d.ts.map +1 -0
- package/dist/e2e/harness/TestHelpers.js +103 -0
- package/dist/e2e/harness/TestHelpers.js.map +1 -0
- package/dist/e2e/harness/TestRebalancer.d.ts +85 -0
- package/dist/e2e/harness/TestRebalancer.d.ts.map +1 -0
- package/dist/e2e/harness/TestRebalancer.js +427 -0
- package/dist/e2e/harness/TestRebalancer.js.map +1 -0
- package/dist/e2e/harness/TransferHelper.d.ts +34 -0
- package/dist/e2e/harness/TransferHelper.d.ts.map +1 -0
- package/dist/e2e/harness/TransferHelper.js +93 -0
- package/dist/e2e/harness/TransferHelper.js.map +1 -0
- package/dist/e2e/inventory-erc20-minAmount.e2e-test.d.ts +2 -0
- package/dist/e2e/inventory-erc20-minAmount.e2e-test.d.ts.map +1 -0
- package/dist/e2e/inventory-erc20-minAmount.e2e-test.js +411 -0
- package/dist/e2e/inventory-erc20-minAmount.e2e-test.js.map +1 -0
- package/dist/e2e/inventory-erc20-weighted.e2e-test.d.ts +2 -0
- package/dist/e2e/inventory-erc20-weighted.e2e-test.d.ts.map +1 -0
- package/dist/e2e/inventory-erc20-weighted.e2e-test.js +408 -0
- package/dist/e2e/inventory-erc20-weighted.e2e-test.js.map +1 -0
- package/dist/e2e/inventory-minAmount.e2e-test.d.ts +2 -0
- package/dist/e2e/inventory-minAmount.e2e-test.d.ts.map +1 -0
- package/dist/e2e/inventory-minAmount.e2e-test.js +411 -0
- package/dist/e2e/inventory-minAmount.e2e-test.js.map +1 -0
- package/dist/e2e/inventory-weighted.e2e-test.d.ts +2 -0
- package/dist/e2e/inventory-weighted.e2e-test.d.ts.map +1 -0
- package/dist/e2e/inventory-weighted.e2e-test.js +409 -0
- package/dist/e2e/inventory-weighted.e2e-test.js.map +1 -0
- package/dist/e2e/minAmount.e2e-test.d.ts +2 -0
- package/dist/e2e/minAmount.e2e-test.d.ts.map +1 -0
- package/dist/e2e/minAmount.e2e-test.js +203 -0
- package/dist/e2e/minAmount.e2e-test.js.map +1 -0
- package/dist/e2e/mixed-weighted.e2e-test.d.ts +2 -0
- package/dist/e2e/mixed-weighted.e2e-test.d.ts.map +1 -0
- package/dist/e2e/mixed-weighted.e2e-test.js +189 -0
- package/dist/e2e/mixed-weighted.e2e-test.js.map +1 -0
- package/dist/e2e/weighted.e2e-test.d.ts +2 -0
- package/dist/e2e/weighted.e2e-test.d.ts.map +1 -0
- package/dist/e2e/weighted.e2e-test.js +198 -0
- package/dist/e2e/weighted.e2e-test.js.map +1 -0
- package/dist/factories/RebalancerContextFactory.d.ts +89 -0
- package/dist/factories/RebalancerContextFactory.d.ts.map +1 -0
- package/dist/factories/RebalancerContextFactory.js +420 -0
- package/dist/factories/RebalancerContextFactory.js.map +1 -0
- package/dist/factories/RebalancerContextFactory.test.d.ts +2 -0
- package/dist/factories/RebalancerContextFactory.test.d.ts.map +1 -0
- package/dist/factories/RebalancerContextFactory.test.js +270 -0
- package/dist/factories/RebalancerContextFactory.test.js.map +1 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/IExternalBridge.d.ts +103 -0
- package/dist/interfaces/IExternalBridge.d.ts.map +1 -0
- package/dist/interfaces/IExternalBridge.js +2 -0
- package/dist/interfaces/IExternalBridge.js.map +1 -0
- package/dist/interfaces/IMetrics.d.ts +5 -0
- package/dist/interfaces/IMetrics.d.ts.map +1 -0
- package/dist/interfaces/IMetrics.js +2 -0
- package/dist/interfaces/IMetrics.js.map +1 -0
- package/dist/interfaces/IMonitor.d.ts +50 -0
- package/dist/interfaces/IMonitor.d.ts.map +1 -0
- package/dist/interfaces/IMonitor.js +14 -0
- package/dist/interfaces/IMonitor.js.map +1 -0
- package/dist/interfaces/IRebalancer.d.ts +31 -0
- package/dist/interfaces/IRebalancer.d.ts.map +1 -0
- package/dist/interfaces/IRebalancer.js +2 -0
- package/dist/interfaces/IRebalancer.js.map +1 -0
- package/dist/interfaces/IStrategy.d.ts +60 -0
- package/dist/interfaces/IStrategy.d.ts.map +1 -0
- package/dist/interfaces/IStrategy.js +13 -0
- package/dist/interfaces/IStrategy.js.map +1 -0
- package/dist/metrics/Metrics.d.ts +25 -0
- package/dist/metrics/Metrics.d.ts.map +1 -0
- package/dist/metrics/Metrics.js +164 -0
- package/dist/metrics/Metrics.js.map +1 -0
- package/dist/metrics/PriceGetter.d.ts +10 -0
- package/dist/metrics/PriceGetter.d.ts.map +1 -0
- package/dist/metrics/PriceGetter.js +41 -0
- package/dist/metrics/PriceGetter.js.map +1 -0
- package/dist/metrics/scripts/metrics.d.ts +28 -0
- package/dist/metrics/scripts/metrics.d.ts.map +1 -0
- package/dist/metrics/scripts/metrics.js +61 -0
- package/dist/metrics/scripts/metrics.js.map +1 -0
- package/dist/monitor/Monitor.d.ts +40 -0
- package/dist/monitor/Monitor.d.ts.map +1 -0
- package/dist/monitor/Monitor.js +211 -0
- package/dist/monitor/Monitor.js.map +1 -0
- package/dist/service.d.ts +3 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +205 -0
- package/dist/service.js.map +1 -0
- package/dist/strategy/BaseStrategy.d.ts +86 -0
- package/dist/strategy/BaseStrategy.d.ts.map +1 -0
- package/dist/strategy/BaseStrategy.js +342 -0
- package/dist/strategy/BaseStrategy.js.map +1 -0
- package/dist/strategy/CollateralDeficitStrategy.d.ts +65 -0
- package/dist/strategy/CollateralDeficitStrategy.d.ts.map +1 -0
- package/dist/strategy/CollateralDeficitStrategy.js +253 -0
- package/dist/strategy/CollateralDeficitStrategy.js.map +1 -0
- package/dist/strategy/CollateralDeficitStrategy.test.d.ts +2 -0
- package/dist/strategy/CollateralDeficitStrategy.test.d.ts.map +1 -0
- package/dist/strategy/CollateralDeficitStrategy.test.js +497 -0
- package/dist/strategy/CollateralDeficitStrategy.test.js.map +1 -0
- package/dist/strategy/CompositeStrategy.d.ts +18 -0
- package/dist/strategy/CompositeStrategy.d.ts.map +1 -0
- package/dist/strategy/CompositeStrategy.js +63 -0
- package/dist/strategy/CompositeStrategy.js.map +1 -0
- package/dist/strategy/CompositeStrategy.test.d.ts +2 -0
- package/dist/strategy/CompositeStrategy.test.d.ts.map +1 -0
- package/dist/strategy/CompositeStrategy.test.js +278 -0
- package/dist/strategy/CompositeStrategy.test.js.map +1 -0
- package/dist/strategy/MinAmountStrategy.d.ts +35 -0
- package/dist/strategy/MinAmountStrategy.d.ts.map +1 -0
- package/dist/strategy/MinAmountStrategy.js +112 -0
- package/dist/strategy/MinAmountStrategy.js.map +1 -0
- package/dist/strategy/MinAmountStrategy.test.d.ts +2 -0
- package/dist/strategy/MinAmountStrategy.test.d.ts.map +1 -0
- package/dist/strategy/MinAmountStrategy.test.js +547 -0
- package/dist/strategy/MinAmountStrategy.test.js.map +1 -0
- package/dist/strategy/StrategyFactory.d.ts +27 -0
- package/dist/strategy/StrategyFactory.d.ts.map +1 -0
- package/dist/strategy/StrategyFactory.js +79 -0
- package/dist/strategy/StrategyFactory.js.map +1 -0
- package/dist/strategy/StrategyFactory.test.d.ts +2 -0
- package/dist/strategy/StrategyFactory.test.d.ts.map +1 -0
- package/dist/strategy/StrategyFactory.test.js +81 -0
- package/dist/strategy/StrategyFactory.test.js.map +1 -0
- package/dist/strategy/WeightedStrategy.d.ts +32 -0
- package/dist/strategy/WeightedStrategy.d.ts.map +1 -0
- package/dist/strategy/WeightedStrategy.js +73 -0
- package/dist/strategy/WeightedStrategy.js.map +1 -0
- package/dist/strategy/WeightedStrategy.test.d.ts +2 -0
- package/dist/strategy/WeightedStrategy.test.d.ts.map +1 -0
- package/dist/strategy/WeightedStrategy.test.js +403 -0
- package/dist/strategy/WeightedStrategy.test.js.map +1 -0
- package/dist/strategy/index.d.ts +7 -0
- package/dist/strategy/index.d.ts.map +1 -0
- package/dist/strategy/index.js +7 -0
- package/dist/strategy/index.js.map +1 -0
- package/dist/test/helpers.d.ts +99 -0
- package/dist/test/helpers.d.ts.map +1 -0
- package/dist/test/helpers.js +311 -0
- package/dist/test/helpers.js.map +1 -0
- package/dist/test/lifiMocks.d.ts +52 -0
- package/dist/test/lifiMocks.d.ts.map +1 -0
- package/dist/test/lifiMocks.js +160 -0
- package/dist/test/lifiMocks.js.map +1 -0
- package/dist/tracking/ActionTracker.d.ts +90 -0
- package/dist/tracking/ActionTracker.d.ts.map +1 -0
- package/dist/tracking/ActionTracker.js +678 -0
- package/dist/tracking/ActionTracker.js.map +1 -0
- package/dist/tracking/ActionTracker.test.d.ts +2 -0
- package/dist/tracking/ActionTracker.test.d.ts.map +1 -0
- package/dist/tracking/ActionTracker.test.js +1351 -0
- package/dist/tracking/ActionTracker.test.js.map +1 -0
- package/dist/tracking/IActionTracker.d.ts +157 -0
- package/dist/tracking/IActionTracker.d.ts.map +1 -0
- package/dist/tracking/IActionTracker.js +2 -0
- package/dist/tracking/IActionTracker.js.map +1 -0
- package/dist/tracking/InflightContextAdapter.d.ts +18 -0
- package/dist/tracking/InflightContextAdapter.d.ts.map +1 -0
- package/dist/tracking/InflightContextAdapter.js +52 -0
- package/dist/tracking/InflightContextAdapter.js.map +1 -0
- package/dist/tracking/InflightContextAdapter.test.d.ts +2 -0
- package/dist/tracking/InflightContextAdapter.test.d.ts.map +1 -0
- package/dist/tracking/InflightContextAdapter.test.js +175 -0
- package/dist/tracking/InflightContextAdapter.test.js.map +1 -0
- package/dist/tracking/store/IStore.d.ts +41 -0
- package/dist/tracking/store/IStore.d.ts.map +1 -0
- package/dist/tracking/store/IStore.js +2 -0
- package/dist/tracking/store/IStore.js.map +1 -0
- package/dist/tracking/store/InMemoryStore.d.ts +21 -0
- package/dist/tracking/store/InMemoryStore.d.ts.map +1 -0
- package/dist/tracking/store/InMemoryStore.js +40 -0
- package/dist/tracking/store/InMemoryStore.js.map +1 -0
- package/dist/tracking/store/InMemoryStore.test.d.ts +2 -0
- package/dist/tracking/store/InMemoryStore.test.d.ts.map +1 -0
- package/dist/tracking/store/InMemoryStore.test.js +290 -0
- package/dist/tracking/store/InMemoryStore.test.js.map +1 -0
- package/dist/tracking/store/index.d.ts +3 -0
- package/dist/tracking/store/index.d.ts.map +1 -0
- package/dist/tracking/store/index.js +2 -0
- package/dist/tracking/store/index.js.map +1 -0
- package/dist/tracking/types.d.ts +77 -0
- package/dist/tracking/types.d.ts.map +1 -0
- package/dist/tracking/types.js +2 -0
- package/dist/tracking/types.js.map +1 -0
- package/dist/utils/ExplorerClient.d.ts +68 -0
- package/dist/utils/ExplorerClient.d.ts.map +1 -0
- package/dist/utils/ExplorerClient.js +324 -0
- package/dist/utils/ExplorerClient.js.map +1 -0
- package/dist/utils/ExplorerClient.test.d.ts +2 -0
- package/dist/utils/ExplorerClient.test.d.ts.map +1 -0
- package/dist/utils/ExplorerClient.test.js +261 -0
- package/dist/utils/ExplorerClient.test.js.map +1 -0
- package/dist/utils/balanceUtils.d.ts +12 -0
- package/dist/utils/balanceUtils.d.ts.map +1 -0
- package/dist/utils/balanceUtils.js +40 -0
- package/dist/utils/balanceUtils.js.map +1 -0
- package/dist/utils/balanceUtils.test.d.ts +2 -0
- package/dist/utils/balanceUtils.test.d.ts.map +1 -0
- package/dist/utils/balanceUtils.test.js +55 -0
- package/dist/utils/balanceUtils.test.js.map +1 -0
- package/dist/utils/blockTag.d.ts +14 -0
- package/dist/utils/blockTag.d.ts.map +1 -0
- package/dist/utils/blockTag.js +46 -0
- package/dist/utils/blockTag.js.map +1 -0
- package/dist/utils/bridgeUtils.d.ts +40 -0
- package/dist/utils/bridgeUtils.d.ts.map +1 -0
- package/dist/utils/bridgeUtils.js +54 -0
- package/dist/utils/bridgeUtils.js.map +1 -0
- package/dist/utils/bridgeUtils.test.d.ts +2 -0
- package/dist/utils/bridgeUtils.test.d.ts.map +1 -0
- package/dist/utils/bridgeUtils.test.js +163 -0
- package/dist/utils/bridgeUtils.test.js.map +1 -0
- package/dist/utils/errors.d.ts +4 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +6 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/gasEstimation.d.ts +65 -0
- package/dist/utils/gasEstimation.d.ts.map +1 -0
- package/dist/utils/gasEstimation.js +206 -0
- package/dist/utils/gasEstimation.js.map +1 -0
- package/dist/utils/solanaKeyParser.d.ts +10 -0
- package/dist/utils/solanaKeyParser.d.ts.map +1 -0
- package/dist/utils/solanaKeyParser.js +87 -0
- package/dist/utils/solanaKeyParser.js.map +1 -0
- package/dist/utils/solanaKeyParser.test.d.ts +2 -0
- package/dist/utils/solanaKeyParser.test.d.ts.map +1 -0
- package/dist/utils/solanaKeyParser.test.js +71 -0
- package/dist/utils/solanaKeyParser.test.js.map +1 -0
- package/dist/utils/tokenUtils.d.ts +38 -0
- package/dist/utils/tokenUtils.d.ts.map +1 -0
- package/dist/utils/tokenUtils.js +65 -0
- package/dist/utils/tokenUtils.js.map +1 -0
- package/dist/utils/transactionUtils.d.ts +22 -0
- package/dist/utils/transactionUtils.d.ts.map +1 -0
- package/dist/utils/transactionUtils.js +26 -0
- package/dist/utils/transactionUtils.js.map +1 -0
- package/package.json +87 -3
- package/src/bridges/LiFiBridge.test.ts +615 -0
- package/src/bridges/LiFiBridge.ts +731 -0
- package/src/config/RebalancerConfig.test.ts +921 -0
- package/src/config/RebalancerConfig.ts +63 -0
- package/src/config/types.ts +613 -0
- package/src/core/InventoryRebalancer.test.ts +1858 -0
- package/src/core/InventoryRebalancer.ts +1455 -0
- package/src/core/Rebalancer.test.ts +686 -0
- package/src/core/Rebalancer.ts +686 -0
- package/src/core/RebalancerOrchestrator.test.ts +871 -0
- package/src/core/RebalancerOrchestrator.ts +273 -0
- package/src/core/RebalancerService.test.ts +1094 -0
- package/src/core/RebalancerService.ts +412 -0
- package/src/e2e/E2E_TEST_CONVENTIONS.md +222 -0
- package/src/e2e/collateral-deficit.e2e-test.ts +403 -0
- package/src/e2e/composite.e2e-test.ts +1008 -0
- package/src/e2e/fixtures/routes.ts +532 -0
- package/src/e2e/harness/BaseLocalDeploymentManager.ts +246 -0
- package/src/e2e/harness/BridgeSetup.ts +95 -0
- package/src/e2e/harness/Erc20InventoryLocalDeploymentManager.ts +183 -0
- package/src/e2e/harness/Erc20LocalDeploymentManager.ts +190 -0
- package/src/e2e/harness/ForkIndexer.test.ts +448 -0
- package/src/e2e/harness/ForkIndexer.ts +183 -0
- package/src/e2e/harness/MixedLocalDeploymentManager.ts +196 -0
- package/src/e2e/harness/MockExplorerClient.ts +94 -0
- package/src/e2e/harness/MockExternalBridge.ts +411 -0
- package/src/e2e/harness/NativeLocalDeploymentManager.ts +138 -0
- package/src/e2e/harness/SnapshotHelper.ts +56 -0
- package/src/e2e/harness/TestHelpers.ts +169 -0
- package/src/e2e/harness/TestRebalancer.ts +758 -0
- package/src/e2e/harness/TransferHelper.ts +188 -0
- package/src/e2e/inventory-erc20-minAmount.e2e-test.ts +735 -0
- package/src/e2e/inventory-erc20-weighted.e2e-test.ts +733 -0
- package/src/e2e/inventory-minAmount.e2e-test.ts +734 -0
- package/src/e2e/inventory-weighted.e2e-test.ts +734 -0
- package/src/e2e/minAmount.e2e-test.ts +313 -0
- package/src/e2e/mixed-weighted.e2e-test.ts +267 -0
- package/src/e2e/weighted.e2e-test.ts +304 -0
- package/src/factories/RebalancerContextFactory.test.ts +394 -0
- package/src/factories/RebalancerContextFactory.ts +771 -0
- package/src/index.ts +125 -0
- package/src/interfaces/IExternalBridge.ts +117 -0
- package/src/interfaces/IMetrics.ts +5 -0
- package/src/interfaces/IMonitor.ts +71 -0
- package/src/interfaces/IRebalancer.ts +57 -0
- package/src/interfaces/IStrategy.ts +81 -0
- package/src/metrics/Metrics.ts +335 -0
- package/src/metrics/PriceGetter.ts +74 -0
- package/src/metrics/scripts/metrics.ts +139 -0
- package/src/monitor/Monitor.ts +324 -0
- package/src/service.ts +285 -0
- package/src/strategy/BaseStrategy.ts +553 -0
- package/src/strategy/CollateralDeficitStrategy.test.ts +728 -0
- package/src/strategy/CollateralDeficitStrategy.ts +421 -0
- package/src/strategy/CompositeStrategy.test.ts +418 -0
- package/src/strategy/CompositeStrategy.ts +102 -0
- package/src/strategy/MinAmountStrategy.test.ts +732 -0
- package/src/strategy/MinAmountStrategy.ts +200 -0
- package/src/strategy/StrategyFactory.test.ts +108 -0
- package/src/strategy/StrategyFactory.ts +157 -0
- package/src/strategy/WeightedStrategy.test.ts +529 -0
- package/src/strategy/WeightedStrategy.ts +126 -0
- package/src/strategy/index.ts +6 -0
- package/src/test/helpers.ts +477 -0
- package/src/test/lifiMocks.ts +211 -0
- package/src/tracking/ActionTracker.test.ts +1625 -0
- package/src/tracking/ActionTracker.ts +1019 -0
- package/src/tracking/IActionTracker.ts +213 -0
- package/src/tracking/InflightContextAdapter.test.ts +206 -0
- package/src/tracking/InflightContextAdapter.ts +75 -0
- package/src/tracking/store/IStore.ts +48 -0
- package/src/tracking/store/InMemoryStore.test.ts +338 -0
- package/src/tracking/store/InMemoryStore.ts +59 -0
- package/src/tracking/store/index.ts +2 -0
- package/src/tracking/types.ts +118 -0
- package/src/utils/ExplorerClient.test.ts +386 -0
- package/src/utils/ExplorerClient.ts +439 -0
- package/src/utils/balanceUtils.test.ts +79 -0
- package/src/utils/balanceUtils.ts +65 -0
- package/src/utils/blockTag.ts +68 -0
- package/src/utils/bridgeUtils.test.ts +193 -0
- package/src/utils/bridgeUtils.ts +101 -0
- package/src/utils/errors.ts +5 -0
- package/src/utils/gasEstimation.ts +306 -0
- package/src/utils/solanaKeyParser.test.ts +103 -0
- package/src/utils/solanaKeyParser.ts +117 -0
- package/src/utils/tokenUtils.ts +89 -0
- package/src/utils/transactionUtils.ts +41 -0
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
import { type Logger } from 'pino';
|
|
2
|
+
|
|
3
|
+
import type { ChainMap, ChainName, Token } from '@humanode-hyperlane/sdk';
|
|
4
|
+
import { toWei } from '@humanode-hyperlane/utils';
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
IStrategy,
|
|
8
|
+
InflightContext,
|
|
9
|
+
RawBalances,
|
|
10
|
+
Route,
|
|
11
|
+
RouteWithContext,
|
|
12
|
+
StrategyRoute,
|
|
13
|
+
} from '../interfaces/IStrategy.js';
|
|
14
|
+
import { type Metrics } from '../metrics/Metrics.js';
|
|
15
|
+
import {
|
|
16
|
+
type BridgeConfig,
|
|
17
|
+
type BridgeConfigWithOverride,
|
|
18
|
+
createStrategyRoute,
|
|
19
|
+
getBridgeConfig,
|
|
20
|
+
} from '../utils/bridgeUtils.js';
|
|
21
|
+
|
|
22
|
+
export type Delta = { chain: ChainName; amount: bigint };
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Base abstract class for rebalancing strategies
|
|
26
|
+
*/
|
|
27
|
+
export abstract class BaseStrategy implements IStrategy {
|
|
28
|
+
abstract readonly name: string;
|
|
29
|
+
protected readonly chains: ChainName[];
|
|
30
|
+
protected readonly metrics?: Metrics;
|
|
31
|
+
protected readonly logger: Logger;
|
|
32
|
+
protected readonly bridgeConfigs: ChainMap<BridgeConfigWithOverride>;
|
|
33
|
+
protected readonly tokensByChainName?: ChainMap<Token>;
|
|
34
|
+
|
|
35
|
+
constructor(
|
|
36
|
+
chains: ChainName[],
|
|
37
|
+
logger: Logger,
|
|
38
|
+
bridgeConfigs: ChainMap<BridgeConfigWithOverride>,
|
|
39
|
+
metrics?: Metrics,
|
|
40
|
+
tokensByChainName?: ChainMap<Token>,
|
|
41
|
+
) {
|
|
42
|
+
// Rebalancing makes sense only with more than one chain.
|
|
43
|
+
if (chains.length < 2) {
|
|
44
|
+
throw new Error('At least two chains must be configured');
|
|
45
|
+
}
|
|
46
|
+
this.chains = chains;
|
|
47
|
+
this.logger = logger;
|
|
48
|
+
this.bridgeConfigs = bridgeConfigs;
|
|
49
|
+
this.metrics = metrics;
|
|
50
|
+
this.tokensByChainName = tokensByChainName;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected getBridgeConfigForRoute(
|
|
54
|
+
origin: ChainName,
|
|
55
|
+
destination: ChainName,
|
|
56
|
+
): BridgeConfig {
|
|
57
|
+
return getBridgeConfig(this.bridgeConfigs, origin, destination);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Main method to get rebalancing routes
|
|
62
|
+
*/
|
|
63
|
+
getRebalancingRoutes(
|
|
64
|
+
rawBalances: RawBalances,
|
|
65
|
+
inflightContext?: InflightContext,
|
|
66
|
+
): StrategyRoute[] {
|
|
67
|
+
const pendingRebalances = inflightContext?.pendingRebalances ?? [];
|
|
68
|
+
const pendingTransfers = inflightContext?.pendingTransfers ?? [];
|
|
69
|
+
const proposedRebalances = inflightContext?.proposedRebalances ?? [];
|
|
70
|
+
|
|
71
|
+
this.logger.info(
|
|
72
|
+
{
|
|
73
|
+
strategy: this.name,
|
|
74
|
+
balances: Object.entries(rawBalances).map(([c, b]) => ({
|
|
75
|
+
chain: c,
|
|
76
|
+
balance: b.toString(),
|
|
77
|
+
})),
|
|
78
|
+
pendingRebalances: pendingRebalances.length,
|
|
79
|
+
pendingTransfers: pendingTransfers.length,
|
|
80
|
+
proposedRebalances: proposedRebalances.length,
|
|
81
|
+
},
|
|
82
|
+
'Strategy evaluating',
|
|
83
|
+
);
|
|
84
|
+
this.validateRawBalances(rawBalances);
|
|
85
|
+
|
|
86
|
+
// Store original balances for filtering step
|
|
87
|
+
const actualBalances = rawBalances;
|
|
88
|
+
|
|
89
|
+
// Step 1: Reserve collateral for pending user transfers
|
|
90
|
+
// This prevents draining collateral needed for incoming user transfers
|
|
91
|
+
const effectiveBalances = this.reserveCollateral(
|
|
92
|
+
rawBalances,
|
|
93
|
+
pendingTransfers,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
// Get balances categorized by surplus and deficit
|
|
97
|
+
// Pass pending and proposed rebalances so strategy can account for them
|
|
98
|
+
const { surpluses, deficits } = this.getCategorizedBalances(
|
|
99
|
+
effectiveBalances,
|
|
100
|
+
pendingRebalances,
|
|
101
|
+
proposedRebalances,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
this.logger.debug(
|
|
105
|
+
{
|
|
106
|
+
context: this.constructor.name,
|
|
107
|
+
surpluses,
|
|
108
|
+
},
|
|
109
|
+
'Surpluses calculated',
|
|
110
|
+
);
|
|
111
|
+
this.logger.debug(
|
|
112
|
+
{
|
|
113
|
+
context: this.constructor.name,
|
|
114
|
+
deficits,
|
|
115
|
+
},
|
|
116
|
+
'Deficits calculated',
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
// Calculate sums of surpluses and deficits
|
|
120
|
+
const totalSurplus = surpluses.reduce(
|
|
121
|
+
(sum, surplus) => sum + surplus.amount,
|
|
122
|
+
0n,
|
|
123
|
+
);
|
|
124
|
+
const totalDeficit = deficits.reduce(
|
|
125
|
+
(sum, deficit) => sum + deficit.amount,
|
|
126
|
+
0n,
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
this.logger.debug(
|
|
130
|
+
{
|
|
131
|
+
context: this.constructor.name,
|
|
132
|
+
totalSurplus: totalSurplus.toString(),
|
|
133
|
+
},
|
|
134
|
+
'Total surplus calculated',
|
|
135
|
+
);
|
|
136
|
+
this.logger.debug(
|
|
137
|
+
{
|
|
138
|
+
context: this.constructor.name,
|
|
139
|
+
totalDeficit: totalDeficit.toString(),
|
|
140
|
+
},
|
|
141
|
+
'Total deficit calculated',
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
// If total surplus is less than total deficit, scale down deficits proportionally
|
|
145
|
+
if (totalSurplus < totalDeficit) {
|
|
146
|
+
this.logger.warn(
|
|
147
|
+
{
|
|
148
|
+
context: this.constructor.name,
|
|
149
|
+
totalSurplus: totalSurplus.toString(),
|
|
150
|
+
totalDeficit: totalDeficit.toString(),
|
|
151
|
+
},
|
|
152
|
+
'Deficits are greater than surpluses. Scaling deficits',
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
// we consider this a failure because we cannot rebalance the route completely
|
|
156
|
+
// however we can still transfer some amount of the deficit to reduce the imbalances
|
|
157
|
+
this.metrics?.recordRebalancerFailure();
|
|
158
|
+
|
|
159
|
+
for (const deficit of deficits) {
|
|
160
|
+
const newAmount = (deficit.amount * totalSurplus) / totalDeficit;
|
|
161
|
+
|
|
162
|
+
deficit.amount = newAmount;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
this.logger.debug(
|
|
166
|
+
{
|
|
167
|
+
context: this.constructor.name,
|
|
168
|
+
deficits,
|
|
169
|
+
},
|
|
170
|
+
'Scaled deficits',
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Sort from largest to smallest amounts as to always transfer largest amounts
|
|
175
|
+
// first and decrease the amount of routes required
|
|
176
|
+
surpluses.sort((a, b) => (a.amount > b.amount ? -1 : 1));
|
|
177
|
+
deficits.sort((a, b) => (a.amount > b.amount ? -1 : 1));
|
|
178
|
+
|
|
179
|
+
const routes: StrategyRoute[] = [];
|
|
180
|
+
|
|
181
|
+
// Transfer from surplus to deficit until all deficits are balanced.
|
|
182
|
+
while (deficits.length > 0 && surpluses.length > 0) {
|
|
183
|
+
const surplus = surpluses[0];
|
|
184
|
+
const deficit = deficits[0];
|
|
185
|
+
|
|
186
|
+
// Transfers the whole surplus or just the amount to balance the deficit
|
|
187
|
+
const transferAmount =
|
|
188
|
+
surplus.amount > deficit.amount ? deficit.amount : surplus.amount;
|
|
189
|
+
|
|
190
|
+
// Skip zero-amount routes (can occur after scaling when surpluses < deficits)
|
|
191
|
+
if (transferAmount > 0n) {
|
|
192
|
+
// Get bridge config for this route (with destination-specific overrides)
|
|
193
|
+
const bridgeConfig = this.getBridgeConfigForRoute(
|
|
194
|
+
surplus.chain,
|
|
195
|
+
deficit.chain,
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
// Create appropriate route type based on execution type
|
|
199
|
+
routes.push(
|
|
200
|
+
createStrategyRoute(
|
|
201
|
+
bridgeConfig,
|
|
202
|
+
surplus.chain,
|
|
203
|
+
deficit.chain,
|
|
204
|
+
transferAmount,
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Decreases the amounts for the following iterations
|
|
210
|
+
deficit.amount -= transferAmount;
|
|
211
|
+
surplus.amount -= transferAmount;
|
|
212
|
+
|
|
213
|
+
// Removes the deficit if it is fully balanced (including scaled-to-zero)
|
|
214
|
+
if (deficit.amount <= 0n) {
|
|
215
|
+
deficits.shift();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Removes the surplus if it has been drained
|
|
219
|
+
if (surplus.amount <= 0n) {
|
|
220
|
+
surpluses.shift();
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
this.logger.debug(
|
|
225
|
+
{
|
|
226
|
+
context: this.constructor.name,
|
|
227
|
+
routes,
|
|
228
|
+
},
|
|
229
|
+
'Generated routes',
|
|
230
|
+
);
|
|
231
|
+
this.logger.info(
|
|
232
|
+
{
|
|
233
|
+
context: this.constructor.name,
|
|
234
|
+
numberOfRoutes: routes.length,
|
|
235
|
+
},
|
|
236
|
+
'Found rebalancing routes',
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
const filteredRoutes = this.filterRoutes(routes, actualBalances);
|
|
240
|
+
|
|
241
|
+
this.logger.debug(
|
|
242
|
+
{
|
|
243
|
+
context: this.constructor.name,
|
|
244
|
+
filteredRoutesCount: filteredRoutes.length,
|
|
245
|
+
droppedCount: routes.length - filteredRoutes.length,
|
|
246
|
+
},
|
|
247
|
+
'Filtered rebalancing routes',
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
// Record metrics for each intent that passed filtering
|
|
251
|
+
for (const route of filteredRoutes) {
|
|
252
|
+
this.metrics?.recordIntentCreated(route, this.name);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return filteredRoutes;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Abstract method to get balances categorized by surplus and deficit
|
|
260
|
+
* Each specific strategy should implement its own logic
|
|
261
|
+
*
|
|
262
|
+
* @param balances - Effective balances (after collateral reservation)
|
|
263
|
+
* @param pendingRebalances - In-flight rebalances (origin tx confirmed, balance already deducted)
|
|
264
|
+
* @param proposedRebalances - Routes from earlier strategies in same cycle (not yet executed)
|
|
265
|
+
* @returns Categorized surpluses and deficits as Delta arrays
|
|
266
|
+
*/
|
|
267
|
+
protected abstract getCategorizedBalances(
|
|
268
|
+
balances: RawBalances,
|
|
269
|
+
pendingRebalances?: Route[],
|
|
270
|
+
proposedRebalances?: StrategyRoute[],
|
|
271
|
+
): {
|
|
272
|
+
surpluses: Delta[];
|
|
273
|
+
deficits: Delta[];
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Validates the raw balances against the chains configuration
|
|
278
|
+
*/
|
|
279
|
+
protected validateRawBalances(rawBalances: RawBalances): void {
|
|
280
|
+
const rawBalancesChains = Object.keys(rawBalances);
|
|
281
|
+
|
|
282
|
+
if (this.chains.length !== rawBalancesChains.length) {
|
|
283
|
+
throw new Error('Config chains do not match raw balances chains length');
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
for (const chain of this.chains) {
|
|
287
|
+
const balance: bigint | undefined = rawBalances[chain];
|
|
288
|
+
|
|
289
|
+
if (balance === undefined) {
|
|
290
|
+
throw new Error(`Raw balance for chain ${chain} not found`);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (balance < 0n) {
|
|
294
|
+
throw new Error(`Raw balance for chain ${chain} is negative`);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Reserve collateral for pending user transfers.
|
|
301
|
+
* Subtracts pending transfer amounts from destination balances.
|
|
302
|
+
* This ensures we don't drain collateral needed for incoming transfers.
|
|
303
|
+
*
|
|
304
|
+
* @param rawBalances - Current on-chain balances
|
|
305
|
+
* @param pendingTransfers - Transfers that will need collateral on destination
|
|
306
|
+
* @returns Balances with reserved amounts subtracted
|
|
307
|
+
*/
|
|
308
|
+
protected reserveCollateral(
|
|
309
|
+
rawBalances: RawBalances,
|
|
310
|
+
pendingTransfers: Route[],
|
|
311
|
+
): RawBalances {
|
|
312
|
+
if (pendingTransfers.length === 0) {
|
|
313
|
+
return rawBalances;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const reserved = { ...rawBalances };
|
|
317
|
+
|
|
318
|
+
for (const transfer of pendingTransfers) {
|
|
319
|
+
const destBalance = reserved[transfer.destination] ?? 0n;
|
|
320
|
+
// Reserve the transfer amount from destination
|
|
321
|
+
// Allow negative values to indicate collateral deficits
|
|
322
|
+
reserved[transfer.destination] = destBalance - transfer.amount;
|
|
323
|
+
|
|
324
|
+
this.logger.debug(
|
|
325
|
+
{
|
|
326
|
+
context: this.constructor.name,
|
|
327
|
+
destination: transfer.destination,
|
|
328
|
+
amount: transfer.amount.toString(),
|
|
329
|
+
newBalance: reserved[transfer.destination].toString(),
|
|
330
|
+
},
|
|
331
|
+
'Reserved collateral for pending transfer',
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
this.logger.info(
|
|
336
|
+
{
|
|
337
|
+
reservations: pendingTransfers.map((t) => ({
|
|
338
|
+
destination: t.destination,
|
|
339
|
+
amount: t.amount.toString(),
|
|
340
|
+
})),
|
|
341
|
+
},
|
|
342
|
+
'Collateral reserved for pending transfers',
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
return reserved;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Simulate pending rebalances based on execution method.
|
|
350
|
+
*
|
|
351
|
+
* For **movable_collateral** (default):
|
|
352
|
+
* - Add full amount to destination (origin already deducted on-chain)
|
|
353
|
+
*
|
|
354
|
+
* For **inventory**:
|
|
355
|
+
* - Simulate the eventual final state as if the entire intent will be fulfilled
|
|
356
|
+
* - Destination: add unfulfilled amount (total - delivered - awaiting)
|
|
357
|
+
* This is what's NOT yet reflected in on-chain balance
|
|
358
|
+
* - Origin: subtract pending amount (total - delivered)
|
|
359
|
+
* This is what WILL decrease when all messages deliver
|
|
360
|
+
*
|
|
361
|
+
* @param rawBalances - Current balances (may already have collateral reserved)
|
|
362
|
+
* @param pendingRebalances - In-flight rebalance operations (in_progress only)
|
|
363
|
+
* @returns Simulated future balances after rebalances complete
|
|
364
|
+
*/
|
|
365
|
+
protected simulatePendingRebalances(
|
|
366
|
+
rawBalances: RawBalances,
|
|
367
|
+
pendingRebalances: RouteWithContext[],
|
|
368
|
+
): RawBalances {
|
|
369
|
+
if (pendingRebalances.length === 0) {
|
|
370
|
+
return rawBalances;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const simulated = { ...rawBalances };
|
|
374
|
+
|
|
375
|
+
for (const rebalance of pendingRebalances) {
|
|
376
|
+
if (rebalance.executionMethod === 'inventory') {
|
|
377
|
+
// Inventory: simulate eventual final state
|
|
378
|
+
const total = rebalance.amount;
|
|
379
|
+
const delivered = rebalance.deliveredAmount ?? 0n;
|
|
380
|
+
const awaiting = rebalance.awaitingDeliveryAmount ?? 0n;
|
|
381
|
+
|
|
382
|
+
// Destination: add unfulfilled amount (total - delivered - awaiting)
|
|
383
|
+
// This is what's NOT yet reflected in on-chain balance
|
|
384
|
+
const destinationAdjustment = total - delivered - awaiting;
|
|
385
|
+
if (destinationAdjustment > 0n) {
|
|
386
|
+
simulated[rebalance.destination] =
|
|
387
|
+
(simulated[rebalance.destination] ?? 0n) + destinationAdjustment;
|
|
388
|
+
|
|
389
|
+
this.logger.debug(
|
|
390
|
+
{
|
|
391
|
+
context: this.constructor.name,
|
|
392
|
+
destination: rebalance.destination,
|
|
393
|
+
destinationAdjustment: destinationAdjustment.toString(),
|
|
394
|
+
},
|
|
395
|
+
'Simulated inventory rebalance (destination increase for unfulfilled)',
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Origin: subtract pending amount (total - delivered)
|
|
400
|
+
// This is what WILL decrease when all messages deliver
|
|
401
|
+
const originAdjustment = total - delivered;
|
|
402
|
+
if (originAdjustment > 0n) {
|
|
403
|
+
simulated[rebalance.origin] =
|
|
404
|
+
(simulated[rebalance.origin] ?? 0n) - originAdjustment;
|
|
405
|
+
|
|
406
|
+
this.logger.debug(
|
|
407
|
+
{
|
|
408
|
+
context: this.constructor.name,
|
|
409
|
+
origin: rebalance.origin,
|
|
410
|
+
originAdjustment: originAdjustment.toString(),
|
|
411
|
+
},
|
|
412
|
+
'Simulated inventory rebalance (origin decrease for pending)',
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
} else {
|
|
416
|
+
// Movable collateral: origin already deducted on-chain, add to destination
|
|
417
|
+
simulated[rebalance.destination] =
|
|
418
|
+
(simulated[rebalance.destination] ?? 0n) + rebalance.amount;
|
|
419
|
+
|
|
420
|
+
this.logger.debug(
|
|
421
|
+
{
|
|
422
|
+
context: this.constructor.name,
|
|
423
|
+
destination: rebalance.destination,
|
|
424
|
+
amount: rebalance.amount.toString(),
|
|
425
|
+
},
|
|
426
|
+
'Simulated movable collateral rebalance (destination increase)',
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
this.logger.info(
|
|
432
|
+
{
|
|
433
|
+
simulations: pendingRebalances.map((r) => ({
|
|
434
|
+
from: r.origin,
|
|
435
|
+
to: r.destination,
|
|
436
|
+
amount: r.amount.toString(),
|
|
437
|
+
executionMethod: r.executionMethod ?? 'movable_collateral',
|
|
438
|
+
deliveredAmount: r.deliveredAmount?.toString() ?? '0',
|
|
439
|
+
awaitingDeliveryAmount: r.awaitingDeliveryAmount?.toString() ?? '0',
|
|
440
|
+
})),
|
|
441
|
+
},
|
|
442
|
+
'Simulated pending rebalances',
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
return simulated;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Simulate proposed rebalances by subtracting from origin AND adding to destination.
|
|
450
|
+
*
|
|
451
|
+
* Unlike pendingRebalances, proposedRebalances are routes from earlier strategies
|
|
452
|
+
* in the same cycle that haven't been executed yet. Therefore:
|
|
453
|
+
* - Origin balance has NOT been deducted on-chain
|
|
454
|
+
* - We must simulate both sides to maintain accurate total balance
|
|
455
|
+
*
|
|
456
|
+
* @param rawBalances - Current balances (may already have pending rebalances simulated)
|
|
457
|
+
* @param proposedRebalances - Routes from earlier strategies (not yet executed)
|
|
458
|
+
* @returns Simulated balances after proposed rebalances complete
|
|
459
|
+
*/
|
|
460
|
+
protected simulateProposedRebalances(
|
|
461
|
+
rawBalances: RawBalances,
|
|
462
|
+
proposedRebalances: Route[],
|
|
463
|
+
): RawBalances {
|
|
464
|
+
if (proposedRebalances.length === 0) {
|
|
465
|
+
return rawBalances;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const simulated = { ...rawBalances };
|
|
469
|
+
|
|
470
|
+
for (const rebalance of proposedRebalances) {
|
|
471
|
+
// Subtract from origin (not yet deducted on-chain)
|
|
472
|
+
simulated[rebalance.origin] =
|
|
473
|
+
(simulated[rebalance.origin] ?? 0n) - rebalance.amount;
|
|
474
|
+
|
|
475
|
+
// Add to destination
|
|
476
|
+
simulated[rebalance.destination] =
|
|
477
|
+
(simulated[rebalance.destination] ?? 0n) + rebalance.amount;
|
|
478
|
+
|
|
479
|
+
this.logger.debug(
|
|
480
|
+
{
|
|
481
|
+
context: this.constructor.name,
|
|
482
|
+
origin: rebalance.origin,
|
|
483
|
+
destination: rebalance.destination,
|
|
484
|
+
amount: rebalance.amount.toString(),
|
|
485
|
+
},
|
|
486
|
+
'Simulated proposed rebalance (origin decrease, destination increase)',
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
this.logger.info(
|
|
491
|
+
{
|
|
492
|
+
simulations: proposedRebalances.map((r) => ({
|
|
493
|
+
from: r.origin,
|
|
494
|
+
to: r.destination,
|
|
495
|
+
amount: r.amount.toString(),
|
|
496
|
+
})),
|
|
497
|
+
},
|
|
498
|
+
'Simulated proposed rebalances',
|
|
499
|
+
);
|
|
500
|
+
|
|
501
|
+
return simulated;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
protected filterRoutes(
|
|
505
|
+
routes: StrategyRoute[],
|
|
506
|
+
actualBalances: RawBalances,
|
|
507
|
+
): StrategyRoute[] {
|
|
508
|
+
return routes.filter((route) => {
|
|
509
|
+
const balance = actualBalances[route.origin] ?? 0n;
|
|
510
|
+
if (balance < route.amount) {
|
|
511
|
+
this.logger.warn(
|
|
512
|
+
{
|
|
513
|
+
context: this.constructor.name,
|
|
514
|
+
origin: route.origin,
|
|
515
|
+
destination: route.destination,
|
|
516
|
+
required: route.amount.toString(),
|
|
517
|
+
available: balance.toString(),
|
|
518
|
+
},
|
|
519
|
+
'Dropping route due to insufficient balance',
|
|
520
|
+
);
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (this.tokensByChainName) {
|
|
525
|
+
const token = this.tokensByChainName[route.origin];
|
|
526
|
+
if (token) {
|
|
527
|
+
const bridgeConfig = this.getBridgeConfigForRoute(
|
|
528
|
+
route.origin,
|
|
529
|
+
route.destination,
|
|
530
|
+
);
|
|
531
|
+
const minAmount = BigInt(
|
|
532
|
+
toWei(bridgeConfig.bridgeMinAcceptedAmount, token.decimals),
|
|
533
|
+
);
|
|
534
|
+
if (route.amount < minAmount) {
|
|
535
|
+
this.logger.info(
|
|
536
|
+
{
|
|
537
|
+
context: this.constructor.name,
|
|
538
|
+
origin: route.origin,
|
|
539
|
+
destination: route.destination,
|
|
540
|
+
amount: route.amount.toString(),
|
|
541
|
+
minAmount: minAmount.toString(),
|
|
542
|
+
},
|
|
543
|
+
'Dropping route below bridgeMinAcceptedAmount',
|
|
544
|
+
);
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
return true;
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
}
|