Code Monkey home page Code Monkey logo

whirlpools's Introduction

Whirlpools

Whirpools is an open-source concentrated liquidity AMM contract on the Solana blockchain. This repository contains the Rust smart contract as well as the Typescript SDK (@orca-so/whirlpools-sdk) to interact with a deployed program.

The contract has been audited by Kudelski and Neodyme.

The contract has been deployed using verifiable build, so that you can ensure that the hash of the on-chain program matches the hash of the program of the given codebase.

Requirements

  • Anchor 0.29.0
  • Solana 1.17.22
  • Rust 1.68.0

Setup

Install Anchor using instructions found here.

Set up a valid Solana keypair at the path specified in the wallet in Anchor.toml to do local testing with anchor test flows.

$NODE_PATH must be set to the node_modules directory of your global installs. For example, using Node 16.10.0 installed through nvm, the $NODE_PATH is the following:

$ echo $NODE_PATH
/Users/<home_dir>/.nvm/versions/node/v16.10.0/lib/node_modules

Usage

Instructions on how to interact with the Whirlpools contract is documented in the Orca Developer Portal.

Tests

  • Run "cargo test --lib" to run Rust unit tests

Whirlpool SDK

Use the SDK to interact with a deployed Whirlpools program via Typescript.

Installation

In your package, run:

yarn add "@orca-so/whirlpools-sdk"
yarn add "@orca-so/common-sdk"
yarn add "@coral-xyz/[email protected]"
yarn add "@solana/web3.js"
yarn add "@solana/spl-token"
yarn add "decimal.js"

Usage

Read instructions on how to use the SDK on the Orca Developer Portal.

Run Typescript tests via local validator

In the whirlpools/sdk folder, run:

anchor test

Generate TypeDoc

In the sdk folder, run yarn run docs

You can also see the generated TypeDoc.

Sample Codes

You can find sample code covering basic operations here.


Support

Questions

Have problems integrating with the SDK? Pop by over to the Orca Discord #dev-questions channel and chat with one of our engineers.

Feedback

Got ideas on how to improve the system? Open up an issue on github with the prefix [FEEDBACK] and let's brainstorm more about it together!

License

Apache 2.0

whirlpools's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

whirlpools's Issues

Get pool functions returns null

let url="https://api.mainnet-beta.solana.com";
let whirlpoolProgramId=new PublicKey("whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc");
const ctx = WhirlpoolContext.withProvider(connection, "whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc");
let solc_usdc= new PublicKey("7qbRF6YsyGuLUVs6Y1q64bdVrfe4ZcUUz1JRdoVNUJnm");
let pool= await ctx.fetcher.getPool(solc_usdc);
console.log(pool) // returns null 

facing this issue with every pool

How to swap assets on-chain?

How does one swap assets on-chain? Namely, within a smart contract or program., in Rust.

SOL <-> token or vise versa, or token <-> token

Is there an example?


Is this the correct repo?

Got TickArraySequenceInvalid error when calling swapQuoteByInputToken function

The dependencies of my project is :

"dependencies": {
    "@coral-xyz/anchor": "^0.29.0",
    "@orca-so/whirlpools-sdk": "^0.11.7",
    "@solana/web3.js": "^1.87.6",
    "bs58": "^5.0.0",
    "decimal.js": "10.3.1"
  }

My code is :

const {Wallet,AnchorProvider,web3} = require("@coral-xyz/anchor");
const {
    WhirlpoolContext,
    buildWhirlpoolClient,
    swapQuoteByInputToken,
    ORCA_WHIRLPOOL_PROGRAM_ID,
    PriceMath,
    IGNORE_CACHE
} = require("@orca-so/whirlpools-sdk");
const {Keypair,Connection,PublicKey} = web3;
const {Percentage,MathUtil} = require("@orca-so/common-sdk");
const Decimal = require("decimal.js");

const key_pair = Keypair.generate();
const my_wallet = new Wallet(key_pair);
const ANCHOR_PROVIDER_URL = "https://api.mainnet-beta.solana.com"
const connection = new Connection(ANCHOR_PROVIDER_URL,"confirmed");
const provider = new AnchorProvider(connection,my_wallet);
const context = WhirlpoolContext.withProvider(provider,ORCA_WHIRLPOOL_PROGRAM_ID)
const client = buildWhirlpoolClient(context);

let sol_usdc_pool_info = {
    "address": "7qbRF6YsyGuLUVs6Y1q64bdVrfe4ZcUUz1JRdoVNUJnm",  
    "tokenA": {
        "mint": "So11111111111111111111111111111111111111112",  
        "symbol": "SOL",
        "name": "Solana",
        "decimals": 9,
        "coingeckoId": "solana",
        "whitelisted": true,
        "poolToken": false
    },
    "tokenB": {
        "mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", 
        "symbol": "USDC",
        "name": "USD Coin",
        "decimals": 6,
        "coingeckoId": "usd-coin",
        "whitelisted": true,
        "poolToken": false
    },
    "whitelisted": true,
    "tickSpacing": 8,             
    "price": 71.12633780037997,    
    "lpFeeRate": 0.0005,           
    "protocolFeeRate": 0.03,        
    "whirlpoolsConfig": "2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ",
};

async function getPool(pool_address) {
    const pool = await client.getPool(pool_address);
    return pool;
} 

