Socket
Socket
Sign inDemoInstall

@swapkit/helpers

Package Overview
Dependencies
Maintainers
0
Versions
204
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@swapkit/helpers - npm Package Compare versions

Comparing version 1.0.0-rc.113 to 1.0.0-rc.114

21

package.json

@@ -5,15 +5,6 @@ {

"dependencies": {
"ky": "1.2.3",
"@swapkit/tokens": "1.0.0-rc.56",
"ky": "1.3.0",
"zod": "3.23.8"
},
"devDependencies": {
"@swapkit/tokens": "1.0.0-rc.55",
"bun-types": "1.1.8",
"ethers": "6.11.1"
},
"peerDependencies": {
"@swapkit/tokens": "1.0.0-rc.55",
"ky": "1.2.3",
"zod": "3.23.8"
},
"files": [

@@ -34,6 +25,6 @@ "src/",

"build": "bun run ./build.ts",
"clean": "rm -rf .turbo dist node_modules *.tsbuildinfo",
"lint": "biome check --apply ./src",
"clean": "rm -rf dist node_modules *.tsbuildinfo",
"lint": "biome check --write ./src",
"test": "bun test",
"test:coverage": "echo 'bun test --coverage'",
"test:coverage": "bun test --coverage",
"type-check": "tsc --noEmit"

@@ -43,3 +34,3 @@ },

"types": "./src/index.ts",
"version": "1.0.0-rc.113"
"version": "1.0.0-rc.114"
}
import { describe, expect, test } from "bun:test";
import { Chain, MemoType } from "../../types";
import { getMemoFor } from "../memo.ts";
import {
getMemoForDeposit,
getMemoForLeaveAndBond,
getMemoForNameRegister,
getMemoForSaverDeposit,
getMemoForSaverWithdraw,
getMemoForWithdraw,
} from "../memo.ts";
describe("getMemoFor", () => {
describe("for Leave, Upgrade, and Bond", () => {
const nodeMemos = [
[MemoType.LEAVE, "LEAVE:ABC123"],
[MemoType.BOND, "BOND:ABC123"],
];
for (const [memoType, expected = ""] of nodeMemos) {
test(`returns correct memo for ${memoType}`, () => {
const result = getMemoFor(memoType as MemoType, { address: "ABC123" });
expect(result).toBe(expected);
});
}
describe("getMemoForSaverDeposit", () => {
test("returns correct memo for single side", () => {
const result = getMemoForSaverDeposit({ chain: Chain.Ethereum, symbol: "ETH" });
expect(result).toBe("+:ETH/ETH");
});
});
describe("for Unbond and Thorname/Mayaname Register", () => {
test("returns correct memo for Unbond", () => {
const result = getMemoFor(MemoType.UNBOND, { address: "ABC123", unbondAmount: 1000000000 });
expect(result).toBe("UNBOND:ABC123:1000000000");
describe("getMemoForSaverWithdraw", () => {
test("returns correct memo for single side", () => {
const result = getMemoForSaverWithdraw({
basisPoints: 5000,
chain: Chain.Ethereum,
symbol: "ETH",
});
expect(result).toBe("-:ETH/ETH:5000");
});
});
test("returns correct memo for Thorname Register", () => {
const result = getMemoFor(MemoType.NAME_REGISTER, {
name: "thorname",
chain: "BNB",
address: "0xABC123",
owner: "0xDEF456",
});
expect(result).toBe("~:thorname:BNB:0xABC123:0xDEF456");
});
describe("getMemoForLeaveAndBond", () => {
test("returns correct memo for Leave", () => {
const result = getMemoForLeaveAndBond({ address: "ABC123", type: MemoType.LEAVE });
expect(result).toBe("LEAVE:ABC123");
});
test("returns correct memo for Mayaname Register", () => {
const result = getMemoFor(MemoType.NAME_REGISTER, {
name: "mayaname",
chain: "BNB",
address: "0xABC123",
owner: "0xDEF456",
});
expect(result).toBe("~:mayaname:BNB:0xABC123:0xDEF456");
});
test("returns correct memo for Bond", () => {
const result = getMemoForLeaveAndBond({ address: "ABC123", type: MemoType.BOND });
expect(result).toBe("BOND:ABC123");
});
});
describe("for Deposit", () => {
test("returns correct memo for Deposit (single side)", () => {
const result = getMemoFor(MemoType.DEPOSIT, {
chain: Chain.Ethereum,
symbol: "ETH",
singleSide: true,
});
expect(result).toBe("+:ETH/ETH");
describe("getMemoForNameRegister", () => {
test("returns correct memo for single side", () => {
const result = getMemoForNameRegister({
name: "asdfg",
chain: Chain.Ethereum,
owner: "thor1234",
address: "0xaasd123",
});
expect(result).toBe("~:asdfg:ETH:0xaasd123:thor1234");
});
});
test("returns correct memo for Deposit (dual side)", () => {
const result = getMemoFor(MemoType.DEPOSIT, {
chain: Chain.Avalanche,
symbol: "AVAX",
address: "0xABC123",
});
expect(result).toBe("+:AVAX.AVAX:0xABC123");
});
describe("getMemoForDeposit", () => {
test("returns correct memo for single side", () => {
const result = getMemoForDeposit({ chain: Chain.Ethereum, symbol: "ETH" });
expect(result).toBe("+:ETH.ETH");
});
});
describe("for Withdraw", () => {
test("returns correct memo for Withdraw (single side)", () => {
const result = getMemoFor(MemoType.WITHDRAW, {
chain: Chain.Bitcoin,
ticker: "BTC",
symbol: "BTC",
basisPoints: 10000,
singleSide: true,
});
expect(result).toBe("-:BTC/BTC:10000");
describe("getMemoForWithdraw", () => {
test("returns correct memo for single side", () => {
const result = getMemoForWithdraw({
chain: Chain.Ethereum,
symbol: "ETH",
ticker: "ETH",
basisPoints: 100,
});
test("returns correct memo for Withdraw (dual side)", () => {
const result = getMemoFor(MemoType.WITHDRAW, {
chain: Chain.Ethereum,
ticker: "ETH",
symbol: "ETH",
basisPoints: 100,
targetAssetString: "ETH.ETH",
});
expect(result).toBe("-:ETH.ETH:100:ETH.ETH");
});
expect(result).toBe("-:ETH.ETH:100");
});
});
import { Chain } from "../types/chains";
import { MemoType } from "../types/sdk";
export type NameRegisterParam = {
name: string;
chain: string;
address: string;
owner?: string;
preferredAsset?: string;
expiryBlock?: string;
};
type WithChain<T extends {}> = T & { chain: Chain };
type WithAffiliate<T extends {}> = T & { affiliateAddress?: string; affiliateBasisPoints?: number };
function addAffiliate(memo: string, { affiliateAddress, affiliateBasisPoints }: WithAffiliate<{}>) {
const affiliatePart = affiliateAddress ? `:${affiliateAddress}:${affiliateBasisPoints || 0}` : "";
return `${memo}${affiliatePart}`;
}
function getPoolIdentifier({ chain, symbol }: { chain: Chain; symbol: string }) {
switch (chain) {
case Chain.Bitcoin:
case Chain.Dogecoin:
case Chain.Litecoin:
return chain.slice(0, 1).toLowerCase();
case Chain.BitcoinCash:
return "c";
default:
return `${chain}.${symbol}`;
}
}
export function getMemoForLeaveAndBond({
type,
address,
}: { type: MemoType.BOND | MemoType.LEAVE; address: string }) {
return `${type}:${address}`;
}
export function getMemoForUnbond({
address,
unbondAmount,
}: { address: string; unbondAmount: number }) {
return `${MemoType.UNBOND}:${address}:${unbondAmount}`;
}
export function getMemoForNameRegister({
name,
chain,
address,
owner,
}: { name: string; chain: string; address: string; owner?: string }) {
const baseMemo = `${MemoType.NAME_REGISTER}:${name}:${chain}:${address}`;
const ownerAssignmentOrChangePart = owner ? `:${owner}` : "";
return `${baseMemo}${ownerAssignmentOrChangePart}`;
}
export function getMemoForLoan(
memoType: MemoType.OPEN_LOAN | MemoType.CLOSE_LOAN,
{
asset,
address,
minAmount,
...affiliate
}: WithAffiliate<{
address: string;
asset: string;
minAmount?: string;
}>,
) {
const baseMemo = `${memoType}:${asset}:${address}`;
const minAmountPart = minAmount ? `:${minAmount}` : "";
return addAffiliate(`${baseMemo}${minAmountPart}`, affiliate);
}
export function getMemoForSaverDeposit({
chain,
symbol,
...affiliate
}: WithAffiliate<{ chain: Chain; symbol: string }>) {
return addAffiliate(`${MemoType.DEPOSIT}:${chain}/${symbol}`, affiliate);
}
export function getMemoForDeposit({
chain,
symbol,
address,
}: { chain: Chain; symbol: string; address?: string }) {
const poolIdentifier = getPoolIdentifier({ chain, symbol });
const addressPart = address ? `:${address}` : "";
return `${MemoType.DEPOSIT}:${poolIdentifier}${addressPart}`;
}
export function getMemoForSaverWithdraw({
chain,
symbol,
basisPoints,
...affiliate
}: WithAffiliate<{ chain: Chain; symbol: string; basisPoints: number }>) {
return addAffiliate(`${MemoType.WITHDRAW}:${chain}/${symbol}:${basisPoints}`, affiliate);
}
export function getMemoForWithdraw({
chain,
symbol,
ticker,
basisPoints,
targetAsset,
...affiliate
}: WithAffiliate<{
chain: Chain;
symbol: string;
ticker: string;
basisPoints: number;
targetAsset?: string;
}>) {
const shortenedSymbol =
chain === "ETH" && ticker !== "ETH" ? `${ticker}-${symbol.slice(-3)}` : symbol;
const targetPart = targetAsset ? `:${targetAsset}` : "";
return addAffiliate(
`${MemoType.WITHDRAW}:${chain}.${shortenedSymbol}:${basisPoints}${targetPart}`,
affiliate,
);
}
/**
* @deprecated - Use separate functions per each memo type like getMemoForDeposit, getMemoForWithdraw, etc.
*/
export type MemoOptions<T extends MemoType> = {

@@ -29,5 +142,8 @@ [MemoType.BOND]: { address: string };

}>;
[MemoType.NAME_REGISTER]: Omit<NameRegisterParam, "preferredAsset" | "expiryBlock">;
[MemoType.NAME_REGISTER]: { name: string; chain: string; address: string };
}[T];
/**
* @deprecated - Use separate functions per each memo type like getMemoForDeposit, getMemoForWithdraw, etc.
*/
export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions<T>) => {

@@ -38,3 +154,3 @@ switch (memoType) {

const { address } = options as MemoOptions<MemoType.BOND>;
return `${memoType}:${address}`;
return getMemoForLeaveAndBond({ type: memoType, address });
}

@@ -44,53 +160,44 @@

const { address, unbondAmount } = options as MemoOptions<MemoType.UNBOND>;
return `${memoType}:${address}:${unbondAmount}`;
return getMemoForUnbond({ address, unbondAmount });
}
case MemoType.NAME_REGISTER: {
const { name, chain, address, owner } = options as MemoOptions<MemoType.NAME_REGISTER>;
return `${memoType}:${name}:${chain}:${address}${owner ? `:${owner}` : ""}`;
return getMemoForNameRegister(options as MemoOptions<MemoType.NAME_REGISTER>);
}
case MemoType.OPEN_LOAN:
case MemoType.CLOSE_LOAN: {
return getMemoForLoan(memoType, options as MemoOptions<MemoType.OPEN_LOAN>);
}
case MemoType.DEPOSIT: {
const { chain, symbol, address, singleSide } = options as MemoOptions<MemoType.DEPOSIT>;
const getPoolIdentifier = (chain: Chain, symbol: string): string => {
switch (chain) {
case Chain.Litecoin:
return "l";
case Chain.Dogecoin:
return "d";
case Chain.BitcoinCash:
return "c";
default:
return `${chain}.${symbol}`;
}
};
if (singleSide) {
return getMemoForSaverDeposit({ chain, symbol });
}
return singleSide
? `${memoType}:${chain}/${symbol}`
: `${memoType}:${getPoolIdentifier(chain, symbol)}:${address || ""}`;
return getMemoForDeposit({ chain, symbol, address });
}
case MemoType.WITHDRAW: {
const { chain, ticker, symbol, basisPoints, targetAssetString, singleSide } =
options as MemoOptions<MemoType.WITHDRAW>;
const {
chain,
ticker,
symbol,
basisPoints,
targetAssetString: targetAsset,
singleSide,
} = options as MemoOptions<MemoType.WITHDRAW>;
const shortenedSymbol =
chain === "ETH" && ticker !== "ETH" ? `${ticker}-${symbol.slice(-3)}` : symbol;
const target = !singleSide && targetAssetString ? `:${targetAssetString}` : "";
const assetDivider = singleSide ? "/" : ".";
if (singleSide) {
return getMemoForSaverWithdraw({ chain, symbol, basisPoints });
}
return `${memoType}:${chain}${assetDivider}${shortenedSymbol}:${basisPoints}${target}`;
return getMemoForWithdraw({ chain, ticker, symbol, basisPoints, targetAsset });
}
case MemoType.OPEN_LOAN:
case MemoType.CLOSE_LOAN: {
const { asset, address } = options as MemoOptions<MemoType.OPEN_LOAN>;
return `${memoType}:${asset}:${address}`; //:${minAmount ? `${minAmount}` : ''}:t:0`;
}
default:
return "";
throw new Error(`Unsupported memo type: ${memoType}`);
}
};

@@ -65,3 +65,3 @@ import type { CommonAssetString } from "../helpers/asset.ts";

eq(assetValue: AssetValue): boolean {
eq(assetValue: AssetValue) {
return this.eqAsset(assetValue) && this.eqValue(assetValue);

@@ -181,2 +181,3 @@ }

const { decimal, identifier } = getCommonAssetInfo(assetString);
return new AssetValue({ value: safeValue(value, decimal), decimal, identifier });

@@ -183,0 +184,0 @@ }

@@ -45,5 +45,3 @@ import { BaseDecimal } from "../types/chains.ts";

// Increment the last decimal place and slice off the rest
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(
Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1
).toString()}`;
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1).toString()}`;
} else {

@@ -281,5 +279,3 @@ // Just slice off the extra digits

// Increment the last decimal place and slice off the rest
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(
Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1
).toString()}`;
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1).toString()}`;
} else {

@@ -286,0 +282,0 @@ // Just slice off the extra digits

@@ -14,3 +14,2 @@ const errorMessages = {

core_chain_halted: 10099,
/**

@@ -28,4 +27,2 @@ * Core - Wallet Connection

core_wallet_keepkey_not_installed: 10109,
core_wallet_talisman_not_installed: 10110,
core_wallet_not_keypair_wallet: 10111,
/**

@@ -46,8 +43,8 @@ * Core - Swap

core_transaction_deposit_error: 10301,
core_transaction_create_liquidity_rune_error: 10302,
core_transaction_create_liquidity_base_error: 10302,
core_transaction_create_liquidity_asset_error: 10303,
core_transaction_create_liquidity_invalid_params: 10304,
core_transaction_add_liquidity_invalid_params: 10305,
core_transaction_add_liquidity_no_rune_address: 10306,
core_transaction_add_liquidity_rune_error: 10307,
core_transaction_add_liquidity_base_address: 10306,
core_transaction_add_liquidity_base_error: 10307,
core_transaction_add_liquidity_asset_error: 10308,

@@ -61,9 +58,6 @@ core_transaction_withdraw_error: 10309,

core_transaction_user_rejected: 10315,
core_transaction_create_liquidity_cacao_error: 10316,
core_transaction_add_liquidity_no_cacao_address: 10306,
core_transaction_add_liquidity_cacao_error: 10307,
/**
* Wallets
*/
wallet_connection_rejected_by_user: 20000,
wallet_ledger_connection_error: 20001,

@@ -74,3 +68,5 @@ wallet_ledger_connection_claimed: 20002,

wallet_ledger_device_locked: 20005,
wallet_phantom_not_found: 20101,
wallet_xdefi_not_found: 20201,
wallet_xdefi_failed_to_add_or_switch_network: 20202,
/**

@@ -81,11 +77,11 @@ * Chainflip

chainflip_broker_recipient_error: 30002,
/**
* THORChain
*/
thorchain_chain_halted: 40001,
thorchain_trading_halted: 40002,
/**
* SwapKit API
*/
api_v2_invalid_response: 40001,
api_v2_invalid_response: 50001,

@@ -95,3 +91,3 @@ /**

*/
helpers_number_different_decimals: 99101,
helpers_number_different_decimals: 99001,
} as const;

@@ -98,0 +94,0 @@

@@ -23,2 +23,3 @@ import { ExplorerUrl, RPCUrl } from "./network";

THORChain = "THOR",
Solana = "SOL",
}

@@ -56,2 +57,3 @@

THORChainStagenet = "thorchain-stagenet-v2",
Solana = "solana",
}

@@ -87,2 +89,3 @@

[ChainId.THORChain]: Chain.THORChain,
[ChainId.Solana]: Chain.Solana,
};

@@ -112,5 +115,6 @@

OP = 18,
SOL = 9,
THOR = 8,
XRD = 18,
ZEC = 8,
XRD = 18,
}

@@ -117,0 +121,0 @@

@@ -30,4 +30,5 @@ import type { Chain } from "./chains";

OP = "m/44'/60'/0'/0",
SOL = "m/44'/501'/0'/0",
THOR = "m/44'/931'/0'/0",
XRD = "////",
THOR = "m/44'/931'/0'/0",
}

@@ -53,2 +54,3 @@

OP: [44, 60, 0, 0, 0],
SOL: [44, 501, 0, 0, 0],
THOR: [44, 931, 0, 0, 0],

@@ -55,0 +57,0 @@

@@ -23,2 +23,3 @@ export enum RPCUrl {

THORChainStagenet = "https://stagenet-rpc.ninerealms.com",
Solana = "https://mainnet.helius-rpc.com/?api-key=2cbe3ae6-cfc5-4141-a093-0055d0fa3d80",
}

@@ -42,6 +43,7 @@

Optimism = "https://optimistic.etherscan.io",
Polkadot = "https://polkadot.subscan.io/",
Polkadot = "https://polkadot.subscan.io",
Polygon = "https://polygonscan.com",
Radix = "https://dashboard.radixdlt.com",
THORChain = "https://runescan.io",
Solana = "https://solscan.io",
}

@@ -12,20 +12,21 @@ import type { Eip1193Provider } from "ethers";

export enum WalletOption {
BRAVE = "BRAVE",
COINBASE_MOBILE = "COINBASE_MOBILE",
COINBASE_WEB = "COINBASE_WEB",
EIP6963 = "EIP6963",
EXODUS = "EXODUS",
KEEPKEY = "KEEPKEY",
KEPLR = "KEPLR",
KEYSTORE = "KEYSTORE",
KEEPKEY = "KEEPKEY",
XDEFI = "XDEFI",
LEDGER = "LEDGER",
METAMASK = "METAMASK",
COINBASE_WEB = "COINBASE_WEB",
COINBASE_MOBILE = "COINBASE_MOBILE",
TREZOR = "TREZOR",
TRUSTWALLET_WEB = "TRUSTWALLET_WEB",
LEDGER = "LEDGER",
KEPLR = "KEPLR",
OKX = "OKX",
OKX_MOBILE = "OKX_MOBILE",
BRAVE = "BRAVE",
WALLETCONNECT = "WALLETCONNECT",
EIP6963 = "EIP6963",
EXODUS = "EXODUS",
PHANTOM = "PHANTOM",
RADIX_WALLET = "RADIX_WALLET",
TREZOR = "TREZOR",
TALISMAN = "TALISMAN",
TRUSTWALLET_WEB = "TRUSTWALLET_WEB",
WALLETCONNECT = "WALLETCONNECT",
XDEFI = "XDEFI",
}

@@ -48,6 +49,4 @@

export type EmptyWallet = { [key in Chain]?: unknown };
export type BaseWallet<T extends EmptyWallet | unknown> = {
// @ts-expect-error
[key in Chain]: ChainWallet & T[key];
export type BaseWallet<T extends EmptyWallet | Record<string, unknown>> = {
[key in Chain]: ChainWallet & (T extends EmptyWallet ? T[key] : unknown);
};

@@ -54,0 +53,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc