Skip to content

Commit d965433

Browse files
authored
fix: smart router for ambient eth (#900)
1 parent 4e8550e commit d965433

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

.changeset/healthy-spoons-allow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sovryn/sdk': patch
3+
---
4+
5+
fix: ambient smart router

packages/sdk/src/_tests/swaps/smart-router.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('SmartRouter', () => {
9191
});
9292

9393
it('get cheapest swap route for DLLR -> BTC', async () => {
94-
const amount = ethers.utils.parseEther('20');
94+
const amount = ethers.utils.parseEther('0.01');
9595
await expect(
9696
router.getBestQuote(chainId, dllr, btc, amount),
9797
).resolves.toMatchObject({
@@ -101,7 +101,7 @@ describe('SmartRouter', () => {
101101
});
102102

103103
it('get cheapest swap route for BTC -> DLLR', async () => {
104-
const amount = ethers.utils.parseEther('20');
104+
const amount = ethers.utils.parseEther('0.01');
105105
await expect(
106106
router.getBestQuote(chainId, dllr, btc, amount),
107107
).resolves.toMatchObject({

packages/sdk/src/swaps/smart-router/routes/ambient.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,19 @@ export const ambientRoute: SwapRouteFunction = (
356356

357357
const isLastPool = i === ambientPools.length - 1;
358358
if (isLastPool) {
359-
const isBaseDestination =
360-
destination.toLowerCase() ===
361-
pool.baseToken.tokenAddr.toLowerCase();
362-
363-
// works for DLLR -> USDT -> USDC swap
364-
baseHop.settlement.useSurplus = !isBaseDestination;
365-
quoteHop.settlement.useSurplus = isBaseDestination;
359+
// ether needs to be withdrawn from dex to user wallet with another hop
360+
if (destination === constants.AddressZero) {
361+
baseHop.settlement.useSurplus = true;
362+
quoteHop.settlement.useSurplus = true;
363+
const ethHop = order.appendHop(constants.AddressZero);
364+
ethHop.settlement.useSurplus = false;
365+
} else {
366+
const isBaseDestination =
367+
destination.toLowerCase() ===
368+
pool.baseToken.tokenAddr.toLowerCase();
369+
baseHop.settlement.useSurplus = !isBaseDestination;
370+
quoteHop.settlement.useSurplus = isBaseDestination;
371+
}
366372
}
367373
}
368374

0 commit comments

Comments
 (0)