@swapkit/helpers
Advanced tools
Comparing version 1.0.0-rc.115 to 1.0.0-rc.116
@@ -5,3 +5,3 @@ { | ||
"dependencies": { | ||
"@swapkit/tokens": "1.0.0-rc.57", | ||
"@swapkit/tokens": "undefined", | ||
"ky": "1.3.0", | ||
@@ -33,3 +33,3 @@ "zod": "3.23.8" | ||
"types": "./src/index.ts", | ||
"version": "1.0.0-rc.115" | ||
"version": "1.0.0-rc.116" | ||
} |
@@ -37,3 +37,3 @@ import { describe, expect, test } from "bun:test"; | ||
test("throws an error for negative years", () => { | ||
expect(() => getTHORNameCost(-1)).toThrow("Invalid number of year"); | ||
expect(() => getTHORNameCost(-1)).toThrow("helpers_invalid_number_of_years"); | ||
}); | ||
@@ -40,0 +40,0 @@ }); |
@@ -0,1 +1,2 @@ | ||
import { SwapKitError } from "../modules/swapKitError"; | ||
import { Chain } from "../types/chains"; | ||
@@ -198,4 +199,7 @@ import { MemoType } from "../types/sdk"; | ||
default: | ||
throw new Error(`Unsupported memo type: ${memoType}`); | ||
throw new SwapKitError({ | ||
errorKey: "helpers_invalid_memo_type", | ||
info: { memoType }, | ||
}); | ||
} | ||
}; |
@@ -8,3 +8,7 @@ import { type ErrorKeys, SwapKitError } from "../modules/swapKitError"; | ||
export function getTHORNameCost(numberOfYears: number) { | ||
if (numberOfYears < 0) throw new Error("Invalid number of years"); | ||
if (numberOfYears < 0) | ||
throw new SwapKitError({ | ||
errorKey: "helpers_invalid_number_of_years", | ||
info: { numberOfYears }, | ||
}); | ||
return 10 + numberOfYears; | ||
@@ -16,3 +20,7 @@ } | ||
export function getMAYANameCost(numberOfYears: number) { | ||
if (numberOfYears < 0) throw new Error("Invalid number of year"); | ||
if (numberOfYears < 0) | ||
throw new SwapKitError({ | ||
errorKey: "helpers_invalid_number_of_years", | ||
info: { numberOfYears }, | ||
}); | ||
// round to max 10 decimals | ||
@@ -19,0 +27,0 @@ return Math.round((10 + numberOfYears * 1.0512) * 1e10) / 1e10; |
@@ -0,1 +1,2 @@ | ||
import { SwapKitError } from "../modules/swapKitError"; | ||
import { Chain } from "../types/chains"; | ||
@@ -15,5 +16,9 @@ | ||
throw new Error( | ||
`Invalid identifier: ${identifier}. Expected format: <Chain>.<Ticker> or <Chain>.<Ticker>-<ContractAddress>`, | ||
); | ||
throw new SwapKitError({ | ||
errorKey: "helpers_invalid_identifier", | ||
info: { | ||
message: `Invalid identifier: ${identifier}. Expected format: <Chain>.<Ticker> or <Chain>.<Ticker>-<ContractAddress>`, | ||
identifier, | ||
}, | ||
}); | ||
} | ||
@@ -20,0 +25,0 @@ |
import type { BrowserProvider } from "ethers"; | ||
import { SwapKitError } from "../modules/swapKitError"; | ||
import { | ||
@@ -88,3 +89,6 @@ ChainId, | ||
} catch (error) { | ||
throw new Error(`Failed to switch network: ${error}`); | ||
throw new SwapKitError({ | ||
errorKey: "helpers_failed_to_switch_network", | ||
info: { error }, | ||
}); | ||
} | ||
@@ -95,3 +99,5 @@ return func(...args); | ||
const providerRequest = ({ provider, params, method }: ProviderRequestParams) => { | ||
if (!provider?.send) throw new Error("Provider not found"); | ||
if (!provider?.send) { | ||
throw new SwapKitError("helpers_not_found_provider"); | ||
} | ||
@@ -98,0 +104,0 @@ const providerParams = params ? (Array.isArray(params) ? params : [params]) : []; |
@@ -414,4 +414,4 @@ import { describe, expect, test } from "bun:test"; | ||
isSynthetic: false, | ||
symbol: "BTC.b-0x152b9d0fdc40c096757f570a51e494bd4b943e50", | ||
ticker: "BTC.b", | ||
symbol: "BTC.B-0x152b9d0fdc40c096757f570a51e494bd4b943e50", | ||
ticker: "BTC.B", | ||
}), | ||
@@ -418,0 +418,0 @@ ); |
@@ -9,2 +9,3 @@ import type { CommonAssetString } from "../helpers/asset.ts"; | ||
import { BigIntArithmetics, formatBigIntToSafeValue } from "./bigIntArithmetics.ts"; | ||
import { SwapKitError } from "./swapKitError.ts"; | ||
import { SwapKitNumber, type SwapKitValueType } from "./swapKitNumber.ts"; | ||
@@ -75,3 +76,8 @@ | ||
const [chain, ticker, symbol] = urlAsset.split("."); | ||
if (!(chain && ticker)) throw new Error("Invalid asset url"); | ||
if (!(chain && ticker)) { | ||
throw new SwapKitError({ | ||
errorKey: "helpers_invalid_asset_url", | ||
info: { urlAsset }, | ||
}); | ||
} | ||
@@ -88,7 +94,3 @@ const assetString = | ||
static fromIdentifier( | ||
assetString: | ||
| `${Chain}.${string}` | ||
| `${Chain}/${string}` | ||
| `${Chain}.${string}-${string}` | ||
| TokenNames, | ||
assetString: `${Chain}.${string}` | `${Chain}/${string}` | TokenNames, | ||
value: NumberPrimitives = 0, | ||
@@ -134,3 +136,3 @@ ) { | ||
}).getBaseValue("string"); | ||
const assetValue = await AssetValue.fromString(assetString, value); | ||
const assetValue = await AssetValue.fromString(assetString); | ||
@@ -264,3 +266,8 @@ return assetValue.set(shiftedAmount); | ||
if (!(synthChain && symbol)) throw new Error("Invalid asset identifier"); | ||
if (!(synthChain && symbol)) { | ||
throw new SwapKitError({ | ||
errorKey: "helpers_invalid_asset_identifier", | ||
info: { identifier }, | ||
}); | ||
} | ||
@@ -292,3 +299,8 @@ return new AssetValue({ | ||
if (isSynthetic && !(synthChain && synthSymbol)) throw new Error("Invalid asset identifier"); | ||
if (isSynthetic && !(synthChain && synthSymbol)) { | ||
throw new SwapKitError({ | ||
errorKey: "helpers_invalid_asset_identifier", | ||
info: { identifier }, | ||
}); | ||
} | ||
@@ -295,0 +307,0 @@ const adjustedIdentifier = |
@@ -1,6 +0,5 @@ | ||
const errorMessages = { | ||
const errorCodes = { | ||
/** | ||
* Core | ||
*/ | ||
core_wallet_connection_not_found: 10001, | ||
core_estimated_max_spendable_chain_not_supported: 10002, | ||
@@ -13,6 +12,6 @@ core_extend_error: 10003, | ||
core_approve_asset_target_invalid: 10008, | ||
core_explorer_unsupported_chain: 10009, | ||
core_chain_halted: 10099, | ||
/** | ||
* Core - Wallet Connection | ||
*/ | ||
core_wallet_connection_not_found: 10100, | ||
core_wallet_xdefi_not_installed: 10101, | ||
@@ -60,10 +59,25 @@ core_wallet_evmwallet_not_installed: 10102, | ||
wallet_connection_rejected_by_user: 20000, | ||
wallet_ledger_connection_error: 20001, | ||
wallet_ledger_connection_claimed: 20002, | ||
wallet_ledger_get_address_error: 20003, | ||
wallet_ledger_device_not_found: 20004, | ||
wallet_ledger_device_locked: 20005, | ||
wallet_phantom_not_found: 20101, | ||
wallet_xdefi_not_found: 20201, | ||
wallet_xdefi_failed_to_add_or_switch_network: 20202, | ||
wallet_missing_api_key: 20001, | ||
wallet_chain_not_supported: 20002, | ||
wallet_missing_params: 20003, | ||
wallet_provider_not_found: 20004, | ||
wallet_failed_to_add_or_switch_network: 20005, | ||
wallet_ledger_connection_error: 20101, | ||
wallet_ledger_connection_claimed: 20102, | ||
wallet_ledger_get_address_error: 20103, | ||
wallet_ledger_device_not_found: 20104, | ||
wallet_ledger_device_locked: 20105, | ||
wallet_phantom_not_found: 20201, | ||
wallet_xdefi_not_found: 20301, | ||
wallet_xdefi_send_transaction_no_address: 20302, | ||
wallet_xdefi_contract_address_not_provided: 20303, | ||
wallet_xdefi_asset_not_defined: 20304, | ||
wallet_walletconnect_project_id_not_specified: 20401, | ||
wallet_walletconnect_connection_not_established: 20402, | ||
wallet_walletconnect_namespace_not_supported: 20403, | ||
wallet_trezor_failed_to_sign_transaction: 20501, | ||
wallet_trezor_derivation_path_not_supported: 20502, | ||
wallet_trezor_failed_to_get_address: 20503, | ||
wallet_talisman_not_enabled: 20601, | ||
wallet_talisman_not_found: 20602, | ||
/** | ||
@@ -73,4 +87,10 @@ * Chainflip | ||
chainflip_channel_error: 30001, | ||
chainflip_broker_recipient_error: 30002, | ||
chainflip_unknown_asset: 30003, | ||
chainflip_unknown_asset: 30002, | ||
chainflip_broker_invalid_params: 30100, | ||
chainflip_broker_recipient_error: 30101, | ||
chainflip_broker_register: 30102, | ||
chainflip_broker_tx_error: 30103, | ||
chainflip_broker_withdraw: 30104, | ||
chainflip_broker_fund_only_flip_supported: 30105, | ||
chainflip_broker_fund_invalid_address: 30106, | ||
/** | ||
@@ -81,2 +101,6 @@ * THORChain | ||
thorchain_trading_halted: 40002, | ||
thorchain_swapin_router_required: 40100, | ||
thorchain_swapin_vault_required: 40101, | ||
thorchain_swapin_memo_required: 40102, | ||
thorchain_swapin_token_required: 40103, | ||
/** | ||
@@ -88,11 +112,32 @@ * SwapKit API | ||
/** | ||
* Toolboxes | ||
*/ | ||
toolbox_cosmos_signer_not_defined: 90101, | ||
toolbox_cosmos_no_accounts_found: 90102, | ||
/** | ||
* Helpers | ||
*/ | ||
helpers_number_different_decimals: 99001, | ||
helpers_invalid_number_different_decimals: 99000, | ||
helpers_invalid_number_of_years: 99001, | ||
helpers_invalid_identifier: 99002, | ||
helpers_invalid_asset_url: 99003, | ||
helpers_invalid_asset_identifier: 99004, | ||
helpers_invalid_memo_type: 99005, | ||
helpers_failed_to_switch_network: 99103, | ||
helpers_not_found_provider: 99200, | ||
} as const; | ||
export type ErrorKeys = keyof typeof errorMessages; | ||
export type ErrorKeys = keyof typeof errorCodes; | ||
export class SwapKitError extends Error { | ||
constructor(errorKey: ErrorKeys, sourceError?: NotWorth) { | ||
static ErrorCode = errorCodes; | ||
constructor( | ||
errorOrErrorKey: ErrorKeys | { errorKey: ErrorKeys; info?: Record<string, NotWorth> }, | ||
sourceError?: NotWorth, | ||
) { | ||
const isErrorString = typeof errorOrErrorKey === "string"; | ||
const errorKey = isErrorString ? errorOrErrorKey : errorOrErrorKey.errorKey; | ||
if (sourceError) { | ||
@@ -106,6 +151,10 @@ console.error(sourceError, { | ||
super(errorKey, { | ||
cause: { code: errorMessages[errorKey], message: errorKey }, | ||
cause: { | ||
code: SwapKitError.ErrorCode[errorKey], | ||
message: `${errorKey}${isErrorString ? "" : `: ${JSON.stringify(errorOrErrorKey.info)}`}`, | ||
}, | ||
}); | ||
Object.setPrototypeOf(this, SwapKitError.prototype); | ||
} | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
433135
5318
- Removed@swapkit/tokens@1.0.0-rc.57(transitive)
Updated@swapkit/tokens@undefined