@kololabs/router 0.1.1
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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/dist/index.cjs +84 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +39 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/package.json +30 -0
- package/src/__tests__/router.test.ts +70 -0
- package/src/index.ts +110 -0
- package/tsconfig.json +8 -0
- package/tsup.config.ts +9 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 poolparty21
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createRouter: () => createRouter
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_web3 = require("@solana/web3.js");
|
|
27
|
+
var import_node_buffer = require("buffer");
|
|
28
|
+
var JUPITER_BASE_URL = "https://api.jup.ag/swap/v2";
|
|
29
|
+
function createRouter(config) {
|
|
30
|
+
const baseUrl = config?.baseUrl ?? JUPITER_BASE_URL;
|
|
31
|
+
const headers = {
|
|
32
|
+
"Content-Type": "application/json"
|
|
33
|
+
};
|
|
34
|
+
if (config?.apiKey) {
|
|
35
|
+
headers["x-api-key"] = config.apiKey;
|
|
36
|
+
}
|
|
37
|
+
const getQuote = async (params) => {
|
|
38
|
+
const url = new URL(baseUrl + "/quote");
|
|
39
|
+
url.searchParams.set("inputMint", params.inputMint);
|
|
40
|
+
url.searchParams.set("outputMint", params.outputMint);
|
|
41
|
+
url.searchParams.set("amount", params.amount);
|
|
42
|
+
url.searchParams.set("slippageBps", String(params.slippageBps));
|
|
43
|
+
const response = await globalThis.fetch(url.toString(), { headers });
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
const text = await response.text();
|
|
46
|
+
throw new Error(`Jupiter quote failed (${response.status}): ${text}`);
|
|
47
|
+
}
|
|
48
|
+
const data = await response.json();
|
|
49
|
+
return {
|
|
50
|
+
inputMint: data.inputMint,
|
|
51
|
+
outputMint: data.outputMint,
|
|
52
|
+
inAmount: data.inAmount,
|
|
53
|
+
outAmount: data.outAmount,
|
|
54
|
+
otherAmountThreshold: data.otherAmountThreshold,
|
|
55
|
+
priceImpactPct: data.priceImpactPct,
|
|
56
|
+
routePlan: data.routePlan
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
const getSwapTransaction = async (params) => {
|
|
60
|
+
const response = await globalThis.fetch(baseUrl + "/swap", {
|
|
61
|
+
method: "POST",
|
|
62
|
+
headers,
|
|
63
|
+
body: JSON.stringify({
|
|
64
|
+
quoteResponse: params.quoteResponse,
|
|
65
|
+
userPublicKey: params.userPublicKey,
|
|
66
|
+
wrapAndUnwrapSol: params.wrapAndUnwrapSol ?? true,
|
|
67
|
+
useSharedAccounts: params.useSharedAccounts ?? true
|
|
68
|
+
})
|
|
69
|
+
});
|
|
70
|
+
if (!response.ok) {
|
|
71
|
+
const text = await response.text();
|
|
72
|
+
throw new Error(`Jupiter swap failed (${response.status}): ${text}`);
|
|
73
|
+
}
|
|
74
|
+
const data = await response.json();
|
|
75
|
+
const txBuffer = import_node_buffer.Buffer.from(data.swapTransaction, "base64");
|
|
76
|
+
return import_web3.VersionedTransaction.deserialize(txBuffer);
|
|
77
|
+
};
|
|
78
|
+
return { getQuote, getSwapTransaction };
|
|
79
|
+
}
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
createRouter
|
|
83
|
+
});
|
|
84
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { VersionedTransaction } from '@solana/web3.js'\nimport { Buffer } from 'node:buffer'\n\nconst JUPITER_BASE_URL = 'https://api.jup.ag/swap/v2'\n\nexport interface QuoteParams {\n inputMint: string\n outputMint: string\n amount: string\n slippageBps: number\n}\n\nexport interface QuoteResponse {\n inputMint: string\n outputMint: string\n inAmount: string\n outAmount: string\n otherAmountThreshold: string\n priceImpactPct: string\n routePlan: Array<{ swapInfo: { ammKey: string; label: string } }>\n}\n\nexport interface SwapParams {\n quoteResponse: QuoteResponse\n userPublicKey: string\n wrapAndUnwrapSol?: boolean\n useSharedAccounts?: boolean\n}\n\nexport interface RouterConfig {\n baseUrl?: string\n apiKey?: string\n}\n\ninterface JupiterQuoteResponse {\n inputMint: string\n outputMint: string\n inAmount: string\n outAmount: string\n otherAmountThreshold: string\n priceImpactPct: string\n routePlan: Array<{ swapInfo: { ammKey: string; label: string } }>\n}\n\ninterface JupiterSwapResponse {\n swapTransaction: string\n lastValidBlockHeight: number\n prioritizationFeeLamports: number\n}\n\nexport function createRouter(config?: RouterConfig) {\n const baseUrl = config?.baseUrl ?? JUPITER_BASE_URL\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n }\n if (config?.apiKey) {\n headers['x-api-key'] = config.apiKey\n }\n\n const getQuote = async (params: QuoteParams): Promise<QuoteResponse> => {\n const url = new URL(baseUrl + '/quote')\n url.searchParams.set('inputMint', params.inputMint)\n url.searchParams.set('outputMint', params.outputMint)\n url.searchParams.set('amount', params.amount)\n url.searchParams.set('slippageBps', String(params.slippageBps))\n\n const response = await globalThis.fetch(url.toString(), { headers })\n if (!response.ok) {\n const text = await response.text()\n throw new Error(`Jupiter quote failed (${response.status}): ${text}`)\n }\n\n const data = (await response.json()) as JupiterQuoteResponse\n return {\n inputMint: data.inputMint,\n outputMint: data.outputMint,\n inAmount: data.inAmount,\n outAmount: data.outAmount,\n otherAmountThreshold: data.otherAmountThreshold,\n priceImpactPct: data.priceImpactPct,\n routePlan: data.routePlan,\n }\n }\n\n const getSwapTransaction = async (params: SwapParams): Promise<VersionedTransaction> => {\n const response = await globalThis.fetch(baseUrl + '/swap', {\n method: 'POST',\n headers,\n body: JSON.stringify({\n quoteResponse: params.quoteResponse,\n userPublicKey: params.userPublicKey,\n wrapAndUnwrapSol: params.wrapAndUnwrapSol ?? true,\n useSharedAccounts: params.useSharedAccounts ?? true,\n }),\n })\n\n if (!response.ok) {\n const text = await response.text()\n throw new Error(`Jupiter swap failed (${response.status}): ${text}`)\n }\n\n const data = (await response.json()) as JupiterSwapResponse\n const txBuffer = Buffer.from(data.swapTransaction, 'base64')\n return VersionedTransaction.deserialize(txBuffer)\n }\n\n return { getQuote, getSwapTransaction }\n}\n\nexport type Router = ReturnType<typeof createRouter>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAqC;AACrC,yBAAuB;AAEvB,IAAM,mBAAmB;AA+ClB,SAAS,aAAa,QAAuB;AAClD,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,UAAkC;AAAA,IACtC,gBAAgB;AAAA,EAClB;AACA,MAAI,QAAQ,QAAQ;AAClB,YAAQ,WAAW,IAAI,OAAO;AAAA,EAChC;AAEA,QAAM,WAAW,OAAO,WAAgD;AACtE,UAAM,MAAM,IAAI,IAAI,UAAU,QAAQ;AACtC,QAAI,aAAa,IAAI,aAAa,OAAO,SAAS;AAClD,QAAI,aAAa,IAAI,cAAc,OAAO,UAAU;AACpD,QAAI,aAAa,IAAI,UAAU,OAAO,MAAM;AAC5C,QAAI,aAAa,IAAI,eAAe,OAAO,OAAO,WAAW,CAAC;AAE9D,UAAM,WAAW,MAAM,WAAW,MAAM,IAAI,SAAS,GAAG,EAAE,QAAQ,CAAC;AACnE,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,OAAO,MAAM,SAAS,KAAK;AACjC,YAAM,IAAI,MAAM,yBAAyB,SAAS,MAAM,MAAM,IAAI,EAAE;AAAA,IACtE;AAEA,UAAM,OAAQ,MAAM,SAAS,KAAK;AAClC,WAAO;AAAA,MACL,WAAW,KAAK;AAAA,MAChB,YAAY,KAAK;AAAA,MACjB,UAAU,KAAK;AAAA,MACf,WAAW,KAAK;AAAA,MAChB,sBAAsB,KAAK;AAAA,MAC3B,gBAAgB,KAAK;AAAA,MACrB,WAAW,KAAK;AAAA,IAClB;AAAA,EACF;AAEA,QAAM,qBAAqB,OAAO,WAAsD;AACtF,UAAM,WAAW,MAAM,WAAW,MAAM,UAAU,SAAS;AAAA,MACzD,QAAQ;AAAA,MACR;AAAA,MACA,MAAM,KAAK,UAAU;AAAA,QACnB,eAAe,OAAO;AAAA,QACtB,eAAe,OAAO;AAAA,QACtB,kBAAkB,OAAO,oBAAoB;AAAA,QAC7C,mBAAmB,OAAO,qBAAqB;AAAA,MACjD,CAAC;AAAA,IACH,CAAC;AAED,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,OAAO,MAAM,SAAS,KAAK;AACjC,YAAM,IAAI,MAAM,wBAAwB,SAAS,MAAM,MAAM,IAAI,EAAE;AAAA,IACrE;AAEA,UAAM,OAAQ,MAAM,SAAS,KAAK;AAClC,UAAM,WAAW,0BAAO,KAAK,KAAK,iBAAiB,QAAQ;AAC3D,WAAO,iCAAqB,YAAY,QAAQ;AAAA,EAClD;AAEA,SAAO,EAAE,UAAU,mBAAmB;AACxC;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { VersionedTransaction } from '@solana/web3.js';
|
|
2
|
+
|
|
3
|
+
interface QuoteParams {
|
|
4
|
+
inputMint: string;
|
|
5
|
+
outputMint: string;
|
|
6
|
+
amount: string;
|
|
7
|
+
slippageBps: number;
|
|
8
|
+
}
|
|
9
|
+
interface QuoteResponse {
|
|
10
|
+
inputMint: string;
|
|
11
|
+
outputMint: string;
|
|
12
|
+
inAmount: string;
|
|
13
|
+
outAmount: string;
|
|
14
|
+
otherAmountThreshold: string;
|
|
15
|
+
priceImpactPct: string;
|
|
16
|
+
routePlan: Array<{
|
|
17
|
+
swapInfo: {
|
|
18
|
+
ammKey: string;
|
|
19
|
+
label: string;
|
|
20
|
+
};
|
|
21
|
+
}>;
|
|
22
|
+
}
|
|
23
|
+
interface SwapParams {
|
|
24
|
+
quoteResponse: QuoteResponse;
|
|
25
|
+
userPublicKey: string;
|
|
26
|
+
wrapAndUnwrapSol?: boolean;
|
|
27
|
+
useSharedAccounts?: boolean;
|
|
28
|
+
}
|
|
29
|
+
interface RouterConfig {
|
|
30
|
+
baseUrl?: string;
|
|
31
|
+
apiKey?: string;
|
|
32
|
+
}
|
|
33
|
+
declare function createRouter(config?: RouterConfig): {
|
|
34
|
+
getQuote: (params: QuoteParams) => Promise<QuoteResponse>;
|
|
35
|
+
getSwapTransaction: (params: SwapParams) => Promise<VersionedTransaction>;
|
|
36
|
+
};
|
|
37
|
+
type Router = ReturnType<typeof createRouter>;
|
|
38
|
+
|
|
39
|
+
export { type QuoteParams, type QuoteResponse, type Router, type RouterConfig, type SwapParams, createRouter };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { VersionedTransaction } from '@solana/web3.js';
|
|
2
|
+
|
|
3
|
+
interface QuoteParams {
|
|
4
|
+
inputMint: string;
|
|
5
|
+
outputMint: string;
|
|
6
|
+
amount: string;
|
|
7
|
+
slippageBps: number;
|
|
8
|
+
}
|
|
9
|
+
interface QuoteResponse {
|
|
10
|
+
inputMint: string;
|
|
11
|
+
outputMint: string;
|
|
12
|
+
inAmount: string;
|
|
13
|
+
outAmount: string;
|
|
14
|
+
otherAmountThreshold: string;
|
|
15
|
+
priceImpactPct: string;
|
|
16
|
+
routePlan: Array<{
|
|
17
|
+
swapInfo: {
|
|
18
|
+
ammKey: string;
|
|
19
|
+
label: string;
|
|
20
|
+
};
|
|
21
|
+
}>;
|
|
22
|
+
}
|
|
23
|
+
interface SwapParams {
|
|
24
|
+
quoteResponse: QuoteResponse;
|
|
25
|
+
userPublicKey: string;
|
|
26
|
+
wrapAndUnwrapSol?: boolean;
|
|
27
|
+
useSharedAccounts?: boolean;
|
|
28
|
+
}
|
|
29
|
+
interface RouterConfig {
|
|
30
|
+
baseUrl?: string;
|
|
31
|
+
apiKey?: string;
|
|
32
|
+
}
|
|
33
|
+
declare function createRouter(config?: RouterConfig): {
|
|
34
|
+
getQuote: (params: QuoteParams) => Promise<QuoteResponse>;
|
|
35
|
+
getSwapTransaction: (params: SwapParams) => Promise<VersionedTransaction>;
|
|
36
|
+
};
|
|
37
|
+
type Router = ReturnType<typeof createRouter>;
|
|
38
|
+
|
|
39
|
+
export { type QuoteParams, type QuoteResponse, type Router, type RouterConfig, type SwapParams, createRouter };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { VersionedTransaction } from "@solana/web3.js";
|
|
3
|
+
import { Buffer } from "buffer";
|
|
4
|
+
var JUPITER_BASE_URL = "https://api.jup.ag/swap/v2";
|
|
5
|
+
function createRouter(config) {
|
|
6
|
+
const baseUrl = config?.baseUrl ?? JUPITER_BASE_URL;
|
|
7
|
+
const headers = {
|
|
8
|
+
"Content-Type": "application/json"
|
|
9
|
+
};
|
|
10
|
+
if (config?.apiKey) {
|
|
11
|
+
headers["x-api-key"] = config.apiKey;
|
|
12
|
+
}
|
|
13
|
+
const getQuote = async (params) => {
|
|
14
|
+
const url = new URL(baseUrl + "/quote");
|
|
15
|
+
url.searchParams.set("inputMint", params.inputMint);
|
|
16
|
+
url.searchParams.set("outputMint", params.outputMint);
|
|
17
|
+
url.searchParams.set("amount", params.amount);
|
|
18
|
+
url.searchParams.set("slippageBps", String(params.slippageBps));
|
|
19
|
+
const response = await globalThis.fetch(url.toString(), { headers });
|
|
20
|
+
if (!response.ok) {
|
|
21
|
+
const text = await response.text();
|
|
22
|
+
throw new Error(`Jupiter quote failed (${response.status}): ${text}`);
|
|
23
|
+
}
|
|
24
|
+
const data = await response.json();
|
|
25
|
+
return {
|
|
26
|
+
inputMint: data.inputMint,
|
|
27
|
+
outputMint: data.outputMint,
|
|
28
|
+
inAmount: data.inAmount,
|
|
29
|
+
outAmount: data.outAmount,
|
|
30
|
+
otherAmountThreshold: data.otherAmountThreshold,
|
|
31
|
+
priceImpactPct: data.priceImpactPct,
|
|
32
|
+
routePlan: data.routePlan
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
const getSwapTransaction = async (params) => {
|
|
36
|
+
const response = await globalThis.fetch(baseUrl + "/swap", {
|
|
37
|
+
method: "POST",
|
|
38
|
+
headers,
|
|
39
|
+
body: JSON.stringify({
|
|
40
|
+
quoteResponse: params.quoteResponse,
|
|
41
|
+
userPublicKey: params.userPublicKey,
|
|
42
|
+
wrapAndUnwrapSol: params.wrapAndUnwrapSol ?? true,
|
|
43
|
+
useSharedAccounts: params.useSharedAccounts ?? true
|
|
44
|
+
})
|
|
45
|
+
});
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
const text = await response.text();
|
|
48
|
+
throw new Error(`Jupiter swap failed (${response.status}): ${text}`);
|
|
49
|
+
}
|
|
50
|
+
const data = await response.json();
|
|
51
|
+
const txBuffer = Buffer.from(data.swapTransaction, "base64");
|
|
52
|
+
return VersionedTransaction.deserialize(txBuffer);
|
|
53
|
+
};
|
|
54
|
+
return { getQuote, getSwapTransaction };
|
|
55
|
+
}
|
|
56
|
+
export {
|
|
57
|
+
createRouter
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { VersionedTransaction } from '@solana/web3.js'\nimport { Buffer } from 'node:buffer'\n\nconst JUPITER_BASE_URL = 'https://api.jup.ag/swap/v2'\n\nexport interface QuoteParams {\n inputMint: string\n outputMint: string\n amount: string\n slippageBps: number\n}\n\nexport interface QuoteResponse {\n inputMint: string\n outputMint: string\n inAmount: string\n outAmount: string\n otherAmountThreshold: string\n priceImpactPct: string\n routePlan: Array<{ swapInfo: { ammKey: string; label: string } }>\n}\n\nexport interface SwapParams {\n quoteResponse: QuoteResponse\n userPublicKey: string\n wrapAndUnwrapSol?: boolean\n useSharedAccounts?: boolean\n}\n\nexport interface RouterConfig {\n baseUrl?: string\n apiKey?: string\n}\n\ninterface JupiterQuoteResponse {\n inputMint: string\n outputMint: string\n inAmount: string\n outAmount: string\n otherAmountThreshold: string\n priceImpactPct: string\n routePlan: Array<{ swapInfo: { ammKey: string; label: string } }>\n}\n\ninterface JupiterSwapResponse {\n swapTransaction: string\n lastValidBlockHeight: number\n prioritizationFeeLamports: number\n}\n\nexport function createRouter(config?: RouterConfig) {\n const baseUrl = config?.baseUrl ?? JUPITER_BASE_URL\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n }\n if (config?.apiKey) {\n headers['x-api-key'] = config.apiKey\n }\n\n const getQuote = async (params: QuoteParams): Promise<QuoteResponse> => {\n const url = new URL(baseUrl + '/quote')\n url.searchParams.set('inputMint', params.inputMint)\n url.searchParams.set('outputMint', params.outputMint)\n url.searchParams.set('amount', params.amount)\n url.searchParams.set('slippageBps', String(params.slippageBps))\n\n const response = await globalThis.fetch(url.toString(), { headers })\n if (!response.ok) {\n const text = await response.text()\n throw new Error(`Jupiter quote failed (${response.status}): ${text}`)\n }\n\n const data = (await response.json()) as JupiterQuoteResponse\n return {\n inputMint: data.inputMint,\n outputMint: data.outputMint,\n inAmount: data.inAmount,\n outAmount: data.outAmount,\n otherAmountThreshold: data.otherAmountThreshold,\n priceImpactPct: data.priceImpactPct,\n routePlan: data.routePlan,\n }\n }\n\n const getSwapTransaction = async (params: SwapParams): Promise<VersionedTransaction> => {\n const response = await globalThis.fetch(baseUrl + '/swap', {\n method: 'POST',\n headers,\n body: JSON.stringify({\n quoteResponse: params.quoteResponse,\n userPublicKey: params.userPublicKey,\n wrapAndUnwrapSol: params.wrapAndUnwrapSol ?? true,\n useSharedAccounts: params.useSharedAccounts ?? true,\n }),\n })\n\n if (!response.ok) {\n const text = await response.text()\n throw new Error(`Jupiter swap failed (${response.status}): ${text}`)\n }\n\n const data = (await response.json()) as JupiterSwapResponse\n const txBuffer = Buffer.from(data.swapTransaction, 'base64')\n return VersionedTransaction.deserialize(txBuffer)\n }\n\n return { getQuote, getSwapTransaction }\n}\n\nexport type Router = ReturnType<typeof createRouter>\n"],"mappings":";AAAA,SAAS,4BAA4B;AACrC,SAAS,cAAc;AAEvB,IAAM,mBAAmB;AA+ClB,SAAS,aAAa,QAAuB;AAClD,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,UAAkC;AAAA,IACtC,gBAAgB;AAAA,EAClB;AACA,MAAI,QAAQ,QAAQ;AAClB,YAAQ,WAAW,IAAI,OAAO;AAAA,EAChC;AAEA,QAAM,WAAW,OAAO,WAAgD;AACtE,UAAM,MAAM,IAAI,IAAI,UAAU,QAAQ;AACtC,QAAI,aAAa,IAAI,aAAa,OAAO,SAAS;AAClD,QAAI,aAAa,IAAI,cAAc,OAAO,UAAU;AACpD,QAAI,aAAa,IAAI,UAAU,OAAO,MAAM;AAC5C,QAAI,aAAa,IAAI,eAAe,OAAO,OAAO,WAAW,CAAC;AAE9D,UAAM,WAAW,MAAM,WAAW,MAAM,IAAI,SAAS,GAAG,EAAE,QAAQ,CAAC;AACnE,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,OAAO,MAAM,SAAS,KAAK;AACjC,YAAM,IAAI,MAAM,yBAAyB,SAAS,MAAM,MAAM,IAAI,EAAE;AAAA,IACtE;AAEA,UAAM,OAAQ,MAAM,SAAS,KAAK;AAClC,WAAO;AAAA,MACL,WAAW,KAAK;AAAA,MAChB,YAAY,KAAK;AAAA,MACjB,UAAU,KAAK;AAAA,MACf,WAAW,KAAK;AAAA,MAChB,sBAAsB,KAAK;AAAA,MAC3B,gBAAgB,KAAK;AAAA,MACrB,WAAW,KAAK;AAAA,IAClB;AAAA,EACF;AAEA,QAAM,qBAAqB,OAAO,WAAsD;AACtF,UAAM,WAAW,MAAM,WAAW,MAAM,UAAU,SAAS;AAAA,MACzD,QAAQ;AAAA,MACR;AAAA,MACA,MAAM,KAAK,UAAU;AAAA,QACnB,eAAe,OAAO;AAAA,QACtB,eAAe,OAAO;AAAA,QACtB,kBAAkB,OAAO,oBAAoB;AAAA,QAC7C,mBAAmB,OAAO,qBAAqB;AAAA,MACjD,CAAC;AAAA,IACH,CAAC;AAED,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,OAAO,MAAM,SAAS,KAAK;AACjC,YAAM,IAAI,MAAM,wBAAwB,SAAS,MAAM,MAAM,IAAI,EAAE;AAAA,IACrE;AAEA,UAAM,OAAQ,MAAM,SAAS,KAAK;AAClC,UAAM,WAAW,OAAO,KAAK,KAAK,iBAAiB,QAAQ;AAC3D,WAAO,qBAAqB,YAAY,QAAQ;AAAA,EAClD;AAEA,SAAO,EAAE,UAAU,mBAAmB;AACxC;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kololabs/router",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@solana/web3.js": "^1.98.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"tsup": "^8.4.0",
|
|
20
|
+
"typescript": "^5.8.0",
|
|
21
|
+
"vitest": "^4.1.0",
|
|
22
|
+
"@types/node": "^22.0.0"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"test": "vitest run",
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"clean": "rm -rf dist"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterAll } from 'vitest'
|
|
2
|
+
|
|
3
|
+
const mockFetch = vi.fn()
|
|
4
|
+
const origFetch = globalThis.fetch
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
vi.clearAllMocks()
|
|
8
|
+
globalThis.fetch = mockFetch
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
afterAll(() => {
|
|
12
|
+
globalThis.fetch = origFetch
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
describe('@kololabs/router', () => {
|
|
16
|
+
it('createRouter returns object with getQuote and getSwapTransaction', async () => {
|
|
17
|
+
const { createRouter } = await import('../index.js')
|
|
18
|
+
const router = createRouter()
|
|
19
|
+
expect(router).toHaveProperty('getQuote')
|
|
20
|
+
expect(router).toHaveProperty('getSwapTransaction')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('getQuote constructs correct URL and returns quote', async () => {
|
|
24
|
+
const { createRouter } = await import('../index.js')
|
|
25
|
+
const mockQuoteResponse = {
|
|
26
|
+
inputMint: 'So11111111111111111111111111111111111111112',
|
|
27
|
+
outputMint: 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB',
|
|
28
|
+
inAmount: '1000000000',
|
|
29
|
+
outAmount: '95000000',
|
|
30
|
+
otherAmountThreshold: '94000000',
|
|
31
|
+
priceImpactPct: '0.05',
|
|
32
|
+
routePlan: [{ swapInfo: { ammKey: 'amm1', label: 'Orca' } }],
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
mockFetch.mockResolvedValueOnce({
|
|
36
|
+
ok: true,
|
|
37
|
+
json: async () => mockQuoteResponse,
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const router = createRouter({ baseUrl: 'https://api.jup.ag/swap/v2' })
|
|
41
|
+
const quote = await router.getQuote({
|
|
42
|
+
inputMint: 'So11111111111111111111111111111111111111112',
|
|
43
|
+
outputMint: 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB',
|
|
44
|
+
amount: '1000000000',
|
|
45
|
+
slippageBps: 50,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
expect(mockFetch).toHaveBeenCalledTimes(1)
|
|
49
|
+
expect(quote.inAmount).toBe('1000000000')
|
|
50
|
+
expect(quote.outAmount).toBe('95000000')
|
|
51
|
+
expect(quote.priceImpactPct).toBe('0.05')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('getQuote throws on non-ok response', async () => {
|
|
55
|
+
const { createRouter } = await import('../index.js')
|
|
56
|
+
mockFetch.mockResolvedValueOnce({
|
|
57
|
+
ok: false,
|
|
58
|
+
status: 400,
|
|
59
|
+
text: async () => 'Bad request',
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const router = createRouter()
|
|
63
|
+
await expect(router.getQuote({
|
|
64
|
+
inputMint: 'So11111111111111111111111111111111111111112',
|
|
65
|
+
outputMint: 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB',
|
|
66
|
+
amount: '1000000000',
|
|
67
|
+
slippageBps: 50,
|
|
68
|
+
})).rejects.toThrow('Jupiter quote failed (400): Bad request')
|
|
69
|
+
})
|
|
70
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { VersionedTransaction } from '@solana/web3.js'
|
|
2
|
+
import { Buffer } from 'node:buffer'
|
|
3
|
+
|
|
4
|
+
const JUPITER_BASE_URL = 'https://api.jup.ag/swap/v2'
|
|
5
|
+
|
|
6
|
+
export interface QuoteParams {
|
|
7
|
+
inputMint: string
|
|
8
|
+
outputMint: string
|
|
9
|
+
amount: string
|
|
10
|
+
slippageBps: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface QuoteResponse {
|
|
14
|
+
inputMint: string
|
|
15
|
+
outputMint: string
|
|
16
|
+
inAmount: string
|
|
17
|
+
outAmount: string
|
|
18
|
+
otherAmountThreshold: string
|
|
19
|
+
priceImpactPct: string
|
|
20
|
+
routePlan: Array<{ swapInfo: { ammKey: string; label: string } }>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SwapParams {
|
|
24
|
+
quoteResponse: QuoteResponse
|
|
25
|
+
userPublicKey: string
|
|
26
|
+
wrapAndUnwrapSol?: boolean
|
|
27
|
+
useSharedAccounts?: boolean
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface RouterConfig {
|
|
31
|
+
baseUrl?: string
|
|
32
|
+
apiKey?: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface JupiterQuoteResponse {
|
|
36
|
+
inputMint: string
|
|
37
|
+
outputMint: string
|
|
38
|
+
inAmount: string
|
|
39
|
+
outAmount: string
|
|
40
|
+
otherAmountThreshold: string
|
|
41
|
+
priceImpactPct: string
|
|
42
|
+
routePlan: Array<{ swapInfo: { ammKey: string; label: string } }>
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface JupiterSwapResponse {
|
|
46
|
+
swapTransaction: string
|
|
47
|
+
lastValidBlockHeight: number
|
|
48
|
+
prioritizationFeeLamports: number
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function createRouter(config?: RouterConfig) {
|
|
52
|
+
const baseUrl = config?.baseUrl ?? JUPITER_BASE_URL
|
|
53
|
+
const headers: Record<string, string> = {
|
|
54
|
+
'Content-Type': 'application/json',
|
|
55
|
+
}
|
|
56
|
+
if (config?.apiKey) {
|
|
57
|
+
headers['x-api-key'] = config.apiKey
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const getQuote = async (params: QuoteParams): Promise<QuoteResponse> => {
|
|
61
|
+
const url = new URL(baseUrl + '/quote')
|
|
62
|
+
url.searchParams.set('inputMint', params.inputMint)
|
|
63
|
+
url.searchParams.set('outputMint', params.outputMint)
|
|
64
|
+
url.searchParams.set('amount', params.amount)
|
|
65
|
+
url.searchParams.set('slippageBps', String(params.slippageBps))
|
|
66
|
+
|
|
67
|
+
const response = await globalThis.fetch(url.toString(), { headers })
|
|
68
|
+
if (!response.ok) {
|
|
69
|
+
const text = await response.text()
|
|
70
|
+
throw new Error(`Jupiter quote failed (${response.status}): ${text}`)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const data = (await response.json()) as JupiterQuoteResponse
|
|
74
|
+
return {
|
|
75
|
+
inputMint: data.inputMint,
|
|
76
|
+
outputMint: data.outputMint,
|
|
77
|
+
inAmount: data.inAmount,
|
|
78
|
+
outAmount: data.outAmount,
|
|
79
|
+
otherAmountThreshold: data.otherAmountThreshold,
|
|
80
|
+
priceImpactPct: data.priceImpactPct,
|
|
81
|
+
routePlan: data.routePlan,
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const getSwapTransaction = async (params: SwapParams): Promise<VersionedTransaction> => {
|
|
86
|
+
const response = await globalThis.fetch(baseUrl + '/swap', {
|
|
87
|
+
method: 'POST',
|
|
88
|
+
headers,
|
|
89
|
+
body: JSON.stringify({
|
|
90
|
+
quoteResponse: params.quoteResponse,
|
|
91
|
+
userPublicKey: params.userPublicKey,
|
|
92
|
+
wrapAndUnwrapSol: params.wrapAndUnwrapSol ?? true,
|
|
93
|
+
useSharedAccounts: params.useSharedAccounts ?? true,
|
|
94
|
+
}),
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
if (!response.ok) {
|
|
98
|
+
const text = await response.text()
|
|
99
|
+
throw new Error(`Jupiter swap failed (${response.status}): ${text}`)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const data = (await response.json()) as JupiterSwapResponse
|
|
103
|
+
const txBuffer = Buffer.from(data.swapTransaction, 'base64')
|
|
104
|
+
return VersionedTransaction.deserialize(txBuffer)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return { getQuote, getSwapTransaction }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export type Router = ReturnType<typeof createRouter>
|
package/tsconfig.json
ADDED