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

More CLI commands #642

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open

More CLI commands #642

wants to merge 15 commits into from

Conversation

yugure-orca
Copy link
Collaborator

@yugure-orca yugure-orca commented Jan 9, 2025

Updates

package.json

The type of CLI (@orca-so/whirlpools-sdk-cli) is module to use top level async.
But it needsdist directory to load common-sdk and whirlpools-sdk.
yarn creates symbolic link to /legacy-sdk/common and /legacy-sdk/whirlpool and those directory may not contain dist (without yarn run build).

So now it uses the previous version of SDKs.
Could we resolve this issue in more smart way ?

Tweaks on existing commands

initializeWhirlpool

  • print Whirlpool account address before the confirm prompt

initializeTickArray

  • Now it asks "initialize more tickArray ?" at the end of an initialization, to help initializing multiple TickArray accounts

sendTransaction function

  • accept defaultPriorityFeeInLamports parameter to skip "initial" ask of priority fee
  • accept alts to use ALTs

New commands

initializePositionBundle

This command initialize a new PositionBundle.

ALT related commands

initializeAltForBundledPositions

This command initialize an ALT and add 256 addresses.
These 256 addresses are the addresses of bundled positions of given PositionBundle.

It helps to merge more TransactionBuilders into one TransactionBuilder to reduce the number of required transactions.

initializeAltForWhirlpool

This command initialize an ALT and add the following addresses.

  • Whirlpool
  • WhirlpoolsConfig
  • mintA
  • mintB
  • vaultA
  • vaultB
  • ATA for mintA (owner = ctx.wallet)
  • ATA for mintB (owner = ctx.wallet)
  • reward mints (if initialized)
  • reward vaults (if initialized)
  • ATA for reward mints (owner = ctx.wallet, if initialized)
  • oracle
  • 11 TickArrays (-5 offset ~ +5 offset)

This ALT also helps to merge more TransactionBuilders into one TransactionBuilder.
The point is that this ALT is just for wallet user because it contains ATA for the wallet.

syncPositionBundleState

This command is the main part of this PR.

It receives a CSV file defining the target state of bundled positions.
Then it will check the current state of the bundled positions, and it will build (and send) transactions to modify state.

Examples

open
If the command receives the following CSV with empty PositionBundle account, it will open 6 positions with the given range and liquidity value.

bundle index,state,lower tick index,upper tick index,liquidity
0,open,-69120,-68992,20982
1,open,-68992,-68864,744581
2,open,-68864,-68736,4280947
3,open,-68736,-68608,13347375
4,open,-68608,-68480,31064042
5,open,-68480,-68352,60993079
6,closed,,,
7,closed,,,
...
254,closed,,,
255,closed,,,

close
If the command receives the following CSV with a PositionBundle containing 6 open positions, it will close 6 positions.

bundle index,state,lower tick index,upper tick index,liquidity
0,closed,,,
1,closed,,,
2,closed,,,
3,closed,,,
4,closed,,,
5,closed,,,
6,closed,,,
7,closed,,,
...
254,closed,,,
255,closed,,,

modify range and liquidity
If the command receives the following CSV with empty PositionBundle account, it will open 5 positions with the given range and liquidity value.

bundle index,state,lower tick index,upper tick index,liquidity
0,open,-69120,-68992,20982
1,open,-68992,-68864,744581
2,open,-68864,-68736,4280947
3,open,-68736,-68608,13347375
4,open,-68608,-68480,31064042
5,closed,,,
6,closed,,,
7,closed,,,
...
254,closed,,,
255,closed,,,

After the first "sync", if the command receives the following CSV, it will do the following.

  • close the 1st position (bundleIndex = 0) and re-open it because its range have been changed
  • deposit assets to the 2nd position because its liquidity have been increased
  • withdraw assets from the 3rd position because its liquidity have been decreased
  • open the 6th (bundleIndex = 5) with the given liquidity because its state have been changed from closed to open
bundle index,state,lower tick index,upper tick index,liquidity
0,open,-69248,-68992,20982
1,open,-68992,-68864,4280947
2,open,-68864,-68736,744581
3,open,-68736,-68608,13347375
4,open,-68608,-68480,31064042
5,open,-68480,-68352,60993079
6,closed,,,
7,closed,,,
...
254,closed,,,
255,closed,,,

Sample Output

