Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add chainflip dca #8152

Open
wants to merge 53 commits into
base: develop
Choose a base branch
from
Open

Conversation

CumpsD
Copy link
Contributor

@CumpsD CumpsD commented Nov 20, 2024

Description

This PR:

  • adds Chainflip DCA (Streaming Swaps) under feature flag
  • splits rates and quotes, and while at it effectively fixes the bug where network fees estimation make rates throw for BTC/SOL sells without a wallet connected

Issue

Risk

High:

  • This is swapper, so inherently high
  • Touches the streaming swap component which is also used by THOR

Testing

Quotes without a wallet connected

  • This splits quotes and rates - ensures it is possible to get rates for non-EVM assets sells (read: Solana / USDC on Solana, and BTC) without a wallet connected

DCA swaps (eng. testing only)

  • Ensure streaming swap params are reduced to a bare minimum i.e:
diff --git a/packages/swapper/src/swappers/ChainflipSwapper/utils/helpers.ts b/packages/swapper/src/swappers/ChainflipSwapper/utils/helpers.ts
index e7a229c124..86508d48eb 100644
--- a/packages/swapper/src/swappers/ChainflipSwapper/utils/helpers.ts
+++ b/packages/swapper/src/swappers/ChainflipSwapper/utils/helpers.ts
@@ -121,2 +121,4 @@ export const getChainFlipSwap = ({
-    swapUrl += `&numberOfChunks=${numberOfChunks}`
-    swapUrl += `&chunkIntervalBlocks=${chunkIntervalBlocks}`
+    // swapUrl += `&numberOfChunks=${numberOfChunks}`
+    // swapUrl += `&chunkIntervalBlocks=${chunkIntervalBlocks}`
+    swapUrl += `&numberOfChunks=4`
+    swapUrl += `&chunkIntervalBlocks=100`
  • Ensure rates and quotes are gotten for large enough of an amount, regardless of what you're actually sending
diff --git a/packages/swapper/src/swappers/ChainflipSwapper/utils/getRateOrQuote.ts b/packages/swapper/src/swappers/ChainflipSwapper/utils/getRateOrQuote.ts
index cca28acf92..81d71d81d6 100644
--- a/packages/swapper/src/swappers/ChainflipSwapper/utils/getRateOrQuote.ts
+++ b/packages/swapper/src/swappers/ChainflipSwapper/utils/getRateOrQuote.ts
@@ -114 +114 @@ export const getRateOrQuote = async (
-      `&amount=${sellAmountIncludingProtocolFeesCryptoBaseUnit}` +
+      `&amount=${1000000000000000000}` +
  • Monkey patch invalid sell amount error out in
diff --git a/src/state/apis/swapper/helpers/validateTradeQuote.ts b/src/state/apis/swapper/helpers/validateTradeQuote.ts
index f1260baad7..07347de62b 100644
--- a/src/state/apis/swapper/helpers/validateTradeQuote.ts
+++ b/src/state/apis/swapper/helpers/validateTradeQuote.ts
@@ -46 +45,0 @@ export const validateTradeQuote = (
-    inputSellAmountCryptoBaseUnit,
@@ -241,6 +239,0 @@ export const validateTradeQuote = (
-  // Ensure the trade is not selling an amount higher than the user input, within a very safe threshold.
-  // Threshold is required because cowswap sometimes quotes a sell amount a teeny-tiny bit more than you input.
-  const invalidQuoteSellAmount = bn(inputSellAmountCryptoBaseUnit).lt(
-    firstHop.sellAmountIncludingProtocolFeesCryptoBaseUnit,
-  )
-
@@ -295 +287,0 @@ export const validateTradeQuote = (
-      invalidQuoteSellAmount && { error: TradeQuoteValidationError.QuoteSellAmountInvalid },

Finally, ensure that you actually send a lower base unit amount (in base unit, obviously adapt that to the asset you're sending to make it 30+ bucks, here 0.008907662026062095 ETH on Arb):

diff --git a/packages/swapper/src/swappers/ChainflipSwapper/endpoints.ts b/packages/swapper/src/swappers/ChainflipSwapper/endpoints.ts
index ab56926bde..4ed5d4e1a1 100644
--- a/packages/swapper/src/swappers/ChainflipSwapper/endpoints.ts
+++ b/packages/swapper/src/swappers/ChainflipSwapper/endpoints.ts
@@ -55 +55 @@ export const chainflipApi: SwapperApi = {
-      value: step.sellAmountIncludingProtocolFeesCryptoBaseUnit,
+      value: '8907662026062095',
@@ -69 +69 @@ export const chainflipApi: SwapperApi = {
-      value: step.sellAmountIncludingProtocolFeesCryptoBaseUnit,
+      value: '8907662026062095',

All of the steps above are to ensure we're getting rates/quotes for a much larger amount (see quote at confirm step), while actually sending a much lower one

  • Proceed to a DCA swap, ensuring you've input at least 30 bucks of a sell asset (Sol/Arb chains preferred as those are cheaper, you can even do a same-chain swap if you feel like it, which is obviously cheaper).
    The quotes will be totally off while testing this with this monkey patch, which is expected.

  • Execute said streaming swap and ensure it goes through and status looks good

Engineering

  • Test DCA swaps with the monkey patch above, and rates for BTC and SOL without a wallet connected☝🏽
  • Also test rates without the monkey patch above and ensure things still look sane
  • Ensure you do not see Chainflip DCA (streaming) rates with the Chainflip DCA flag off
  • Ensure THORChain streaming swaps progress still looks good

Operations

  • 🏁 My feature is behind a flag and doesn't require operations testing (yet)

  • It is... ish. Streaming swaps are under flag and do not require ops testing for the time being. However, the changes in the streaming swap component do bring risks, regression test THORChain streaming swaps progress still look good

  • Quotes without a wallet connected for BTC and Solana chain (SOL/USDC.SOL) sells are fixed by this PR, so should be tested

  • Ensure you do not see Chainflip DCA (streaming) rates, as the flag is off in prodish envs

Screenshots

Quote

image

Chainflip Details available on rate

image

Deposit Channel opened with chunks

image

Stream starting

image

Stream in progress

image

Stream Done

image

Swap Done

image

@CumpsD CumpsD changed the title Add Chainflip DCA feat: add chainflip dca Nov 21, 2024
@CumpsD CumpsD marked this pull request as ready for review November 25, 2024 02:02
@CumpsD CumpsD requested a review from a team as a code owner November 25, 2024 02:02
@CumpsD
Copy link
Contributor Author

CumpsD commented Nov 26, 2024

Took all remarks into account, code is cleaned up a lot (even compared to the initial Chainflip implementation), so it might actually be interesting to take this along. Even though it adds the complexity of DCA, it makes the overall code more logical and cleaner actually :)

Copy link
Contributor

@gomesalexandre gomesalexandre left a comment

Choose a reason for hiding this comment

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

@CumpsD pushed a few cleanup commits and fixes as well as actually consuming the flag to programmatically enable DCA quotes (it wasn't consumed previously which rendered the flag effectively useless).

Happy with latest changes here and confirmed no regressions on regular streaming swap component, however spotted two issues:

  • Percentage different is borked for Chainflip in this PR
  • Execution is borked for regular swaps regardless of flag (tested with tokens sends, which may or may not be the only ones broken here)

See testing in https://gist.github.com/gomesalexandre/6865417ce909c59990700b7f5d541d30

Happy to get this guy in once these two are tackled, and whenever we are ready to merge this after base Chainflip functionality gets some love.

Copy link
Contributor

@gomesalexandre gomesalexandre left a comment

Choose a reason for hiding this comment

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

see review above

@CumpsD
Copy link
Contributor Author

CumpsD commented Dec 2, 2024

It seems the power is wrong somewhere in that price. I have been trying to reproduce this, but I think it must have been a temporarly glitch, because I can't manage to get it to repeat this. I recloned this branch from scratch and it worked as expected.

I think it is related to the monkey hack on Chainflip pricing, while all others still return the real price for 30$ to ETH, while the Chainflip one gives you a quote for 30000$ to ETH, causing those weird percentages?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Chainflip non-EVM sells without wallet connected Chainflip swapper DCA (streaming swaps)
3 participants