From 98b68da51e9c27b969c65eba764dbd9c5dce0467 Mon Sep 17 00:00:00 2001 From: 0xscuba Date: Tue, 5 Oct 2021 15:49:42 -0700 Subject: [PATCH 1/2] Update Usage section of readme --- README.md | 124 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 95 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 741322e..8486ce8 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,11 @@ Learn more Orca [here](https://docs.orca.so). - Remember to withdraw the LP token in exchange for farm token before withdrawing liquidity from Orca Pool +**DoubleDip Support** + +- For farms with double-dip, the aquafarm tokens can be deposited into + double-dip farm to receive double-dip rewards + **Features Coming Soon** - More trader information (APY, Volume) @@ -46,37 +51,98 @@ npm install @orca-so/sdk @solana/web3.js decimal.js # Usage ```typescript +import { readFile } from "mz/fs"; import { Connection, Keypair } from "@solana/web3.js"; -import { getOrca, OrcaPoolConfig, OrcaU64 } from "@orca-so/sdk"; - -try { - const connection = new Connection(url, "singleGossip"); +import { getOrca, OrcaFarmConfig, OrcaPoolConfig } from "@orca-so/sdk"; +import Decimal from "decimal.js"; + +const main = async () => { + /*** Setup ***/ + // 1. Read secret key file to get owner keypair + const secretKeyString = await readFile("/Users/scuba/my-wallet/my-keypair.json", { + encoding: "utf8", + }); + const secretKey = Uint8Array.from(JSON.parse(secretKeyString)); + const owner = Keypair.fromSecretKey(secretKey); + + // 2. Initialzie Orca object with mainnet connection + const connection = new Connection("https://api.mainnet-beta.solana.com", "singleGossip"); const orca = getOrca(connection); - const owner: Keypair = getKeyPair(); - - // Get an instance of the ETH-USDC orca pool - let pool = orca.getPool(OrcaPoolConfig.ETH_USDC); - - // Get the number of ETH-USDC LP tokens in your wallet - let ethUsdcLPBalance = await pool.getLPBalance(owner.publicKey); - - // Get the total supply of ETH-USDC LP tokens - let ethUsdcLPSupply = await pool.getLPSupply(); - - // Get a quote of exchanging 1.1 ETH to USDC with a slippage tolerance of 0.1% - // From the quote, you can get the current rate, fees, expected output amount and minimum output amount - let ethToken = pool.getTokenA(); - let tradeValue = new Decimal(1.1); - let quote = await pool.getQuote(ethToken, tradeValue, new Decimal(0.1)); - - // Perform a swap for 1USDC to the quoted minimum amount of ETH - // If the user does not have the Associated Token Address(ATA) to receive the output token, the ATA - // instructions will be appended in the transaction. - const txPayload = await pool.swap(owner, usdcToken, tradeValue, quote.getMinOutputAmount()); - const txId = await txPayload.execute(); -} catch (err) { - // Handle errors -} + + try { + /*** Swap ***/ + // 3. We will be swapping 0.1 SOL for some ORCA + const orcaSolPool = orca.getPool(OrcaPoolConfig.ORCA_SOL); + const solToken = orcaSolPool.getTokenB(); + const solAmount = new Decimal(0.1); + const quote = await orcaSolPool.getQuote(solToken, solAmount); + const orcaAmount = quote.getMinOutputAmount(); + + console.log(`Swap ${solAmount.toString()} SOL for at least ${orcaAmount.toNumber()} ORCA`); + const swapPayload = await orcaSolPool.swap(owner, solToken, solAmount, orcaAmount); + const swapTxId = await swapPayload.execute(); + console.log("Swapped:", swapTxId, "\n"); + + /*** Pool Deposit ***/ + // 4. Deposit SOL and ORCA for LP token + const poolTokenAmount = await orcaSolPool.getDepositQuote(orcaAmount, solAmount); + // ^ the order of parameters matters + + console.log( + `Deposit at most ${solAmount.toString()} SOL and ${orcaAmount.toNumber()} ORCA, for at least ${poolTokenAmount.toNumber()} LP tokens` + ); + const poolDepositPayload = await orcaSolPool.deposit( + owner, + orcaAmount, + solAmount, + poolTokenAmount + ); + const poolDepositTxId = await poolDepositPayload.execute(); + console.log("Pool deposited:", poolDepositTxId, "\n"); + + /*** Farm Deposit ***/ + // 5. Deposit some ORCA_SOL LP token for farm token + const orcaSolFarm = orca.getFarm(OrcaFarmConfig.ORCA_SOL_AQ); + const farmDepositPayload = await orcaSolFarm.deposit(owner, poolTokenAmount); + const farmDepositTxId = await farmDepositPayload.execute(); + console.log("Farm deposited:", farmDepositTxId, "\n"); + // Note: for double dip, repeat step 5 but with the double dip farm + + /*** Farm Withdraw ***/ + // 6. Withdraw ORCA_SOL LP token, in exchange for farm token + const farmBalance = await orcaSolFarm.getFarmBalance(owner.publicKey); // withdraw the entire balance + const farmWithdrawPayload = await orcaSolFarm.withdraw(owner, farmBalance); + const farmWithdrawTxId = await farmWithdrawPayload.execute(); + console.log("Farm withdrawn:", farmWithdrawTxId, "\n"); + + /*** Pool Withdraw ***/ + // 6. Withdraw SOL and ORCA, in exchange for ORCA_SOL LP token + const lpBalance = await orcaSolPool.getLPBalance(owner.publicKey); // withdraw the entire balance + const { minTokenAOut, minTokenBOut } = await orcaSolPool.getWithdrawQuote(lpBalance); + + console.log( + `Withdraw at most ${lpBalance.toNumber()} ORCA_SOL LP token for at least ${minTokenAOut.toNumber()} ORCA and ${minTokenBOut.toNumber()} SOL` + ); + const poolWithdrawPayload = await orcaSolPool.withdraw( + owner, + lpBalance, + minTokenAOut, + minTokenBOut + ); + const poolWithdrawTxId = await poolWithdrawPayload.execute(); + console.log("Pool withdrawn:", poolWithdrawTxId, "\n"); + } catch (err) { + console.warn(err); + } +}; + +main() + .then(() => { + console.log("Done"); + }) + .catch((e) => { + console.error(e); + }); ``` # Technical Notes From 194c48458e3342e021d1f03f360abf49c2ca27cf Mon Sep 17 00:00:00 2001 From: 0xscuba Date: Thu, 7 Oct 2021 15:32:55 -0700 Subject: [PATCH 2/2] update with new api --- README.md | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8486ce8..cfea099 100644 --- a/README.md +++ b/README.md @@ -85,28 +85,33 @@ const main = async () => { /*** Pool Deposit ***/ // 4. Deposit SOL and ORCA for LP token - const poolTokenAmount = await orcaSolPool.getDepositQuote(orcaAmount, solAmount); - // ^ the order of parameters matters + const { maxTokenAIn, maxTokenBIn, minPoolTokenAmountOut } = await orcaSolPool.getDepositQuote( + orcaAmount, + solAmount + ); console.log( - `Deposit at most ${solAmount.toString()} SOL and ${orcaAmount.toNumber()} ORCA, for at least ${poolTokenAmount.toNumber()} LP tokens` + `Deposit at most ${maxTokenBIn.toNumber()} SOL and ${maxTokenAIn.toNumber()} ORCA, for at least ${minPoolTokenAmountOut.toNumber()} LP tokens` ); const poolDepositPayload = await orcaSolPool.deposit( owner, - orcaAmount, - solAmount, - poolTokenAmount + maxTokenAIn, + maxTokenBIn, + minPoolTokenAmountOut ); const poolDepositTxId = await poolDepositPayload.execute(); console.log("Pool deposited:", poolDepositTxId, "\n"); /*** Farm Deposit ***/ // 5. Deposit some ORCA_SOL LP token for farm token + const lpBalance = await orcaSolPool.getLPBalance(owner.publicKey); const orcaSolFarm = orca.getFarm(OrcaFarmConfig.ORCA_SOL_AQ); - const farmDepositPayload = await orcaSolFarm.deposit(owner, poolTokenAmount); + const farmDepositPayload = await orcaSolFarm.deposit(owner, lpBalance); const farmDepositTxId = await farmDepositPayload.execute(); console.log("Farm deposited:", farmDepositTxId, "\n"); - // Note: for double dip, repeat step 5 but with the double dip farm + // Note 1: for double dip, repeat step 5 but with the double dip farm + // Note 2: to harvest reward, orcaSolFarm.harvest(owner) + // Note 3: to get harvestable reward amount, orcaSolFarm.getHarvestableAmount(owner.publicKey) /*** Farm Withdraw ***/ // 6. Withdraw ORCA_SOL LP token, in exchange for farm token @@ -117,15 +122,19 @@ const main = async () => { /*** Pool Withdraw ***/ // 6. Withdraw SOL and ORCA, in exchange for ORCA_SOL LP token - const lpBalance = await orcaSolPool.getLPBalance(owner.publicKey); // withdraw the entire balance - const { minTokenAOut, minTokenBOut } = await orcaSolPool.getWithdrawQuote(lpBalance); + const withdrawTokenAmount = await orcaSolPool.getLPBalance(owner.publicKey); + const withdrawTokenMint = orcaSolPool.getPoolTokenMint(); + const { maxPoolTokenAmountIn, minTokenAOut, minTokenBOut } = await orcaSolPool.getWithdrawQuote( + withdrawTokenAmount, + withdrawTokenMint + ); console.log( - `Withdraw at most ${lpBalance.toNumber()} ORCA_SOL LP token for at least ${minTokenAOut.toNumber()} ORCA and ${minTokenBOut.toNumber()} SOL` + `Withdraw at most ${maxPoolTokenAmountIn.toNumber()} ORCA_SOL LP token for at least ${minTokenAOut.toNumber()} ORCA and ${minTokenBOut.toNumber()} SOL` ); const poolWithdrawPayload = await orcaSolPool.withdraw( owner, - lpBalance, + maxPoolTokenAmountIn, minTokenAOut, minTokenBOut );