function getPrice(pool,decimalsA,decimalsB) {
    const sqrt_price_x64 = pool.getData().sqrtPrice;
    const price = PriceMath.sqrtPriceX64ToPrice(sqrt_price_x64, decimalsA,decimalsB);
    return price.toFixed(decimalsB);
}

async function getQuote(pool,tokenIn,amountIn) {
    let quote = await swapQuoteByInputToken(
        pool,
        tokenIn,
        amountIn,
        Percentage.fromFraction(10, 1000), 
        context.program.programId,
        client.getFetcher(),
        IGNORE_CACHE
    );
    return quote;
}

const tokenIn = new PublicKey("So11111111111111111111111111111111111111112");
const amountIn =MathUtil.toX64(new Decimal(10000000)); 

async function demo() {
    let pool = await getPool("7qbRF6YsyGuLUVs6Y1q64bdVrfe4ZcUUz1JRdoVNUJnm");
    let price = getPrice(pool,9,6);
    console.log("price:",price);

    let quote = await getQuote(pool,tokenIn,amountIn);
    console.log("quote:",quote);

    const txId = await (await pool.swap(quote)).buildAndExecute();
    console.log("txId:",txId);
}

demo();

When I run the code to see the output of the simulated swap, I get the following error:

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[WhirlpoolsError: Swap input value traversed too many arrays. Out of bounds at attempt to traverse tick index - -28168.] {
  errorCode: 'TickArraySequenceInvalid'
}

Node.js v18.14.2

I have tried multiple pools with the same result. Could you please tell me what is causing this?

Impossible to harvest a single position without completely closing it.

Hello,

https://docs.orca.so/orca-for-liquidity-providers/master#how-do-i-earn-fees-on-orca suggests that a single whirlpool position can be harvested by withdrawing "some" amount. And the withdrawing dialogue suggests the same - it promises to harvest accrued fees. However it never does that - there is only transaction for liquidity withdrawal. Even complete emptying of the position does not harvest the fees.

The fees are harvested only on burning of the position NFT.

Since re-creating the position is a somewhat costly operation (significantly costlier than adding/withdrawing the liquidity) and a quite tedious one, and harvesting all positions at once creates an accounting mess it would be great if a single position harvest functionality is restored.

(0 , codecs_1.getBytesCodec) is not a function

i have installed the latest sdk version along decimal.js and @solana/web3.js packages
but the moment i try to initialize the WhirlpoolContext i get a weird runtime error
i am using react js with node version 18.17.1

my implementation :
`
const { connection } = useConnection();
const anchrWallet = useAnchorWallet();

useEffect(() => {
if (connection && anchrWallet) {
const ctx = WhirlpoolContext.from(
connection,
anchrWallet,
WHIRLPOOL_PROGRAM_ID
);
}
}, [anchrWallet, connection]);
`

runtime error:
(0 , codecs_1.getBytesCodec) is not a function TypeError: (0 , codecs_1.getBytesCodec) is not a function at ./node_modules/@solana/spl-token-group/lib/cjs/state/tokenGroup.js (http://localhost:3000/static/js/bundle.js:138583:102) at options.factory (http://localhost:3000/static/js/bundle.js:176739:31) at __webpack_require__ (http://localhost:3000/static/js/bundle.js:176138:32) at fn (http://localhost:3000/static/js/bundle.js:176397:21) at ./node_modules/@solana/spl-token-group/lib/cjs/state/index.js (http://localhost:3000/static/js/bundle.js:138562:14) at options.factory (http://localhost:3000/static/js/bundle.js:176739:31) at __webpack_require__ (http://localhost:3000/static/js/bundle.js:176138:32) at fn (http://localhost:3000/static/js/bundle.js:176397:21) at ./node_modules/@solana/spl-token-group/lib/cjs/index.js (http://localhost:3000/static/js/bundle.js:138382:14) at options.factory (http://localhost:3000/static/js/bundle.js:176739:31)

throw new Error(`Unable to fetch Whirlpool at address at ${poolAddress}`);

I'm try to implement the SDK to perform token swaps, I pass in the SPL Token mint addresses for the input and output token, so I can get the PDA and in turn use it to get the pool data, but apparently the whirlpoolPDA. I get from PDAUtil.getWhirlpool is not valid, and i get a throw new Error(Unable to fetch Whirlpool at address at ${poolAddress});
image

The docs makes reference that that parameter is the whirlpoolsConfigKey but I believe its the whirlpools Address.
PS: There's not other reference to what a WhirlpoolConfigKey is anywhere the Orca's docs.
The whirlpoolProgramId and ConfigAddress are whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc and 2LecshUwdy9xi7meFgHtFJQNSKk4KdTrcpvaB56dP2NQ

We are experiencing a data encoding issue when working with Orca Whirlpool ProgramID.

Hi Team,

Firstly, I want to express my gratitude for the library, which has proven to be an excellent solution for our client.

Currently, we are encountering an issue related to retrieving data from the "getProgramAccounts" function. Our team utilizes Node.js for calling this method, and while the function successfully fetches the data, the challenge lies in the fact that the data field within each account is in Buffer format.

I kindly request your prompt response and guidance on resolving this issue. Specifically, we are seeking a solution to retrieve live pools data from the "getProgramAccounts" method in a decoded format. Alternatively, if there is any other method or library available that can assist us in obtaining live data for the same purpose, we would greatly appreciate your recommendations.

Thank you in advance for your assistance.

Thanks,
Sagar Bhalla
Sr. Technical Project Manager || OODLES Technologies || India

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.