@hyperlane-xyz/rebalancer 2.0.0 → 3.1.0

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.
Files changed (213) hide show
  1. package/dist/bridges/LiFiBridge.d.ts +67 -0
  2. package/dist/bridges/LiFiBridge.d.ts.map +1 -0
  3. package/dist/bridges/LiFiBridge.js +386 -0
  4. package/dist/bridges/LiFiBridge.js.map +1 -0
  5. package/dist/config/RebalancerConfig.d.ts +8 -2
  6. package/dist/config/RebalancerConfig.d.ts.map +1 -1
  7. package/dist/config/RebalancerConfig.js +9 -4
  8. package/dist/config/RebalancerConfig.js.map +1 -1
  9. package/dist/config/RebalancerConfig.test.js +135 -1
  10. package/dist/config/RebalancerConfig.test.js.map +1 -1
  11. package/dist/config/types.d.ts +1023 -304
  12. package/dist/config/types.d.ts.map +1 -1
  13. package/dist/config/types.js +113 -10
  14. package/dist/config/types.js.map +1 -1
  15. package/dist/core/InventoryRebalancer.d.ts +190 -0
  16. package/dist/core/InventoryRebalancer.d.ts.map +1 -0
  17. package/dist/core/InventoryRebalancer.js +892 -0
  18. package/dist/core/InventoryRebalancer.js.map +1 -0
  19. package/dist/core/InventoryRebalancer.test.d.ts +2 -0
  20. package/dist/core/InventoryRebalancer.test.d.ts.map +1 -0
  21. package/dist/core/InventoryRebalancer.test.js +1382 -0
  22. package/dist/core/InventoryRebalancer.test.js.map +1 -0
  23. package/dist/core/Rebalancer.d.ts +11 -4
  24. package/dist/core/Rebalancer.d.ts.map +1 -1
  25. package/dist/core/Rebalancer.js +92 -9
  26. package/dist/core/Rebalancer.js.map +1 -1
  27. package/dist/core/Rebalancer.test.js +82 -49
  28. package/dist/core/Rebalancer.test.js.map +1 -1
  29. package/dist/core/RebalancerOrchestrator.d.ts +30 -9
  30. package/dist/core/RebalancerOrchestrator.d.ts.map +1 -1
  31. package/dist/core/RebalancerOrchestrator.js +79 -71
  32. package/dist/core/RebalancerOrchestrator.js.map +1 -1
  33. package/dist/core/RebalancerOrchestrator.test.d.ts +2 -0
  34. package/dist/core/RebalancerOrchestrator.test.d.ts.map +1 -0
  35. package/dist/core/RebalancerOrchestrator.test.js +719 -0
  36. package/dist/core/RebalancerOrchestrator.test.js.map +1 -0
  37. package/dist/core/RebalancerService.d.ts +7 -3
  38. package/dist/core/RebalancerService.d.ts.map +1 -1
  39. package/dist/core/RebalancerService.js +44 -24
  40. package/dist/core/RebalancerService.js.map +1 -1
  41. package/dist/core/RebalancerService.test.js +74 -110
  42. package/dist/core/RebalancerService.test.js.map +1 -1
  43. package/dist/e2e/collateral-deficit.e2e-test.js +1 -3
  44. package/dist/e2e/collateral-deficit.e2e-test.js.map +1 -1
  45. package/dist/e2e/composite.e2e-test.js.map +1 -1
  46. package/dist/e2e/harness/BridgeSetup.d.ts +6 -0
  47. package/dist/e2e/harness/BridgeSetup.d.ts.map +1 -1
  48. package/dist/e2e/harness/BridgeSetup.js +10 -1
  49. package/dist/e2e/harness/BridgeSetup.js.map +1 -1
  50. package/dist/e2e/harness/ForkIndexer.d.ts.map +1 -1
  51. package/dist/e2e/harness/ForkIndexer.js +1 -0
  52. package/dist/e2e/harness/ForkIndexer.js.map +1 -1
  53. package/dist/e2e/harness/TestHelpers.d.ts.map +1 -1
  54. package/dist/e2e/harness/TestHelpers.js +1 -4
  55. package/dist/e2e/harness/TestHelpers.js.map +1 -1
  56. package/dist/e2e/harness/TestRebalancer.d.ts +1 -1
  57. package/dist/e2e/harness/TestRebalancer.d.ts.map +1 -1
  58. package/dist/e2e/harness/TestRebalancer.js +9 -9
  59. package/dist/e2e/harness/TestRebalancer.js.map +1 -1
  60. package/dist/e2e/minAmount.e2e-test.js +0 -1
  61. package/dist/e2e/minAmount.e2e-test.js.map +1 -1
  62. package/dist/e2e/weighted.e2e-test.js +0 -1
  63. package/dist/e2e/weighted.e2e-test.js.map +1 -1
  64. package/dist/factories/RebalancerContextFactory.d.ts +48 -6
  65. package/dist/factories/RebalancerContextFactory.d.ts.map +1 -1
  66. package/dist/factories/RebalancerContextFactory.js +171 -17
  67. package/dist/factories/RebalancerContextFactory.js.map +1 -1
  68. package/dist/index.d.ts +6 -6
  69. package/dist/index.d.ts.map +1 -1
  70. package/dist/index.js +2 -2
  71. package/dist/index.js.map +1 -1
  72. package/dist/interfaces/IExternalBridge.d.ts +101 -0
  73. package/dist/interfaces/IExternalBridge.d.ts.map +1 -0
  74. package/dist/interfaces/IExternalBridge.js +2 -0
  75. package/dist/interfaces/IExternalBridge.js.map +1 -0
  76. package/dist/interfaces/IMonitor.d.ts +1 -0
  77. package/dist/interfaces/IMonitor.d.ts.map +1 -1
  78. package/dist/interfaces/IRebalancer.d.ts +25 -25
  79. package/dist/interfaces/IRebalancer.d.ts.map +1 -1
  80. package/dist/interfaces/IStrategy.d.ts +36 -3
  81. package/dist/interfaces/IStrategy.d.ts.map +1 -1
  82. package/dist/interfaces/IStrategy.js +12 -1
  83. package/dist/interfaces/IStrategy.js.map +1 -1
  84. package/dist/metrics/PriceGetter.js +1 -1
  85. package/dist/metrics/PriceGetter.js.map +1 -1
  86. package/dist/metrics/scripts/metrics.d.ts +3 -3
  87. package/dist/monitor/Monitor.d.ts +12 -2
  88. package/dist/monitor/Monitor.d.ts.map +1 -1
  89. package/dist/monitor/Monitor.js +46 -1
  90. package/dist/monitor/Monitor.js.map +1 -1
  91. package/dist/service.js +40 -17
  92. package/dist/service.js.map +1 -1
  93. package/dist/strategy/BaseStrategy.d.ts +12 -6
  94. package/dist/strategy/BaseStrategy.d.ts.map +1 -1
  95. package/dist/strategy/BaseStrategy.js +56 -21
  96. package/dist/strategy/BaseStrategy.js.map +1 -1
  97. package/dist/strategy/CollateralDeficitStrategy.d.ts +1 -1
  98. package/dist/strategy/CollateralDeficitStrategy.d.ts.map +1 -1
  99. package/dist/strategy/CollateralDeficitStrategy.js +19 -11
  100. package/dist/strategy/CollateralDeficitStrategy.js.map +1 -1
  101. package/dist/strategy/CollateralDeficitStrategy.test.js +135 -2
  102. package/dist/strategy/CollateralDeficitStrategy.test.js.map +1 -1
  103. package/dist/strategy/CompositeStrategy.test.js +13 -0
  104. package/dist/strategy/CompositeStrategy.test.js.map +1 -1
  105. package/dist/strategy/MinAmountStrategy.test.js +4 -0
  106. package/dist/strategy/MinAmountStrategy.test.js.map +1 -1
  107. package/dist/strategy/StrategyFactory.d.ts +2 -1
  108. package/dist/strategy/StrategyFactory.d.ts.map +1 -1
  109. package/dist/strategy/StrategyFactory.js +24 -8
  110. package/dist/strategy/StrategyFactory.js.map +1 -1
  111. package/dist/strategy/WeightedStrategy.test.js +6 -0
  112. package/dist/strategy/WeightedStrategy.test.js.map +1 -1
  113. package/dist/test/helpers.d.ts +8 -7
  114. package/dist/test/helpers.d.ts.map +1 -1
  115. package/dist/test/helpers.js +23 -5
  116. package/dist/test/helpers.js.map +1 -1
  117. package/dist/test/lifiMocks.d.ts +51 -0
  118. package/dist/test/lifiMocks.d.ts.map +1 -0
  119. package/dist/test/lifiMocks.js +130 -0
  120. package/dist/test/lifiMocks.js.map +1 -0
  121. package/dist/tracking/ActionTracker.d.ts +34 -1
  122. package/dist/tracking/ActionTracker.d.ts.map +1 -1
  123. package/dist/tracking/ActionTracker.js +233 -26
  124. package/dist/tracking/ActionTracker.js.map +1 -1
  125. package/dist/tracking/ActionTracker.test.js +380 -19
  126. package/dist/tracking/ActionTracker.test.js.map +1 -1
  127. package/dist/tracking/IActionTracker.d.ts +48 -3
  128. package/dist/tracking/IActionTracker.d.ts.map +1 -1
  129. package/dist/tracking/InflightContextAdapter.d.ts.map +1 -1
  130. package/dist/tracking/InflightContextAdapter.js +24 -7
  131. package/dist/tracking/InflightContextAdapter.js.map +1 -1
  132. package/dist/tracking/InflightContextAdapter.test.js +7 -4
  133. package/dist/tracking/InflightContextAdapter.test.js.map +1 -1
  134. package/dist/tracking/types.d.ts +33 -2
  135. package/dist/tracking/types.d.ts.map +1 -1
  136. package/dist/utils/ExplorerClient.d.ts +3 -1
  137. package/dist/utils/ExplorerClient.d.ts.map +1 -1
  138. package/dist/utils/ExplorerClient.js +16 -8
  139. package/dist/utils/ExplorerClient.js.map +1 -1
  140. package/dist/utils/bridgeUtils.d.ts +27 -4
  141. package/dist/utils/bridgeUtils.d.ts.map +1 -1
  142. package/dist/utils/bridgeUtils.js +38 -0
  143. package/dist/utils/bridgeUtils.js.map +1 -1
  144. package/dist/utils/bridgeUtils.test.js +9 -0
  145. package/dist/utils/bridgeUtils.test.js.map +1 -1
  146. package/dist/utils/gasEstimation.d.ts +65 -0
  147. package/dist/utils/gasEstimation.d.ts.map +1 -0
  148. package/dist/utils/gasEstimation.js +176 -0
  149. package/dist/utils/gasEstimation.js.map +1 -0
  150. package/dist/utils/tokenUtils.d.ts +9 -1
  151. package/dist/utils/tokenUtils.d.ts.map +1 -1
  152. package/dist/utils/tokenUtils.js +11 -0
  153. package/dist/utils/tokenUtils.js.map +1 -1
  154. package/package.json +9 -7
  155. package/src/bridges/LiFiBridge.ts +538 -0
  156. package/src/config/RebalancerConfig.test.ts +162 -0
  157. package/src/config/RebalancerConfig.ts +21 -3
  158. package/src/config/types.ts +147 -10
  159. package/src/core/InventoryRebalancer.test.ts +1721 -0
  160. package/src/core/InventoryRebalancer.ts +1265 -0
  161. package/src/core/Rebalancer.test.ts +84 -30
  162. package/src/core/Rebalancer.ts +144 -23
  163. package/src/core/RebalancerOrchestrator.test.ts +869 -0
  164. package/src/core/RebalancerOrchestrator.ts +146 -95
  165. package/src/core/RebalancerService.test.ts +86 -124
  166. package/src/core/RebalancerService.ts +67 -33
  167. package/src/e2e/collateral-deficit.e2e-test.ts +2 -4
  168. package/src/e2e/composite.e2e-test.ts +5 -5
  169. package/src/e2e/harness/BridgeSetup.ts +28 -1
  170. package/src/e2e/harness/ForkIndexer.ts +1 -0
  171. package/src/e2e/harness/TestHelpers.ts +1 -4
  172. package/src/e2e/harness/TestRebalancer.ts +10 -7
  173. package/src/e2e/minAmount.e2e-test.ts +1 -2
  174. package/src/e2e/weighted.e2e-test.ts +1 -2
  175. package/src/factories/RebalancerContextFactory.ts +294 -24
  176. package/src/index.ts +22 -5
  177. package/src/interfaces/IExternalBridge.ts +115 -0
  178. package/src/interfaces/IMonitor.ts +1 -0
  179. package/src/interfaces/IRebalancer.ts +45 -29
  180. package/src/interfaces/IStrategy.ts +50 -3
  181. package/src/metrics/PriceGetter.ts +1 -1
  182. package/src/monitor/Monitor.ts +81 -2
  183. package/src/service.ts +59 -18
  184. package/src/strategy/BaseStrategy.ts +77 -24
  185. package/src/strategy/CollateralDeficitStrategy.test.ts +181 -4
  186. package/src/strategy/CollateralDeficitStrategy.ts +42 -15
  187. package/src/strategy/CompositeStrategy.test.ts +13 -0
  188. package/src/strategy/MinAmountStrategy.test.ts +4 -0
  189. package/src/strategy/StrategyFactory.ts +33 -6
  190. package/src/strategy/WeightedStrategy.test.ts +6 -0
  191. package/src/test/helpers.ts +39 -14
  192. package/src/test/lifiMocks.ts +174 -0
  193. package/src/tracking/ActionTracker.test.ts +443 -19
  194. package/src/tracking/ActionTracker.ts +339 -28
  195. package/src/tracking/IActionTracker.ts +59 -3
  196. package/src/tracking/InflightContextAdapter.test.ts +7 -4
  197. package/src/tracking/InflightContextAdapter.ts +42 -9
  198. package/src/tracking/types.ts +45 -2
  199. package/src/utils/ExplorerClient.ts +27 -10
  200. package/src/utils/bridgeUtils.test.ts +9 -0
  201. package/src/utils/bridgeUtils.ts +75 -6
  202. package/src/utils/gasEstimation.ts +272 -0
  203. package/src/utils/tokenUtils.ts +12 -0
  204. package/dist/tracking/index.d.ts +0 -7
  205. package/dist/tracking/index.d.ts.map +0 -1
  206. package/dist/tracking/index.js +0 -6
  207. package/dist/tracking/index.js.map +0 -1
  208. package/dist/utils/index.d.ts +0 -5
  209. package/dist/utils/index.d.ts.map +0 -1
  210. package/dist/utils/index.js +0 -5
  211. package/dist/utils/index.js.map +0 -1
  212. package/src/tracking/index.ts +0 -36
  213. package/src/utils/index.ts +0 -4
