@carrot-protocol/clend-rpc 0.0.1-mrgn-fork1-dev-7be6ef2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,109 @@
1
+ import { web3 } from "@coral-xyz/anchor";
2
+ import Decimal from "decimal.js";
3
+ import { QuoteResponse, SwapMode } from "@jup-ag/api";
4
+ import { IJupiterUtils, SwapInputs, SwapQuote, SwapIxs } from "./jupiterUtils";
5
+ import { JupiterUtils } from "./jupiterUtils";
6
+
7
+ /**
8
+ * MockJupiterUtils implements the IJupiterUtils interface for testing purposes.
9
+ * It allows for bypassing actual Jupiter API calls and returning predefined values.
10
+ */
11
+ export class MockJupiterUtils implements IJupiterUtils {
12
+ private mockSwaps: Map<string, SwapIxs>;
13
+ private realJupiter: JupiterUtils;
14
+
15
+ constructor() {
16
+ this.mockSwaps = new Map();
17
+ this.realJupiter = new JupiterUtils();
18
+ }
19
+
20
+ /**
21
+ * Set mock swap instructions
22
+ * @param payer Payer public key
23
+ * @param inputs Swap inputs
24
+ * @param swapIxs Swap instructions to return
25
+ */
26
+ setMockSwap(
27
+ payer: web3.PublicKey,
28
+ inputs: SwapInputs,
29
+ swapIxs: SwapIxs,
30
+ ): void {
31
+ const key = this.getSwapKey(payer, inputs);
32
+ this.mockSwaps.set(key, swapIxs);
33
+ }
34
+
35
+ /**
36
+ * Get Jupiter price for a token pair
37
+ * @param inputMint Input token mint
38
+ * @param outputMint Output token mint
39
+ * @returns Mocked price for the token pair
40
+ */
41
+ async getJupiterPrice(mint: web3.PublicKey | string): Promise<number> {
42
+ // Use real Jupiter API for price
43
+ return this.realJupiter.getJupiterPrice(mint);
44
+ }
45
+
46
+ /**
47
+ * Get Jupiter quote for a swap
48
+ * @param inputMint Input token mint
49
+ * @param outputMint Output token mint
50
+ * @param inputMintDecimals Input token decimals
51
+ * @param outputMintDecimals Output token decimals
52
+ * @param inputAmountLamports Input amount in lamports
53
+ * @param slippageBps Slippage in basis points
54
+ * @param swapMode Swap mode
55
+ * @returns Mocked quote for the swap
56
+ */
57
+ async getJupiterQuote(
58
+ inputMint: web3.PublicKey,
59
+ outputMint: web3.PublicKey,
60
+ inputMintDecimals: number,
61
+ outputMintDecimals: number,
62
+ inputAmountLamports: Decimal,
63
+ slippageBps: number,
64
+ swapMode?: SwapMode,
65
+ ): Promise<SwapQuote<QuoteResponse>> {
66
+ // Use real Jupiter API for quote
67
+ return this.realJupiter.getJupiterQuote(
68
+ inputMint,
69
+ outputMint,
70
+ inputMintDecimals,
71
+ outputMintDecimals,
72
+ inputAmountLamports,
73
+ slippageBps,
74
+ swapMode,
75
+ );
76
+ }
77
+
78
+ /**
79
+ * Get Jupiter swap instructions
80
+ * @param payer Payer public key
81
+ * @param inputs Swap inputs
82
+ * @param quote Swap quote
83
+ * @returns Mocked swap instructions
84
+ */
85
+ async getJupiterSwap(
86
+ payer: web3.PublicKey,
87
+ inputs: SwapInputs,
88
+ quote: SwapQuote<QuoteResponse>,
89
+ ): Promise<SwapIxs> {
90
+ const key = this.getSwapKey(payer, inputs);
91
+ const swapIxs = this.mockSwaps.get(key);
92
+
93
+ if (!swapIxs) {
94
+ throw new Error(`Mock swap not set for ${key}`);
95
+ }
96
+
97
+ return swapIxs;
98
+ }
99
+
100
+ /**
101
+ * Generate a key for swap lookups
102
+ * @param payer Payer public key
103
+ * @param inputs Swap inputs
104
+ * @returns Key for the swap
105
+ */
106
+ private getSwapKey(payer: web3.PublicKey, inputs: SwapInputs): string {
107
+ return `${payer.toString()}-${inputs.inputMint.toString()}-${inputs.outputMint.toString()}`;
108
+ }
109
+ }