@deserialize/swap-sdk 0.0.2 → 0.0.3
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.
- package/README.md +145 -0
- package/dist/swapSDK.d.ts +36 -3
- package/dist/swapSDK.js +48 -7
- package/dist/swapSDK.js.map +1 -1
- package/package.json +3 -2
- package/src/swapSDK.ts +71 -9
- package/.env +0 -1
package/README.md
CHANGED
@@ -1 +1,146 @@
|
|
1
1
|
# deserialize-swap-sdk
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
# Swap SDK
|
6
|
+
|
7
|
+
The **Swap SDK** is a TypeScript library that simplifies interacting with a Solana swap endpoint. It abstracts away the low-level details of converting between Solana types (e.g., `PublicKey`, `Keypair`) and the API’s JSON formats. Use it to easily construct swap transactions or retrieve underlying instructions for advanced scenarios.
|
8
|
+
|
9
|
+
## Features
|
10
|
+
|
11
|
+
- **Simple API:**
|
12
|
+
Accepts Solana `PublicKey` objects and numerical amounts while handling all necessary conversions.
|
13
|
+
- **Transaction Deserialization & Signing:**
|
14
|
+
Automatically converts a base64-encoded transaction from the API into a `VersionedTransaction` and applies signatures.
|
15
|
+
- **Flexible Usage:**
|
16
|
+
Choose between getting a fully constructed transaction (using `swapTx`) or the raw instructions (using `swapIx`) for manual transaction assembly.
|
17
|
+
- **Custom Routing Options:**
|
18
|
+
Pass options (e.g., limiting swap hops) to tailor the swap behavior to your needs.
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Install the package via npm (or yarn):
|
23
|
+
|
24
|
+
```bash
|
25
|
+
npm install swap-sdk
|
26
|
+
# or
|
27
|
+
yarn add swap-sdk
|
28
|
+
```
|
29
|
+
|
30
|
+
*Note:* Replace `swap-sdk` with the actual package name when you publish it.
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
Below is a simple example of how to use the SDK in your project:
|
35
|
+
|
36
|
+
```typescript
|
37
|
+
// index.ts
|
38
|
+
import { SwapSDK } from "@deserialize/swap-sdk";
|
39
|
+
import { Buffer } from "buffer";
|
40
|
+
(async () => {
|
41
|
+
const deserialize = new SwapSDK();
|
42
|
+
|
43
|
+
const privKey = "PRIVATE_KEY";
|
44
|
+
|
45
|
+
const privateKeyArray = deserialize.base58.decode(privKey);
|
46
|
+
const userKeyPair = deserialize.web3.Keypair.fromSecretKey(privateKeyArray);
|
47
|
+
|
48
|
+
const params = {
|
49
|
+
tokenB: new deserialize.web3.PublicKey(
|
50
|
+
"BeRUj3h7BqkbdfFU7FBNYbodgf8GCHodzKvF9aVjNNfL"
|
51
|
+
),
|
52
|
+
tokenA: new deserialize.web3.PublicKey(
|
53
|
+
"GU7NS9xCwgNPiAdJ69iusFrRfawjDDPjeMBovhV1d4kn"
|
54
|
+
),
|
55
|
+
publicKey: new deserialize.web3.PublicKey(
|
56
|
+
"8PE7zNHVmn1zmFqNxPHpgjriDd8MNfTHWadPKenYDQX2"
|
57
|
+
),
|
58
|
+
amountIn: 0.0028,
|
59
|
+
dexId: "INVARIANT",
|
60
|
+
options: {
|
61
|
+
reduceToTwoHops: false, //set to true if you always want two hops
|
62
|
+
},
|
63
|
+
};
|
64
|
+
const response = await deserialize.swapTx(params);
|
65
|
+
|
66
|
+
console.log("Instructions:", response);
|
67
|
+
//sign and simulate the transaction
|
68
|
+
|
69
|
+
const connection = new deserialize.web3.Connection(
|
70
|
+
"https:///eclipse.lgns.net",
|
71
|
+
"confirmed"
|
72
|
+
);
|
73
|
+
const tx = response.transaction;
|
74
|
+
|
75
|
+
const serializedTx = Buffer.from(tx.serialize()).toString("base64");
|
76
|
+
console.log("serializedTx: ", serializedTx);
|
77
|
+
|
78
|
+
const { value } = await connection.simulateTransaction(tx);
|
79
|
+
console.log("value: ", value);
|
80
|
+
|
81
|
+
tx.sign([userKeyPair]);
|
82
|
+
const sign = await connection.sendTransaction(tx, { skipPreflight: false });
|
83
|
+
console.log("sign: ", sign);
|
84
|
+
})();
|
85
|
+
|
86
|
+
```
|
87
|
+
|
88
|
+
## API Reference
|
89
|
+
|
90
|
+
### SwapSDK
|
91
|
+
|
92
|
+
#### Constructor
|
93
|
+
|
94
|
+
```typescript
|
95
|
+
new SwapSDK(baseUrl?: string)
|
96
|
+
```
|
97
|
+
|
98
|
+
|
99
|
+
#### Methods
|
100
|
+
|
101
|
+
- **`swapTx(params: SwapRequestParams): Promise<SwapTxResult>`**
|
102
|
+
Sends a swap request and returns a fully constructed, unsigned `VersionedTransaction` along with:
|
103
|
+
- `amountOut`: Raw output amount.
|
104
|
+
- `amountOutUi`: Human-readable output amount.
|
105
|
+
- `routePlan`: An array of route steps (with tokens as `PublicKey`).
|
106
|
+
- `lookupAccounts`: Lookup accounts as `PublicKey` objects.
|
107
|
+
- `signers`: Signers as `Keypair` objects.
|
108
|
+
|
109
|
+
- **`swapIx(params: SwapRequestParams): Promise<SwapIxResult>`**
|
110
|
+
Sends a swap request and returns the underlying instruction groups and additional swap details:
|
111
|
+
- `instructionGroups`: Groups of main and cleanup instructions (as `TransactionInstruction` objects).
|
112
|
+
- `amountOut`: Raw output amount.
|
113
|
+
- `amountOutUi`: Human-readable output amount.
|
114
|
+
- `routePlan`: An array of route steps (with tokens as `PublicKey`).
|
115
|
+
- `lookupAccounts`: Lookup accounts as `PublicKey` objects.
|
116
|
+
- `signers`: Top-level signers as base64-encoded strings.
|
117
|
+
|
118
|
+
### Interfaces
|
119
|
+
|
120
|
+
#### SwapRequestParams
|
121
|
+
|
122
|
+
- **`publicKey`**: User's public key (`PublicKey`).
|
123
|
+
- **`tokenA`**: Token A's address (`PublicKey`).
|
124
|
+
- **`tokenB`**: Token B's address (`PublicKey`).
|
125
|
+
- **`amountIn`**: Amount of token A to swap (number, in human-readable units).
|
126
|
+
- **`dexId`**: `"INVARIANT"` (string literal).
|
127
|
+
- **`options?`**: Optional routing options (`RouteOptions`).
|
128
|
+
|
129
|
+
#### RouteOptions
|
130
|
+
|
131
|
+
- **`reduceToTwoHops`**: `boolean` — if set to `true`, limits the swap to two hops to avoid errors like *TooManyAccountLocks*.
|
132
|
+
|
133
|
+
#### SwapTxResult & SwapIxResult
|
134
|
+
|
135
|
+
Both results include the output amounts, route plan, and lookup accounts; the key difference is that `SwapTxResult` returns a full transaction (as a `VersionedTransaction`), whereas `SwapIxResult` returns raw instruction groups.
|
136
|
+
|
137
|
+
## License
|
138
|
+
|
139
|
+
This project is licensed under the [MIT License](LICENSE).
|
140
|
+
|
141
|
+
---
|
142
|
+
|
143
|
+
Happy swapping!
|
144
|
+
```
|
145
|
+
|
146
|
+
---
|
package/dist/swapSDK.d.ts
CHANGED
@@ -13,7 +13,8 @@ export interface SwapRequestParams {
|
|
13
13
|
amountIn: number;
|
14
14
|
/** DEX identifier – currently only "INVARIANT" is supported */
|
15
15
|
dexId: string;
|
16
|
-
/**
|
16
|
+
/**
|
17
|
+
* This is used to set options like limit the swap to just two hops to
|
17
18
|
* prevent errors like TooManyAccountLocks
|
18
19
|
*/
|
19
20
|
options?: RouteOptions;
|
@@ -75,7 +76,18 @@ export interface SwapIxResult {
|
|
75
76
|
signers: string[];
|
76
77
|
}
|
77
78
|
/**
|
78
|
-
*
|
79
|
+
* Token details returned from the /tokenList endpoint.
|
80
|
+
*/
|
81
|
+
export interface Token {
|
82
|
+
name: string;
|
83
|
+
symbol: string;
|
84
|
+
address: PublicKey;
|
85
|
+
chainId: number;
|
86
|
+
decimals: number;
|
87
|
+
logoURL: string;
|
88
|
+
}
|
89
|
+
/**
|
90
|
+
* SwapSDK simplifies calling the swap endpoint as well as token-related endpoints.
|
79
91
|
*
|
80
92
|
* Usage:
|
81
93
|
* ```ts
|
@@ -90,7 +102,7 @@ export interface SwapIxResult {
|
|
90
102
|
* tokenA: new PublicKey("GU7NS9xCwgNPiAdJ69iusFrRfawjDDPjeMBovhV1d4kn"),
|
91
103
|
* tokenB: new PublicKey("CEBP3CqAbW4zdZA57H2wfaSG1QNdzQ72GiQEbQXyW9Tm"),
|
92
104
|
* amountIn: 10.0,
|
93
|
-
* dexId: "INVARIANT"
|
105
|
+
* dexId: "INVARIANT",
|
94
106
|
* };
|
95
107
|
*
|
96
108
|
* // To get the full transaction:
|
@@ -100,6 +112,14 @@ export interface SwapIxResult {
|
|
100
112
|
* // Or, to get the underlying instructions and additional data:
|
101
113
|
* const ixResult = await sdk.swapIx(params);
|
102
114
|
* console.log("Instructions:", ixResult.instructionGroups);
|
115
|
+
*
|
116
|
+
* // Fetch token list:
|
117
|
+
* const tokens = await sdk.tokenList();
|
118
|
+
* console.log("Tokens:", tokens);
|
119
|
+
*
|
120
|
+
* // Fetch token price:
|
121
|
+
* const price = await sdk.tokenPrice(new PublicKey("So11111111111111111111111111111111111111112"));
|
122
|
+
* console.log("Token Price:", price);
|
103
123
|
* }
|
104
124
|
*
|
105
125
|
* runSwap();
|
@@ -131,6 +151,19 @@ export declare class SwapSDK {
|
|
131
151
|
* @returns A promise that resolves to a SwapIxResult.
|
132
152
|
*/
|
133
153
|
swapIx(params: SwapRequestParams): Promise<SwapIxResult>;
|
154
|
+
/**
|
155
|
+
* Calls the /tokenList endpoint and returns an array of tokens.
|
156
|
+
*
|
157
|
+
* @returns A promise that resolves to an array of Token objects.
|
158
|
+
*/
|
159
|
+
tokenList(): Promise<Token[]>;
|
160
|
+
/**
|
161
|
+
* Calls the /tokenPrice/{tokenAddress} endpoint and returns the token price.
|
162
|
+
*
|
163
|
+
* @param tokenAddress The token's public key.
|
164
|
+
* @returns A promise that resolves to the token's price (number).
|
165
|
+
*/
|
166
|
+
tokenPrice(tokenAddress: PublicKey): Promise<number>;
|
134
167
|
/**
|
135
168
|
* Converts an API instruction (in plain JSON format) to a TransactionInstruction.
|
136
169
|
*
|
package/dist/swapSDK.js
CHANGED
@@ -41,7 +41,7 @@ exports.BASE_URL = exports.SwapSDK = void 0;
|
|
41
41
|
const web3_js_1 = __importStar(require("@solana/web3.js"));
|
42
42
|
const bs58_1 = __importDefault(require("bs58"));
|
43
43
|
/**
|
44
|
-
* SwapSDK simplifies calling the swap endpoint.
|
44
|
+
* SwapSDK simplifies calling the swap endpoint as well as token-related endpoints.
|
45
45
|
*
|
46
46
|
* Usage:
|
47
47
|
* ```ts
|
@@ -56,7 +56,7 @@ const bs58_1 = __importDefault(require("bs58"));
|
|
56
56
|
* tokenA: new PublicKey("GU7NS9xCwgNPiAdJ69iusFrRfawjDDPjeMBovhV1d4kn"),
|
57
57
|
* tokenB: new PublicKey("CEBP3CqAbW4zdZA57H2wfaSG1QNdzQ72GiQEbQXyW9Tm"),
|
58
58
|
* amountIn: 10.0,
|
59
|
-
* dexId: "INVARIANT"
|
59
|
+
* dexId: "INVARIANT",
|
60
60
|
* };
|
61
61
|
*
|
62
62
|
* // To get the full transaction:
|
@@ -66,6 +66,14 @@ const bs58_1 = __importDefault(require("bs58"));
|
|
66
66
|
* // Or, to get the underlying instructions and additional data:
|
67
67
|
* const ixResult = await sdk.swapIx(params);
|
68
68
|
* console.log("Instructions:", ixResult.instructionGroups);
|
69
|
+
*
|
70
|
+
* // Fetch token list:
|
71
|
+
* const tokens = await sdk.tokenList();
|
72
|
+
* console.log("Tokens:", tokens);
|
73
|
+
*
|
74
|
+
* // Fetch token price:
|
75
|
+
* const price = await sdk.tokenPrice(new PublicKey("So11111111111111111111111111111111111111112"));
|
76
|
+
* console.log("Token Price:", price);
|
69
77
|
* }
|
70
78
|
*
|
71
79
|
* runSwap();
|
@@ -113,7 +121,7 @@ class SwapSDK {
|
|
113
121
|
*/
|
114
122
|
async swapTx(params) {
|
115
123
|
const data = await this.callSwapEndpoint(params);
|
116
|
-
// Convert the base64-encoded transaction into a
|
124
|
+
// Convert the base64-encoded transaction into a VersionedTransaction.
|
117
125
|
const txBuffer = Buffer.from(data.transaction, "base64");
|
118
126
|
const transaction = web3_js_1.VersionedTransaction.deserialize(txBuffer);
|
119
127
|
// Convert lookup accounts from strings to PublicKey objects.
|
@@ -124,9 +132,8 @@ class SwapSDK {
|
|
124
132
|
tokenB: new web3_js_1.PublicKey(rp.tokenB),
|
125
133
|
dexId: rp.dexId,
|
126
134
|
}));
|
127
|
-
|
128
|
-
|
129
|
-
});
|
135
|
+
// Convert each base64-encoded signer secret into a Keypair.
|
136
|
+
const signers = data.signers.map((s) => web3_js_1.Keypair.fromSecretKey(bs58_1.default.decode(s)));
|
130
137
|
transaction.sign(signers);
|
131
138
|
return {
|
132
139
|
transaction,
|
@@ -134,7 +141,7 @@ class SwapSDK {
|
|
134
141
|
amountOutUi: Number(data.amountOutUi),
|
135
142
|
routePlan,
|
136
143
|
lookupAccounts,
|
137
|
-
signers
|
144
|
+
signers,
|
138
145
|
};
|
139
146
|
}
|
140
147
|
/**
|
@@ -168,6 +175,40 @@ class SwapSDK {
|
|
168
175
|
signers: data.signers,
|
169
176
|
};
|
170
177
|
}
|
178
|
+
/**
|
179
|
+
* Calls the /tokenList endpoint and returns an array of tokens.
|
180
|
+
*
|
181
|
+
* @returns A promise that resolves to an array of Token objects.
|
182
|
+
*/
|
183
|
+
async tokenList() {
|
184
|
+
const response = await fetch(`${this.baseUrl}/tokenList`);
|
185
|
+
if (!response.ok) {
|
186
|
+
throw new Error(`API Error: ${response.statusText}`);
|
187
|
+
}
|
188
|
+
const data = await response.json();
|
189
|
+
return data.map((token) => ({
|
190
|
+
name: token.name,
|
191
|
+
symbol: token.symbol,
|
192
|
+
address: new web3_js_1.PublicKey(token.address),
|
193
|
+
chainId: token.chainId,
|
194
|
+
decimals: token.decimals,
|
195
|
+
logoURL: token.logoURL,
|
196
|
+
}));
|
197
|
+
}
|
198
|
+
/**
|
199
|
+
* Calls the /tokenPrice/{tokenAddress} endpoint and returns the token price.
|
200
|
+
*
|
201
|
+
* @param tokenAddress The token's public key.
|
202
|
+
* @returns A promise that resolves to the token's price (number).
|
203
|
+
*/
|
204
|
+
async tokenPrice(tokenAddress) {
|
205
|
+
const response = await fetch(`${this.baseUrl}/tokenPrice/${tokenAddress.toBase58()}`);
|
206
|
+
if (!response.ok) {
|
207
|
+
throw new Error(`API Error: ${response.statusText}`);
|
208
|
+
}
|
209
|
+
const data = await response.json();
|
210
|
+
return Number(data.price);
|
211
|
+
}
|
171
212
|
/**
|
172
213
|
* Converts an API instruction (in plain JSON format) to a TransactionInstruction.
|
173
214
|
*
|
package/dist/swapSDK.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"swapSDK.js","sourceRoot":"","sources":["../src/swapSDK.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,aAAa;AACb,2DAOyB;AACzB,gDAA0B;AA+
|
1
|
+
{"version":3,"file":"swapSDK.js","sourceRoot":"","sources":["../src/swapSDK.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,aAAa;AACb,2DAOyB;AACzB,gDAA0B;AA+F1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAa,OAAO;IAElB,YAAY,OAAgB;QAoC5B,WAAM,GAAG,cAAM,CAAC;QAChB,SAAI,GAAG,iBAAI,CAAC;QApCV,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,gBAAQ,CAAC;IACrC,CAAC;IAED;;;;;OAKG;IACK,KAAK,CAAC,gBAAgB,CAAC,MAAyB;QACtD,iEAAiE;QACjE,MAAM,IAAI,GAAG;YACX,SAAS,EAAE,MAAM,CAAC,SAAS,CAAC,QAAQ,EAAE;YACtC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE;YAChC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE;YAChC,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,QAAQ,EAAE;YACpC,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,OAAO,EAAE,MAAM,CAAC,OAAO;SACxB,CAAC;QAEF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,OAAO,gBAAgB,EAAE;YAC5D,MAAM,EAAE,MAAM;YAC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,EAAE,EAAE,CACxD,CAAC;QACF,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,cAAc,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC;QACvD,CAAC;QACD,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACnC,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED;;;;;OAKG;IACK,qBAAqB,CAAC,OAAY;QACxC,OAAO,IAAI,gCAAsB,CAAC;YAChC,IAAI,EAAE,CAAC,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAQ,EAAE,EAAE,CAAC,CAAC;gBAC5C,MAAM,EAAE,IAAI,mBAAS,CAAC,GAAG,CAAC,MAAM,CAAC;gBACjC,QAAQ,EAAE,GAAG,CAAC,QAAQ;gBACtB,UAAU,EAAE,GAAG,CAAC,UAAU;aAC3B,CAAC,CAAC;YACH,SAAS,EAAE,IAAI,mBAAS,CAAC,OAAO,CAAC,SAAS,CAAC;YAC3C,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;SAChC,CAAC,CAAC;IACL,CAAC;CACF;AApLD,0BAoLC;AAEY,QAAA,QAAQ,GAAG,4BAA4B,CAAC"}
|
package/package.json
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
{
|
2
2
|
"name": "@deserialize/swap-sdk",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.3",
|
4
4
|
"description": "Swap SDK for the deserialize aggregator",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
7
7
|
"scripts": {
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1",
|
9
|
-
"build": "tsc"
|
9
|
+
"build": "tsc",
|
10
|
+
"publish": "npm run build && npm publish --access=public"
|
10
11
|
},
|
11
12
|
"repository": {
|
12
13
|
"type": "git",
|
package/src/swapSDK.ts
CHANGED
@@ -8,6 +8,7 @@ import web3, {
|
|
8
8
|
VersionedTransaction,
|
9
9
|
} from "@solana/web3.js";
|
10
10
|
import base58 from "bs58";
|
11
|
+
|
11
12
|
/**
|
12
13
|
* Parameters to request a swap.
|
13
14
|
*/
|
@@ -22,14 +23,17 @@ export interface SwapRequestParams {
|
|
22
23
|
amountIn: number;
|
23
24
|
/** DEX identifier – currently only "INVARIANT" is supported */
|
24
25
|
dexId: string;
|
25
|
-
/**
|
26
|
+
/**
|
27
|
+
* This is used to set options like limit the swap to just two hops to
|
26
28
|
* prevent errors like TooManyAccountLocks
|
27
29
|
*/
|
28
30
|
options?: RouteOptions;
|
29
31
|
}
|
32
|
+
|
30
33
|
export interface RouteOptions {
|
31
34
|
reduceToTwoHops: boolean;
|
32
35
|
}
|
36
|
+
|
33
37
|
/**
|
34
38
|
* Result from the swapTx method.
|
35
39
|
*/
|
@@ -87,7 +91,19 @@ export interface SwapIxResult {
|
|
87
91
|
}
|
88
92
|
|
89
93
|
/**
|
90
|
-
*
|
94
|
+
* Token details returned from the /tokenList endpoint.
|
95
|
+
*/
|
96
|
+
export interface Token {
|
97
|
+
name: string;
|
98
|
+
symbol: string;
|
99
|
+
address: PublicKey;
|
100
|
+
chainId: number;
|
101
|
+
decimals: number;
|
102
|
+
logoURL: string;
|
103
|
+
}
|
104
|
+
|
105
|
+
/**
|
106
|
+
* SwapSDK simplifies calling the swap endpoint as well as token-related endpoints.
|
91
107
|
*
|
92
108
|
* Usage:
|
93
109
|
* ```ts
|
@@ -102,7 +118,7 @@ export interface SwapIxResult {
|
|
102
118
|
* tokenA: new PublicKey("GU7NS9xCwgNPiAdJ69iusFrRfawjDDPjeMBovhV1d4kn"),
|
103
119
|
* tokenB: new PublicKey("CEBP3CqAbW4zdZA57H2wfaSG1QNdzQ72GiQEbQXyW9Tm"),
|
104
120
|
* amountIn: 10.0,
|
105
|
-
* dexId: "INVARIANT"
|
121
|
+
* dexId: "INVARIANT",
|
106
122
|
* };
|
107
123
|
*
|
108
124
|
* // To get the full transaction:
|
@@ -112,6 +128,14 @@ export interface SwapIxResult {
|
|
112
128
|
* // Or, to get the underlying instructions and additional data:
|
113
129
|
* const ixResult = await sdk.swapIx(params);
|
114
130
|
* console.log("Instructions:", ixResult.instructionGroups);
|
131
|
+
*
|
132
|
+
* // Fetch token list:
|
133
|
+
* const tokens = await sdk.tokenList();
|
134
|
+
* console.log("Tokens:", tokens);
|
135
|
+
*
|
136
|
+
* // Fetch token price:
|
137
|
+
* const price = await sdk.tokenPrice(new PublicKey("So11111111111111111111111111111111111111112"));
|
138
|
+
* console.log("Token Price:", price);
|
115
139
|
* }
|
116
140
|
*
|
117
141
|
* runSwap();
|
@@ -156,7 +180,6 @@ export class SwapSDK {
|
|
156
180
|
}
|
157
181
|
|
158
182
|
base58 = base58;
|
159
|
-
|
160
183
|
web3 = web3;
|
161
184
|
|
162
185
|
/**
|
@@ -168,7 +191,7 @@ export class SwapSDK {
|
|
168
191
|
async swapTx(params: SwapRequestParams): Promise<SwapTxResult> {
|
169
192
|
const data = await this.callSwapEndpoint(params);
|
170
193
|
|
171
|
-
// Convert the base64-encoded transaction into a
|
194
|
+
// Convert the base64-encoded transaction into a VersionedTransaction.
|
172
195
|
const txBuffer = Buffer.from(data.transaction, "base64");
|
173
196
|
const transaction = VersionedTransaction.deserialize(txBuffer);
|
174
197
|
|
@@ -184,9 +207,10 @@ export class SwapSDK {
|
|
184
207
|
dexId: rp.dexId,
|
185
208
|
}));
|
186
209
|
|
187
|
-
|
188
|
-
|
189
|
-
|
210
|
+
// Convert each base64-encoded signer secret into a Keypair.
|
211
|
+
const signers = data.signers.map((s: string) =>
|
212
|
+
Keypair.fromSecretKey(base58.decode(s))
|
213
|
+
);
|
190
214
|
transaction.sign(signers);
|
191
215
|
return {
|
192
216
|
transaction,
|
@@ -194,7 +218,7 @@ export class SwapSDK {
|
|
194
218
|
amountOutUi: Number(data.amountOutUi),
|
195
219
|
routePlan,
|
196
220
|
lookupAccounts,
|
197
|
-
signers
|
221
|
+
signers,
|
198
222
|
};
|
199
223
|
}
|
200
224
|
|
@@ -242,6 +266,44 @@ export class SwapSDK {
|
|
242
266
|
};
|
243
267
|
}
|
244
268
|
|
269
|
+
/**
|
270
|
+
* Calls the /tokenList endpoint and returns an array of tokens.
|
271
|
+
*
|
272
|
+
* @returns A promise that resolves to an array of Token objects.
|
273
|
+
*/
|
274
|
+
async tokenList(): Promise<Token[]> {
|
275
|
+
const response = await fetch(`${this.baseUrl}/tokenList`);
|
276
|
+
if (!response.ok) {
|
277
|
+
throw new Error(`API Error: ${response.statusText}`);
|
278
|
+
}
|
279
|
+
const data = await response.json();
|
280
|
+
return data.map((token: any) => ({
|
281
|
+
name: token.name,
|
282
|
+
symbol: token.symbol,
|
283
|
+
address: new PublicKey(token.address),
|
284
|
+
chainId: token.chainId,
|
285
|
+
decimals: token.decimals,
|
286
|
+
logoURL: token.logoURL,
|
287
|
+
}));
|
288
|
+
}
|
289
|
+
|
290
|
+
/**
|
291
|
+
* Calls the /tokenPrice/{tokenAddress} endpoint and returns the token price.
|
292
|
+
*
|
293
|
+
* @param tokenAddress The token's public key.
|
294
|
+
* @returns A promise that resolves to the token's price (number).
|
295
|
+
*/
|
296
|
+
async tokenPrice(tokenAddress: PublicKey): Promise<number> {
|
297
|
+
const response = await fetch(
|
298
|
+
`${this.baseUrl}/tokenPrice/${tokenAddress.toBase58()}`
|
299
|
+
);
|
300
|
+
if (!response.ok) {
|
301
|
+
throw new Error(`API Error: ${response.statusText}`);
|
302
|
+
}
|
303
|
+
const data = await response.json();
|
304
|
+
return Number(data.price);
|
305
|
+
}
|
306
|
+
|
245
307
|
/**
|
246
308
|
* Converts an API instruction (in plain JSON format) to a TransactionInstruction.
|
247
309
|
*
|
package/.env
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
DEV_KEY=""
|