@deserialize/swap-sdk 0.0.4 → 0.0.6
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 +5 -5
- package/dist/errors.d.ts +4 -0
- package/dist/errors.js +17 -0
- package/dist/errors.js.map +1 -0
- package/dist/swapSDK.d.ts +9 -1
- package/dist/swapSDK.js +16 -0
- package/dist/swapSDK.js.map +1 -1
- package/package.json +1 -1
- package/src/errors.ts +13 -0
- package/src/swapSDK.ts +21 -0
package/README.md
CHANGED
@@ -4,12 +4,12 @@
|
|
4
4
|
|
5
5
|
# Swap SDK
|
6
6
|
|
7
|
-
The **Swap SDK** is a TypeScript library that simplifies interacting with a
|
7
|
+
The **Swap SDK** is a TypeScript library that simplifies interacting with a Deserialize swap endpoint. It abstracts away the low-level details of converting between SVM 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
8
|
|
9
9
|
## Features
|
10
10
|
|
11
11
|
- **Simple API:**
|
12
|
-
Accepts
|
12
|
+
Accepts SVM `PublicKey` objects and numerical amounts while handling all necessary conversions.
|
13
13
|
- **Transaction Deserialization & Signing:**
|
14
14
|
Automatically converts a base64-encoded transaction from the API into a `VersionedTransaction` and applies signatures.
|
15
15
|
- **Flexible Usage:**
|
@@ -22,12 +22,11 @@ The **Swap SDK** is a TypeScript library that simplifies interacting with a Sola
|
|
22
22
|
Install the package via npm (or yarn):
|
23
23
|
|
24
24
|
```bash
|
25
|
-
npm install swap-sdk
|
25
|
+
npm install @deserialize/swap-sdk
|
26
26
|
# or
|
27
|
-
yarn add swap-sdk
|
27
|
+
yarn add @deserialize/swap-sdk
|
28
28
|
```
|
29
29
|
|
30
|
-
*Note:* Replace `swap-sdk` with the actual package name when you publish it.
|
31
30
|
|
32
31
|
## Usage
|
33
32
|
|
@@ -144,3 +143,4 @@ Happy swapping!
|
|
144
143
|
```
|
145
144
|
|
146
145
|
---
|
146
|
+
|
package/dist/errors.d.ts
ADDED
package/dist/errors.js
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.TooManyAccountsLockError = exports.handleSimulationError = void 0;
|
4
|
+
const handleSimulationError = (e) => {
|
5
|
+
if (e.toString().includes("TooManyAccountLocks")) {
|
6
|
+
throw new TooManyAccountsLockError("Too many account locks: consider using the option reduceToTwoHops in the options when getting the routes");
|
7
|
+
}
|
8
|
+
};
|
9
|
+
exports.handleSimulationError = handleSimulationError;
|
10
|
+
class TooManyAccountsLockError extends Error {
|
11
|
+
constructor(message) {
|
12
|
+
super(message);
|
13
|
+
this.name = "TooManyAccountsLockError";
|
14
|
+
}
|
15
|
+
}
|
16
|
+
exports.TooManyAccountsLockError = TooManyAccountsLockError;
|
17
|
+
//# sourceMappingURL=errors.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAO,MAAM,qBAAqB,GAAG,CAAC,CAAc,EAAE,EAAE;IACtD,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,qBAAqB,CAAC,EAAE,CAAC;QACjD,MAAM,IAAI,wBAAwB,CAChC,0GAA0G,CAC3G,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AANW,QAAA,qBAAqB,yBAMhC;AACF,MAAa,wBAAyB,SAAQ,KAAK;IACjD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CACF;AALD,4DAKC"}
|
package/dist/swapSDK.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import web3, { Keypair, PublicKey, TransactionInstruction, VersionedTransaction } from "@solana/web3.js";
|
1
|
+
import web3, { Connection, Keypair, PublicKey, TransactionInstruction, VersionedTransaction } from "@solana/web3.js";
|
2
2
|
/**
|
3
3
|
* Parameters to request a swap.
|
4
4
|
*/
|
@@ -164,6 +164,14 @@ export declare class SwapSDK {
|
|
164
164
|
* @returns A promise that resolves to the token's price (number).
|
165
165
|
*/
|
166
166
|
tokenPrice(tokenAddress: PublicKey): Promise<number>;
|
167
|
+
/**
|
168
|
+
* Simulates a transaction.
|
169
|
+
* @param connection The connection to use.
|
170
|
+
* @param transaction The transaction to simulate.
|
171
|
+
* @returns The simulation result.
|
172
|
+
* @throws If the simulation fails. with the simulation error
|
173
|
+
*/
|
174
|
+
simulateTransaction(connection: Connection, transaction: VersionedTransaction): Promise<web3.SimulatedTransactionResponse>;
|
167
175
|
/**
|
168
176
|
* Converts an API instruction (in plain JSON format) to a TransactionInstruction.
|
169
177
|
*
|
package/dist/swapSDK.js
CHANGED
@@ -40,6 +40,7 @@ exports.BASE_URL = exports.SwapSDK = void 0;
|
|
40
40
|
// SwapSDK.ts
|
41
41
|
const web3_js_1 = __importStar(require("@solana/web3.js"));
|
42
42
|
const bs58_1 = __importDefault(require("bs58"));
|
43
|
+
const errors_1 = require("./errors");
|
43
44
|
/**
|
44
45
|
* SwapSDK simplifies calling the swap endpoint as well as token-related endpoints.
|
45
46
|
*
|
@@ -209,6 +210,21 @@ class SwapSDK {
|
|
209
210
|
const data = await response.json();
|
210
211
|
return Number(data.price);
|
211
212
|
}
|
213
|
+
/**
|
214
|
+
* Simulates a transaction.
|
215
|
+
* @param connection The connection to use.
|
216
|
+
* @param transaction The transaction to simulate.
|
217
|
+
* @returns The simulation result.
|
218
|
+
* @throws If the simulation fails. with the simulation error
|
219
|
+
*/
|
220
|
+
async simulateTransaction(connection, transaction) {
|
221
|
+
const config = { commitment: "confirmed" };
|
222
|
+
const { value } = await connection.simulateTransaction(transaction, config);
|
223
|
+
if (value.err) {
|
224
|
+
(0, errors_1.handleSimulationError)(value.err);
|
225
|
+
}
|
226
|
+
return value;
|
227
|
+
}
|
212
228
|
/**
|
213
229
|
* Converts an API instruction (in plain JSON format) to a TransactionInstruction.
|
214
230
|
*
|
package/dist/swapSDK.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"swapSDK.js","sourceRoot":"","sources":["../src/swapSDK.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,aAAa;AACb,
|
1
|
+
{"version":3,"file":"swapSDK.js","sourceRoot":"","sources":["../src/swapSDK.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,aAAa;AACb,2DASyB;AACzB,gDAA0B;AAC1B,qCAAiD;AA+FjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;YACd,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IA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
2
|
+
if (e.toString().includes("TooManyAccountLocks")) {
|
3
|
+
throw new TooManyAccountsLockError(
|
4
|
+
"Too many account locks: consider using the option reduceToTwoHops in the options when getting the routes"
|
5
|
+
);
|
6
|
+
}
|
7
|
+
};
|
8
|
+
export class TooManyAccountsLockError extends Error {
|
9
|
+
constructor(message: string) {
|
10
|
+
super(message);
|
11
|
+
this.name = "TooManyAccountsLockError";
|
12
|
+
}
|
13
|
+
}
|
package/src/swapSDK.ts
CHANGED
@@ -1,13 +1,16 @@
|
|
1
1
|
// SwapSDK.ts
|
2
2
|
import web3, {
|
3
|
+
Connection,
|
3
4
|
Keypair,
|
4
5
|
PublicKey,
|
6
|
+
SimulateTransactionConfig,
|
5
7
|
Transaction,
|
6
8
|
TransactionInstruction,
|
7
9
|
VersionedMessage,
|
8
10
|
VersionedTransaction,
|
9
11
|
} from "@solana/web3.js";
|
10
12
|
import base58 from "bs58";
|
13
|
+
import { handleSimulationError } from "./errors";
|
11
14
|
|
12
15
|
/**
|
13
16
|
* Parameters to request a swap.
|
@@ -303,6 +306,24 @@ export class SwapSDK {
|
|
303
306
|
const data = await response.json();
|
304
307
|
return Number(data.price);
|
305
308
|
}
|
309
|
+
/**
|
310
|
+
* Simulates a transaction.
|
311
|
+
* @param connection The connection to use.
|
312
|
+
* @param transaction The transaction to simulate.
|
313
|
+
* @returns The simulation result.
|
314
|
+
* @throws If the simulation fails. with the simulation error
|
315
|
+
*/
|
316
|
+
async simulateTransaction(
|
317
|
+
connection: Connection,
|
318
|
+
transaction: VersionedTransaction
|
319
|
+
) {
|
320
|
+
const config: SimulateTransactionConfig = { commitment: "confirmed" };
|
321
|
+
const { value } = await connection.simulateTransaction(transaction, config);
|
322
|
+
if (value.err) {
|
323
|
+
handleSimulationError(value.err);
|
324
|
+
}
|
325
|
+
return value;
|
326
|
+
}
|
306
327
|
|
307
328
|
/**
|
308
329
|
* Converts an API instruction (in plain JSON format) to a TransactionInstruction.
|