@@ -0,0 +1,1382 @@
1
+ import chai, { expect } from 'chai';
2
+ import chaiAsPromised from 'chai-as-promised';
3
+ import { Wallet } from 'ethers';
4
+ import { pino } from 'pino';
5
+ import Sinon from 'sinon';
6
+ import { TokenStandard, } from '@hyperlane-xyz/sdk';
7
+ import { ExternalBridgeType } from '../config/types.js';
8
+ import { createMockBridgeQuote } from '../test/lifiMocks.js';
9
+ import { InventoryRebalancer, } from './InventoryRebalancer.js';
10
+ chai.use(chaiAsPromised);
11
+ const testLogger = pino({ level: 'silent' });
12
+ describe('InventoryRebalancer E2E', () => {
13
+ let inventoryRebalancer;
14
+ let config;
15
+ let actionTracker;
16
+ let bridge;
17
+ let warpCore;
18
+ let multiProvider;
19
+ let adapterStub;
20
+ // Test constants
21
+ const ARBITRUM_CHAIN = 'arbitrum';
22
+ const SOLANA_CHAIN = 'solanamainnet';
23
+ const ARBITRUM_DOMAIN = 42161;
24
+ const SOLANA_DOMAIN = 1399811149;
25
+ const TEST_PRIVATE_KEY = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
26
+ const TEST_WALLET = new Wallet(TEST_PRIVATE_KEY);
27
+ const INVENTORY_SIGNER = TEST_WALLET.address;
28
+ beforeEach(() => {
29
+ // Config
30
+ config = {
31
+ inventorySigner: INVENTORY_SIGNER,
32
+ inventoryChains: [ARBITRUM_CHAIN, SOLANA_CHAIN],
33
+ };
34
+ // Mock IActionTracker
35
+ actionTracker = {
36
+ initialize: Sinon.stub(),
37
+ syncTransfers: Sinon.stub(),
38
+ syncRebalanceIntents: Sinon.stub(),
39
+ syncRebalanceActions: Sinon.stub(),
40
+ syncInventoryMovementActions: Sinon.stub(),
41
+ getInProgressTransfers: Sinon.stub(),
42
+ getTransfersByDestination: Sinon.stub(),
43
+ getActiveRebalanceIntents: Sinon.stub(),
44
+ getRebalanceIntentsByDestination: Sinon.stub(),
45
+ createRebalanceIntent: Sinon.stub(),
46
+ completeRebalanceIntent: Sinon.stub(),
47
+ cancelRebalanceIntent: Sinon.stub(),
48
+ failRebalanceIntent: Sinon.stub(),
49
+ getActionsByType: Sinon.stub(),
50
+ getInflightInventoryMovements: Sinon.stub(),
51
+ getPartiallyFulfilledInventoryIntents: Sinon.stub(),
52
+ createRebalanceAction: Sinon.stub(),
53
+ completeRebalanceAction: Sinon.stub(),
54
+ failRebalanceAction: Sinon.stub(),
55
+ logStoreContents: Sinon.stub(),
56
+ };
57
+ // Default: No active (partial) inventory intents
58
+ actionTracker.getPartiallyFulfilledInventoryIntents.resolves([]);
59
+ bridge = {
60
+ bridgeId: 'lifi',
61
+ quote: Sinon.stub(),
62
+ execute: Sinon.stub(),
63
+ getStatus: Sinon.stub(),
64
+ getNativeTokenAddress: Sinon.stub().returns('0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'),
65
+ };
66
+ // Mock adapter for WarpCore tokens
67
+ adapterStub = {
68
+ quoteTransferRemoteGas: Sinon.stub().resolves({
69
+ igpQuote: { amount: 1000000n },
70
+ }),
71
+ populateTransferRemoteTx: Sinon.stub().resolves({
72
+ to: '0xRouterAddress',
73
+ data: '0xTransferRemoteData',
74
+ value: 1000000n,
75
+ }),
76
+ };
77
+ // Mock WarpCore with tokens for both chains
78
+ // Note: We need tokens for both origin (surplus) and destination (deficit) chains
79
+ // because transferRemote is called FROM destination (after direction swap)
80
+ const arbitrumToken = {
81
+ chainName: ARBITRUM_CHAIN,
82
+ standard: TokenStandard.EvmHypCollateral, // Non-native: no IGP reservation needed
83
+ addressOrDenom: '0xArbitrumToken',
84
+ getHypAdapter: Sinon.stub().returns(adapterStub),
85
+ };
86
+ const solanaToken = {
87
+ chainName: SOLANA_CHAIN,
88
+ standard: TokenStandard.EvmHypCollateral, // Non-native: no IGP reservation needed
89
+ addressOrDenom: '0xSolanaToken',
90
+ getHypAdapter: Sinon.stub().returns(adapterStub),
91
+ };
92
+ warpCore = {
93
+ tokens: [arbitrumToken, solanaToken],
94
+ multiProvider: {
95
+ getProvider: Sinon.stub(),
96
+ getSigner: Sinon.stub(),
97
+ },
98
+ };
99
+ // Mock provider with getFeeData for gas estimation, estimateGas for actual gas estimation,
100
+ // and waitForTransaction for confirmations
101
+ const mockProvider = {
102
+ getFeeData: Sinon.stub().resolves({
103
+ maxFeePerGas: 10000000000n, // 10 gwei
104
+ gasPrice: 10000000000n,
105
+ }),
106
+ estimateGas: Sinon.stub().resolves(300000n), // Mock gas estimate for transferRemote
107
+ waitForTransaction: Sinon.stub().resolves({
108
+ blockNumber: 100,
109
+ status: 1,
110
+ }),
111
+ };
112
+ // Mock MultiProvider
113
+ multiProvider = {
114
+ getDomainId: Sinon.stub().callsFake((chain) => {
115
+ if (chain === ARBITRUM_CHAIN)
116
+ return ARBITRUM_DOMAIN;
117
+ if (chain === SOLANA_CHAIN)
118
+ return SOLANA_DOMAIN;
119
+ return 0;
120
+ }),
121
+ getChainId: Sinon.stub().callsFake((chain) => {
122
+ if (chain === ARBITRUM_CHAIN)
123
+ return 42161;
124
+ if (chain === SOLANA_CHAIN)
125
+ return 1399811149;
126
+ return 0;
127
+ }),
128
+ getChainName: Sinon.stub().callsFake((domain) => {
129
+ if (domain === ARBITRUM_DOMAIN)
130
+ return ARBITRUM_CHAIN;
131
+ if (domain === SOLANA_DOMAIN)
132
+ return SOLANA_CHAIN;
133
+ return 'unknown';
134
+ }),
135
+ getChainMetadata: Sinon.stub().returns({
136
+ blocks: { reorgPeriod: 1 }, // Quick confirmations for tests
137
+ }),
138
+ getProvider: Sinon.stub().returns(mockProvider),
139
+ getSigner: Sinon.stub().returns(TEST_WALLET),
140
+ sendTransaction: Sinon.stub().resolves({
141
+ transactionHash: '0xTransferRemoteTxHash',
142
+ logs: [], // Required for HyperlaneCore.getDispatchedMessages
143
+ }),
144
+ };
145
+ // Create InventoryRebalancer
146
+ inventoryRebalancer = new InventoryRebalancer(config, actionTracker, { lifi: bridge }, warpCore, multiProvider, testLogger);
147
+ // Set default high inventory balances - tests can override via setInventoryBalances
148
+ inventoryRebalancer.setInventoryBalances({
149
+ [ARBITRUM_CHAIN]: BigInt('1000000000000000000000'),
150
+ [SOLANA_CHAIN]: BigInt('1000000000000000000000'),
151
+ });
152
+ });
153
+ afterEach(() => {
154
+ Sinon.restore();
155
+ });
156
+ // Helper to create test routes and intents
157
+ function createTestRoute(overrides) {
158
+ return {
159
+ origin: ARBITRUM_CHAIN,
160
+ destination: SOLANA_CHAIN,
161
+ amount: 10000000000n, // 10k USDC (6 decimals)
162
+ executionType: 'inventory',
163
+ externalBridge: ExternalBridgeType.LiFi,
164
+ ...overrides,
165
+ };
166
+ }
167
+ function createTestIntent(overrides) {
168
+ const now = Date.now();
169
+ const intent = {
170
+ id: 'intent-1',
171
+ status: 'not_started',
172
+ origin: ARBITRUM_DOMAIN,
173
+ destination: SOLANA_DOMAIN,
174
+ amount: 10000000000n,
175
+ executionMethod: 'inventory',
176
+ createdAt: now,
177
+ updatedAt: now,
178
+ ...overrides,
179
+ };
180
+ // Configure mock to return this intent when createRebalanceIntent is called
181
+ actionTracker.createRebalanceIntent.resolves(intent);
182
+ return intent;
183
+ }
184
+ describe('Basic Inventory Rebalance (Sufficient Inventory)', () => {
185
+ // NOTE: Strategy route is arbitrum (surplus) → solana (deficit)
186
+ // But execution calls transferRemote FROM solana TO arbitrum (swapped direction)
187
+ // This ADDS collateral to solana (filling deficit) and RELEASES from arbitrum (has surplus)
188
+ it('executes transferRemote when inventory is available on destination chain', async () => {
189
+ // Setup: Strategy says move from arbitrum→solana
190
+ // We need inventory on SOLANA (destination/deficit) to call transferRemote FROM there
191
+ const route = createTestRoute();
192
+ createTestIntent();
193
+ // Inventory is checked on DESTINATION (solana), not origin
194
+ inventoryRebalancer.setInventoryBalances({
195
+ [SOLANA_CHAIN]: 10000000000n,
196
+ [ARBITRUM_CHAIN]: 0n,
197
+ });
198
+ // Execute
199
+ const results = await inventoryRebalancer.rebalance([route]);
200
+ // Verify: Single successful result
201
+ expect(results).to.have.lengthOf(1);
202
+ expect(results[0].success).to.be.true;
203
+ expect(results[0].route).to.deep.equal(route);
204
+ // Verify: transferRemote was called via adapter
205
+ expect(adapterStub.quoteTransferRemoteGas.calledOnce).to.be.true;
206
+ expect(adapterStub.populateTransferRemoteTx.calledOnce).to.be.true;
207
+ // Verify: Transaction was sent FROM SOLANA (destination chain, swapped)
208
+ expect(multiProvider.sendTransaction.calledOnce).to.be.true;
209
+ const [chainArg, txArg] = multiProvider.sendTransaction.firstCall.args;
210
+ expect(chainArg).to.equal(SOLANA_CHAIN); // Called FROM destination (swapped)
211
+ expect(txArg.to).to.equal('0xRouterAddress');
212
+ // Verify: inventory_deposit action was created
213
+ expect(actionTracker.createRebalanceAction.calledOnce).to.be.true;
214
+ const actionParams = actionTracker.createRebalanceAction.firstCall.args[0];
215
+ expect(actionParams.intentId).to.equal('intent-1');
216
+ expect(actionParams.type).to.equal('inventory_deposit');
217
+ expect(actionParams.amount).to.equal(10000000000n);
218
+ expect(actionParams.txHash).to.equal('0xTransferRemoteTxHash');
219
+ });
220
+ it('executes transferRemote with correct parameters (swapped direction)', async () => {
221
+ const route = createTestRoute({ amount: 5000000000n }); // 5k USDC
222
+ createTestIntent({ amount: 5000000000n });
223
+ // Inventory checked on DESTINATION (solana)
224
+ inventoryRebalancer.setInventoryBalances({
225
+ [SOLANA_CHAIN]: 5000000000n,
226
+ [ARBITRUM_CHAIN]: 0n,
227
+ });
228
+ await inventoryRebalancer.rebalance([route]);
229
+ // Verify populateTransferRemoteTx params
230
+ // Direction is SWAPPED: transferRemote from solana TO arbitrum
231
+ const populateParams = adapterStub.populateTransferRemoteTx.firstCall.args[0];
232
+ expect(populateParams.destination).to.equal(ARBITRUM_DOMAIN); // Goes TO arbitrum (swapped)
233
+ expect(populateParams.recipient).to.equal(INVENTORY_SIGNER);
234
+ expect(populateParams.weiAmountOrId).to.equal(5000000000n);
235
+ });
236
+ });
237
+ describe('Partial Fulfillment (Insufficient Inventory)', () => {
238
+ // Partial transfers happen when maxTransferable >= minViableTransfer
239
+ // For non-native tokens (EvmHypCollateral), minViableTransfer = 0, so partial always viable
240
+ const PARTIAL_AMOUNT = BigInt(5e15); // 0.005 ETH - above threshold
241
+ const FULL_AMOUNT = BigInt(1e16); // 0.01 ETH
242
+ it('executes partial transferRemote when maxTransferable >= minViableTransfer', async () => {
243
+ // Setup: Need 0.01 ETH, but only 0.005 ETH available on destination
244
+ // For non-native tokens, minViableTransfer = 0, so partial transfer is viable
245
+ const route = createTestRoute({ amount: FULL_AMOUNT });
246
+ createTestIntent({ amount: FULL_AMOUNT });
247
+ // Inventory checked on DESTINATION (solana) - Only 0.005 ETH available
248
+ inventoryRebalancer.setInventoryBalances({
249
+ [SOLANA_CHAIN]: PARTIAL_AMOUNT,
250
+ [ARBITRUM_CHAIN]: 0n,
251
+ });
252
+ // Execute
253
+ const results = await inventoryRebalancer.rebalance([route]);
254
+ // Verify: Success with partial amount
255
+ expect(results).to.have.lengthOf(1);
256
+ expect(results[0].success).to.be.true;
257
+ // Verify: transferRemote was called with available amount (0.005 ETH), not full amount (0.01 ETH)
258
+ const populateParams = adapterStub.populateTransferRemoteTx.firstCall.args[0];
259
+ expect(populateParams.weiAmountOrId).to.equal(PARTIAL_AMOUNT);
260
+ // Verify: Action created for partial amount
261
+ const actionParams = actionTracker.createRebalanceAction.firstCall.args[0];
262
+ expect(actionParams.amount).to.equal(PARTIAL_AMOUNT);
263
+ });
264
+ it('intent remains in_progress after partial fulfillment', async () => {
265
+ const route = createTestRoute({ amount: FULL_AMOUNT });
266
+ createTestIntent({ amount: FULL_AMOUNT });
267
+ // Inventory checked on DESTINATION (solana)
268
+ inventoryRebalancer.setInventoryBalances({
269
+ [SOLANA_CHAIN]: PARTIAL_AMOUNT,
270
+ [ARBITRUM_CHAIN]: 0n,
271
+ });
272
+ const results = await inventoryRebalancer.rebalance([route]);
273
+ // Verify: Partial transfer succeeded
274
+ expect(results[0].success).to.be.true;
275
+ });
276
+ });
277
+ describe('No Inventory Available', () => {
278
+ it('returns failure when no inventory on destination chain and no other source available', async () => {
279
+ const route = createTestRoute();
280
+ createTestIntent();
281
+ // No inventory anywhere
282
+ inventoryRebalancer.setInventoryBalances({
283
+ [SOLANA_CHAIN]: 0n,
284
+ [ARBITRUM_CHAIN]: 0n,
285
+ });
286
+ // Execute
287
+ const results = await inventoryRebalancer.rebalance([route]);
288
+ // Verify: Failure result
289
+ expect(results).to.have.lengthOf(1);
290
+ expect(results[0].success).to.be.false;
291
+ expect(results[0].error).to.include('No inventory available');
292
+ // Verify: No transferRemote attempted
293
+ expect(adapterStub.populateTransferRemoteTx.called).to.be.false;
294
+ expect(multiProvider.sendTransaction.called).to.be.false;
295
+ expect(actionTracker.createRebalanceAction.called).to.be.false;
296
+ });
297
+ });
298
+ describe('Single Intent Architecture', () => {
299
+ it('takes only first route when multiple routes provided', async () => {
300
+ // Route 1: arbitrum → solana (check inventory on solana)
301
+ const route1 = createTestRoute({ amount: 5000000000n });
302
+ // Route 2: solana → arbitrum (would check inventory on arbitrum if processed)
303
+ const route2 = createTestRoute({
304
+ origin: SOLANA_CHAIN,
305
+ destination: ARBITRUM_CHAIN,
306
+ amount: 3000000000n,
307
+ });
308
+ createTestIntent({ id: 'intent-1', amount: 5000000000n });
309
+ // Inventory on DESTINATION of first route (solana)
310
+ inventoryRebalancer.setInventoryBalances({
311
+ [SOLANA_CHAIN]: 5000000000n,
312
+ [ARBITRUM_CHAIN]: 0n,
313
+ });
314
+ // Execute with multiple routes
315
+ const results = await inventoryRebalancer.rebalance([route1, route2]);
316
+ // Verify: Only ONE route processed (single-intent architecture)
317
+ expect(results).to.have.lengthOf(1);
318
+ expect(results[0].success).to.be.true;
319
+ // Verify: Only one intent created
320
+ expect(actionTracker.createRebalanceIntent.calledOnce).to.be.true;
321
+ // Verify: Only one action created
322
+ expect(actionTracker.createRebalanceAction.calledOnce).to.be.true;
323
+ });
324
+ it('continues existing intent instead of processing new routes', async () => {
325
+ // Setup: existing partial intent
326
+ const existingIntent = createTestIntent({
327
+ id: 'existing-intent',
328
+ status: 'in_progress',
329
+ amount: 10000000000n,
330
+ });
331
+ // Configure mock to return existing partial intent
332
+ actionTracker.getPartiallyFulfilledInventoryIntents.resolves([
333
+ {
334
+ intent: existingIntent,
335
+ completedAmount: 3000000000n,
336
+ remaining: 7000000000n, // 7k remaining
337
+ hasInflightDeposit: false,
338
+ },
339
+ ]);
340
+ // New route that would be ignored
341
+ const newRoute = createTestRoute({ amount: 5000000000n });
342
+ // Inventory on DESTINATION of existing intent (solana) - Plenty of inventory
343
+ inventoryRebalancer.setInventoryBalances({
344
+ [SOLANA_CHAIN]: 10000000000n,
345
+ [ARBITRUM_CHAIN]: 0n,
346
+ });
347
+ // Execute with new route (should be ignored in favor of existing intent)
348
+ const results = await inventoryRebalancer.rebalance([newRoute]);
349
+ // Verify: Existing intent was continued (not new route)
350
+ expect(results).to.have.lengthOf(1);
351
+ expect(results[0].success).to.be.true;
352
+ // Verify: No new intent was created (existing was used)
353
+ expect(actionTracker.createRebalanceIntent.called).to.be.false;
354
+ });
355
+ it('returns empty when active intent has in-flight deposit (prevents oscillation)', async () => {
356
+ const existingIntent = createTestIntent({
357
+ id: 'inflight-deposit-intent',
358
+ status: 'in_progress',
359
+ amount: 10000000000n,
360
+ });
361
+ // Configure mock: active intent WITH in-flight deposit
362
+ actionTracker.getPartiallyFulfilledInventoryIntents.resolves([
363
+ {
364
+ intent: existingIntent,
365
+ completedAmount: 3000000000n,
366
+ remaining: 7000000000n,
367
+ hasInflightDeposit: true,
368
+ },
369
+ ]);
370
+ // New routes that WOULD create a new intent or continue
371
+ const newRoute = createTestRoute({ amount: 5000000000n });
372
+ inventoryRebalancer.setInventoryBalances({
373
+ [SOLANA_CHAIN]: 10000000000n,
374
+ [ARBITRUM_CHAIN]: 0n,
375
+ });
376
+ const results = await inventoryRebalancer.rebalance([newRoute]);
377
+ // Must return empty — wait for in-flight deposit to complete
378
+ expect(results).to.have.lengthOf(0);
379
+ // Must NOT create a new intent
380
+ expect(actionTracker.createRebalanceIntent.called).to.be.false;
381
+ // Must NOT call createRebalanceAction (proves continueIntent was never reached)
382
+ expect(actionTracker.createRebalanceAction.called).to.be.false;
383
+ });
384
+ it('returns empty results when no routes provided and no active intent', async () => {
385
+ const results = await inventoryRebalancer.rebalance([]);
386
+ expect(results).to.have.lengthOf(0);
387
+ expect(actionTracker.createRebalanceIntent.called).to.be.false;
388
+ });
389
+ it('continues existing not_started intent instead of creating new one', async () => {
390
+ // Setup: Create an intent that stays 'not_started' (simulating failed bridges)
391
+ const existingIntent = createTestIntent({
392
+ id: 'stuck-not-started-intent',
393
+ status: 'not_started', // Never transitioned to in_progress
394
+ amount: 10000000000n,
395
+ });
396
+ // Configure mock to return the not_started intent as a partial intent
397
+ // (this is the fix - getPartiallyFulfilledInventoryIntents now includes not_started)
398
+ actionTracker.getPartiallyFulfilledInventoryIntents.resolves([
399
+ {
400
+ intent: existingIntent,
401
+ completedAmount: 0n,
402
+ remaining: 10000000000n,
403
+ hasInflightDeposit: false,
404
+ },
405
+ ]);
406
+ // New route that would be ignored in favor of existing intent
407
+ const newRoute = createTestRoute({ amount: 5000000000n });
408
+ // Provide sufficient inventory for execution
409
+ inventoryRebalancer.setInventoryBalances({
410
+ [SOLANA_CHAIN]: 10000000000n,
411
+ [ARBITRUM_CHAIN]: 0n,
412
+ });
413
+ const results = await inventoryRebalancer.rebalance([newRoute]);
414
+ // Verify: Existing not_started intent was continued (not new route)
415
+ expect(results).to.have.lengthOf(1);
416
+ expect(results[0].success).to.be.true;
417
+ // Verify: No new intent was created (existing was continued)
418
+ expect(actionTracker.createRebalanceIntent.called).to.be.false;
419
+ });
420
+ });
421
+ describe('Error Handling', () => {
422
+ it('handles transaction send failure', async () => {
423
+ const route = createTestRoute();
424
+ createTestIntent();
425
+ // Inventory on DESTINATION (solana)
426
+ inventoryRebalancer.setInventoryBalances({
427
+ [SOLANA_CHAIN]: 10000000000n,
428
+ [ARBITRUM_CHAIN]: 0n,
429
+ });
430
+ multiProvider.sendTransaction.rejects(new Error('Transaction failed'));
431
+ const results = await inventoryRebalancer.rebalance([route]);
432
+ expect(results).to.have.lengthOf(1);
433
+ expect(results[0].success).to.be.false;
434
+ expect(results[0].error).to.include('Transaction failed');
435
+ });
436
+ it('handles missing token for chain', async () => {
437
+ // Clear tokens to simulate missing token
438
+ warpCore.tokens = [];
439
+ const route = createTestRoute();
440
+ createTestIntent();
441
+ // Even with inventory, if no token for destination, it should fail
442
+ inventoryRebalancer.setInventoryBalances({
443
+ [SOLANA_CHAIN]: 10000000000n,
444
+ [ARBITRUM_CHAIN]: 0n,
445
+ });
446
+ const results = await inventoryRebalancer.rebalance([route]);
447
+ expect(results).to.have.lengthOf(1);
448
+ expect(results[0].success).to.be.false;
449
+ expect(results[0].error).to.include('No token found');
450
+ });
451
+ it('handles adapter quoteTransferRemoteGas failure', async () => {
452
+ const route = createTestRoute();
453
+ createTestIntent();
454
+ // Inventory on DESTINATION (solana)
455
+ inventoryRebalancer.setInventoryBalances({
456
+ [SOLANA_CHAIN]: 10000000000n,
457
+ [ARBITRUM_CHAIN]: 0n,
458
+ });
459
+ adapterStub.quoteTransferRemoteGas.rejects(new Error('Gas quote failed'));
460
+ const results = await inventoryRebalancer.rebalance([route]);
461
+ expect(results).to.have.lengthOf(1);
462
+ expect(results[0].success).to.be.false;
463
+ expect(results[0].error).to.include('Gas quote failed');
464
+ });
465
+ it('throws when signer is not a Wallet instance', async () => {
466
+ multiProvider.getSigner = Sinon.stub().returns({
467
+ getAddress: Sinon.stub().resolves(INVENTORY_SIGNER),
468
+ });
469
+ inventoryRebalancer = new InventoryRebalancer(config, actionTracker, { lifi: bridge }, warpCore, multiProvider, testLogger);
470
+ const route = createTestRoute({ amount: BigInt(1e18) });
471
+ createTestIntent({ amount: BigInt(1e18) });
472
+ inventoryRebalancer.setInventoryBalances({
473
+ [ARBITRUM_CHAIN]: BigInt(10e18),
474
+ [SOLANA_CHAIN]: 0n,
475
+ });
476
+ bridge.quote.resolves(createMockBridgeQuote({
477
+ fromAmount: BigInt(1e18),
478
+ toAmount: BigInt(0.98e18),
479
+ }));
480
+ const results = await inventoryRebalancer.rebalance([route]);
481
+ expect(results).to.have.lengthOf(1);
482
+ expect(results[0].success).to.be.false;
483
+ expect(results[0].error).to.include('Wallet');
484
+ expect(bridge.execute.called).to.be.false;
485
+ });
486
+ });
487
+ describe('Native Token IGP Reservation', () => {
488
+ // Gas estimation: 300,000 gas × 10 gwei = 3,000,000,000,000 wei
489
+ // Buffered gas limit (10%): 330,000 gas
490
+ // Buffered gas cost: 330,000 × 10 gwei = 3,300,000,000,000 wei
491
+ // IGP quote: 1,000,000 wei
492
+ // Total reservation: IGP + buffered gas = 3,300,001,000,000 wei (~0.0033 ETH)
493
+ // Total cost (for min viable): IGP + buffered gas = 3,300,001,000,000 wei
494
+ // Min viable transfer (2x total cost): 6,600,002,000,000 wei (~0.0066 ETH)
495
+ const GAS_LIMIT = 300000n;
496
+ const BUFFERED_GAS_LIMIT = (GAS_LIMIT * 110n) / 100n; // 10% buffer
497
+ const GAS_PRICE = 10000000000n; // 10 gwei
498
+ const BUFFERED_GAS_COST = GAS_PRICE * BUFFERED_GAS_LIMIT;
499
+ const IGP_COST = 1000000n;
500
+ const TOTAL_RESERVATION = IGP_COST + BUFFERED_GAS_COST;
501
+ // Note: MIN_VIABLE_TRANSFER = TOTAL_COST * 2n = ~6.6e12 wei (~0.0066 ETH)
502
+ it('reserves IGP and gas cost when transferring native tokens', async () => {
503
+ // Setup: Native token on DESTINATION (solana) where IGP and gas must be reserved
504
+ // Strategy: arbitrum → solana, so transferRemote is called FROM solana
505
+ const arbitrumToken = {
506
+ chainName: ARBITRUM_CHAIN,
507
+ standard: TokenStandard.EvmHypNative,
508
+ getHypAdapter: Sinon.stub().returns(adapterStub),
509
+ };
510
+ const solanaToken = {
511
+ chainName: SOLANA_CHAIN,
512
+ standard: TokenStandard.EvmHypNative, // Native token: reservation needed
513
+ getHypAdapter: Sinon.stub().returns(adapterStub),
514
+ };
515
+ warpCore.tokens = [arbitrumToken, solanaToken];
516
+ const requestedAmount = 10000000000000000n; // 0.01 ETH
517
+ const availableInventory = requestedAmount + TOTAL_RESERVATION; // Enough for amount + costs
518
+ const route = createTestRoute({ amount: requestedAmount });
519
+ createTestIntent({ amount: requestedAmount });
520
+ // Inventory on DESTINATION (solana) where transferRemote is called FROM
521
+ inventoryRebalancer.setInventoryBalances({
522
+ [SOLANA_CHAIN]: availableInventory,
523
+ [ARBITRUM_CHAIN]: 0n,
524
+ });
525
+ // Execute
526
+ const results = await inventoryRebalancer.rebalance([route]);
527
+ // Verify: Success with full requested amount (since we have enough for amount + costs)
528
+ expect(results).to.have.lengthOf(1);
529
+ expect(results[0].success).to.be.true;
530
+ // Verify: transferRemote was called with full amount (costs are separate)
531
+ // Note: populateTransferRemoteTx is called multiple times:
532
+ // - First calls: gas estimation with minimal amount (1n)
533
+ // - Last call: actual transfer with requested amount
534
+ const populateParams = adapterStub.populateTransferRemoteTx.lastCall.args[0];
535
+ expect(populateParams.weiAmountOrId).to.equal(requestedAmount);
536
+ });
537
+ it('reduces transfer amount when inventory is limited', async () => {
538
+ // Setup: Native token on DESTINATION where we have less inventory than needed
539
+ const arbitrumToken = {
540
+ chainName: ARBITRUM_CHAIN,
541
+ standard: TokenStandard.EvmHypNative,
542
+ getHypAdapter: Sinon.stub().returns(adapterStub),
543
+ };
544
+ const solanaToken = {
545
+ chainName: SOLANA_CHAIN,
546
+ standard: TokenStandard.EvmHypNative,
547
+ getHypAdapter: Sinon.stub().returns(adapterStub),
548
+ };
549
+ warpCore.tokens = [arbitrumToken, solanaToken];
550
+ // Request more than we have available
551
+ const requestedAmount = 20000000000000000n; // 0.02 ETH
552
+ // Have enough for costs + partial transfer that exceeds min viable threshold
553
+ // availableInventory = TOTAL_RESERVATION + partialAmount
554
+ // where partialAmount >= MIN_VIABLE_TRANSFER (2x base cost)
555
+ const partialAmount = 7000000000000000n; // 0.007 ETH (> MIN_VIABLE_TRANSFER of ~0.006 ETH)
556
+ const availableInventory = TOTAL_RESERVATION + partialAmount;
557
+ const route = createTestRoute({ amount: requestedAmount });
558
+ createTestIntent({ amount: requestedAmount });
559
+ // Inventory on DESTINATION (solana)
560
+ // Since 100% is consolidated on destination, partial transfer should happen
561
+ inventoryRebalancer.setInventoryBalances({
562
+ [SOLANA_CHAIN]: availableInventory,
563
+ [ARBITRUM_CHAIN]: 0n,
564
+ });
565
+ // Execute
566
+ const results = await inventoryRebalancer.rebalance([route]);
567
+ // Verify: Success with reduced amount
568
+ expect(results).to.have.lengthOf(1);
569
+ expect(results[0].success).to.be.true;
570
+ // Verify: transferRemote was called with reduced amount (inventory - costs)
571
+ // Note: populateTransferRemoteTx is called multiple times:
572
+ // - First in estimateTransferRemoteGas (for calculateMaxTransferable)
573
+ // - Second in estimateTransferRemoteGas (for calculateMinViableTransfer)
574
+ // - Third in executeTransferRemote (the actual transfer)
575
+ // We check the last call which is the actual execution
576
+ const populateParams = adapterStub.populateTransferRemoteTx.lastCall.args[0];
577
+ expect(populateParams.weiAmountOrId).to.equal(partialAmount);
578
+ });
579
+ it('returns failure when inventory cannot cover costs', async () => {
580
+ // Setup: Native token on DESTINATION where inventory is less than total reservation
581
+ const arbitrumToken = {
582
+ chainName: ARBITRUM_CHAIN,
583
+ standard: TokenStandard.EvmHypNative,
584
+ getHypAdapter: Sinon.stub().returns(adapterStub),
585
+ };
586
+ const solanaToken = {
587
+ chainName: SOLANA_CHAIN,
588
+ standard: TokenStandard.EvmHypNative,
589
+ getHypAdapter: Sinon.stub().returns(adapterStub),
590
+ };
591
+ warpCore.tokens = [arbitrumToken, solanaToken];
592
+ const route = createTestRoute({ amount: 10000000000000000n });
593
+ createTestIntent({ amount: 10000000000000000n });
594
+ // Available inventory on DESTINATION (solana) is less than total reservation (IGP + gas)
595
+ // Just under the threshold, no inventory anywhere else
596
+ inventoryRebalancer.setInventoryBalances({
597
+ [SOLANA_CHAIN]: TOTAL_RESERVATION - 1n,
598
+ [ARBITRUM_CHAIN]: 0n,
599
+ });
600
+ // Execute
601
+ const results = await inventoryRebalancer.rebalance([route]);
602
+ // Verify: Failure due to insufficient funds
603
+ expect(results).to.have.lengthOf(1);
604
+ expect(results[0].success).to.be.false;
605
+ expect(results[0].error).to.include('No inventory available');
606
+ // Verify: No actual transferRemote executed (only gas estimation calls allowed)
607
+ // Note: With gas estimation, populateTransferRemoteTx IS called for estimation,
608
+ // so we can't check that it wasn't called at all. Instead, verify no action was created.
609
+ expect(actionTracker.createRebalanceAction.called).to.be.false;
610
+ });
611
+ it('does not reserve IGP for non-native tokens', async () => {
612
+ // Setup: Collateral tokens on both chains (IGP paid separately)
613
+ const arbitrumToken = {
614
+ chainName: ARBITRUM_CHAIN,
615
+ standard: TokenStandard.EvmHypCollateral, // Non-native: no IGP reservation
616
+ getHypAdapter: Sinon.stub().returns(adapterStub),
617
+ };
618
+ const solanaToken = {
619
+ chainName: SOLANA_CHAIN,
620
+ standard: TokenStandard.EvmHypCollateral, // Non-native: no IGP reservation
621
+ getHypAdapter: Sinon.stub().returns(adapterStub),
622
+ };
623
+ warpCore.tokens = [arbitrumToken, solanaToken];
624
+ const route = createTestRoute({ amount: 10000000000n });
625
+ createTestIntent({ amount: 10000000000n });
626
+ // Inventory on DESTINATION (solana)
627
+ inventoryRebalancer.setInventoryBalances({
628
+ [SOLANA_CHAIN]: 10000000000n,
629
+ [ARBITRUM_CHAIN]: 0n,
630
+ });
631
+ // Execute
632
+ const results = await inventoryRebalancer.rebalance([route]);
633
+ // Verify: Success with full amount (no IGP deduction)
634
+ expect(results).to.have.lengthOf(1);
635
+ expect(results[0].success).to.be.true;
636
+ const populateParams = adapterStub.populateTransferRemoteTx.firstCall.args[0];
637
+ expect(populateParams.weiAmountOrId).to.equal(10000000000n); // Full amount
638
+ });
639
+ });
640
+ describe('Native Token Fee Quote Reservation', () => {
641
+ // Token fee quote scenarios: tests verify that tokenFeeQuote is properly deducted
642
+ // from maxTransferable when it represents a native token fee
643
+ const GAS_LIMIT = 300000n;
644
+ const BUFFERED_GAS_LIMIT = (GAS_LIMIT * 110n) / 100n; // 10% buffer
645
+ const GAS_PRICE = 10000000000n; // 10 gwei
646
+ const BUFFERED_GAS_COST = GAS_PRICE * BUFFERED_GAS_LIMIT;
647
+ const IGP_COST = 1000000n;
648
+ const TOKEN_FEE_AMOUNT = 500000n; // Significant token fee
649
+ const TOTAL_RESERVATION = IGP_COST + BUFFERED_GAS_COST;
650
+ it('deducts tokenFeeQuote when addressOrDenom is undefined (native token)', async () => {
651
+ // Setup: Native token with tokenFeeQuote but no addressOrDenom (undefined = native)
652
+ // Override the global adapterStub to include tokenFeeQuote
653
+ adapterStub.quoteTransferRemoteGas.resolves({
654
+ igpQuote: { amount: IGP_COST },
655
+ tokenFeeQuote: { amount: TOKEN_FEE_AMOUNT }, // No addressOrDenom = native
656
+ });
657
+ const arbitrumToken = {
658
+ chainName: ARBITRUM_CHAIN,
659
+ standard: TokenStandard.EvmHypNative,
660
+ getHypAdapter: Sinon.stub().returns(adapterStub),
661
+ };
662
+ const solanaToken = {
663
+ chainName: SOLANA_CHAIN,
664
+ standard: TokenStandard.EvmHypNative,
665
+ getHypAdapter: Sinon.stub().returns(adapterStub),
666
+ };
667
+ warpCore.tokens = [arbitrumToken, solanaToken];
668
+ const requestedAmount = 10000000000000000n; // 0.01 ETH
669
+ // Available = requested + IGP + gas + tokenFee (enough for full transfer)
670
+ const availableInventory = requestedAmount + TOTAL_RESERVATION + TOKEN_FEE_AMOUNT;
671
+ const route = createTestRoute({ amount: requestedAmount });
672
+ createTestIntent({ amount: requestedAmount });
673
+ inventoryRebalancer.setInventoryBalances({
674
+ [SOLANA_CHAIN]: availableInventory,
675
+ [ARBITRUM_CHAIN]: 0n,
676
+ });
677
+ const results = await inventoryRebalancer.rebalance([route]);
678
+ expect(results).to.have.lengthOf(1);
679
+ expect(results[0].success).to.be.true;
680
+ // Verify tokenFee was deducted from maxTransferable
681
+ const populateParams = adapterStub.populateTransferRemoteTx.lastCall.args[0];
682
+ expect(populateParams.weiAmountOrId).to.equal(requestedAmount);
683
+ });
684
+ it('deducts tokenFeeQuote when addressOrDenom is zero address', async () => {
685
+ // Setup: Native token with tokenFeeQuote and zero-address denom (isZeroishAddress)
686
+ const zeroAddressAdapter = {
687
+ quoteTransferRemoteGas: Sinon.stub().resolves({
688
+ igpQuote: { amount: IGP_COST },
689
+ tokenFeeQuote: {
690
+ addressOrDenom: '0x0000000000000000000000000000000000000000',
691
+ amount: TOKEN_FEE_AMOUNT,
692
+ },
693
+ }),
694
+ populateTransferRemoteTx: Sinon.stub().resolves({
695
+ to: '0xRouterAddress',
696
+ data: '0xTransferRemoteData',
697
+ value: 1000000n,
698
+ }),
699
+ };
700
+ const arbitrumToken = {
701
+ chainName: ARBITRUM_CHAIN,
702
+ standard: TokenStandard.EvmHypNative,
703
+ getHypAdapter: Sinon.stub().returns(zeroAddressAdapter),
704
+ };
705
+ const solanaToken = {
706
+ chainName: SOLANA_CHAIN,
707
+ standard: TokenStandard.EvmHypNative,
708
+ getHypAdapter: Sinon.stub().returns(zeroAddressAdapter),
709
+ };
710
+ warpCore.tokens = [arbitrumToken, solanaToken];
711
+ const requestedAmount = 10000000000000000n;
712
+ const availableInventory = requestedAmount + TOTAL_RESERVATION + TOKEN_FEE_AMOUNT;
713
+ const route = createTestRoute({ amount: requestedAmount });
714
+ createTestIntent({ amount: requestedAmount });
715
+ inventoryRebalancer.setInventoryBalances({
716
+ [SOLANA_CHAIN]: availableInventory,
717
+ [ARBITRUM_CHAIN]: 0n,
718
+ });
719
+ const results = await inventoryRebalancer.rebalance([route]);
720
+ expect(results).to.have.lengthOf(1);
721
+ expect(results[0].success).to.be.true;
722
+ // Verify tokenFee was deducted (zero address is treated as native)
723
+ const populateParams = zeroAddressAdapter.populateTransferRemoteTx.lastCall.args[0];
724
+ expect(populateParams.weiAmountOrId).to.equal(requestedAmount);
725
+ });
726
+ it('does NOT deduct tokenFeeQuote when addressOrDenom is ERC20 address', async () => {
727
+ // Setup: Native token with tokenFeeQuote but ERC20 denom (not native)
728
+ const erc20Address = '0x1234567890abcdef1234567890abcdef12345678';
729
+ const erc20Adapter = {
730
+ quoteTransferRemoteGas: Sinon.stub().resolves({
731
+ igpQuote: { amount: IGP_COST },
732
+ tokenFeeQuote: {
733
+ addressOrDenom: erc20Address,
734
+ amount: TOKEN_FEE_AMOUNT,
735
+ },
736
+ }),
737
+ populateTransferRemoteTx: Sinon.stub().resolves({
738
+ to: '0xRouterAddress',
739
+ data: '0xTransferRemoteData',
740
+ value: 1000000n,
741
+ }),
742
+ };
743
+ const arbitrumToken = {
744
+ chainName: ARBITRUM_CHAIN,
745
+ standard: TokenStandard.EvmHypNative,
746
+ getHypAdapter: Sinon.stub().returns(erc20Adapter),
747
+ };
748
+ const solanaToken = {
749
+ chainName: SOLANA_CHAIN,
750
+ standard: TokenStandard.EvmHypNative,
751
+ getHypAdapter: Sinon.stub().returns(erc20Adapter),
752
+ };
753
+ warpCore.tokens = [arbitrumToken, solanaToken];
754
+ const requestedAmount = 10000000000000000n;
755
+ // Available = requested + IGP + gas (NO tokenFee deduction since it's ERC20)
756
+ const availableInventory = requestedAmount + TOTAL_RESERVATION;
757
+ const route = createTestRoute({ amount: requestedAmount });
758
+ createTestIntent({ amount: requestedAmount });
759
+ inventoryRebalancer.setInventoryBalances({
760
+ [SOLANA_CHAIN]: availableInventory,
761
+ [ARBITRUM_CHAIN]: 0n,
762
+ });
763
+ const results = await inventoryRebalancer.rebalance([route]);
764
+ expect(results).to.have.lengthOf(1);
765
+ expect(results[0].success).to.be.true;
766
+ // Verify tokenFee was NOT deducted (ERC20 denom, not native)
767
+ const populateParams = erc20Adapter.populateTransferRemoteTx.lastCall.args[0];
768
+ expect(populateParams.weiAmountOrId).to.equal(requestedAmount);
769
+ });
770
+ it('handles undefined tokenFeeQuote (backward compatibility with v<10)', async () => {
771
+ // Setup: Native token without tokenFeeQuote (old contract version)
772
+ const oldAdapter = {
773
+ quoteTransferRemoteGas: Sinon.stub().resolves({
774
+ igpQuote: { amount: IGP_COST },
775
+ // No tokenFeeQuote field
776
+ }),
777
+ populateTransferRemoteTx: Sinon.stub().resolves({
778
+ to: '0xRouterAddress',
779
+ data: '0xTransferRemoteData',
780
+ value: 1000000n,
781
+ }),
782
+ };
783
+ const arbitrumToken = {
784
+ chainName: ARBITRUM_CHAIN,
785
+ standard: TokenStandard.EvmHypNative,
786
+ getHypAdapter: Sinon.stub().returns(oldAdapter),
787
+ };
788
+ const solanaToken = {
789
+ chainName: SOLANA_CHAIN,
790
+ standard: TokenStandard.EvmHypNative,
791
+ getHypAdapter: Sinon.stub().returns(oldAdapter),
792
+ };
793
+ warpCore.tokens = [arbitrumToken, solanaToken];
794
+ const requestedAmount = 10000000000000000n;
795
+ // Available = requested + IGP + gas (no tokenFee since undefined)
796
+ const availableInventory = requestedAmount + TOTAL_RESERVATION;
797
+ const route = createTestRoute({ amount: requestedAmount });
798
+ createTestIntent({ amount: requestedAmount });
799
+ inventoryRebalancer.setInventoryBalances({
800
+ [SOLANA_CHAIN]: availableInventory,
801
+ [ARBITRUM_CHAIN]: 0n,
802
+ });
803
+ const results = await inventoryRebalancer.rebalance([route]);
804
+ expect(results).to.have.lengthOf(1);
805
+ expect(results[0].success).to.be.true;
806
+ // Verify transfer succeeded without tokenFee deduction
807
+ const populateParams = oldAdapter.populateTransferRemoteTx.lastCall.args[0];
808
+ expect(populateParams.weiAmountOrId).to.equal(requestedAmount);
809
+ });
810
+ it('reduces maxTransferable when large tokenFeeQuote is present', async () => {
811
+ // Setup: Native token with large tokenFeeQuote that significantly reduces maxTransferable
812
+ adapterStub.quoteTransferRemoteGas.resolves({
813
+ igpQuote: { amount: IGP_COST },
814
+ tokenFeeQuote: { amount: 5000000000000000n }, // Large fee (5e15 wei)
815
+ });
816
+ const arbitrumToken = {
817
+ chainName: ARBITRUM_CHAIN,
818
+ standard: TokenStandard.EvmHypNative,
819
+ getHypAdapter: Sinon.stub().returns(adapterStub),
820
+ };
821
+ const solanaToken = {
822
+ chainName: SOLANA_CHAIN,
823
+ standard: TokenStandard.EvmHypNative,
824
+ getHypAdapter: Sinon.stub().returns(adapterStub),
825
+ };
826
+ warpCore.tokens = [arbitrumToken, solanaToken];
827
+ const requestedAmount = 10000000000000000n; // 0.01 ETH
828
+ // Available = requested + IGP + gas + large tokenFee
829
+ const largeTokenFee = 5000000000000000n;
830
+ const availableInventory = requestedAmount + TOTAL_RESERVATION + largeTokenFee;
831
+ const route = createTestRoute({ amount: requestedAmount });
832
+ createTestIntent({ amount: requestedAmount });
833
+ inventoryRebalancer.setInventoryBalances({
834
+ [SOLANA_CHAIN]: availableInventory,
835
+ [ARBITRUM_CHAIN]: 0n,
836
+ });
837
+ const results = await inventoryRebalancer.rebalance([route]);
838
+ // Verify: Transfer succeeds with full amount (we have enough for amount + all costs)
839
+ // This demonstrates that tokenFeeQuote is properly deducted from maxTransferable
840
+ expect(results).to.have.lengthOf(1);
841
+ expect(results[0].success).to.be.true;
842
+ });
843
+ });
844
+ describe('LiFi Bridge Integration (for future inventory_movement)', () => {
845
+ // These tests validate the mock utilities are working correctly
846
+ // and prepare for when inventory_movement is implemented
847
+ it('mock utilities create valid quote', () => {
848
+ const quote = createMockBridgeQuote({
849
+ fromAmount: 10000000000n,
850
+ toAmount: 9950000000n,
851
+ });
852
+ expect(quote.id).to.equal('quote-123');
853
+ expect(quote.tool).to.equal('across');
854
+ expect(quote.fromAmount).to.equal(10000000000n);
855
+ expect(quote.toAmount).to.equal(9950000000n);
856
+ });
857
+ it('bridge mock can be configured for quote', async () => {
858
+ bridge.quote.resolves(createMockBridgeQuote({
859
+ fromAmount: 10000000000n,
860
+ toAmount: 9950000000n,
861
+ }));
862
+ const quote = await bridge.quote({
863
+ fromChain: ARBITRUM_DOMAIN,
864
+ toChain: SOLANA_DOMAIN,
865
+ fromToken: '0xUSDC',
866
+ toToken: '0xUSDC',
867
+ fromAmount: 10000000000n,
868
+ fromAddress: INVENTORY_SIGNER,
869
+ });
870
+ expect(quote.fromAmount).to.equal(10000000000n);
871
+ expect(quote.toAmount).to.equal(9950000000n);
872
+ });
873
+ });
874
+ describe('Smart Partial Transfer Threshold', () => {
875
+ // Test the 90% consolidation threshold for partial transfers
876
+ // Tests use non-native tokens (EvmHypCollateral) so minViableTransfer = 0
877
+ it('does partial transfer when inventory is available on destination', async () => {
878
+ // amount = 1 ETH, availableOnDestination = 0.5 ETH
879
+ // With simplified logic: if maxTransferable >= minViableTransfer, do partial transfer
880
+ // For non-native tokens, minViableTransfer = 0, so partial transfer happens
881
+ const amount = BigInt(1e18); // 1 ETH
882
+ const availableOnDestination = BigInt(0.5e18); // 0.5 ETH on destination
883
+ const route = createTestRoute({ amount });
884
+ createTestIntent({ amount });
885
+ // Inventory on destination (SOLANA)
886
+ inventoryRebalancer.setInventoryBalances({
887
+ [SOLANA_CHAIN]: availableOnDestination,
888
+ [ARBITRUM_CHAIN]: 0n,
889
+ });
890
+ const results = await inventoryRebalancer.rebalance([route]);
891
+ expect(results).to.have.lengthOf(1);
892
+ expect(results[0].success).to.be.true;
893
+ // Verify: transferRemote WAS called (partial transfer happened)
894
+ const populateParams = adapterStub.populateTransferRemoteTx.lastCall.args[0];
895
+ expect(populateParams.weiAmountOrId).to.equal(availableOnDestination);
896
+ // Verify: Bridge was NOT called (no need to bridge when partial transfer is viable)
897
+ expect(bridge.execute.called).to.be.false;
898
+ });
899
+ it('does partial transfer when maxTransferable >= minViableTransfer', async () => {
900
+ // For non-native tokens (EvmHypCollateral), minViableTransfer = 0
901
+ // So any positive maxTransferable triggers partial transfer
902
+ const amount = BigInt(2e18); // 2 ETH requested
903
+ const maxTransferable = BigInt(0.6e18); // 0.6 ETH available
904
+ const route = createTestRoute({ amount });
905
+ createTestIntent({ amount });
906
+ // 0.6 ETH available on destination
907
+ inventoryRebalancer.setInventoryBalances({
908
+ [SOLANA_CHAIN]: maxTransferable,
909
+ [ARBITRUM_CHAIN]: 0n,
910
+ });
911
+ const results = await inventoryRebalancer.rebalance([route]);
912
+ expect(results).to.have.lengthOf(1);
913
+ expect(results[0].success).to.be.true;
914
+ // Verify: transferRemote WAS called with partial amount
915
+ const populateParams = adapterStub.populateTransferRemoteTx.lastCall.args[0];
916
+ expect(populateParams.weiAmountOrId).to.equal(maxTransferable);
917
+ });
918
+ it('does NOT do partial transfer when maxTransferable < minViableTransfer (native tokens)', async () => {
919
+ // For native tokens (EvmHypNative), minViableTransfer = totalCost * 2
920
+ // When available inventory minus costs is below minViableTransfer, falls through to bridging
921
+ const arbitrumToken = {
922
+ chainName: ARBITRUM_CHAIN,
923
+ standard: TokenStandard.EvmHypNative,
924
+ getHypAdapter: Sinon.stub().returns(adapterStub),
925
+ };
926
+ const solanaToken = {
927
+ chainName: SOLANA_CHAIN,
928
+ standard: TokenStandard.EvmHypNative,
929
+ getHypAdapter: Sinon.stub().returns(adapterStub),
930
+ };
931
+ warpCore.tokens = [arbitrumToken, solanaToken];
932
+ const amount = BigInt(2e18); // 2 ETH
933
+ // Available inventory is small - after subtracting costs, maxTransferable < minViableTransfer
934
+ // minViableTransfer = ~0.0066 ETH (totalCost * 2)
935
+ // If available = 0.005 ETH, after costs ~0, maxTransferable < minViableTransfer
936
+ const availableOnDestination = BigInt(0.003e18); // 0.003 ETH - below minViableTransfer
937
+ const availableOnSource = BigInt(0.6e18); // 0.6 ETH on ARBITRUM
938
+ const route = createTestRoute({ amount });
939
+ createTestIntent({ amount });
940
+ // Inventory on destination - too small after costs, but source has inventory
941
+ inventoryRebalancer.setInventoryBalances({
942
+ [SOLANA_CHAIN]: availableOnDestination,
943
+ [ARBITRUM_CHAIN]: availableOnSource,
944
+ });
945
+ // Mock bridge
946
+ bridge.quote.resolves(createMockBridgeQuote({
947
+ fromAmount: BigInt(0.5e18),
948
+ toAmount: BigInt(0.48e18),
949
+ }));
950
+ bridge.execute.resolves({
951
+ txHash: '0xBridgeTxHash',
952
+ fromChain: 42161,
953
+ toChain: 1399811149,
954
+ });
955
+ const results = await inventoryRebalancer.rebalance([route]);
956
+ expect(results).to.have.lengthOf(1);
957
+ expect(results[0].success).to.be.true;
958
+ // Verify: inventory movement via bridge happened (NOT partial transferRemote)
959
+ expect(bridge.execute.called).to.be.true;
960
+ });
961
+ });
962
+ describe('Early Exit for Small Amounts', () => {
963
+ // Gas estimation: 300,000 gas × 10 gwei = 3,000,000,000,000 wei
964
+ // Buffered gas limit (10%): 330,000 gas
965
+ // Buffered gas cost: 330,000 × 10 gwei = 3,300,000,000,000 wei
966
+ // IGP quote: 1,000,000 wei
967
+ // Total cost: IGP + buffered gas = 3,300,001,000,000 wei (~0.0033 ETH)
968
+ // Min viable transfer (2x total cost): ~0.0066 ETH
969
+ const MIN_VIABLE = BigInt(6.6e12); // ~0.0066 ETH
970
+ it('completes intent when amount < minViableTransfer', async () => {
971
+ // Use native token to get non-zero minViableTransfer
972
+ const arbitrumToken = {
973
+ chainName: ARBITRUM_CHAIN,
974
+ standard: TokenStandard.EvmHypNative,
975
+ getHypAdapter: Sinon.stub().returns(adapterStub),
976
+ };
977
+ const solanaToken = {
978
+ chainName: SOLANA_CHAIN,
979
+ standard: TokenStandard.EvmHypNative,
980
+ getHypAdapter: Sinon.stub().returns(adapterStub),
981
+ };
982
+ warpCore.tokens = [arbitrumToken, solanaToken];
983
+ // Amount smaller than minViableTransfer
984
+ const smallAmount = MIN_VIABLE / 2n; // 0.0033 ETH < minViable 0.0066 ETH
985
+ const route = createTestRoute({ amount: smallAmount });
986
+ createTestIntent({ amount: smallAmount });
987
+ // Even with plenty of inventory, small amount triggers early exit
988
+ inventoryRebalancer.setInventoryBalances({
989
+ [SOLANA_CHAIN]: BigInt(10e18),
990
+ [ARBITRUM_CHAIN]: BigInt(10e18),
991
+ });
992
+ const results = await inventoryRebalancer.rebalance([route]);
993
+ expect(results).to.have.lengthOf(1);
994
+ expect(results[0].success).to.be.true;
995
+ expect(results[0].reason).to.equal('completed_with_acceptable_loss');
996
+ // Verify: Intent was completed (not left in progress)
997
+ expect(actionTracker.completeRebalanceIntent.calledOnce).to.be.true;
998
+ expect(actionTracker.completeRebalanceIntent.calledWith('intent-1')).to.be
999
+ .true;
1000
+ // Verify: No transferRemote was attempted
1001
+ expect(actionTracker.createRebalanceAction.called).to.be.false;
1002
+ });
1003
+ });
1004
+ describe('Parallel Multi-Source Bridging', () => {
1005
+ // Third chain for multi-source tests
1006
+ const BASE_CHAIN = 'base';
1007
+ const BASE_DOMAIN = 8453;
1008
+ beforeEach(() => {
1009
+ // Extend multiProvider to handle BASE_CHAIN
1010
+ multiProvider.getDomainId.callsFake((chain) => {
1011
+ if (chain === ARBITRUM_CHAIN)
1012
+ return ARBITRUM_DOMAIN;
1013
+ if (chain === SOLANA_CHAIN)
1014
+ return SOLANA_DOMAIN;
1015
+ if (chain === BASE_CHAIN)
1016
+ return BASE_DOMAIN;
1017
+ return 0;
1018
+ });
1019
+ multiProvider.getChainId = Sinon.stub().callsFake((chain) => {
1020
+ if (chain === ARBITRUM_CHAIN)
1021
+ return 42161;
1022
+ if (chain === SOLANA_CHAIN)
1023
+ return 1399811149;
1024
+ if (chain === BASE_CHAIN)
1025
+ return 8453;
1026
+ return 0;
1027
+ });
1028
+ // Add token for BASE_CHAIN
1029
+ const baseToken = {
1030
+ chainName: BASE_CHAIN,
1031
+ standard: TokenStandard.EvmHypCollateral,
1032
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1033
+ addressOrDenom: '0xBaseToken',
1034
+ };
1035
+ warpCore.tokens.push(baseToken);
1036
+ multiProvider.getSigner = Sinon.stub().returns(TEST_WALLET);
1037
+ });
1038
+ it('bridges from multiple sources in parallel', async () => {
1039
+ // Need 1 ETH on solana, have 0.6 ETH on arbitrum and 0.6 ETH on base
1040
+ const amount = BigInt(1e18);
1041
+ const perChainInventory = BigInt(0.6e18);
1042
+ const route = createTestRoute({ amount });
1043
+ createTestIntent({ amount });
1044
+ // No inventory on destination, inventory on sources
1045
+ inventoryRebalancer.setInventoryBalances({
1046
+ [SOLANA_CHAIN]: 0n,
1047
+ [ARBITRUM_CHAIN]: perChainInventory,
1048
+ [BASE_CHAIN]: perChainInventory,
1049
+ });
1050
+ // Mock bridge quotes and execution
1051
+ bridge.quote.resolves(createMockBridgeQuote({
1052
+ fromAmount: BigInt(0.55e18),
1053
+ toAmount: BigInt(0.525e18),
1054
+ }));
1055
+ bridge.execute.resolves({
1056
+ txHash: '0xBridgeTxHash',
1057
+ fromChain: 42161,
1058
+ toChain: 1399811149,
1059
+ });
1060
+ const results = await inventoryRebalancer.rebalance([route]);
1061
+ expect(results).to.have.lengthOf(1);
1062
+ expect(results[0].success).to.be.true;
1063
+ // Verify: Bridge was called twice (once for each source)
1064
+ expect(bridge.execute.callCount).to.equal(2);
1065
+ });
1066
+ it('applies 5% buffer to total bridge amount', async () => {
1067
+ // Need 1 ETH -> should plan to bridge 1.05 ETH total (with 5% buffer)
1068
+ const amount = BigInt(1e18); // 1 ETH
1069
+ const availableInventory = BigInt(2e18); // 2 ETH on source
1070
+ const route = createTestRoute({ amount });
1071
+ createTestIntent({ amount });
1072
+ // No inventory on destination, plenty on source
1073
+ inventoryRebalancer.setInventoryBalances({
1074
+ [SOLANA_CHAIN]: 0n,
1075
+ [ARBITRUM_CHAIN]: availableInventory,
1076
+ });
1077
+ // Capture the quote amount from executeInventoryMovement
1078
+ // (calculateMaxViableBridgeAmount doesn't quote for ERC20 tokens)
1079
+ let quotedFromAmount;
1080
+ bridge.quote.callsFake(async (params) => {
1081
+ quotedFromAmount = params.fromAmount;
1082
+ return createMockBridgeQuote({
1083
+ fromAmount: params.fromAmount ?? params.toAmount,
1084
+ toAmount: params.fromAmount ?? params.toAmount,
1085
+ });
1086
+ });
1087
+ bridge.execute.resolves({
1088
+ txHash: '0xBridgeTxHash',
1089
+ fromChain: 42161,
1090
+ toChain: 1399811149,
1091
+ });
1092
+ await inventoryRebalancer.rebalance([route]);
1093
+ // Verify: 5% buffer applied (1 ETH * 1.05 = 1.05 ETH)
1094
+ // The bridge plan uses pre-validated amounts (for ERC20, full inventory available)
1095
+ // But the target is (amount * 105%), so if source has >= target, we bridge exactly target
1096
+ const expectedWithBuffer = (amount * 105n) / 100n;
1097
+ expect(quotedFromAmount).to.equal(expectedWithBuffer);
1098
+ });
1099
+ it('continues when some bridges fail', async () => {
1100
+ const amount = BigInt(1e18);
1101
+ const perChainInventory = BigInt(0.6e18);
1102
+ const route = createTestRoute({ amount });
1103
+ createTestIntent({ amount });
1104
+ // No inventory on destination, inventory on sources
1105
+ inventoryRebalancer.setInventoryBalances({
1106
+ [SOLANA_CHAIN]: 0n,
1107
+ [ARBITRUM_CHAIN]: perChainInventory,
1108
+ [BASE_CHAIN]: perChainInventory,
1109
+ });
1110
+ bridge.quote.resolves(createMockBridgeQuote({
1111
+ fromAmount: BigInt(0.55e18),
1112
+ toAmount: BigInt(0.525e18),
1113
+ }));
1114
+ // First bridge succeeds, second fails
1115
+ bridge.execute
1116
+ .onFirstCall()
1117
+ .resolves({
1118
+ txHash: '0xSuccessTxHash',
1119
+ fromChain: 42161,
1120
+ toChain: 1399811149,
1121
+ })
1122
+ .onSecondCall()
1123
+ .rejects(new Error('Bridge execution failed'));
1124
+ const results = await inventoryRebalancer.rebalance([route]);
1125
+ // Verify: Overall success (at least one bridge succeeded)
1126
+ expect(results).to.have.lengthOf(1);
1127
+ expect(results[0].success).to.be.true;
1128
+ });
1129
+ it('returns failure when all bridges fail', async () => {
1130
+ const amount = BigInt(1e18);
1131
+ const perChainInventory = BigInt(0.6e18);
1132
+ const route = createTestRoute({ amount });
1133
+ createTestIntent({ amount });
1134
+ // No inventory on destination, inventory on sources
1135
+ inventoryRebalancer.setInventoryBalances({
1136
+ [SOLANA_CHAIN]: 0n,
1137
+ [ARBITRUM_CHAIN]: perChainInventory,
1138
+ [BASE_CHAIN]: perChainInventory,
1139
+ });
1140
+ bridge.quote.resolves(createMockBridgeQuote({
1141
+ fromAmount: BigInt(0.55e18),
1142
+ toAmount: BigInt(0.525e18),
1143
+ }));
1144
+ // All bridges fail
1145
+ bridge.execute.rejects(new Error('Bridge execution failed'));
1146
+ const results = await inventoryRebalancer.rebalance([route]);
1147
+ // Verify: Failure when all bridges fail
1148
+ expect(results).to.have.lengthOf(1);
1149
+ expect(results[0].success).to.be.false;
1150
+ expect(results[0].error).to.include('All inventory movements failed');
1151
+ });
1152
+ });
1153
+ describe('Bridge Viability Check', () => {
1154
+ // Tests for the gas-aware planning approach that prevents "insufficient funds for gas * price + value" errors
1155
+ // by calculating max viable bridge amounts BEFORE creating bridge plans.
1156
+ // Uses calculateMaxViableBridgeAmount which:
1157
+ // 1. Gets a quote to determine gas costs
1158
+ // 2. Applies 20x multiplier on quoted gas (LiFi underestimates)
1159
+ // 3. Returns 0 if gas exceeds 10% of inventory (not economically viable)
1160
+ it('filters out sources where gas cost exceeds 10% of inventory', async () => {
1161
+ // Setup: Native token bridge where gas cost is too high relative to balance
1162
+ // Scenario: Arbitrum has 0.00219 ETH, estimated gas (with 20x buffer) exceeds 10% threshold
1163
+ const arbitrumToken = {
1164
+ chainName: ARBITRUM_CHAIN,
1165
+ standard: TokenStandard.EvmHypNative, // Native token for gas check
1166
+ addressOrDenom: '0xArbitrumNative',
1167
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1168
+ };
1169
+ const solanaToken = {
1170
+ chainName: SOLANA_CHAIN,
1171
+ standard: TokenStandard.EvmHypNative,
1172
+ addressOrDenom: '0xSolanaNative',
1173
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1174
+ };
1175
+ warpCore.tokens = [arbitrumToken, solanaToken];
1176
+ const amount = BigInt(1e18); // 1 ETH requested on destination
1177
+ const route = createTestRoute({ amount });
1178
+ createTestIntent({ amount });
1179
+ // Raw balance on source chain (ARBITRUM) - the limiting factor
1180
+ const rawBalance = BigInt('2194632084196208'); // ~0.00219 ETH
1181
+ // No inventory on destination, low balance on source
1182
+ inventoryRebalancer.setInventoryBalances({
1183
+ [SOLANA_CHAIN]: 0n,
1184
+ [ARBITRUM_CHAIN]: rawBalance,
1185
+ });
1186
+ // Mock quote with gas costs that exceed 10% threshold when multiplied by 20
1187
+ // rawBalance = 0.00219 ETH
1188
+ // 10% threshold = 0.000219 ETH
1189
+ // gasCosts = 0.00005 ETH, estimated = 0.001 ETH (20x multiplier)
1190
+ // 0.001 > 0.000219 → not viable
1191
+ bridge.quote.resolves(createMockBridgeQuote({
1192
+ fromAmount: rawBalance,
1193
+ toAmount: rawBalance - BigInt(1e14), // Some output
1194
+ gasCosts: BigInt('50000000000000'), // 0.00005 ETH, becomes 0.001 ETH with 20x
1195
+ feeCosts: 0n,
1196
+ }));
1197
+ const results = await inventoryRebalancer.rebalance([route]);
1198
+ // Verify: Should fail because no sources pass viability check at planning time
1199
+ expect(results).to.have.lengthOf(1);
1200
+ expect(results[0].success).to.be.false;
1201
+ // New behavior: Error is "No viable bridge sources" (filtered at planning time)
1202
+ // rather than "Insufficient funds" (which was at execution time)
1203
+ expect(results[0].error).to.include('No viable bridge sources');
1204
+ // Verify: Bridge.execute should NOT have been called (filtered during planning)
1205
+ expect(bridge.execute.called).to.be.false;
1206
+ });
1207
+ it('proceeds with bridge when total cost is within available balance', async () => {
1208
+ // Setup: Native token bridge where we have enough balance
1209
+ const arbitrumToken = {
1210
+ chainName: ARBITRUM_CHAIN,
1211
+ standard: TokenStandard.EvmHypNative,
1212
+ addressOrDenom: '0xArbitrumNative',
1213
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1214
+ };
1215
+ const solanaToken = {
1216
+ chainName: SOLANA_CHAIN,
1217
+ standard: TokenStandard.EvmHypNative,
1218
+ addressOrDenom: '0xSolanaNative',
1219
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1220
+ };
1221
+ warpCore.tokens = [arbitrumToken, solanaToken];
1222
+ const amount = BigInt(1e18); // 1 ETH requested
1223
+ const route = createTestRoute({ amount });
1224
+ createTestIntent({ amount });
1225
+ // Plenty of balance on source chain
1226
+ const rawBalance = BigInt(2e18); // 2 ETH - more than enough
1227
+ inventoryRebalancer.setInventoryBalances({
1228
+ [SOLANA_CHAIN]: 0n,
1229
+ [ARBITRUM_CHAIN]: rawBalance,
1230
+ });
1231
+ // Quote with reasonable costs well under the balance
1232
+ bridge.quote.resolves(createMockBridgeQuote({
1233
+ fromAmount: BigInt(1.05e18), // 1.05 ETH (with buffer)
1234
+ toAmount: BigInt(1e18),
1235
+ gasCosts: BigInt(1e15), // 0.001 ETH gas
1236
+ feeCosts: 0n,
1237
+ }));
1238
+ // Mock successful execution
1239
+ bridge.execute.resolves({
1240
+ txHash: '0xSuccessBridgeTxHash',
1241
+ fromChain: 42161,
1242
+ toChain: 1399811149,
1243
+ });
1244
+ const results = await inventoryRebalancer.rebalance([route]);
1245
+ // Verify: Should succeed
1246
+ expect(results).to.have.lengthOf(1);
1247
+ expect(results[0].success).to.be.true;
1248
+ // Verify: Bridge.execute WAS called (not abandoned)
1249
+ expect(bridge.execute.calledOnce).to.be.true;
1250
+ });
1251
+ it('viability check only applies to native tokens (not ERC20)', async () => {
1252
+ // Setup: ERC20 token bridge - gas is paid separately in ETH, not from token balance
1253
+ const arbitrumToken = {
1254
+ chainName: ARBITRUM_CHAIN,
1255
+ standard: TokenStandard.EvmHypCollateral, // ERC20, not native
1256
+ addressOrDenom: '0xArbitrumToken',
1257
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1258
+ };
1259
+ const solanaToken = {
1260
+ chainName: SOLANA_CHAIN,
1261
+ standard: TokenStandard.EvmHypCollateral,
1262
+ addressOrDenom: '0xSolanaToken',
1263
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1264
+ };
1265
+ warpCore.tokens = [arbitrumToken, solanaToken];
1266
+ const amount = BigInt(1e18); // 1 token
1267
+ const route = createTestRoute({ amount });
1268
+ createTestIntent({ amount });
1269
+ // Small token balance (but gas is paid in ETH, so this shouldn't matter for viability)
1270
+ const tokenBalance = BigInt(1e18);
1271
+ inventoryRebalancer.setInventoryBalances({
1272
+ [SOLANA_CHAIN]: 0n,
1273
+ [ARBITRUM_CHAIN]: tokenBalance,
1274
+ });
1275
+ // Quote with high gas costs (but for ERC20, this shouldn't trigger viability check)
1276
+ bridge.quote.resolves(createMockBridgeQuote({
1277
+ fromAmount: BigInt(1.05e18),
1278
+ toAmount: BigInt(1e18),
1279
+ gasCosts: BigInt(1e18), // Very high gas (would fail viability if this were native)
1280
+ feeCosts: 0n,
1281
+ }));
1282
+ bridge.execute.resolves({
1283
+ txHash: '0xERC20BridgeTxHash',
1284
+ fromChain: 42161,
1285
+ toChain: 1399811149,
1286
+ });
1287
+ const results = await inventoryRebalancer.rebalance([route]);
1288
+ // Verify: Should succeed because ERC20 viability check doesn't include gasCosts
1289
+ expect(results).to.have.lengthOf(1);
1290
+ expect(results[0].success).to.be.true;
1291
+ expect(bridge.execute.calledOnce).to.be.true;
1292
+ });
1293
+ it('calculates max viable amount as inventory minus (gasCosts * 20) for native tokens', async () => {
1294
+ // Setup: Native token with enough balance for viable bridge
1295
+ const arbitrumToken = {
1296
+ chainName: ARBITRUM_CHAIN,
1297
+ standard: TokenStandard.EvmHypNative,
1298
+ addressOrDenom: '0xArbitrumNative',
1299
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1300
+ };
1301
+ const solanaToken = {
1302
+ chainName: SOLANA_CHAIN,
1303
+ standard: TokenStandard.EvmHypNative,
1304
+ addressOrDenom: '0xSolanaNative',
1305
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1306
+ };
1307
+ warpCore.tokens = [arbitrumToken, solanaToken];
1308
+ const amount = BigInt(0.5e18); // 0.5 ETH requested
1309
+ const route = createTestRoute({ amount });
1310
+ createTestIntent({ amount });
1311
+ // Large balance - should be viable
1312
+ const rawBalance = BigInt(1e18); // 1 ETH
1313
+ inventoryRebalancer.setInventoryBalances({
1314
+ [SOLANA_CHAIN]: 0n,
1315
+ [ARBITRUM_CHAIN]: rawBalance,
1316
+ });
1317
+ // gasCosts = 0.001 ETH, estimated = 0.02 ETH (20x multiplier)
1318
+ // maxViable = 1 ETH - 0.02 ETH = 0.98 ETH
1319
+ // 10% threshold = 0.1 ETH, estimatedGas (0.02) < threshold → viable
1320
+ const gasCosts = BigInt(0.001e18); // 0.001 ETH
1321
+ bridge.quote.resolves(createMockBridgeQuote({
1322
+ fromAmount: rawBalance,
1323
+ toAmount: rawBalance - BigInt(1e15),
1324
+ gasCosts,
1325
+ feeCosts: 0n,
1326
+ }));
1327
+ bridge.execute.resolves({
1328
+ txHash: '0xSuccessBridgeTxHash',
1329
+ fromChain: 42161,
1330
+ toChain: 1399811149,
1331
+ });
1332
+ const results = await inventoryRebalancer.rebalance([route]);
1333
+ // Verify: Should succeed - bridge is viable
1334
+ expect(results).to.have.lengthOf(1);
1335
+ expect(results[0].success).to.be.true;
1336
+ expect(bridge.execute.calledOnce).to.be.true;
1337
+ // Verify: The quoted fromAmount should be the target (since maxViable > target)
1338
+ // For the execution quote (second quote call):
1339
+ // targetWithBuffer = (0.5 ETH) * 1.05 = 0.525 ETH (for non-inventory execution, costs are 0)
1340
+ const executionQuoteCall = bridge.quote
1341
+ .getCalls()
1342
+ .find((call) => call.args[0].fromAmount !== undefined &&
1343
+ call.args[0].fromAmount !== rawBalance);
1344
+ // Since maxViable (0.98 ETH) > targetWithBuffer (0.525 ETH), we bridge exactly targetWithBuffer
1345
+ expect(executionQuoteCall).to.exist;
1346
+ });
1347
+ it('handles quote failures gracefully by skipping the source chain', async () => {
1348
+ // Setup: Native token where quote fails
1349
+ const arbitrumToken = {
1350
+ chainName: ARBITRUM_CHAIN,
1351
+ standard: TokenStandard.EvmHypNative,
1352
+ addressOrDenom: '0xArbitrumNative',
1353
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1354
+ };
1355
+ const solanaToken = {
1356
+ chainName: SOLANA_CHAIN,
1357
+ standard: TokenStandard.EvmHypNative,
1358
+ addressOrDenom: '0xSolanaNative',
1359
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1360
+ };
1361
+ warpCore.tokens = [arbitrumToken, solanaToken];
1362
+ const amount = BigInt(1e18);
1363
+ const route = createTestRoute({ amount });
1364
+ createTestIntent({ amount });
1365
+ const rawBalance = BigInt(2e18); // 2 ETH
1366
+ inventoryRebalancer.setInventoryBalances({
1367
+ [SOLANA_CHAIN]: 0n,
1368
+ [ARBITRUM_CHAIN]: rawBalance,
1369
+ });
1370
+ // Quote fails for viability check
1371
+ bridge.quote.rejects(new Error('LiFi API timeout'));
1372
+ const results = await inventoryRebalancer.rebalance([route]);
1373
+ // Verify: Should fail because quote error means no viable sources
1374
+ expect(results).to.have.lengthOf(1);
1375
+ expect(results[0].success).to.be.false;
1376
+ expect(results[0].error).to.include('No viable bridge sources');
1377
+ // Verify: Bridge.execute should NOT have been called
1378
+ expect(bridge.execute.called).to.be.false;
1379
+ });
1380
+ });
1381
+ });
1382
+ //# sourceMappingURL=InventoryRebalancer.test.js.map