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

Commit e7264c7

Browse files
author
scuba
authored
Update Usage section of readme (#48)
* Update Usage section of readme * update with new api
1 parent f3de5c2 commit e7264c7

File tree

1 file changed

+104
-29
lines changed

1 file changed

+104
-29
lines changed

README.md

+104-29
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ Learn more Orca [here](https://docs.orca.so).
2727
- Remember to withdraw the LP token in exchange for farm token before
2828
withdrawing liquidity from Orca Pool
2929

30+
**DoubleDip Support**
31+
32+
- For farms with double-dip, the aquafarm tokens can be deposited into
33+
double-dip farm to receive double-dip rewards
34+
3035
**Features Coming Soon**
3136

3237
- More trader information (APY, Volume)
@@ -46,37 +51,107 @@ npm install @orca-so/sdk @solana/web3.js decimal.js
4651
# Usage
4752

4853
```typescript
54+
import { readFile } from "mz/fs";
4955
import { Connection, Keypair } from "@solana/web3.js";
50-
import { getOrca, OrcaPoolConfig, OrcaU64 } from "@orca-so/sdk";
51-
52-
try {
53-
const connection = new Connection(url, "singleGossip");
56+
import { getOrca, OrcaFarmConfig, OrcaPoolConfig } from "@orca-so/sdk";
57+
import Decimal from "decimal.js";
58+
59+
const main = async () => {
60+
/*** Setup ***/
61+
// 1. Read secret key file to get owner keypair
62+
const secretKeyString = await readFile("/Users/scuba/my-wallet/my-keypair.json", {
63+
encoding: "utf8",
64+
});
65+
const secretKey = Uint8Array.from(JSON.parse(secretKeyString));
66+
const owner = Keypair.fromSecretKey(secretKey);
67+
68+
// 2. Initialzie Orca object with mainnet connection
69+
const connection = new Connection("https://api.mainnet-beta.solana.com", "singleGossip");
5470
const orca = getOrca(connection);
55-
const owner: Keypair = getKeyPair();
56-
57-
// Get an instance of the ETH-USDC orca pool
58-
let pool = orca.getPool(OrcaPoolConfig.ETH_USDC);
59-
60-
// Get the number of ETH-USDC LP tokens in your wallet
61-
let ethUsdcLPBalance = await pool.getLPBalance(owner.publicKey);
62-
63-
// Get the total supply of ETH-USDC LP tokens
64-
let ethUsdcLPSupply = await pool.getLPSupply();
65-
66-
// Get a quote of exchanging 1.1 ETH to USDC with a slippage tolerance of 0.1%
67-
// From the quote, you can get the current rate, fees, expected output amount and minimum output amount
68-
let ethToken = pool.getTokenA();
69-
let tradeValue = new Decimal(1.1);
70-
let quote = await pool.getQuote(ethToken, tradeValue, new Decimal(0.1));
71-
72-
// Perform a swap for 1USDC to the quoted minimum amount of ETH
73-
// If the user does not have the Associated Token Address(ATA) to receive the output token, the ATA
74-
// instructions will be appended in the transaction.
75-
const txPayload = await pool.swap(owner, usdcToken, tradeValue, quote.getMinOutputAmount());
76-
const txId = await txPayload.execute();
77-
} catch (err) {
78-
// Handle errors
79-
}
71+
72+
try {
73+
/*** Swap ***/
74+
// 3. We will be swapping 0.1 SOL for some ORCA
75+
const orcaSolPool = orca.getPool(OrcaPoolConfig.ORCA_SOL);
76+
const solToken = orcaSolPool.getTokenB();
77+
const solAmount = new Decimal(0.1);
78+
const quote = await orcaSolPool.getQuote(solToken, solAmount);
79+
const orcaAmount = quote.getMinOutputAmount();
80+
81+
console.log(`Swap ${solAmount.toString()} SOL for at least ${orcaAmount.toNumber()} ORCA`);
82+
const swapPayload = await orcaSolPool.swap(owner, solToken, solAmount, orcaAmount);
83+
const swapTxId = await swapPayload.execute();
84+
console.log("Swapped:", swapTxId, "\n");
85+
86+
/*** Pool Deposit ***/
87+
// 4. Deposit SOL and ORCA for LP token
88+
const { maxTokenAIn, maxTokenBIn, minPoolTokenAmountOut } = await orcaSolPool.getDepositQuote(
89+
orcaAmount,
90+
solAmount
91+
);
92+
93+
console.log(
94+
`Deposit at most ${maxTokenBIn.toNumber()} SOL and ${maxTokenAIn.toNumber()} ORCA, for at least ${minPoolTokenAmountOut.toNumber()} LP tokens`
95+
);
96+
const poolDepositPayload = await orcaSolPool.deposit(
97+
owner,
98+
maxTokenAIn,
99+
maxTokenBIn,
100+
minPoolTokenAmountOut
101+
);
102+
const poolDepositTxId = await poolDepositPayload.execute();
103+
console.log("Pool deposited:", poolDepositTxId, "\n");
104+
105+
/*** Farm Deposit ***/
106+
// 5. Deposit some ORCA_SOL LP token for farm token
107+
const lpBalance = await orcaSolPool.getLPBalance(owner.publicKey);
108+
const orcaSolFarm = orca.getFarm(OrcaFarmConfig.ORCA_SOL_AQ);
109+
const farmDepositPayload = await orcaSolFarm.deposit(owner, lpBalance);
110+
const farmDepositTxId = await farmDepositPayload.execute();
111+
console.log("Farm deposited:", farmDepositTxId, "\n");
112+
// Note 1: for double dip, repeat step 5 but with the double dip farm
113+
// Note 2: to harvest reward, orcaSolFarm.harvest(owner)
114+
// Note 3: to get harvestable reward amount, orcaSolFarm.getHarvestableAmount(owner.publicKey)
115+
116+
/*** Farm Withdraw ***/
117+
// 6. Withdraw ORCA_SOL LP token, in exchange for farm token
118+
const farmBalance = await orcaSolFarm.getFarmBalance(owner.publicKey); // withdraw the entire balance
119+
const farmWithdrawPayload = await orcaSolFarm.withdraw(owner, farmBalance);
120+
const farmWithdrawTxId = await farmWithdrawPayload.execute();
121+
console.log("Farm withdrawn:", farmWithdrawTxId, "\n");
122+
123+
/*** Pool Withdraw ***/
124+
// 6. Withdraw SOL and ORCA, in exchange for ORCA_SOL LP token
125+
const withdrawTokenAmount = await orcaSolPool.getLPBalance(owner.publicKey);
126+
const withdrawTokenMint = orcaSolPool.getPoolTokenMint();
127+
const { maxPoolTokenAmountIn, minTokenAOut, minTokenBOut } = await orcaSolPool.getWithdrawQuote(
128+
withdrawTokenAmount,
129+
withdrawTokenMint
130+
);
131+
132+
console.log(
133+
`Withdraw at most ${maxPoolTokenAmountIn.toNumber()} ORCA_SOL LP token for at least ${minTokenAOut.toNumber()} ORCA and ${minTokenBOut.toNumber()} SOL`
134+
);
135+
const poolWithdrawPayload = await orcaSolPool.withdraw(
136+
owner,
137+
maxPoolTokenAmountIn,
138+
minTokenAOut,
139+
minTokenBOut
140+
);
141+
const poolWithdrawTxId = await poolWithdrawPayload.execute();
142+
console.log("Pool withdrawn:", poolWithdrawTxId, "\n");
143+
} catch (err) {
144+
console.warn(err);
145+
}
146+
};
147+
148+
main()
149+
.then(() => {
150+
console.log("Done");
151+
})
152+
.catch((e) => {
153+
console.error(e);
154+
});
80155
```
81156

82157
# Technical Notes

0 commit comments

Comments
 (0)