connection endpoint http://localhost:8899
wallet r21Gamwd9DtyjHeGywsneoQYR39C1VDwrw7tWxHAwh6
sync PositionBundle state...
✔ positionBundlePubkey … qHbk42b2ub8K6Rw6p7t1aUoJpwGZ6xpzDC75CQ4QgPD
✔ whirlpoolPubkey … 95XaJMqCLiWtUwF9DtSvDpDbPYhEHoVyCeeNwmUD7cwr
✔ positionBundleTargetStateCsvPath … sample/position_bundle_state/open.csv
✔ commaSeparatedAltPubkeys … 7Vyx1y8vG9e9Q1MedmXpopRC6ZhVaZzGcvYh5Z3Cs75i, AnXmyHSfuAaWkCxaUuTW39SN5H5ztH8bBxm647uESgTd, FjTZwDecYM3G66VKFuAaLgw3rY1QitziKdM5Ng4EpoKd
check positionBundle...
check whirlpool...
check ALTs...
    loaded ALT 7Vyx1y8vG9e9Q1MedmXpopRC6ZhVaZzGcvYh5Z3Cs75i, 254 entries
    loaded ALT AnXmyHSfuAaWkCxaUuTW39SN5H5ztH8bBxm647uESgTd, 256 entries
    loaded ALT FjTZwDecYM3G66VKFuAaLgw3rY1QitziKdM5Ng4EpoKd, 20 entries
read position bundle target state...
check if required TickArrays are initialized...
check if required ATAs are initialized...
check position bundle state difference...
building transactions...

📝 ACTION SUMMARY

Pool:           95XaJMqCLiWtUwF9DtSvDpDbPYhEHoVyCeeNwmUD7cwr
PositionBundle: qHbk42b2ub8K6Rw6p7t1aUoJpwGZ6xpzDC75CQ4QgPD
Target state:   sample/position_bundle_target_state_open.csv

Position state changes:

    close position:       0 position(s)
    open  position:     126 position(s)
    withdraw liquidity:   0 position(s)
    deposit  liquidity:   0 position(s)

Balance changes:

    slippage: 1 %

    tokenA withdrawn (est): 0
    tokenB withdrawn (est): 0
    tokenA withdrawn (min): 0
    tokenB withdrawn (min): 0
    tokenA collected:       0
    tokenB collected:       0
    rewards collected:      no reward, no reward, no reward
    tokenA deposited (est): 0
    tokenB deposited (est): 711326.515908
    tokenA deposited (max): 0
    tokenB deposited (max): 718439.781001

    tokenA balance delta (est): 0
    tokenB balance delta (est): -711326.515908

    * negative balance delta means deposited more than withdrawn

Wallet balances:

    tokenA: 1000000000
    tokenB: 999999999.999622

Transactions:

    withdraw: 0 transaction(s)
    deposit:  14 transaction(s)

✔ proceed? › Yes

estimatedComputeUnits: 1400000
process transaction...
transaction is still valid, 151 blocks left (at most)
sending...
confirming...
✅successfully landed
signature 3K2nYSwpSCcHNvNtUCQZ9UCbpeR64BL7GGf2xdW1RMLim1wU53Ju9rfEYv1qLbhmm1vMYRvtV3g2CYxnoz4MQWTT
...
...
...
estimatedComputeUnits: 1400000
process transaction...
transaction is still valid, 151 blocks left (at most)
sending...
confirming...
✅successfully landed
signature 2U1SLJSQnH434DgTTiAQw52YrSMBKUELJ73knfvNMV2KbMofSTk94w4DKY1fEuMe8rD2bTPT8V6C1qavBYMB2JCc
check position bundle state difference...
synced

Future work

There are some TODOs on syncPositionBundleState.

  • ask default priority fee (now it is hard coded (0.00001 SOL))
  • ask slippage (now it is hard coded (1%))
  • use parallel transaction execution (now it executes required transactions serially)

This is not a major problem for use and will be postponed to the future as the urgent need for this command has disappeared. 😇

@wjthieme
Copy link
Member

wjthieme commented Jan 9, 2025

So now it uses the previous version of SDKs.
Could we resolve this issue in more smart way ?

I think it is already hooked into the monorepo. In order for it to execute the prereqs I think you would need to run the commands from the root of the repo and specify the project.

So instead of

cd legacy-sdk/cli
yarn start ...

you can do

yarn start legacy-sdk/cli ...

Copy link
Member

@wjthieme wjthieme left a comment

Choose a reason for hiding this comment

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

Nice! LGTM

@yugure-orca yugure-orca reopened this Jan 12, 2025
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