Skip to content

Commit

Permalink
Merge branch 'main' into wjthieme/rust-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
wjthieme committed Nov 15, 2024
2 parents 3f523aa + 3dd9201 commit f78f475
Show file tree
Hide file tree
Showing 101 changed files with 8,157 additions and 2,724 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Splash Pools are the easiest way to get started:
```tsx
import { createSplashPoolInstructions } from '@orca-so/whirlpools'

const { poolAddress, instructions, estInitializationCost } = await createSplashPoolInstructions(
const { poolAddress, instructions, initializationCost } = await createSplashPoolInstructions(
rpc,
tokenMintOne,
tokenMintTwo,
Expand All @@ -60,7 +60,7 @@ Concentrated Liquidity Pools offer more flexibility:
```tsx
import { createConcentratedLiquidityPool } from '@orca-so/whirlpools'

const { poolAddress, instructions, estInitializationCost } = await createConcentratedLiquidityPool(
const { poolAddress, instructions, initializationCost } = await createConcentratedLiquidityPool(
rpc,
tokenMintOne,
tokenMintTwo,
Expand Down
4 changes: 2 additions & 2 deletions docs/whirlpool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"clean": "rimraf dist .docusaurus"
},
"dependencies": {
"@docusaurus/core": "^3.6.0",
"@docusaurus/preset-classic": "^3.6.0",
"@docusaurus/core": "^3.6.1",
"@docusaurus/preset-classic": "^3.6.1",
"@mdx-js/react": "^3.1.0",
"clsx": "^2.1.1",
"prism-react-renderer": "^2.4.0",
Expand Down
74 changes: 74 additions & 0 deletions legacy-sdk/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Setting up your script environment
```bash
yarn
```

# Set your RPC and wallet
```bash
export ANCHOR_PROVIDER_URL=<RPC URL>
export ANCHOR_WALLET=<WALLET JSON PATH>
```

Example:
```bash
export ANCHOR_PROVIDER_URL=http://localhost:8899
export ANCHOR_WALLET=~/.config/solana/id.json
```

# Supported commands
Token-2022 tokens are acceptable 👍

## Config & FeeTier
### initialize
- `yarn start initializeConfig`: initialize new WhirlpoolsConfig account
- `yarn start initializeConfigExtension`: initialize new WhirlpoolsConfigExtension account
- `yarn start initializeFeeTier`: initialize new FeeTier account

### update
- `yarn start setTokenBadgeAuthority`: set new TokenBadge authority on WhirlpoolsConfigExtension
- `yarn start setDefaultProtocolFeeRate`: set new default protocol fee rate on WhirlpoolsConfig
- `yarn start setFeeAuthority`: set new fee authority on WhirlpoolsConfig
- `yarn start setCollectProtocolFeesAuthority`: set new collect protocol fees authority on WhirlpoolsConfig
- `yarn start setRewardEmissionsSuperAuthority`: set new reward emissions super authority on WhirlpoolsConfig
- TODO: set config extension authority

## Whirlpool & TickArray
- `yarn start initializeWhirlpool`: initialize new Whirlpool account
- `yarn start initializeTickArray`: initialize new TickArray account

## TokenBadge
- `yarn start initializeTokenBadge`: initialize new TokenBadge account
- `yarn start deleteTokenBadge`: delete TokenBadge account

## Reward
- `yarn start setRewardAuthority`: set new reward authority of rewards on a whirlpool
- `yarn start initializeReward`: initialize new reward for a whirlpool
- TODO: set reward emission

## Position
- `yarn start openPosition`: open a new position
- `yarn start increaseLiquidity`: deposit to a position
- `yarn start decreaseLiquidity`: withdraw from a position
- `yarn start collectFees`: collect fees from a position
- `yarn start collectRewards`: collect rewards from a position
- `yarn start closePosition`: close an empty position

## Swap
- `yarn start pushPrice`: adjust pool price (possible if pool liquidity is zero or very small)

## WSOL and ATA creation
TODO: WSOL handling & create ATA if needed (workaround exists, please see the following)

### workaround for WSOL
`whirlpool-mgmt-tools` works well with ATA, so using WSOL on ATA is workaround.

- wrap 1 SOL: `spl-token wrap 1` (ATA for WSOL will be initialized with 1 SOL)
- unwrap: `spl-token unwrap` (ATA for WSOL will be closed)
- add 1 WSOL: `solana transfer <WSOL ATA address> 1` then `spl-token sync-native` (transfer & sync are needed)

### workaround for ATA
We can easily initialize ATA with spl-token CLI.

```
spl-token create-account <mint address>
```
28 changes: 28 additions & 0 deletions legacy-sdk/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@orca-so/whirlpools-sdk-cli",
"private": true,
"version": "1.0.0",
"type": "module",
"scripts": {
"build": "tsc --noEmit",
"start": "tsx src/index.ts"
},
"dependencies": {
"@coral-xyz/anchor": "0.29.0",
"@orca-so/common-sdk": "0.6.4",
"@orca-so/orca-sdk": "0.2.0",
"@orca-so/whirlpools-sdk": "*",
"@solana/spl-token": "0.4.1",
"@solana/web3.js": "^1.90.0",
"@types/bn.js": "^5.1.0",
"bs58": "^6.0.0",
"decimal.js": "^10.4.3",
"js-convert-case": "^4.2.0",
"prompts": "^2.4.2"
},
"devDependencies": {
"@types/prompts": "^2.4.9",
"tsx": "^4.19.0",
"typescript": "^5.4.5"
}
}
92 changes: 92 additions & 0 deletions legacy-sdk/cli/src/commands/close_position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { PublicKey } from "@solana/web3.js";
import { WhirlpoolIx } from "@orca-so/whirlpools-sdk";
import { TransactionBuilder } from "@orca-so/common-sdk";
import {
getAssociatedTokenAddressSync,
TOKEN_2022_PROGRAM_ID,
TOKEN_PROGRAM_ID,
} from "@solana/spl-token";
import { sendTransaction } from "../utils/transaction_sender";
import { ctx } from "../utils/provider";
import { promptText } from "../utils/prompt";

console.info("close Position...");

// prompt
const positionPubkeyStr = await promptText("positionPubkey");

const positionPubkey = new PublicKey(positionPubkeyStr);
const position = await ctx.fetcher.getPosition(positionPubkey);
if (!position) {
throw new Error("position not found");
}
const positionMint = await ctx.fetcher.getMintInfo(position.positionMint);
if (!positionMint) {
throw new Error("positionMint not found");
}

if (!position.liquidity.isZero()) {
throw new Error("position is not empty (liquidity is not zero)");
}

if (!position.feeOwedA.isZero() || !position.feeOwedB.isZero()) {
throw new Error("position has collectable fees");
}

if (!position.rewardInfos.every((r) => r.amountOwed.isZero())) {
throw new Error("position has collectable rewards");
}

const builder = new TransactionBuilder(ctx.connection, ctx.wallet);

if (positionMint.tokenProgram.equals(TOKEN_PROGRAM_ID)) {
builder.addInstruction(
WhirlpoolIx.closePositionIx(ctx.program, {
position: positionPubkey,
positionAuthority: ctx.wallet.publicKey,
positionTokenAccount: getAssociatedTokenAddressSync(
position.positionMint,
ctx.wallet.publicKey,
),
positionMint: position.positionMint,
receiver: ctx.wallet.publicKey,
}),
);
} else {
builder.addInstruction(
WhirlpoolIx.closePositionWithTokenExtensionsIx(ctx.program, {
position: positionPubkey,
positionAuthority: ctx.wallet.publicKey,
positionTokenAccount: getAssociatedTokenAddressSync(
position.positionMint,
ctx.wallet.publicKey,
undefined,
TOKEN_2022_PROGRAM_ID,
),
positionMint: position.positionMint,
receiver: ctx.wallet.publicKey,
}),
);
}

await sendTransaction(builder);

/*
SAMPLE EXECUTION LOG
connection endpoint http://localhost:8899
wallet r21Gamwd9DtyjHeGywsneoQYR39C1VDwrw7tWxHAwh6
close Position...
prompt: positionPubkey: H4WEb57EYh5AhorHArjgRXVgSBJRMZi3DvsLb3J1XNj6
estimatedComputeUnits: 120649
prompt: priorityFeeInSOL: 0
Priority fee: 0 SOL
process transaction...
transaction is still valid, 150 blocks left (at most)
sending...
confirming...
✅successfully landed
signature dQwedycTbM9UTYwQiiUE5Q7ydZRzL3zywaQ3xEo3RhHxDvfsY8wkAakSXQRdXswxdQCLLMwwDJVSNHYcTCDDcf3
*/
Loading

0 comments on commit f78f475

Please sign in to comment.