-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
170 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Transaction, PublicKey } from '@solana/web3.js' | ||
import { NATIVE_MINT } from '@solana/spl-token' | ||
import axios, { AxiosResponse } from 'axios' | ||
import { connection, owner } from '../config' | ||
import { | ||
API_URLS, | ||
ApiSwapV1Out, | ||
USDCMint, | ||
PoolKeys, | ||
getATAAddress, | ||
swapBaseInAutoAccount, | ||
ALL_PROGRAM_ID, | ||
printSimulate, | ||
addComputeBudget, | ||
} from '@raydium-io/raydium-sdk-v2' | ||
import BN from 'bn.js' | ||
|
||
export const apiSwap = async () => { | ||
// const inputMint = NATIVE_MINT.toBase58() | ||
// const outputMint = USDCMint.toBase58() | ||
|
||
const inputMint = USDCMint.toBase58() | ||
const outputMint = NATIVE_MINT.toBase58() | ||
const amount = 1000000 | ||
const slippage = 0.5 // in percent, for this example, 0.5 means 0.5% | ||
const txVersion: 'LEGACY' | 'VO' = 'LEGACY' | ||
|
||
const { data: swapResponse } = await axios.get<ApiSwapV1Out>( | ||
`${ | ||
API_URLS.SWAP_HOST | ||
}/compute/swap-base-out?inputMint=${inputMint}&outputMint=${outputMint}&amount=${amount}&slippageBps=${ | ||
slippage * 100 | ||
}&txVersion=${txVersion}` | ||
) | ||
|
||
if (!swapResponse.success) { | ||
throw new Error(swapResponse.msg) | ||
} | ||
|
||
const res = await axios.get<AxiosResponse<PoolKeys[]>>( | ||
API_URLS.BASE_HOST + API_URLS.POOL_KEY_BY_ID + `?ids=${swapResponse.data.routePlan.map((r) => r.poolId).join(',')}` | ||
) | ||
|
||
const allMints = res.data.data.map((r) => [r.mintA, r.mintB]).flat() | ||
const [mintAProgram, mintBProgram] = [ | ||
allMints.find((m) => m.address === inputMint)!.programId, | ||
allMints.find((m) => m.address === outputMint)!.programId, | ||
] | ||
|
||
// get input/output token account ata | ||
// please ensure your input token account has balance | ||
const inputAccount = getATAAddress(owner.publicKey, new PublicKey(inputMint), new PublicKey(mintAProgram)).publicKey | ||
const outputAccount = getATAAddress(owner.publicKey, new PublicKey(outputMint), new PublicKey(mintBProgram)).publicKey | ||
|
||
const ins = swapBaseInAutoAccount({ | ||
programId: ALL_PROGRAM_ID.Router, | ||
wallet: owner.publicKey, | ||
amount: new BN(amount), | ||
inputAccount, | ||
outputAccount, | ||
routeInfo: swapResponse, | ||
poolKeys: res.data.data, | ||
}) | ||
|
||
const recentBlockhash = (await connection.getLatestBlockhash()).blockhash | ||
const tx = new Transaction() | ||
|
||
// set up compute units | ||
const { instructions } = addComputeBudget({ | ||
units: 600000, | ||
microLamports: 6000000, | ||
}) | ||
instructions.forEach((ins) => tx.add(ins)) | ||
|
||
tx.add(ins) | ||
tx.feePayer = owner.publicKey | ||
tx.recentBlockhash = recentBlockhash | ||
tx.sign(owner) | ||
|
||
printSimulate([tx]) | ||
} | ||
apiSwap() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Transaction, PublicKey } from '@solana/web3.js' | ||
import { NATIVE_MINT } from '@solana/spl-token' | ||
import axios, { AxiosResponse } from 'axios' | ||
import { connection, owner } from '../config' | ||
import { | ||
API_URLS, | ||
ApiSwapV1Out, | ||
USDCMint, | ||
PoolKeys, | ||
getATAAddress, | ||
swapBaseOutAutoAccount, | ||
ALL_PROGRAM_ID, | ||
printSimulate, | ||
addComputeBudget, | ||
} from '@raydium-io/raydium-sdk-v2' | ||
import BN from 'bn.js' | ||
|
||
export const swapBaseOutInstruction = async () => { | ||
const inputMint = USDCMint.toBase58() | ||
const outputMint = NATIVE_MINT.toBase58() | ||
const amount = 1000000 | ||
const slippage = 0.5 // in percent, for this example, 0.5 means 0.5% | ||
const txVersion: 'LEGACY' | 'VO' = 'LEGACY' | ||
|
||
const { data: swapResponse } = await axios.get<ApiSwapV1Out>( | ||
`${ | ||
API_URLS.SWAP_HOST | ||
}/compute/swap-base-out?inputMint=${inputMint}&outputMint=${outputMint}&amount=${amount}&slippageBps=${ | ||
slippage * 100 | ||
}&txVersion=${txVersion}` | ||
) | ||
|
||
if (!swapResponse.success) { | ||
throw new Error(swapResponse.msg) | ||
} | ||
|
||
const res = await axios.get<AxiosResponse<PoolKeys[]>>( | ||
API_URLS.BASE_HOST + API_URLS.POOL_KEY_BY_ID + `?ids=${swapResponse.data.routePlan.map((r) => r.poolId).join(',')}` | ||
) | ||
|
||
const allMints = res.data.data.map((r) => [r.mintA, r.mintB]).flat() | ||
const [mintAProgram, mintBProgram] = [ | ||
allMints.find((m) => m.address === inputMint)!.programId, | ||
allMints.find((m) => m.address === outputMint)!.programId, | ||
] | ||
|
||
// get input/output token account ata | ||
const inputAccount = getATAAddress(owner.publicKey, new PublicKey(inputMint), new PublicKey(mintAProgram)).publicKey | ||
const outputAccount = getATAAddress(owner.publicKey, new PublicKey(outputMint), new PublicKey(mintBProgram)).publicKey | ||
|
||
const ins = swapBaseOutAutoAccount({ | ||
programId: ALL_PROGRAM_ID.Router, | ||
wallet: owner.publicKey, | ||
inputAccount, | ||
outputAccount, | ||
routeInfo: swapResponse, | ||
poolKeys: res.data.data, | ||
}) | ||
|
||
const { instructions } = addComputeBudget({ | ||
units: 600000, | ||
microLamports: 6000000, | ||
}) | ||
const recentBlockhash = (await connection.getLatestBlockhash()).blockhash | ||
const tx = new Transaction() | ||
instructions.forEach((ins) => tx.add(ins)) | ||
tx.add(ins) | ||
tx.feePayer = owner.publicKey | ||
tx.recentBlockhash = recentBlockhash | ||
tx.sign(owner) | ||
|
||
printSimulate([tx]) | ||
} | ||
swapBaseOutInstruction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,10 +202,10 @@ | |
"@pnpm/network.ca-file" "^1.0.1" | ||
config-chain "^1.1.11" | ||
|
||
"@raydium-io/[email protected].73-alpha": | ||
version "0.1.73-alpha" | ||
resolved "https://registry.yarnpkg.com/@raydium-io/raydium-sdk-v2/-/raydium-sdk-v2-0.1.73-alpha.tgz#1501e5c216f8f6ae7afe19880cf7ac751e528939" | ||
integrity sha512-qUhsEV/phGW6fwzHkdWcKa+qdomTouheo0xZTcoIZQNF0xAHQ4bNgnhi+Q28oD37+wuL+SzreKnbIRgE3r629Q== | ||
"@raydium-io/[email protected].92-alpha": | ||
version "0.1.92-alpha" | ||
resolved "https://registry.yarnpkg.com/@raydium-io/raydium-sdk-v2/-/raydium-sdk-v2-0.1.92-alpha.tgz#06b782725b3a7c5188d6fb0f0292cec6f0af12ed" | ||
integrity sha512-oCI2a+eN0JafnBa4zcEYBd9PUzqIVFjDnonXqUQF2WKcSiYRAdZq2zRx8QgVh7bZTK1Q4Jetjvw3wT2s4bZoig== | ||
dependencies: | ||
"@solana/buffer-layout" "^4.0.1" | ||
"@solana/spl-token" "^0.4.8" | ||
|
@@ -215,6 +215,7 @@ | |
bn.js "^5.2.1" | ||
dayjs "^1.11.5" | ||
decimal.js-light "^2.5.1" | ||
jsonfile "^6.1.0" | ||
lodash "^4.17.21" | ||
toformat "^2.0.0" | ||
tsconfig-paths "^4.2.0" | ||
|