Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Update Usage section of readme #48

Merged
2 commits merged into from
Oct 8, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 95 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down