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

SparseSwap (SDK) #162

Merged
merged 9 commits into from
Aug 1, 2024
Merged

SparseSwap (SDK) #162

merged 9 commits into from
Aug 1, 2024

Conversation

yugure-orca
Copy link
Collaborator

@yugure-orca yugure-orca commented Jul 9, 2024

This PR introduces the benefits provided by SparseSwap into SDK.

We no longer need to stop quote right before uninitialized tickarray

The swapQuoteX function gets the estimate without any concern for the existence of uninitialized TickArrays.
Whirlpool.swap and the swapAsync behind it no longer pre-verify whether a TickArray is initialized or not.

To enjoy the benefit of not having to worry about uninitialized TickArrays by default, the SDK interpolate uninitialized TickArrays with zeroed data in swapQuoteWithParams function.
As a result, functions that use swapQuoteWithParams, such as sqapQuoteByInputToken, also benefit.

  • swapQuoteByInputToken --> swapQuoteWithParams
  • swapQuoteByOutputToken --> swapQuoteWithParams
  • swapQuoteWithParams (direct use)

User can insert fallback tickarray if they want

On the other hand, the behavior of supplemental tick arrays and preliminary additions to previous tick arrays are disabled by default. (UseFallbackTickArray.Never)
This is to prevent breaking changes on the function interface.

If users want to add a fallback, they must explicitly use UseFallbackTickArray.Always or UseFallbackTickArray.Situational.

export async function swapQuoteByInputToken(
  whirlpool: Whirlpool,
  inputTokenMint: Address,
  tokenAmount: BN,
  slippageTolerance: Percentage,
  programId: Address,
  fetcher: WhirlpoolAccountFetcherInterface,
  opts?: WhirlpoolAccountFetchOptions,
  useFallbackTickArray: UseFallbackTickArray = UseFallbackTickArray.Never, 👈
): Promise<SwapQuote> {

UseFallbackTickArray.Situational

Add a TickArray in the opposite direction as a fallback only if the price is at the end 1/4 of the current TickArray.

b to a
[  fallback  ][//p///      ta0   ][      ta1   ][      ta2   ]  ---> fallback will be added
[  fallback  ][//////  p   ta0   ][      ta1   ][      ta2   ]  ---> fallback will NOT be added

a to b
[   ta2      ][   ta1      ][   ta0             //p///][  fallback  ]  ---> fallback will be added
[   ta2      ][   ta1      ][   ta0        p    //////][  fallback  ]  ---> fallback will NOT be added

1/4 is fixed value. Devs who want to tune the behavior shoud use swapQuoteWithparams and SwapUtils.getFallbackTickArrayPublicKey directly.

How to add fallback tick array

If only one or two tickarrays are used in a trade (ta1=ta2), add it as ta2. (it is safe way because ta2 is dup of ta1)
This way is prefer because V1 users can also benefit.

On the other hand, if three TickArrays are used, it is added to the estimate as supplementalTickArrays and used only in the V2 instruction.

Whirlpool.swap uses V2 if the pool uses TokenExtension or if supplementalTickArrays is not empty.

@@ -170,7 +302,7 @@ describe("swap arrays test", () => {
adjustForSlippage(quote.estimatedAmountOut, slippageTolerance, false).toString()
);
assert.equal(quote.estimatedAmountIn.toString(), tradeAmount);
assert.doesNotThrow(async () => await (await whirlpool.swap(quote)).buildAndExecute());
await assert.doesNotReject(async () => await (await whirlpool.swap(quote)).buildAndExecute());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

for async fn, we should use doesNotReject.

* @param tickArrays - Fetched tickArrays to interpolate.
* @returns An array of TickArray objects with zeroed tick data if they are not initialized.
*/
public static interporateUninitializedTickArrays(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: interpolate

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh...🤦‍
Good catch! 🙏

@yugure-orca yugure-orca merged commit bcc061a into yugure/sparse-swap Aug 1, 2024
yugure-orca added a commit that referenced this pull request Aug 1, 2024
* draft for swap ix

* use vec

* add SparseSwapTickSequenceBuilder

* use ref

* v2 support, add errors

* refactor: use named field

* update two_hop_swap v1/v2 to support ss

* add cargo test test cases

* add integration test (partial)

* add ProxiedTickArray to reduce heap usage

program can use up to 32KB heap

* fix testcases

* refactor s/additional/supplemental

* add twoHopSwap test b to a

* add twoHopSwap test a to b

* update client-side remaining accounts handling

* add supplemental tick arrays test cases

* add supplemental tick array test cases (2)

* fix existing test cases

* address review comment (add comments, small refactor)

* address review comments (refactor with TickArrayType trait)

* fix testcases on swap

* adjust error code (merge introduce 1 new error code)

* SparseSwap (SDK) (#162)

* intro the benefit of SparseSwap into SDK

* avoid breaking parameter change

* fix failed test cases

* adjust fallback tickarray handling

* fix typo

* add new test cases

* add test cases for fallback

* address review comment (fix typo)
@yugure-orca yugure-orca deleted the yugure/sparse-swap-sdk branch August 1, 2024 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants