@tcswap/helpers 4.5.15

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.
Files changed (139) hide show
  1. package/dist/api/index.cjs +4 -0
  2. package/dist/api/index.cjs.map +16 -0
  3. package/dist/api/index.js +4 -0
  4. package/dist/api/index.js.map +16 -0
  5. package/dist/chunk-pfmeq01a.js +5 -0
  6. package/dist/chunk-pfmeq01a.js.map +9 -0
  7. package/dist/chunk-vb4wtm2w.js +4 -0
  8. package/dist/chunk-vb4wtm2w.js.map +9 -0
  9. package/dist/contracts.cjs +4 -0
  10. package/dist/contracts.cjs.map +10 -0
  11. package/dist/contracts.js +4 -0
  12. package/dist/contracts.js.map +10 -0
  13. package/dist/index.cjs +7 -0
  14. package/dist/index.cjs.map +30 -0
  15. package/dist/index.js +7 -0
  16. package/dist/index.js.map +30 -0
  17. package/dist/tokens.cjs +4 -0
  18. package/dist/tokens.cjs.map +10 -0
  19. package/dist/tokens.js +4 -0
  20. package/dist/tokens.js.map +10 -0
  21. package/dist/types/api/index.d.ts +502 -0
  22. package/dist/types/api/index.d.ts.map +1 -0
  23. package/dist/types/api/memoless/endpoints.d.ts +56 -0
  24. package/dist/types/api/memoless/endpoints.d.ts.map +1 -0
  25. package/dist/types/api/memoless/types.d.ts +85 -0
  26. package/dist/types/api/memoless/types.d.ts.map +1 -0
  27. package/dist/types/api/midgard/endpoints.d.ts +80 -0
  28. package/dist/types/api/midgard/endpoints.d.ts.map +1 -0
  29. package/dist/types/api/midgard/types.d.ts +543 -0
  30. package/dist/types/api/midgard/types.d.ts.map +1 -0
  31. package/dist/types/api/thornode/endpoints.d.ts +34 -0
  32. package/dist/types/api/thornode/endpoints.d.ts.map +1 -0
  33. package/dist/types/api/thornode/types.d.ts +264 -0
  34. package/dist/types/api/thornode/types.d.ts.map +1 -0
  35. package/dist/types/api/uswap/endpoints.d.ts +372 -0
  36. package/dist/types/api/uswap/endpoints.d.ts.map +1 -0
  37. package/dist/types/api/uswap/types.d.ts +1487 -0
  38. package/dist/types/api/uswap/types.d.ts.map +1 -0
  39. package/dist/types/contracts.d.ts +2 -0
  40. package/dist/types/contracts.d.ts.map +1 -0
  41. package/dist/types/index.d.ts +32 -0
  42. package/dist/types/index.d.ts.map +1 -0
  43. package/dist/types/modules/assetValue.d.ts +82 -0
  44. package/dist/types/modules/assetValue.d.ts.map +1 -0
  45. package/dist/types/modules/bigIntArithmetics.d.ts +60 -0
  46. package/dist/types/modules/bigIntArithmetics.d.ts.map +1 -0
  47. package/dist/types/modules/feeMultiplier.d.ts +48 -0
  48. package/dist/types/modules/feeMultiplier.d.ts.map +1 -0
  49. package/dist/types/modules/requestClient.d.ts +33 -0
  50. package/dist/types/modules/requestClient.d.ts.map +1 -0
  51. package/dist/types/modules/uSwapConfig.d.ts +249 -0
  52. package/dist/types/modules/uSwapConfig.d.ts.map +1 -0
  53. package/dist/types/modules/uSwapError.d.ts +879 -0
  54. package/dist/types/modules/uSwapError.d.ts.map +1 -0
  55. package/dist/types/modules/uSwapNumber.d.ts +10 -0
  56. package/dist/types/modules/uSwapNumber.d.ts.map +1 -0
  57. package/dist/types/tokens.d.ts +2 -0
  58. package/dist/types/tokens.d.ts.map +1 -0
  59. package/dist/types/types/commonTypes.d.ts +16 -0
  60. package/dist/types/types/commonTypes.d.ts.map +1 -0
  61. package/dist/types/types/derivationPath.d.ts +4 -0
  62. package/dist/types/types/derivationPath.d.ts.map +1 -0
  63. package/dist/types/types/errors/apiV1.d.ts +2 -0
  64. package/dist/types/types/errors/apiV1.d.ts.map +1 -0
  65. package/dist/types/types/index.d.ts +6 -0
  66. package/dist/types/types/index.d.ts.map +1 -0
  67. package/dist/types/types/quotes.d.ts +180 -0
  68. package/dist/types/types/quotes.d.ts.map +1 -0
  69. package/dist/types/types/sdk.d.ts +35 -0
  70. package/dist/types/types/sdk.d.ts.map +1 -0
  71. package/dist/types/types/wallet.d.ts +130 -0
  72. package/dist/types/types/wallet.d.ts.map +1 -0
  73. package/dist/types/utils/asset.d.ts +37 -0
  74. package/dist/types/utils/asset.d.ts.map +1 -0
  75. package/dist/types/utils/chains.d.ts +13 -0
  76. package/dist/types/utils/chains.d.ts.map +1 -0
  77. package/dist/types/utils/derivationPath.d.ts +21 -0
  78. package/dist/types/utils/derivationPath.d.ts.map +1 -0
  79. package/dist/types/utils/explorerUrls.d.ts +10 -0
  80. package/dist/types/utils/explorerUrls.d.ts.map +1 -0
  81. package/dist/types/utils/liquidity.d.ts +62 -0
  82. package/dist/types/utils/liquidity.d.ts.map +1 -0
  83. package/dist/types/utils/memo.d.ts +65 -0
  84. package/dist/types/utils/memo.d.ts.map +1 -0
  85. package/dist/types/utils/others.d.ts +15 -0
  86. package/dist/types/utils/others.d.ts.map +1 -0
  87. package/dist/types/utils/validators.d.ts +6 -0
  88. package/dist/types/utils/validators.d.ts.map +1 -0
  89. package/dist/types/utils/wallets.d.ts +36 -0
  90. package/dist/types/utils/wallets.d.ts.map +1 -0
  91. package/package.json +67 -0
  92. package/src/api/index.ts +15 -0
  93. package/src/api/memoless/endpoints.ts +62 -0
  94. package/src/api/memoless/types.ts +83 -0
  95. package/src/api/midgard/endpoints.ts +352 -0
  96. package/src/api/midgard/types.ts +515 -0
  97. package/src/api/thornode/endpoints.ts +109 -0
  98. package/src/api/thornode/types.ts +247 -0
  99. package/src/api/uswap/endpoints.ts +252 -0
  100. package/src/api/uswap/types.ts +626 -0
  101. package/src/contracts.ts +1 -0
  102. package/src/index.ts +32 -0
  103. package/src/modules/__tests__/assetValue.test.ts +2452 -0
  104. package/src/modules/__tests__/bigIntArithmetics.test.ts +410 -0
  105. package/src/modules/__tests__/feeMultiplier.test.ts +131 -0
  106. package/src/modules/__tests__/uSwapConfig.test.ts +429 -0
  107. package/src/modules/__tests__/uSwapNumber.test.ts +439 -0
  108. package/src/modules/assetValue.ts +536 -0
  109. package/src/modules/bigIntArithmetics.ts +366 -0
  110. package/src/modules/feeMultiplier.ts +84 -0
  111. package/src/modules/requestClient.ts +116 -0
  112. package/src/modules/uSwapConfig.ts +189 -0
  113. package/src/modules/uSwapError.ts +474 -0
  114. package/src/modules/uSwapNumber.ts +17 -0
  115. package/src/tokens.ts +1 -0
  116. package/src/types/commonTypes.ts +10 -0
  117. package/src/types/derivationPath.ts +11 -0
  118. package/src/types/errors/apiV1.ts +0 -0
  119. package/src/types/index.ts +5 -0
  120. package/src/types/quotes.ts +182 -0
  121. package/src/types/sdk.ts +38 -0
  122. package/src/types/wallet.ts +124 -0
  123. package/src/utils/__tests__/asset.test.ts +186 -0
  124. package/src/utils/__tests__/derivationPath.test.ts +142 -0
  125. package/src/utils/__tests__/explorerUrls.test.ts +59 -0
  126. package/src/utils/__tests__/liquidity.test.ts +302 -0
  127. package/src/utils/__tests__/memo.test.ts +99 -0
  128. package/src/utils/__tests__/others.test.ts +169 -0
  129. package/src/utils/__tests__/validators.test.ts +84 -0
  130. package/src/utils/__tests__/wallets.test.ts +625 -0
  131. package/src/utils/asset.ts +399 -0
  132. package/src/utils/chains.ts +104 -0
  133. package/src/utils/derivationPath.ts +101 -0
  134. package/src/utils/explorerUrls.ts +32 -0
  135. package/src/utils/liquidity.ts +154 -0
  136. package/src/utils/memo.ts +102 -0
  137. package/src/utils/others.ts +64 -0
  138. package/src/utils/validators.ts +36 -0
  139. package/src/utils/wallets.ts +238 -0
File without changes
@@ -0,0 +1,5 @@
1
+ export * from "./commonTypes";
2
+ export * from "./derivationPath";
3
+ export * from "./quotes";
4
+ export * from "./sdk";
5
+ export * from "./wallet";
@@ -0,0 +1,182 @@
1
+ export enum ErrorCode {
2
+ unknownError = "unknownError",
3
+ test_error = "test_error",
4
+ providerDetailsError = "providerDetailsError",
5
+ blockHeaderNotFound = "blockHeaderNotFound",
6
+ blockHashNotFoundAtHeight = "blockHashNotFoundAtHeight",
7
+ blockHashNotFoundAtHash = "blockHashNotFoundAtHash",
8
+ txHashMissing = "txHashMissing",
9
+ assetValueMissingInfo = "assetValueMissingInfo",
10
+ invalidAsset = "invalidAsset",
11
+ blockIsRequired = "blockIsRequired",
12
+ currentBlockHeaderNotFound = "currentBlockHeaderNotFound",
13
+ failedToRetrieveBalance = "failedToRetrieveBalance",
14
+ failedToRetrieveBlock = "failedToRetrieveBlock",
15
+ failedToRetrieveFees = "failedToRetrieveFees",
16
+ notImplementedBCH = "notImplementedBCH",
17
+ notImplementedDoge = "notImplementedDoge",
18
+ noPoolsFound = "noPoolsFound",
19
+ noVaultsFound = "noVaultsFound",
20
+ noTxFound = "noTxFound",
21
+ noInputCoinFound = "noInputCoinFound",
22
+ noBlockDataFound = "noBlockDataFound",
23
+ multipleCosmosMessages = "multipleCosmosMessages",
24
+ heightOrHashNotProvided = "heightOrHashNotProvided",
25
+ unknownDenom = "unknownDenom",
26
+ invalidBlockHeight = "invalidBlockHeight",
27
+ timestampExtrinsicNoArgumentsForBlock = "timestampExtrinsicNoArgumentsForBlock",
28
+ timestampExtrinsicNoTimestampForBlock = "timestampExtrinsicNoTimestampForBlock",
29
+ noTimestampExtrinsicForHash = "noTimestampExtrinsicForHash",
30
+ timestampExtrinsicNoArgumentsForHash = "timestampExtrinsicNoArgumentsForHash",
31
+ txMemoUndefined = "txMemoUndefined",
32
+ txMemoIncorrect = "txMemoIncorrect",
33
+ txTypeNotFound = "txTypeNotFound",
34
+ txNoMessage = "txNoMessage",
35
+ txNotFound = "txNotFound",
36
+ txReceiptNotFound = "txReceiptNotFound",
37
+ txParsingError = "txParsingError",
38
+ txLogsParsingError = "txLogsParsingError",
39
+ blockNotFound = "blockNotFound",
40
+ balanceNotFound = "balanceNotFound",
41
+ configError = "configError",
42
+ synthSwapDisallowed = "synthSwapDisallowed",
43
+ providerQuoteTimeout = "providerQuoteTimeout",
44
+ noQuoteResponse = "noQuoteResponse",
45
+ noPoolAssetsFound = "noPoolAssetsFound",
46
+ noThorchainPools = "noThorchainPools",
47
+ noMayachainPools = "noMayachainPools",
48
+ noThorchainNetworkInfo = "noThorchainNetworkInfo",
49
+ invalidAffiliateFee = "invalidAffiliateFee",
50
+ invalidBuyAssetAddress = "invalidBuyAssetAddress",
51
+ invalidSellAssetAddress = "invalidSellAssetAddress",
52
+ invalidSourceAddress = "invalidSourceAddress",
53
+ invalidDestinationAddress = "invalidDestinationAddress",
54
+ sourceAddressIsSmartContract = "sourceAddressIsSmartContract",
55
+ destinationAddressIsSmartContract = "destinationAddressIsSmartContract",
56
+ invalidChainId = "invalidChainId",
57
+ unsupportedChainId = "unsupportedChainId",
58
+ unsupportedEVMChainId = "unsupportedEVMChainId",
59
+ unsupportedMethod = "unsupportedMethod",
60
+ unsupportedProvider = "unsupportedProvider",
61
+ invalidParamsForMethod = "invalidParamsForMethod",
62
+ unsupportedAdapter = "unsupportedAdapter",
63
+ noWhitelistTokens = "noWhitelistTokens",
64
+ failedFetchGasPrice = "failedFetchGasPrice",
65
+ failedToCreateDepositChannel = "failedToCreateDepositChannel",
66
+ noProviderDetailsFound = "noProviderDetailsFound",
67
+ noTokenListsFound = "noTokenListsFound",
68
+ tokenNotFound = "tokenNotFound",
69
+ tokenPriceNotFound = "tokenPriceNotFound",
70
+ tokenPriceFailedToUpdate = "tokenPriceFailedToUpdate",
71
+ swapAmountTooSmall = "swapAmountTooSmall",
72
+ legsArrayIsEmpty = "legsArrayIsEmpty",
73
+ failedToFetchQuoteForLeg = "failedToFetchQuoteForLeg",
74
+ noBlockHeaderFound = "noBlockHeaderFound",
75
+ failedToSimulateSwap = "failedToSimulateSwap",
76
+ addressScreeningFailed = "addressScreeningFailed",
77
+ noLiquidtyProvidersFound = "noLiquidtyProvidersFound",
78
+ insufficientLiquidity = "insufficientLiquidity",
79
+ noInboundDataFound = "noInbounDataFound",
80
+ noInboundAddressesFound = "noInboundAddressesFound",
81
+ noInboundAddressFoundForChain = "noInboundAddressFoundForChain",
82
+ noLastBlocksFound = "noLastBlocksFound",
83
+ noVersionFound = "noVersionFound",
84
+ noConstantsFound = "noConstantsFound",
85
+ noMimirsFound = "noMimirsFound",
86
+ noRoutesFound = "noRoutesFound",
87
+ quoteNotFound = "quoteNotFound",
88
+ ledgerSignFailed = "ledgerSignFailed",
89
+ ledgerWrongPayload = "ledgerWrongPayload",
90
+ ledgerFetchSwapFailed = "ledgerFetchSwapFailed",
91
+ failedToFetchTx = "failedToFetchTx",
92
+ failedBuildTransactionDetails = "failedBuildTransactionDetails",
93
+ noLegsForRoute = "noLegsForRoute",
94
+ noRouterAddressFound = "noRouterAddressFound",
95
+ noAggregatorAddressFound = "noAggregatorAddressFound",
96
+ noContractInstanceFound = "noContractInstanceFound",
97
+ noContractAddressFound = "noContractAddressFound",
98
+ invalidAffiliate = "invalidAffiliate",
99
+ thornameAffiliate = "thornameAffiliate",
100
+ providerNotfound = "No provider found",
101
+ noRecordFound = "No Record found",
102
+ slippageTooLow = "Slippage too low",
103
+ tradingHalted = "tradingHalted",
104
+ noWrappedGasAsset = "noWrappedGasAsset",
105
+ aggregatorAddressNotFound = "aggregatorAddressNotFound",
106
+ routerAddressNotFound = "routerAddressNotFound",
107
+ dummyAddressNotFound = "dummyAddressNotFound",
108
+ trackerError = "trackerError",
109
+ thorchainPoolUnavailable = "thorchainPoolUnavailable",
110
+ noOhlcvDataFound = "noOhlcvDataFound",
111
+ noTradingPairs = "noTradingPairs",
112
+ noLendingAvailability = "noLendingAvailability",
113
+ lendingRepayTooSmall = "lendingRepayTooSmall",
114
+ missingState = "missingState",
115
+ ledgerSwapNotFound = "ledgerSwapNotFound",
116
+ ledgerSwapNotReadyForTracking = "ledgerSwapNotReadyForTracking",
117
+ errorEstimatingGas = "errorEstimatingGas",
118
+ apiKeyInvalid = "apiKeyInvalid",
119
+ apiKeyFailedToUpdate = "apiKeyFailedToUpdate",
120
+ apiKeyExpired = "apiKeyExpired",
121
+ unauthorized = "unauthorized",
122
+ failedToCreateMemo = "failedToCreateMemo",
123
+ radixIncorrectInstructions = "radixIncorrectInstructions",
124
+ radixTxMissedParam = "radixTxMissedParam",
125
+ radixTxMissedAccount = "radixTxMissedAccount",
126
+ radixManifestParseError = "radixManifestParseError",
127
+ radixManifestBuildError = "radixManifestBuildError",
128
+ invalidAddressForChain = "invalidAddressForChain",
129
+ riskyAddress = "riskyAddress",
130
+ noRoutesToProcess = "noRoutesToProcess",
131
+ sellAssetAmountTooSmall = "sellAssetAmountTooSmall",
132
+ missingPrivateKey = "missingPrivateKey",
133
+ noMemoPriceProtection = "noMemoPriceProtection",
134
+ rateExpired = "rateExpired",
135
+ amountTooSmall = "amountTooSmall",
136
+ amountTooBig = "amountTooBig",
137
+ amountOutOfRange = "amountOutOfRange",
138
+ requestTimeOut = "requestTimeOut",
139
+ networkError = "networkError",
140
+ unknownApiError = "unknownApiError",
141
+ invalidResponseFormat = "invalidResponseFormat",
142
+ }
143
+
144
+ export enum WarningCodeEnum {
145
+ highSlippage = "highSlippage",
146
+ highPriceImpact = "highPriceImpact",
147
+ }
148
+
149
+ export enum ProviderName {
150
+ CAVIAR_V1 = "CAVIAR_V1",
151
+ CAMELOT_V3 = "CAMELOT_V3",
152
+ CHAINFLIP = "CHAINFLIP",
153
+ CHAINFLIP_STREAMING = "CHAINFLIP_STREAMING",
154
+ JUPITER = "JUPITER",
155
+ MAYACHAIN = "MAYACHAIN",
156
+ MAYACHAIN_STREAMING = "MAYACHAIN_STREAMING",
157
+ OCISWAP_V1 = "OCISWAP_V1",
158
+ ONEINCH = "ONEINCH",
159
+ OPENOCEAN_V2 = "OPENOCEAN_V2",
160
+ PANCAKESWAP = "PANCAKESWAP",
161
+ PANGOLIN_V1 = "PANGOLIN_V1",
162
+ SUSHISWAP_V2 = "SUSHISWAP_V2",
163
+ THORCHAIN = "THORCHAIN",
164
+ THORCHAIN_STREAMING = "THORCHAIN_STREAMING",
165
+ TRADERJOE_V2 = "TRADERJOE_V2",
166
+ UNISWAP_V2 = "UNISWAP_V2",
167
+ UNISWAP_V3 = "UNISWAP_V3",
168
+ NEAR = "NEAR",
169
+ GARDEN = "GARDEN",
170
+ OKX = "OKX",
171
+ }
172
+
173
+ export enum FeeTypeEnum {
174
+ LIQUIDITY = "liquidity",
175
+ NETWORK = "network",
176
+ INBOUND = "inbound",
177
+ OUTBOUND = "outbound",
178
+ AFFILIATE = "affiliate",
179
+ TAX = "tax",
180
+ PRIORITY = "priority",
181
+ SERVICE = "service",
182
+ }
@@ -0,0 +1,38 @@
1
+ import type { AssetValue } from "../modules/assetValue";
2
+
3
+ export type GenericSwapParams<T = unknown> = {
4
+ buyAsset?: AssetValue;
5
+ sellAsset?: AssetValue;
6
+ recipient?: string;
7
+ feeOptionKey?: FeeOption;
8
+ route: T;
9
+ };
10
+
11
+ export type SwapParams<PluginNames = string, R = unknown> = GenericSwapParams<R> & { pluginName?: PluginNames };
12
+
13
+ export enum FeeOption {
14
+ Average = "average",
15
+ Fast = "fast",
16
+ Fastest = "fastest",
17
+ }
18
+
19
+ export enum ApproveMode {
20
+ Approve = "approve",
21
+ CheckOnly = "checkOnly",
22
+ }
23
+
24
+ export type ApproveReturnType<T extends ApproveMode> = T extends "checkOnly" ? Promise<boolean> : Promise<string>;
25
+
26
+ export enum MemoType {
27
+ NAME_REGISTER = "~",
28
+ BOND = "BOND",
29
+ DEPOSIT = "+",
30
+ LEAVE = "LEAVE",
31
+ UNBOND = "UNBOND",
32
+ WITHDRAW = "-",
33
+ RUNEPOOL_DEPOSIT = "POOL+",
34
+ RUNEPOOL_WITHDRAW = "POOL-",
35
+ CLAIM_TCY = "tcy",
36
+ STAKE_TCY = "tcy+",
37
+ UNSTAKE_TCY = "tcy-",
38
+ }
@@ -0,0 +1,124 @@
1
+ import type { Chain, getChainConfig } from "@tcswap/types";
2
+ import type { BrowserProvider, Eip1193Provider } from "ethers";
3
+
4
+ import type { AssetValue } from "../modules/assetValue";
5
+ import type { FeeOption } from "./sdk";
6
+
7
+ declare global {
8
+ interface WindowEventMap {
9
+ "eip6963:announceProvider": CustomEvent;
10
+ }
11
+ }
12
+
13
+ export type EthereumWindowProvider = BrowserProvider & {
14
+ __XDEFI?: boolean;
15
+ isBraveWallet?: boolean;
16
+ isCoinbaseWallet?: boolean;
17
+ isMetaMask?: boolean;
18
+ isOkxWallet?: boolean;
19
+ isKeepKeyWallet?: boolean;
20
+ isTrust?: boolean;
21
+ isTalisman?: boolean;
22
+ on: (event: string, callback?: () => void) => void;
23
+ overrideIsMetaMask?: boolean;
24
+ request: <T = unknown>(args: { method: string; params?: unknown[] }) => Promise<T>;
25
+ selectedProvider?: EthereumWindowProvider;
26
+ };
27
+
28
+ export type NetworkParams = {
29
+ chainId: ReturnType<typeof getChainConfig>["chainIdHex"];
30
+ chainName: string;
31
+ nativeCurrency: { name: string; symbol: string; decimals: number };
32
+ rpcUrls: string[];
33
+ blockExplorerUrls: string[];
34
+ };
35
+
36
+ export enum WalletOption {
37
+ BITGET = "BITGET",
38
+ BRAVE = "BRAVE",
39
+ COINBASE_MOBILE = "COINBASE_MOBILE",
40
+ COINBASE_WEB = "COINBASE_WEB",
41
+ COSMOSTATION = "COSMOSTATION",
42
+ CTRL = "CTRL",
43
+ EIP6963 = "EIP6963",
44
+ /**
45
+ * @deprecated Use PASSKEYS instead
46
+ */
47
+ EXODUS = "EXODUS",
48
+ KEEPKEY = "KEEPKEY",
49
+ KEEPKEY_BEX = "KEEPKEY_BEX",
50
+ KEPLR = "KEPLR",
51
+ KEYSTORE = "KEYSTORE",
52
+ LEAP = "LEAP",
53
+ LEDGER = "LEDGER",
54
+ LEDGER_LIVE = "LEDGER_LIVE",
55
+ METAMASK = "METAMASK",
56
+ OKX = "OKX",
57
+ OKX_MOBILE = "OKX_MOBILE",
58
+ ONEKEY = "ONEKEY",
59
+ PASSKEYS = "PASSKEYS",
60
+ PHANTOM = "PHANTOM",
61
+ POLKADOT_JS = "POLKADOT_JS",
62
+ RADIX_WALLET = "RADIX_WALLET",
63
+ TALISMAN = "TALISMAN",
64
+ TREZOR = "TREZOR",
65
+ TRONLINK = "TRONLINK",
66
+ TRUSTWALLET_WEB = "TRUSTWALLET_WEB",
67
+ VULTISIG = "VULTISIG",
68
+ WALLETCONNECT = "WALLETCONNECT",
69
+ WALLET_SELECTOR = "WALLET_SELECTOR",
70
+ XAMAN = "XAMAN",
71
+ }
72
+
73
+ export enum LedgerErrorCode {
74
+ NoError = 0x9000,
75
+ LockedDevice = 0x5515,
76
+ TC_NotFound = 65535,
77
+ }
78
+
79
+ /**
80
+ * @deprecated CryptoChain has been deprecated - use Chain instead
81
+ */
82
+ export type CryptoChain = Chain;
83
+
84
+ export type ChainWallet<T extends Chain> = {
85
+ chain: T;
86
+ address: string;
87
+ balance: AssetValue[];
88
+ walletType: WalletOption | string;
89
+ disconnect?: () => void;
90
+ signMessage?: (message: string) => Promise<string>;
91
+ };
92
+
93
+ export type EmptyWallet = { [key in Chain]?: unknown };
94
+ export type BaseWallet<T extends EmptyWallet | Record<string, unknown>> = {
95
+ [key in Chain]: ChainWallet<key> & (T extends EmptyWallet ? T[key] : never);
96
+ };
97
+
98
+ export type EIP6963ProviderInfo = { walletId: string; uuid: string; name: string; icon: string };
99
+
100
+ export type EIP6963ProviderDetail = { info: EIP6963ProviderInfo; provider: Eip1193Provider };
101
+
102
+ export type EIP6963Provider = { info: EIP6963ProviderInfo; provider: Eip1193Provider };
103
+
104
+ // This type represents the structure of an event dispatched by a wallet to announce its presence based on EIP-6963.
105
+ export type EIP6963AnnounceProviderEvent = Event & { detail: EIP6963Provider };
106
+
107
+ export type ChainSigner<T, S> = {
108
+ signTransaction: (params: T) => Promise<S> | S;
109
+ getAddress: () => Promise<string> | string;
110
+ sign?: (message: string) => Promise<string> | string;
111
+ };
112
+
113
+ export type GenericTransferParams = {
114
+ recipient: string;
115
+ assetValue: AssetValue;
116
+ memo?: string;
117
+ feeRate?: number;
118
+ feeOptionKey?: FeeOption;
119
+ };
120
+
121
+ export type GenericCreateTransactionParams = Omit<GenericTransferParams, "feeOptionKey" & "feeRate"> & {
122
+ sender: string;
123
+ feeRate: number;
124
+ };
@@ -0,0 +1,186 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { AllChains, Chain, getChainConfig } from "@tcswap/types";
3
+
4
+ import { assetFromString, fetchTokenInfo, getAssetType } from "../asset";
5
+
6
+ // TODO: this should be handled via AssetValue
7
+ const tickerMap: Record<string, string> = {
8
+ [Chain.Arbitrum]: "ETH",
9
+ [Chain.Aurora]: "ETH",
10
+ [Chain.Base]: "ETH",
11
+ [Chain.BinanceSmartChain]: "BNB",
12
+ [Chain.Monad]: "MON",
13
+ [Chain.Cosmos]: "ATOM",
14
+ [Chain.Maya]: "CACAO",
15
+ [Chain.Optimism]: "ETH",
16
+ [Chain.THORChain]: "RUNE",
17
+ [Chain.Tron]: "TRX",
18
+ [Chain.XLayer]: "OKB",
19
+ };
20
+
21
+ describe("getAssetType", () => {
22
+ describe("when isSynth is true", () => {
23
+ test('should return "Synth"', () => {
24
+ const result = getAssetType({ chain: Chain.Bitcoin, symbol: "BTC/BTC" });
25
+ expect(result).toBe("Synth");
26
+ });
27
+ });
28
+
29
+ describe("when isSynth is false", () => {
30
+ describe("for gas assets on given chain", () => {
31
+ for (const chain of AllChains) {
32
+ test(`should return "Native" for chain ${chain} asset`, () => {
33
+ const ticker = tickerMap[chain] || chain;
34
+ const result = getAssetType({ chain: chain as Chain, symbol: ticker });
35
+
36
+ expect(result).toBe("Native");
37
+ });
38
+ }
39
+ });
40
+
41
+ describe("for non-gas assets on given chain", () => {
42
+ for (const chain of AllChains) {
43
+ test(`should return ${chain} for chain ${chain} asset`, () => {
44
+ const result = getAssetType({ chain: chain as Chain, symbol: "USDT" });
45
+
46
+ expect(result).toBe(chain);
47
+ });
48
+ }
49
+ });
50
+ });
51
+ });
52
+
53
+ describe("fetchTokenInfo", () => {
54
+ /**
55
+ * Test out native
56
+ */
57
+ const filteredChains = AllChains.filter((c) => ![Chain.Ethereum, Chain.Avalanche].includes(c));
58
+
59
+ for (const chain of filteredChains) {
60
+ describe(chain, () => {
61
+ test(`returns proper decimal for native ${chain} asset`, async () => {
62
+ const { decimals } = await fetchTokenInfo({ address: "", chain });
63
+ const { baseDecimal } = getChainConfig(chain);
64
+ expect(decimals).toBe(baseDecimal);
65
+ });
66
+ });
67
+ }
68
+
69
+ describe("ETH", () => {
70
+ // TODO: if too many requests, this will fail due to timeout
71
+ test.todo("returns proper decimal for eth and it's assets", async () => {
72
+ const { decimals: ethDecimal } = await fetchTokenInfo({ address: "", chain: Chain.Ethereum });
73
+ expect(ethDecimal).toBe(getChainConfig(Chain.Ethereum).baseDecimal);
74
+ await Bun.sleep(500);
75
+
76
+ const { decimals: usdcDecimal } = await fetchTokenInfo({
77
+ address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
78
+ chain: Chain.Ethereum,
79
+ });
80
+ expect(usdcDecimal).toBe(6);
81
+ await Bun.sleep(500);
82
+
83
+ const { decimals: wbtcDecimal } = await fetchTokenInfo({
84
+ address: "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
85
+ chain: Chain.Ethereum,
86
+ });
87
+ expect(wbtcDecimal).toBe(8);
88
+ await Bun.sleep(500);
89
+
90
+ const { decimals: kindDecimal } = await fetchTokenInfo({
91
+ address: "0x4618519de4c304f3444ffa7f812dddc2971cc688",
92
+ chain: Chain.Ethereum,
93
+ });
94
+ expect(kindDecimal).toBe(8);
95
+ await Bun.sleep(500);
96
+
97
+ const { decimals: shitcoinDecimal } = await fetchTokenInfo({
98
+ address: "0xCa208BfD69ae6D2667f1FCbE681BAe12767c0078",
99
+ chain: Chain.Ethereum,
100
+ });
101
+ expect(shitcoinDecimal).toBe(0);
102
+ await Bun.sleep(500);
103
+ });
104
+ });
105
+
106
+ describe("AVAX", () => {
107
+ // TODO: if too many requests, this will fail due to timeout
108
+ test.todo("returns proper decimal for avax and it's assets", async () => {
109
+ const { decimals: avaxDecimal } = await fetchTokenInfo({ address: "", chain: Chain.Avalanche });
110
+ expect(avaxDecimal).toBe(getChainConfig(Chain.Avalanche).baseDecimal);
111
+
112
+ const { decimals: wbtceDecimal } = await fetchTokenInfo({
113
+ address: "0x50b7545627a5162f82a992c33b87adc75187b218",
114
+ chain: Chain.Avalanche,
115
+ });
116
+ expect(wbtceDecimal).toBe(8);
117
+
118
+ const { decimals: btcbDecimal } = await fetchTokenInfo({
119
+ address: "0x152b9d0FdC40C096757F570A51E494bd4b943E50",
120
+ chain: Chain.Avalanche,
121
+ });
122
+ expect(btcbDecimal).toBe(8);
123
+
124
+ const { decimals: timeDecimal } = await fetchTokenInfo({
125
+ address: "0xb54f16fB19478766A268F172C9480f8da1a7c9C3",
126
+ chain: Chain.Avalanche,
127
+ });
128
+ expect(timeDecimal).toBe(9);
129
+
130
+ const { decimals: usdtDecimal } = await fetchTokenInfo({
131
+ address: "0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7",
132
+ chain: Chain.Avalanche,
133
+ });
134
+ expect(usdtDecimal).toBe(6);
135
+
136
+ const { decimals: usdcDecimal } = await fetchTokenInfo({
137
+ address: "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
138
+ chain: Chain.Avalanche,
139
+ });
140
+ expect(usdcDecimal).toBe(6);
141
+ });
142
+ });
143
+
144
+ describe("Radix", () => {
145
+ test.todo("returns proper decimal for radix and it's assets", async () => {
146
+ const { decimals: radixDecimal } = await fetchTokenInfo({ address: "", chain: Chain.Radix });
147
+ expect(radixDecimal).toBe(getChainConfig(Chain.Radix).baseDecimal);
148
+ await Bun.sleep(500);
149
+
150
+ const { decimals: xwBTCDecimal } = await fetchTokenInfo({
151
+ address: "xwBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
152
+ chain: Chain.Radix,
153
+ });
154
+ expect(xwBTCDecimal).toBe(8);
155
+ await Bun.sleep(500);
156
+ });
157
+ });
158
+ });
159
+
160
+ describe("assetFromString", () => {
161
+ test("should return the correct object", () => {
162
+ const assetString = "THOR.RUNE";
163
+ const result = assetFromString(assetString);
164
+
165
+ expect(result).toEqual({ chain: Chain.THORChain, symbol: "RUNE", synth: false, ticker: "RUNE" });
166
+ });
167
+
168
+ test("should return the correct object for multiple dashes", () => {
169
+ const assetString = "ETH.PENDLE-LPT-0x1234";
170
+ const result = assetFromString(assetString);
171
+
172
+ expect(result).toEqual({ chain: Chain.Ethereum, symbol: "PENDLE-LPT-0x1234", synth: false, ticker: "PENDLE-LPT" });
173
+ });
174
+
175
+ test.todo("should return the correct object for Radix resource", () => {
176
+ const assetString = "XRD.xwBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75";
177
+ const result = assetFromString(assetString);
178
+
179
+ expect(result).toEqual({
180
+ chain: Chain.Radix,
181
+ symbol: "xwBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75",
182
+ synth: false,
183
+ ticker: "xwBTC",
184
+ });
185
+ });
186
+ });
@@ -0,0 +1,142 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { Chain } from "@tcswap/types";
3
+
4
+ import type { DerivationPathArray } from "../../types";
5
+ import {
6
+ DerivationPath,
7
+ derivationPathToString,
8
+ getDerivationPathFor,
9
+ getWalletFormatFor,
10
+ updateDerivationPath,
11
+ } from "../derivationPath";
12
+
13
+ describe("derivationPathToString", () => {
14
+ test("full 5-element path", () => {
15
+ expect(derivationPathToString([44, 60, 0, 0, 0] as DerivationPathArray)).toBe("m/44'/60'/0'/0/0");
16
+ expect(derivationPathToString([84, 0, 0, 0, 5] as DerivationPathArray)).toBe("m/84'/0'/0'/0/5");
17
+ });
18
+
19
+ test("4-element path without index", () => {
20
+ expect(derivationPathToString([44, 60, 0, 0] as DerivationPathArray)).toBe("m/44'/60'/0'/0");
21
+ expect(derivationPathToString([49, 2, 0, 1] as DerivationPathArray)).toBe("m/49'/2'/0'/1");
22
+ });
23
+
24
+ test("3-element account path", () => {
25
+ expect(derivationPathToString([44, 60, 0] as unknown as DerivationPathArray)).toBe("m/44'/60'/0'");
26
+ expect(derivationPathToString([44, 501, 5] as unknown as DerivationPathArray)).toBe("m/44'/501'/5'");
27
+ });
28
+ });
29
+
30
+ describe("updateDerivationPath", () => {
31
+ const basePath: DerivationPathArray = [44, 60, 0, 0, 0];
32
+
33
+ test("updates index (last element)", () => {
34
+ expect(updateDerivationPath(basePath, { index: 5 })).toEqual([44, 60, 0, 0, 5]);
35
+ expect(updateDerivationPath(basePath, { index: 100 })).toEqual([44, 60, 0, 0, 100]);
36
+ });
37
+
38
+ test("updates change (4th element)", () => {
39
+ expect(updateDerivationPath(basePath, { change: 1 })).toEqual([44, 60, 0, 1, 0]);
40
+ expect(updateDerivationPath([84, 0, 2, 0, 3], { change: 1 })).toEqual([84, 0, 2, 1, 3]);
41
+ });
42
+
43
+ test("updates account (3rd element)", () => {
44
+ expect(updateDerivationPath(basePath, { account: 5 })).toEqual([44, 60, 5, 0, 0]);
45
+ expect(updateDerivationPath([84, 0, 0, 1, 10], { account: 3 })).toEqual([84, 0, 3, 1, 10]);
46
+ });
47
+
48
+ test("returns unchanged path for empty params", () => {
49
+ expect(updateDerivationPath(basePath, {} as any)).toEqual(basePath);
50
+ });
51
+ });
52
+
53
+ describe("getDerivationPathFor", () => {
54
+ describe("EVM chains", () => {
55
+ test("Ethereum default path", () => {
56
+ expect(getDerivationPathFor({ chain: Chain.Ethereum, index: 0 })).toEqual([44, 60, 0, 0, 0]);
57
+ expect(getDerivationPathFor({ chain: Chain.Ethereum, index: 5 })).toEqual([44, 60, 0, 0, 5]);
58
+ });
59
+
60
+ test("Ethereum legacy type", () => {
61
+ expect(getDerivationPathFor({ chain: Chain.Ethereum, index: 3, type: "legacy" })).toEqual([44, 60, 0, 3]);
62
+ });
63
+
64
+ test("Ethereum account type", () => {
65
+ expect(getDerivationPathFor({ chain: Chain.Ethereum, index: 2, type: "account" })).toEqual([44, 60, 0, 2]);
66
+ });
67
+
68
+ test("Ethereum ledgerLive type", () => {
69
+ expect(getDerivationPathFor({ chain: Chain.Ethereum, index: 0, type: "ledgerLive" })).toEqual([44, 60, 0, 0, 0]);
70
+ expect(getDerivationPathFor({ addressIndex: 1, chain: Chain.Ethereum, index: 2, type: "ledgerLive" })).toEqual([
71
+ 44, 60, 2, 0, 1,
72
+ ]);
73
+ });
74
+
75
+ test("other EVM chains use same derivation", () => {
76
+ expect(getDerivationPathFor({ chain: Chain.BinanceSmartChain, index: 0 })).toEqual([44, 60, 0, 0, 0]);
77
+ expect(getDerivationPathFor({ chain: Chain.Avalanche, index: 0 })).toEqual([44, 60, 0, 0, 0]);
78
+ expect(getDerivationPathFor({ chain: Chain.Arbitrum, index: 0 })).toEqual([44, 60, 0, 0, 0]);
79
+ });
80
+ });
81
+
82
+ describe("Solana", () => {
83
+ test("default path", () => {
84
+ const result = getDerivationPathFor({ chain: Chain.Solana, index: 0 });
85
+ expect(result[1]).toBe(501);
86
+ });
87
+
88
+ test("account type", () => {
89
+ expect(getDerivationPathFor({ chain: Chain.Solana, index: 3, type: "account" })).toEqual([44, 501, 0, 3]);
90
+ });
91
+ });
92
+
93
+ describe("UTXO chains", () => {
94
+ test("Bitcoin nativeSegwitMiddleAccount", () => {
95
+ expect(getDerivationPathFor({ chain: Chain.Bitcoin, index: 0, type: "nativeSegwitMiddleAccount" })).toEqual([
96
+ 84, 0, 0, 0, 0,
97
+ ]);
98
+ expect(
99
+ getDerivationPathFor({ addressIndex: 5, chain: Chain.Bitcoin, index: 2, type: "nativeSegwitMiddleAccount" }),
100
+ ).toEqual([84, 0, 2, 0, 5]);
101
+ });
102
+
103
+ test("Bitcoin segwit", () => {
104
+ expect(getDerivationPathFor({ chain: Chain.Bitcoin, index: 3, type: "segwit" })).toEqual([49, 0, 0, 0, 3]);
105
+ });
106
+
107
+ test("Bitcoin legacy", () => {
108
+ expect(getDerivationPathFor({ chain: Chain.Bitcoin, index: 0, type: "legacy" })).toEqual([44, 0, 0, 0, 0]);
109
+ expect(getDerivationPathFor({ chain: Chain.Bitcoin, index: 5, type: "legacy" })).toEqual([44, 0, 0, 0, 5]);
110
+ });
111
+
112
+ test("Litecoin uses chainId 2", () => {
113
+ expect(getDerivationPathFor({ chain: Chain.Litecoin, index: 0, type: "legacy" })).toEqual([44, 2, 0, 0, 0]);
114
+ expect(getDerivationPathFor({ chain: Chain.Litecoin, index: 0, type: "segwit" })).toEqual([49, 2, 0, 0, 0]);
115
+ });
116
+ });
117
+ });
118
+
119
+ describe("getWalletFormatFor", () => {
120
+ test("returns legacy for purpose 44", () => {
121
+ expect(getWalletFormatFor("m/44'/0'/0'/0/0")).toBe("legacy");
122
+ expect(getWalletFormatFor("m/44'/60'/0'/0/0")).toBe("legacy");
123
+ });
124
+
125
+ test("returns p2sh for purpose 49", () => {
126
+ expect(getWalletFormatFor("m/49'/0'/0'/0/0")).toBe("p2sh");
127
+ expect(getWalletFormatFor("m/49'/2'/0'/0/0")).toBe("p2sh");
128
+ });
129
+
130
+ test("returns bech32 for purpose 84 and others", () => {
131
+ expect(getWalletFormatFor("m/84'/0'/0'/0/0")).toBe("bech32");
132
+ expect(getWalletFormatFor("m/86'/0'/0'/0/0")).toBe("bech32");
133
+ });
134
+ });
135
+
136
+ describe("DerivationPath export", () => {
137
+ test("exports string paths for common chains", () => {
138
+ expect(typeof DerivationPath[Chain.Ethereum]).toBe("string");
139
+ expect(typeof DerivationPath[Chain.Bitcoin]).toBe("string");
140
+ expect(DerivationPath[Chain.Ethereum]).toMatch(/^m\/\d+'\/\d+'\/\d+'/);
141
+ });
142
+ });