@swapkit/helpers
Advanced tools
Comparing version
@@ -6,3 +6,2 @@ { | ||
"@swapkit/tokens": "1.0.3", | ||
"ky": "1.4.0", | ||
"picocolors": "1.0.1", | ||
@@ -12,8 +11,8 @@ "zod": "3.23.8" | ||
"devDependencies": { | ||
"@swapkit/toolbox-cosmos": "1.0.8", | ||
"@swapkit/toolbox-evm": "1.1.3", | ||
"@swapkit/toolbox-solana": "1.0.8", | ||
"@swapkit/toolbox-radix": "1.0.8", | ||
"@swapkit/toolbox-substrate": "1.1.3", | ||
"@swapkit/toolbox-utxo": "1.0.8" | ||
"@swapkit/toolbox-cosmos": "1.0.9", | ||
"@swapkit/toolbox-evm": "1.1.4", | ||
"@swapkit/toolbox-solana": "1.0.9", | ||
"@swapkit/toolbox-radix": "1.0.9", | ||
"@swapkit/toolbox-substrate": "1.1.4", | ||
"@swapkit/toolbox-utxo": "1.0.9" | ||
}, | ||
@@ -43,3 +42,3 @@ "files": [ | ||
"types": "./src/index.ts", | ||
"version": "1.1.3" | ||
"version": "1.2.0" | ||
} |
@@ -9,2 +9,4 @@ import { describe, expect, test } from "bun:test"; | ||
getMemoForNameRegister, | ||
getMemoForRunePoolDeposit, | ||
getMemoForRunePoolWithdraw, | ||
getMemoForSaverDeposit, | ||
@@ -77,2 +79,12 @@ getMemoForSaverWithdraw, | ||
}); | ||
test("returns correct memo when paired address is not available but affiliate info is present", () => { | ||
const result = getMemoForDeposit({ | ||
chain: Chain.Ethereum, | ||
symbol: "ETH", | ||
affiliateAddress: "thor1abc123", | ||
affiliateBasisPoints: 500, | ||
}); | ||
expect(result).toBe("+:ETH.ETH::thor1abc123:500"); | ||
}); | ||
}); | ||
@@ -91,1 +103,24 @@ | ||
}); | ||
describe("getMemoForRunePoolDeposit", () => { | ||
test("returns correct memo for single side", () => { | ||
const result = getMemoForRunePoolDeposit(); | ||
expect(result).toBe("POOL+"); | ||
}); | ||
}); | ||
describe("getMemoForRunePoolWithdraw", () => { | ||
test("returns correct memo for single side", () => { | ||
const result = getMemoForRunePoolWithdraw({ basisPoints: 500 }); | ||
expect(result).toBe("POOL-:500"); | ||
}); | ||
test("returns correct memo when affiliate info is present", () => { | ||
const result = getMemoForRunePoolWithdraw({ | ||
basisPoints: 500, | ||
affiliateAddress: "thor1abc123", | ||
affiliateBasisPoints: 500, | ||
}); | ||
expect(result).toBe("POOL-:500:thor1abc123:500"); | ||
}); | ||
}); |
@@ -131,3 +131,4 @@ import { SwapKitError } from "../modules/swapKitError"; | ||
const poolIdentifier = getPoolIdentifier({ chain, symbol }); | ||
const addressPart = address ? `:${address}` : ""; | ||
const hasAffiliateInfo = !!affiliate.affiliateAddress; | ||
const addressPart = address ? `:${address}` : hasAffiliateInfo ? ":" : ""; | ||
@@ -141,5 +142,4 @@ return addAffiliate(`${MemoType.DEPOSIT}:${poolIdentifier}${addressPart}`, affiliate); | ||
basisPoints, | ||
...affiliate | ||
}: WithAffiliate<{ chain: Chain; symbol: string; basisPoints: number }>) { | ||
return addAffiliate(`${MemoType.WITHDRAW}:${chain}/${symbol}:${basisPoints}`, affiliate); | ||
}: { chain: Chain; symbol: string; basisPoints: number }) { | ||
return `${MemoType.WITHDRAW}:${chain}/${symbol}:${basisPoints}`; | ||
} | ||
@@ -153,4 +153,3 @@ | ||
targetAsset, | ||
...affiliate | ||
}: WithAffiliate<{ | ||
}: { | ||
chain: Chain; | ||
@@ -161,3 +160,3 @@ symbol: string; | ||
targetAsset?: string; | ||
}>) { | ||
}) { | ||
const shortenedSymbol = | ||
@@ -167,8 +166,16 @@ chain === "ETH" && ticker !== "ETH" ? `${ticker}-${symbol.slice(-3)}` : symbol; | ||
return addAffiliate( | ||
`${MemoType.WITHDRAW}:${chain}.${shortenedSymbol}:${basisPoints}${targetPart}`, | ||
affiliate, | ||
); | ||
return `${MemoType.WITHDRAW}:${chain}.${shortenedSymbol}:${basisPoints}${targetPart}`; | ||
} | ||
export function getMemoForRunePoolDeposit() { | ||
return `${MemoType.RUNEPOOL_DEPOSIT}`; | ||
} | ||
export function getMemoForRunePoolWithdraw({ | ||
basisPoints, | ||
...affiliate | ||
}: WithAffiliate<{ basisPoints: number }>) { | ||
return addAffiliate(`${MemoType.RUNEPOOL_WITHDRAW}:${basisPoints}`, affiliate); | ||
} | ||
/** | ||
@@ -196,2 +203,8 @@ * @deprecated - Use separate functions per each memo type like getMemoForDeposit, getMemoForWithdraw, etc. | ||
[MemoType.NAME_REGISTER]: { name: string; chain: string; address: string }; | ||
[MemoType.RUNEPOOL_DEPOSIT]: {}; | ||
[MemoType.RUNEPOOL_WITHDRAW]: { | ||
basisPoints: number; | ||
affiliateAddress?: string; | ||
affiliateBasisPoints?: number; | ||
}; | ||
}[T]; | ||
@@ -198,0 +211,0 @@ |
@@ -1,5 +0,11 @@ | ||
import type { KyInstance, Options } from "ky"; | ||
import ky from "ky"; | ||
type Options = { | ||
headers?: Record<string, string>; | ||
apiKey?: string; | ||
method?: "GET" | "POST"; | ||
onError?: (error: NotWorth) => NotWorth; | ||
responseHandler?: (response: NotWorth) => NotWorth; | ||
[key: string]: NotWorth; | ||
}; | ||
let kyClientConfig: Options & { apiKey?: string } = {}; | ||
let clientConfig: Options = {}; | ||
@@ -11,30 +17,41 @@ export const defaultRequestHeaders = | ||
export function setRequestClientConfig({ apiKey, ...config }: Options & { apiKey?: string }) { | ||
kyClientConfig = { ...config, apiKey }; | ||
export function setRequestClientConfig({ apiKey, ...config }: Options) { | ||
clientConfig = { ...config, apiKey }; | ||
} | ||
function getKyClient() { | ||
const { apiKey, ...config } = kyClientConfig; | ||
return ky.create({ | ||
...config, | ||
headers: { ...defaultRequestHeaders, ...config.headers, "x-api-key": apiKey }, | ||
}); | ||
async function fetchWithConfig(url: string, options: Options = {}) { | ||
const { apiKey, ...config } = clientConfig; | ||
const headers = { ...defaultRequestHeaders, ...config.headers, ...options.headers }; | ||
if (apiKey) headers["x-api-key"] = apiKey; | ||
try { | ||
const response = await fetch(url, { ...config, ...options, headers }); | ||
const body = await response.json(); | ||
if (options.responseHandler) return options.responseHandler(body); | ||
return body; | ||
} catch (error) { | ||
if (options.onError) return options.onError(error); | ||
console.error(error); | ||
} | ||
} | ||
const getTypedBaseRequestClient = (ky: KyInstance) => ({ | ||
get: async <T>(url: string | URL | Request, options?: Options) => | ||
(await ky.get(url, options)).json<T>(), | ||
post: async <T>(url: string | URL | Request, options?: Options) => | ||
(await ky.post(url, options)).json<T>(), | ||
}); | ||
export const RequestClient = { | ||
...getTypedBaseRequestClient(getKyClient()), | ||
get: async <T>(url: string, options?: Options): Promise<T> => | ||
fetchWithConfig(url, { ...options, method: "GET" }), | ||
post: async <T>(url: string, options?: Options): Promise<T> => | ||
fetchWithConfig(url, { ...options, method: "POST" }), | ||
extend: (options: Options) => { | ||
const extendedClient = getKyClient().extend(options); | ||
const extendedConfig = { ...clientConfig, ...options }; | ||
return { | ||
...getTypedBaseRequestClient(extendedClient), | ||
extend: RequestClient.extend, | ||
get: async <T>(url: string, options?: Options): Promise<T> => | ||
fetchWithConfig(url, { ...extendedConfig, ...options, method: "GET" }), | ||
post: async <T>(url: string, options?: Options): Promise<T> => | ||
fetchWithConfig(url, { ...extendedConfig, ...options, method: "POST" }), | ||
extend: (newOptions: Options) => RequestClient.extend({ ...extendedConfig, ...newOptions }), | ||
}; | ||
}, | ||
}; |
@@ -65,2 +65,4 @@ import type { CovalentApiType, EthplorerApiType } from "@swapkit/toolbox-evm"; | ||
CLOSE_LOAN = "$-", | ||
RUNEPOOL_DEPOSIT = "POOL+", | ||
RUNEPOOL_WITHDRAW = "POOL-", | ||
} | ||
@@ -67,0 +69,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
444618
1%3
-25%5522
1.08%1
Infinity%34
-2.86%56
-1.75%