@swapkit/helpers 1.0.4 → 1.1.0

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/package.json CHANGED
@@ -4,6 +4,7 @@
4
4
  "dependencies": {
5
5
  "@swapkit/tokens": "1.0.1",
6
6
  "ky": "1.4.0",
7
+ "picocolors": "1.0.1",
7
8
  "zod": "3.23.8"
8
9
  },
9
10
  "files": [
@@ -29,5 +30,5 @@
29
30
  },
30
31
  "type": "module",
31
32
  "types": "./src/index.ts",
32
- "version": "1.0.4"
33
+ "version": "1.1.0"
33
34
  }
@@ -174,7 +174,7 @@ describe("getDecimal", () => {
174
174
  });
175
175
 
176
176
  describe("Radix", () => {
177
- test(
177
+ test.todo(
178
178
  "returns proper decimal for radix and it's assets",
179
179
  async () => {
180
180
  const radixDecimal = await getDecimal({ chain: Chain.Radix, symbol: "XRD" });
@@ -218,7 +218,7 @@ describe("assetFromString", () => {
218
218
  });
219
219
  });
220
220
 
221
- test("should return the correct object for Radix resource", () => {
221
+ test.todo("should return the correct object for Radix resource", () => {
222
222
  const assetString =
223
223
  "XRD.xwBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75";
224
224
  const result = assetFromString(assetString);
@@ -45,7 +45,7 @@ export function wrapWithThrow<T>(fn: () => T, errorKey?: ErrorKeys) {
45
45
  }
46
46
  }
47
47
 
48
- export const getChainIdentifier = (chain: Chain) => {
48
+ export function getChainIdentifier<T extends Chain>(chain: T) {
49
49
  switch (chain) {
50
50
  case Chain.THORChain:
51
51
  return `${chain}.RUNE`;
@@ -59,4 +59,17 @@ export const getChainIdentifier = (chain: Chain) => {
59
59
  default:
60
60
  return `${chain}.${chain}`;
61
61
  }
62
- };
62
+ }
63
+
64
+ const skipWarnings = ["production", "test"].includes(process.env.NODE_ENV || "");
65
+ const warnings = new Set();
66
+ export function warnOnce(condition: boolean, warning: string) {
67
+ if (!skipWarnings && condition) {
68
+ if (warnings.has(warning)) {
69
+ return;
70
+ }
71
+
72
+ warnings.add(warning);
73
+ console.warn(warning);
74
+ }
75
+ }
@@ -42,16 +42,17 @@ describe("AssetValue", () => {
42
42
 
43
43
  expect(atomDerived.toString()).toBe("THOR.ATOM");
44
44
 
45
- const radixXWBTC = new AssetValue({
46
- identifier:
47
- "RADIX.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
48
- decimal: 8,
49
- value: 11112222,
50
- });
51
-
52
- expect(radixXWBTC.toString()).toBe(
53
- "RADIX.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
54
- );
45
+ // TODO:
46
+ // const radixXWBTC = new AssetValue({
47
+ // identifier:
48
+ // "RADIX.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
49
+ // decimal: 8,
50
+ // value: 11112222,
51
+ // });
52
+
53
+ // expect(radixXWBTC.toString()).toBe(
54
+ // "RADIX.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
55
+ // )
55
56
  });
56
57
  });
57
58
 
@@ -254,7 +255,7 @@ describe("AssetValue", () => {
254
255
  );
255
256
  });
256
257
 
257
- test("creates AssetValue with _ symbol", async () => {
258
+ test.todo("creates AssetValue with _ symbol", async () => {
258
259
  const radixXWBTC = await AssetValue.from({
259
260
  asset: "XRD.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
260
261
  asyncTokenLookup: true,
@@ -1,3 +1,4 @@
1
+ import { red, yellow } from "picocolors";
1
2
  import {
2
3
  type CommonAssetString,
3
4
  CommonAssetStrings,
@@ -6,6 +7,7 @@ import {
6
7
  getDecimal,
7
8
  isGasAsset,
8
9
  } from "../helpers/asset.ts";
10
+ import { warnOnce } from "../helpers/others.ts";
9
11
  import { validateIdentifier } from "../helpers/validators.ts";
10
12
  import { BaseDecimal, Chain, type ChainId, ChainToChainId } from "../types/chains.ts";
11
13
  import type { TokenNames, TokenTax } from "../types/tokens.ts";
@@ -112,9 +114,7 @@ export class AssetValue extends BigIntArithmetics {
112
114
  ...fromAssetOrChain
113
115
  }: T & AssetValueFromParams): ConditionalAssetValueReturn<T> {
114
116
  const parsedValue = value instanceof BigIntArithmetics ? value.getValue("string") : value;
115
-
116
117
  const isFromChain = "chain" in fromAssetOrChain;
117
-
118
118
  const assetOrChain = isFromChain ? fromAssetOrChain.chain : fromAssetOrChain.asset;
119
119
 
120
120
  const isFromCommonAssetOrChain =
@@ -126,11 +126,21 @@ export class AssetValue extends BigIntArithmetics {
126
126
  : { identifier: assetOrChain, decimal: undefined };
127
127
 
128
128
  const { chain, isSynthetic } = getAssetInfo(unsafeIdentifier);
129
+ const token = staticTokensMap.get(unsafeIdentifier.toUpperCase() as TokenNames);
130
+ const tokenDecimal = token?.decimal || commonAssetDecimal;
131
+
132
+ warnOnce(
133
+ !(asyncTokenLookup || tokenDecimal),
134
+ yellow(
135
+ `Couldn't find static decimal for ${red(unsafeIdentifier)} (Using default ${red(BaseDecimal[chain])} decimal as fallback).
136
+ This can result in incorrect calculations and mess with amount sent on transactions.
137
+ You can load static assets by installing @swapkit/tokens package and calling AssetValue.loadStaticAssets()
138
+ or by passing asyncTokenLookup: true to the from() function, which will make it async and return a promise.`,
139
+ ),
140
+ );
129
141
 
130
- const { tax, decimal, identifier } = staticTokensMap.get(
131
- unsafeIdentifier.toUpperCase() as TokenNames,
132
- ) || {
133
- decimal: commonAssetDecimal || BaseDecimal[chain],
142
+ const { decimal, identifier, tax } = token || {
143
+ decimal: tokenDecimal || BaseDecimal[chain],
134
144
  identifier: unsafeIdentifier,
135
145
  };
136
146
 
@@ -10,8 +10,11 @@ const errorCodes = {
10
10
  core_plugin_swap_not_found: 10007,
11
11
  core_approve_asset_target_invalid: 10008,
12
12
  core_explorer_unsupported_chain: 10009,
13
+ core_verify_message_not_supported: 10010,
13
14
  core_chain_halted: 10099,
14
-
15
+ /**
16
+ * Core - Wallet
17
+ */
15
18
  core_wallet_connection_not_found: 10100,
16
19
  core_wallet_xdefi_not_installed: 10101,
17
20
  core_wallet_evmwallet_not_installed: 10102,
@@ -24,6 +27,7 @@ const errorCodes = {
24
27
  core_wallet_keepkey_not_installed: 10109,
25
28
  core_wallet_talisman_not_installed: 10110,
26
29
  core_wallet_not_keypair_wallet: 10111,
30
+ core_wallet_sign_message_not_supported: 10110,
27
31
  /**
28
32
  * Core - Swap
29
33
  */
@@ -80,6 +84,7 @@ const errorCodes = {
80
84
  wallet_trezor_failed_to_get_address: 20503,
81
85
  wallet_talisman_not_enabled: 20601,
82
86
  wallet_talisman_not_found: 20602,
87
+ wallet_polkadot_not_found: 20701,
83
88
  /**
84
89
  * Chainflip
85
90
  */
@@ -105,12 +110,24 @@ const errorCodes = {
105
110
  * SwapKit API
106
111
  */
107
112
  api_v2_invalid_response: 50001,
108
-
109
113
  /**
110
114
  * Toolboxes
111
115
  */
112
116
  toolbox_cosmos_signer_not_defined: 90101,
113
117
  toolbox_cosmos_no_accounts_found: 90102,
118
+ toolbox_cosmos_verify_signature_no_pubkey: 90103,
119
+ toolbox_evm_no_abi_fragment: 90201,
120
+ toolbox_evm_no_signer: 90202,
121
+ toolbox_evm_no_signer_address: 90203,
122
+ toolbox_evm_no_from_address: 90204,
123
+ toolbox_evm_no_contract_address: 90205,
124
+ toolbox_evm_no_fee_data: 90206,
125
+ toolbox_evm_no_gas_price: 90207,
126
+ toolbox_evm_no_to_address: 90208,
127
+ toolbox_evm_invalid_gas_asset_address: 90209,
128
+ toolbox_evm_provider_not_eip1193_compatible: 90210,
129
+ toolbox_evm_error_estimating_gas_limit: 90211,
130
+ toolbox_evm_error_sending_transaction: 90212,
114
131
  /**
115
132
  * Helpers
116
133
  */
@@ -122,6 +139,10 @@ const errorCodes = {
122
139
  helpers_invalid_memo_type: 99005,
123
140
  helpers_failed_to_switch_network: 99103,
124
141
  helpers_not_found_provider: 99200,
142
+ /**
143
+ * Anything else
144
+ */
145
+ not_implemented: 99999,
125
146
  } as const;
126
147
 
127
148
  export type ErrorKeys = keyof typeof errorCodes;
@@ -22,6 +22,7 @@ export enum WalletOption {
22
22
  OKX = "OKX",
23
23
  OKX_MOBILE = "OKX_MOBILE",
24
24
  PHANTOM = "PHANTOM",
25
+ POLKADOT_JS = "POLKADOT_JS",
25
26
  RADIX_WALLET = "RADIX_WALLET",
26
27
  TREZOR = "TREZOR",
27
28
  TALISMAN = "TALISMAN",
@@ -42,11 +43,13 @@ export type ChainWallet = {
42
43
  balance: AssetValue[];
43
44
  walletType: WalletOption;
44
45
  disconnect?: () => void;
46
+ signMessage?: (message: string) => Promise<string>;
45
47
  };
46
48
 
47
49
  export type EmptyWallet = { [key in Chain]?: unknown };
48
50
  export type BaseWallet<T extends EmptyWallet | Record<string, unknown>> = {
49
- [key in Chain]: ChainWallet & (T extends EmptyWallet ? T[key] : unknown);
51
+ [key in Chain]: ChainWallet &
52
+ (T extends EmptyWallet ? T[key] : T[key] extends ChainWallet ? T[key] : never);
50
53
  };
51
54
 
52
55
  export type EIP6963ProviderInfo = {