Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonliuuu committed Jan 4, 2023
1 parent 377d903 commit 6481673
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 162 deletions.
33 changes: 29 additions & 4 deletions tests/testAdapterJupiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("Gateway", () => {
const zapInAmount = 10000;

it("Swap in Jupiter", async () => {
const swapParams: SwapParams = {
const swapParams1: SwapParams = {
protocol: SupportedProtocols.Jupiter,
fromTokenMint: new PublicKey(
// "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" // USDC
Expand All @@ -60,10 +60,34 @@ describe("Gateway", () => {
),
toTokenMint: new PublicKey(
// "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R" // RAY
// "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB" // USDT
"Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB" // USDT
// "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" // USDC
"SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt" // SRM
// "SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt" // SRM
// "So11111111111111111111111111111111111111112" // WSOL
// "GENEtH5amGSi8kHAtQoezp1XEXwZJ8vcuePYnXdKrMYz" // GENE
// "66edZnAPEJSxnAK4SckuupssXpbu5doV57FUcghaqPsY" // PRGC
// "7s6NLX42eURZfpyuKkVLrr9ED9hJE8718cyXFsYKqq5g" // GEAR
// "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263" // Bonk
),
amount: zapInAmount, // Swap half of the fromToken to proceed zapIn
slippage: 1,
jupiterMarketUrl: "https://rpc-mainnet-fork.epochs.studio/jup/market.json",
};

const swapParams2: SwapParams = {
protocol: SupportedProtocols.Jupiter,
fromTokenMint: new PublicKey(
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" // USDC
// "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R" // RAY
// "So11111111111111111111111111111111111111112" // WSOL
// "SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt" // SRM
),
toTokenMint: new PublicKey(
// "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R" // RAY
// "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB" // USDT
// "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" // USDC
// "SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt" // SRM
"So11111111111111111111111111111111111111112" // WSOL
// "GENEtH5amGSi8kHAtQoezp1XEXwZJ8vcuePYnXdKrMYz" // GENE
// "66edZnAPEJSxnAK4SckuupssXpbu5doV57FUcghaqPsY" // PRGC
// "7s6NLX42eURZfpyuKkVLrr9ED9hJE8718cyXFsYKqq5g" // GEAR
Expand All @@ -76,7 +100,8 @@ describe("Gateway", () => {

const gateway = new GatewayBuilder(provider);

await gateway.swap(swapParams);
await gateway.swap(swapParams1);
await gateway.swap(swapParams2);
await gateway.finalize();

console.log(gateway.params);
Expand Down
236 changes: 78 additions & 158 deletions tests/testAdapterRaydium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,42 @@ describe("Gateway", () => {

const zapInAmount = 10000;

it("Swap + AddLiquidity + Stake in Raydium", async () => {
it("Swap(Jupiter) + AddLiquidity(Raydium) + Stake(Raydium)", async () => {
const poolId = new anchor.web3.PublicKey(
// "GaqgfieVmnmY4ZsZHHA6L5RSVzCGL3sKx4UgHBaYNy8m" // RAY-SRM
"GaqgfieVmnmY4ZsZHHA6L5RSVzCGL3sKx4UgHBaYNy8m" // RAY-SRM
// "AVs9TA4nWDzfPJE9gGVNJMVhcQy3V9PGazuz33BfG2RA" // RAY-WSOL
// "DVa7Qmb5ct9RCpaU7UTpSaf3GVMYz17vNVU67XpdCRut" // RAY-USDT
// "6UmmUiYoBjSrhakAobJw8BvkmJtDVxaeBtbt7rxWo1mg" // RAY-USDC
// "Enq8vJucRbkzKA1i1PahJNhMyUTzoVL5Cs8n5rC3NLGn" // GENE-USDC (Pool V4)
// "8FrCybrh7UFznP1hVHg8kXZ8bhii37c7BGzmjkdcsGJp" // GENE-RAY (Pool V4)
// "DPgYdwgz7ZytfrzLeRYJwXq9JGeskaXTRrM8biFEnLs1" // PRGC-USDC
// "2N5HpqiZe2b5EKGhW1adx56chbWCAURHVqaH1gL9mppH" // GEAR-USDC
"58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2" // SOL-USDC
// "58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2" // SOL-USDC
);
const farmId = new anchor.web3.PublicKey(
// "5DFbcYNLLy5SJiBpCCDzNSs7cWCsUbYnCkLXzcPQiKnR" // RAY-SRM
"5DFbcYNLLy5SJiBpCCDzNSs7cWCsUbYnCkLXzcPQiKnR" // RAY-SRM
// "HUDr9BDaAGqi37xbQHzxCyXvfMCKPTPNF8g9c9bPu1Fu" // RAY-WSOL
// "AvbVWpBi2e4C9HPmZgShGdPoNydG4Yw8GJvG9HUcLgce" // RAY-USDT
// "CHYrUBX2RKX8iBg7gYTkccoGNBzP44LdaazMHCLcdEgS" // RAY-USDC
// "DDRNVVJBEXEemcprVVUcrTbYnR88JyN6jjT2ypgAQHC8" // GENE-USDC (Farm V5)
// "GVfLbXA3dpEHPvc4do9HvMZ8TACxm3x54BVrHPMEixcr" // GENE-RAY (Farm V5)
// "GBjTMHf9TsRdMnP6S3ewAgpSoCacpZqQF1tXmnchborv" // PRGC-USDC (Farm V5)
// "B7A3hAej7ZbAsVPM3M5ietDigQgxyucYPANJSGDVpQEw" // GEAR-USDC (Farm V5)
"GUzaohfNuFbBqQTnPgPSNciv3aUvriXYjQduRE3ZkqFw" // SOL-USDC
// "GUzaohfNuFbBqQTnPgPSNciv3aUvriXYjQduRE3ZkqFw" // SOL-USDC
);
const swapParams: SwapParams = {
protocol: SupportedProtocols.Jupiter,
fromTokenMint: new PublicKey(
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" // USDC
// "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R" // RAY
// "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" // USDC
"4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R" // RAY
// "So11111111111111111111111111111111111111112" // WSOL
),
toTokenMint: new PublicKey(
// "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R" // RAY
// "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB" // USDT
// "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" // USDC
// "SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt" // SRM
"So11111111111111111111111111111111111111112" // WSOL
"SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt" // SRM
// "So11111111111111111111111111111111111111112" // WSOL
// "GENEtH5amGSi8kHAtQoezp1XEXwZJ8vcuePYnXdKrMYz" // GENE
// "66edZnAPEJSxnAK4SckuupssXpbu5doV57FUcghaqPsY" // PRGC
// "7s6NLX42eURZfpyuKkVLrr9ED9hJE8718cyXFsYKqq5g" // GEAR
Expand All @@ -94,7 +95,7 @@ describe("Gateway", () => {
const stakeParams: StakeParams = {
protocol: SupportedProtocols.Raydium,
farmId,
version: 5,
version: 3,
};

const gateway = new GatewayBuilder(provider);
Expand All @@ -116,18 +117,33 @@ describe("Gateway", () => {

console.log("======");
console.log("Txs are sent...");
const recentBlockhash = await connection.getLatestBlockhash();
for (let tx of txs) {
const sig = await provider.sendAndConfirm(tx as anchor.web3.Transaction, [], {
skipPreflight: true,
commitment: "confirmed",
} as unknown as anchor.web3.ConfirmOptions);
console.log(sig);
if ((tx as anchor.web3.Transaction).instructions) {
const sig = await provider.sendAndConfirm(tx as anchor.web3.Transaction, [], {
skipPreflight: true,
commitment: "confirmed",
} as unknown as anchor.web3.ConfirmOptions);
console.log(sig);
} else {
const txV2 = tx as anchor.web3.VersionedTransaction;
txV2.message.recentBlockhash = recentBlockhash.blockhash;
console.log(txV2.serialize().length);
txV2.sign([wallet.payer]);
let versionMessage = txV2.serialize();
//const result = sendAndConfirmTransaction(connection, tx, wallet);
const sig = await connection.sendRawTransaction(versionMessage, {
skipPreflight: true,
commitment: "confirmed",
} as unknown as anchor.web3.ConfirmOptions);
console.log(sig);
}
}
console.log("Txs are executed");
console.log("======");
});

it("Unstake + Harvest + RemoveLiquidity + Swap in Raydium", async () => {
it("Unstake(Raydium) + Harvest(Raydium) + RemoveLiquidity(Raydium) + Swap(Jupiter)", async () => {
const poolId = new anchor.web3.PublicKey(
"GaqgfieVmnmY4ZsZHHA6L5RSVzCGL3sKx4UgHBaYNy8m" // RAY-SRM
// "AVs9TA4nWDzfPJE9gGVNJMVhcQy3V9PGazuz33BfG2RA" // RAY-WSOL
Expand All @@ -152,7 +168,7 @@ describe("Gateway", () => {
// Get share amount
const ledgerKey = await raydium.infos.getFarmerId(farm, provider.wallet.publicKey, farm.version);
const ledger = (await raydium.infos.getFarmer(connection, ledgerKey, farm.version)) as raydium.FarmerInfo;
const shareAmount = ledger.amount as number;
const shareAmount = Math.floor((ledger.amount as number) / 10);
console.log(shareAmount);

const { tokenAAmount, tokenBAmount } = await poolWrapper.getTokenAmounts(shareAmount);
Expand Down Expand Up @@ -188,8 +204,8 @@ describe("Gateway", () => {
// "SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt" // SRM
// "So11111111111111111111111111111111111111112" // WSOL
),
amount: tokenAAmount, // swap coin to pc
slippage: 3,
amount: Math.floor(tokenAAmount), // swap coin to pc
slippage: 1,
jupiterMarketUrl: "https://rpc-mainnet-fork.epochs.studio/jup/market.json",
};

Expand All @@ -210,144 +226,33 @@ describe("Gateway", () => {

console.log("======");
console.log("Txs are sent...");
// for (let tx of txs) {
// const sig = await provider.sendAndConfirm(tx, [], {
// skipPreflight: true,
// commitment: "confirmed",
// } as unknown as anchor.web3.ConfirmOptions);
// console.log(sig);
// }
console.log("Txs are executed");
console.log("======");
});

it("Directly Stake and Unstake in Raydium", async () => {
const gateway1 = new GatewayBuilder(provider);
const poolId = new anchor.web3.PublicKey(
"GaqgfieVmnmY4ZsZHHA6L5RSVzCGL3sKx4UgHBaYNy8m" // RAY-SRM
// "AVs9TA4nWDzfPJE9gGVNJMVhcQy3V9PGazuz33BfG2RA" // RAY-WSOL
// "DVa7Qmb5ct9RCpaU7UTpSaf3GVMYz17vNVU67XpdCRut" // RAY-USDT
// "6UmmUiYoBjSrhakAobJw8BvkmJtDVxaeBtbt7rxWo1mg" // RAY-USDC
);
const farmId = new anchor.web3.PublicKey(
"5DFbcYNLLy5SJiBpCCDzNSs7cWCsUbYnCkLXzcPQiKnR" // RAY-SRM
// "HUDr9BDaAGqi37xbQHzxCyXvfMCKPTPNF8g9c9bPu1Fu" // RAY-WSOL
// "AvbVWpBi2e4C9HPmZgShGdPoNydG4Yw8GJvG9HUcLgce" // RAY-USDT
// "CHYrUBX2RKX8iBg7gYTkccoGNBzP44LdaazMHCLcdEgS" // RAY-USDC
);
const swapParams: SwapParams = {
protocol: SupportedProtocols.Jupiter,
fromTokenMint: new PublicKey(
// "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" // USDC
"4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R" // RAY
),
toTokenMint: new PublicKey(
// "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB" // USDT
"SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt" // SRM
// "So11111111111111111111111111111111111111112" // WSOL
),
amount: zapInAmount / 2, // Swap half of the fromToken to proceed zapIn
slippage: 3,
jupiterMarketUrl: "https://rpc-mainnet-fork.epochs.studio/jup/market.json",
};
const addLiquidityParams: AddLiquidityParams = {
protocol: SupportedProtocols.Raydium,
poolId,
};

await gateway1.swap(swapParams);
await gateway1.addLiquidity(addLiquidityParams);

await gateway1.finalize();

console.log(gateway1.params);
// console.log(`swapInAmount: ${gateway1.params.swapInAmount}`);
// console.log(`swapMinOutAmount: ${gateway1.params.swapMinOutAmount}`);

const txs1 = gateway1.transactions();

console.log("======");
console.log("Txs are sent...");
// for (let tx of txs1) {
// const sig = await provider.sendAndConfirm(tx, [], {
// skipPreflight: true,
// commitment: "confirmed",
// } as unknown as anchor.web3.ConfirmOptions);
// console.log(sig);
// }
console.log("Txs are executed");
console.log("======");

// Get LP amount
const pool = await raydium.infos.getPool(connection, poolId);
const userLpAta = await getAssociatedTokenAddress(pool.lpMint, provider.wallet.publicKey);
const userLpAtaAccount = await getAccount(connection, userLpAta);
const lpAmount = new anchor.BN(userLpAtaAccount.amount.toString()).toNumber();
console.log(lpAmount);

// Stake
const gateway2 = new GatewayBuilder(provider);
const stakeParams: StakeParams = {
protocol: SupportedProtocols.Raydium,
farmId,
lpAmount,
};

await gateway2.stake(stakeParams);
await gateway2.finalize();

const txs2 = gateway2.transactions();

console.log(gateway2.params);

console.log("======");
console.log("Txs are sent...");
// for (let tx of txs2) {
// const sig = await provider.sendAndConfirm(tx, [], {
// skipPreflight: true,
// commitment: "confirmed",
// } as unknown as anchor.web3.ConfirmOptions);
// console.log(sig);
// }
console.log("Txs are executed");
console.log("======");

// Get share amount

const farm = (await raydium.infos.getFarm(connection, farmId)) as raydium.FarmInfo;
const ledgerKey = await raydium.infos.getFarmerId(farm, provider.wallet.publicKey, farm.version);
const ledger = await raydium.infos.getFarmer(connection, ledgerKey);
const shareAmount = ledger.amount as number;
console.log(shareAmount);

const gateway3 = new GatewayBuilder(provider);
const unstakeParams: UnstakeParams = {
protocol: SupportedProtocols.Raydium,
farmId,
shareAmount: shareAmount / 2, // Unstake only half
};

await gateway3.unstake(unstakeParams);
await gateway3.finalize();

const txs3 = gateway3.transactions();

console.log(gateway3.params);

console.log("======");
console.log("Txs are sent...");
// for (let tx of txs3) {
// const sig = await provider.sendAndConfirm(tx, [], {
// skipPreflight: true,
// commitment: "confirmed",
// } as unknown as anchor.web3.ConfirmOptions);
// console.log(sig);
// }
const recentBlockhash = await connection.getLatestBlockhash();
for (let tx of txs) {
if ((tx as anchor.web3.Transaction).instructions) {
const sig = await provider.sendAndConfirm(tx as anchor.web3.Transaction, [], {
skipPreflight: true,
commitment: "confirmed",
} as unknown as anchor.web3.ConfirmOptions);
console.log(sig);
} else {
const txV2 = tx as anchor.web3.VersionedTransaction;
txV2.message.recentBlockhash = recentBlockhash.blockhash;
console.log(txV2.serialize().length);
txV2.sign([wallet.payer]);
let versionMessage = txV2.serialize();
//const result = sendAndConfirmTransaction(connection, tx, wallet);
const sig = await connection.sendRawTransaction(versionMessage, {
skipPreflight: true,
commitment: "confirmed",
} as unknown as anchor.web3.ConfirmOptions);
console.log(sig);
}
}
console.log("Txs are executed");
console.log("======");
});

it("Swap (USDC => tokenA) + Swap (tokenA => tokenB) + AddLiquidity + Stake in Raydium", async () => {
it("Swap(USDC => tokenA) + Swap(USDC => tokenB) + AddLiquidity(Raydium) + Stake(Raydium)", async () => {
const gateway = new GatewayBuilder(provider);
const poolId = new anchor.web3.PublicKey(
// "GaqgfieVmnmY4ZsZHHA6L5RSVzCGL3sKx4UgHBaYNy8m" // RAY-SRM
Expand Down Expand Up @@ -427,12 +332,27 @@ describe("Gateway", () => {

console.log("======");
console.log("Txs are sent...");
const recentBlockhash = await connection.getLatestBlockhash();
for (let tx of txs) {
const sig = await provider.sendAndConfirm(tx as anchor.web3.Transaction, [], {
skipPreflight: true,
commitment: "confirmed",
} as unknown as anchor.web3.ConfirmOptions);
console.log(sig);
if ((tx as anchor.web3.Transaction).instructions) {
const sig = await provider.sendAndConfirm(tx as anchor.web3.Transaction, [], {
skipPreflight: true,
commitment: "confirmed",
} as unknown as anchor.web3.ConfirmOptions);
console.log(sig);
} else {
const txV2 = tx as anchor.web3.VersionedTransaction;
txV2.message.recentBlockhash = recentBlockhash.blockhash;
console.log(txV2.serialize().length);
txV2.sign([wallet.payer]);
let versionMessage = txV2.serialize();
//const result = sendAndConfirmTransaction(connection, tx, wallet);
const sig = await connection.sendRawTransaction(versionMessage, {
skipPreflight: true,
commitment: "confirmed",
} as unknown as anchor.web3.ConfirmOptions);
console.log(sig);
}
}
console.log("Txs are executed");
console.log("======");
Expand Down

0 comments on commit 6481673

Please sign in to comment.