@swapkit/toolboxes 1.0.0-beta.0 → 1.0.0-beta.1

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 (103) hide show
  1. package/dist/chunk-0f0249b1.js +3 -0
  2. package/dist/chunk-0f0249b1.js.map +10 -0
  3. package/dist/chunk-0h4xdrwz.js +4 -0
  4. package/dist/chunk-0h4xdrwz.js.map +10 -0
  5. package/dist/chunk-4yap1fvd.js +3 -0
  6. package/dist/chunk-4yap1fvd.js.map +10 -0
  7. package/dist/chunk-fjfxga2v.js +3 -0
  8. package/dist/chunk-fjfxga2v.js.map +10 -0
  9. package/dist/{chunk-tvrdndbw.js → chunk-p1kdg37m.js} +2 -2
  10. package/dist/{chunk-tvrdndbw.js.map → chunk-p1kdg37m.js.map} +1 -1
  11. package/dist/cosmos/index.cjs +2 -2
  12. package/dist/cosmos/index.cjs.map +10 -13
  13. package/dist/cosmos/index.js +2 -2
  14. package/dist/cosmos/index.js.map +10 -13
  15. package/dist/evm/index.cjs +2 -2
  16. package/dist/evm/index.cjs.map +10 -16
  17. package/dist/evm/index.js +2 -2
  18. package/dist/evm/index.js.map +10 -16
  19. package/dist/index.cjs +2 -2
  20. package/dist/index.cjs.map +4 -3
  21. package/dist/index.js +2 -2
  22. package/dist/index.js.map +4 -3
  23. package/dist/radix/index.cjs +2 -2
  24. package/dist/radix/index.cjs.map +3 -3
  25. package/dist/radix/index.js +2 -2
  26. package/dist/radix/index.js.map +3 -3
  27. package/dist/ripple/index.cjs +3 -0
  28. package/dist/ripple/index.cjs.map +10 -0
  29. package/dist/ripple/index.js +3 -0
  30. package/dist/ripple/index.js.map +10 -0
  31. package/dist/solana/index.cjs +2 -2
  32. package/dist/solana/index.cjs.map +3 -3
  33. package/dist/solana/index.js +2 -2
  34. package/dist/solana/index.js.map +3 -3
  35. package/dist/substrate/index.cjs +2 -2
  36. package/dist/substrate/index.cjs.map +5 -6
  37. package/dist/substrate/index.js +2 -2
  38. package/dist/substrate/index.js.map +5 -6
  39. package/dist/utxo/index.cjs +2 -2
  40. package/dist/utxo/index.cjs.map +9 -11
  41. package/dist/utxo/index.js +2 -2
  42. package/dist/utxo/index.js.map +9 -11
  43. package/package.json +30 -24
  44. package/src/cosmos/index.ts +2 -9
  45. package/src/cosmos/thorchainUtils/addressFormat.ts +1 -2
  46. package/src/cosmos/thorchainUtils/index.ts +1 -1
  47. package/src/cosmos/thorchainUtils/messages.ts +74 -56
  48. package/src/cosmos/thorchainUtils/registry.ts +16 -23
  49. package/src/cosmos/thorchainUtils/types/{proto/MsgCompiled.ts → MsgCompiled.ts} +1 -3
  50. package/src/cosmos/thorchainUtils/types/client-types.ts +16 -23
  51. package/src/cosmos/toolbox/cosmos.ts +334 -0
  52. package/src/cosmos/toolbox/index.ts +33 -0
  53. package/src/cosmos/toolbox/thorchain.ts +118 -131
  54. package/src/cosmos/types.ts +37 -18
  55. package/src/cosmos/util.ts +21 -71
  56. package/src/evm/__tests__/ethereum.test.ts +110 -116
  57. package/src/evm/api.ts +11 -147
  58. package/src/evm/helpers.ts +111 -83
  59. package/src/evm/index.ts +1 -17
  60. package/src/evm/toolbox/baseEVMToolbox.ts +742 -0
  61. package/src/evm/toolbox/evm.ts +69 -0
  62. package/src/evm/toolbox/index.ts +36 -0
  63. package/src/evm/toolbox/op.ts +97 -143
  64. package/src/evm/types.ts +50 -28
  65. package/src/index.ts +235 -0
  66. package/src/radix/index.ts +18 -19
  67. package/src/ripple/index.ts +203 -0
  68. package/src/solana/index.ts +11 -5
  69. package/src/solana/toolbox.ts +223 -133
  70. package/src/substrate/index.ts +2 -3
  71. package/src/substrate/{toolbox/baseSubstrateToolbox.ts → substrate.ts} +104 -72
  72. package/src/substrate/types.ts +120 -0
  73. package/src/utils.ts +27 -0
  74. package/src/utxo/helpers/api.ts +27 -23
  75. package/src/utxo/helpers/bchaddrjs.ts +21 -21
  76. package/src/utxo/helpers/index.ts +0 -1
  77. package/src/utxo/helpers/txSize.ts +3 -4
  78. package/src/utxo/index.ts +3 -7
  79. package/src/utxo/toolbox/bitcoinCash.ts +164 -154
  80. package/src/utxo/toolbox/index.ts +63 -24
  81. package/src/utxo/toolbox/utxo.ts +376 -229
  82. package/src/utxo/types.ts +24 -39
  83. package/src/cosmos/thorchainUtils/types/proto/MsgCompiled.js +0 -2806
  84. package/src/cosmos/thorchainUtils/util.ts +0 -46
  85. package/src/cosmos/toolbox/BaseCosmosToolbox.ts +0 -254
  86. package/src/cosmos/toolbox/gaia.ts +0 -39
  87. package/src/cosmos/toolbox/getToolboxByChain.ts +0 -29
  88. package/src/cosmos/toolbox/kujira.ts +0 -61
  89. package/src/evm/provider.ts +0 -6
  90. package/src/evm/toolbox/EVMToolbox.ts +0 -662
  91. package/src/evm/toolbox/arb.ts +0 -61
  92. package/src/evm/toolbox/avax.ts +0 -36
  93. package/src/evm/toolbox/base.ts +0 -42
  94. package/src/evm/toolbox/bsc.ts +0 -34
  95. package/src/evm/toolbox/eth.ts +0 -44
  96. package/src/evm/toolbox/getToolboxByChain.ts +0 -42
  97. package/src/evm/toolbox/matic.ts +0 -42
  98. package/src/radix/toolbox.ts +0 -693
  99. package/src/substrate/toolbox/index.ts +0 -40
  100. package/src/substrate/types/index.ts +0 -2
  101. package/src/substrate/types/network.ts +0 -42
  102. package/src/substrate/types/wallet.ts +0 -78
  103. package/src/utxo/helpers/utils.ts +0 -45
@@ -1,46 +0,0 @@
1
- import { type AssetValue, Chain, ChainId, SKConfig, StagenetChain } from "@swapkit/helpers";
2
-
3
- import { createStargateClient, getDenomWithChain } from "../util";
4
- import { bech32ToBase64 } from "./addressFormat";
5
-
6
- export const DEFAULT_GAS_VALUE = "5000000000";
7
-
8
- export const buildDepositTx = async ({
9
- signer,
10
- memo = "",
11
- assetValue,
12
- }: {
13
- signer: string;
14
- memo?: string;
15
- assetValue: AssetValue;
16
- }) => {
17
- const { isStagenet } = SKConfig.get("envs");
18
- const client = await createStargateClient(
19
- SKConfig.get("rpcUrls")[isStagenet ? StagenetChain.THORChain : Chain.THORChain],
20
- );
21
- const accountOnChain = await client.getAccount(signer);
22
-
23
- if (!accountOnChain) {
24
- throw new Error("Account does not exist");
25
- }
26
-
27
- return {
28
- memo,
29
- accountNumber: accountOnChain.accountNumber,
30
- chainId: ChainId.THORChain,
31
- fee: { amount: [], gas: DEFAULT_GAS_VALUE },
32
- sequence: accountOnChain.sequence,
33
- msgs: [
34
- {
35
- typeUrl: "/types.MsgDeposit",
36
- value: {
37
- coins: [
38
- { amount: assetValue.getBaseValue("string"), asset: getDenomWithChain(assetValue) },
39
- ],
40
- signer: bech32ToBase64(signer),
41
- memo,
42
- },
43
- },
44
- ],
45
- };
46
- };
@@ -1,254 +0,0 @@
1
- import { base64, bech32 } from "@scure/base";
2
- import {
3
- AssetValue,
4
- Chain,
5
- type ChainId,
6
- type CosmosChain,
7
- CosmosChainPrefixes,
8
- DerivationPath,
9
- SKConfig,
10
- SwapKitError,
11
- } from "@swapkit/helpers";
12
- import { SwapKitApi } from "@swapkit/helpers/api";
13
-
14
- import type { TransferParams } from "../types";
15
- import {
16
- DEFAULT_COSMOS_FEE_MAINNET,
17
- createSigningStargateClient,
18
- createStargateClient,
19
- getAssetFromDenom,
20
- getMsgSendDenom,
21
- } from "../util";
22
-
23
- type Params = {
24
- chain: CosmosChain;
25
- derivationPath?: DerivationPath;
26
- prefix?: string;
27
- index?: number;
28
- };
29
-
30
- export async function getFeeRateFromThorswap(chainId: ChainId, safeDefault: number) {
31
- try {
32
- const response = await SwapKitApi.getGasRate();
33
- const responseGasRate = response.find((gas) => gas.chainId === chainId)?.value;
34
-
35
- return responseGasRate ? Number.parseFloat(responseGasRate) : safeDefault;
36
- } catch (e) {
37
- console.error(e);
38
- return safeDefault;
39
- }
40
- }
41
-
42
- export function BaseCosmosToolbox({
43
- chain,
44
- derivationPath: paramsDerivationPath,
45
- index = 0,
46
- prefix,
47
- }: Params) {
48
- const rpcUrl = SKConfig.get("rpcUrls")[chain];
49
- const chainPrefix = prefix || CosmosChainPrefixes[chain];
50
- const derivationPath = paramsDerivationPath
51
- ? `${paramsDerivationPath}/${index}`
52
- : `${DerivationPath[chain]}/${index}`;
53
-
54
- const getCosmosAccount = cosmosAccountGetter(chainPrefix);
55
- const getCosmosBalance = cosmosBalanceGetter({ chain, rpcUrl });
56
- const getCosmosBalanceAsDenoms = cosmosBalanceDenomsGetter(rpcUrl);
57
-
58
- return {
59
- transfer: cosmosTransfer(rpcUrl),
60
- getSigner: async (phrase: string) => {
61
- const { DirectSecp256k1HdWallet } = await import("@cosmjs/proto-signing");
62
- const { stringToPath } = await import("@cosmjs/crypto");
63
-
64
- return DirectSecp256k1HdWallet.fromMnemonic(phrase, {
65
- prefix,
66
- hdPaths: [stringToPath(derivationPath)],
67
- });
68
- },
69
- getSignerFromPrivateKey: async (privateKey: Uint8Array) => {
70
- const { DirectSecp256k1Wallet } = await import("@cosmjs/proto-signing");
71
-
72
- return DirectSecp256k1Wallet.fromKey(privateKey, prefix);
73
- },
74
- createPrivateKeyFromPhrase: async (phrase: string) => {
75
- const { Bip39, EnglishMnemonic, Slip10, Slip10Curve, stringToPath } = await import(
76
- "@cosmjs/crypto"
77
- );
78
-
79
- const mnemonicChecked = new EnglishMnemonic(phrase);
80
- const seed = await Bip39.mnemonicToSeed(mnemonicChecked);
81
-
82
- const { privkey } = Slip10.derivePath(
83
- Slip10Curve.Secp256k1,
84
- seed,
85
- stringToPath(derivationPath),
86
- );
87
-
88
- return privkey;
89
- },
90
- getAccount: async (address: string) => {
91
- const client = await createStargateClient(rpcUrl);
92
- return client.getAccount(address);
93
- },
94
- validateAddress: (address: string) => validateCosmosAddress({ prefix: chainPrefix, address }),
95
- getAddressFromMnemonic: async (phrase: string) => {
96
- const walletAccount = await getCosmosAccount({ phrase, derivationPath });
97
-
98
- return walletAccount.address;
99
- },
100
- getPubKeyFromMnemonic: async (phrase: string) => {
101
- const account = await getCosmosAccount({ phrase, derivationPath });
102
- return base64.encode(account.pubkey);
103
- },
104
- getFeeRateFromThorswap,
105
- getBalanceAsDenoms: getCosmosBalanceAsDenoms,
106
- getBalance: getCosmosBalance,
107
- };
108
- }
109
-
110
- export type BaseCosmosToolboxType = ReturnType<typeof BaseCosmosToolbox>;
111
-
112
- export function cosmosValidateAddress({
113
- address,
114
- chain,
115
- prefix: chainPrefix,
116
- }: { address: string } & (
117
- | { prefix: string; chain?: undefined }
118
- | { chain: CosmosChain; prefix?: undefined }
119
- )) {
120
- const prefix = chainPrefix || getPrefix(chain);
121
-
122
- if (!(prefix && address)) {
123
- throw new SwapKitError("toolbox_cosmos_validate_address_prefix_not_found");
124
- }
125
-
126
- return validateCosmosAddress({ prefix, address });
127
- }
128
-
129
- export function estimateTransactionFee({ assetValue: { chain } }: { assetValue: AssetValue }) {
130
- return AssetValue.from({ chain, value: getMinTransactionFee(chain) });
131
- }
132
-
133
- export type BaseCosmosWallet = ReturnType<typeof BaseCosmosToolbox>;
134
- export type CosmosWallets = {
135
- [chain in Chain.Cosmos | Chain.Kujira]: BaseCosmosWallet;
136
- };
137
-
138
- function getPrefix(chain?: CosmosChain) {
139
- const { isStagenet } = SKConfig.get("envs");
140
- const useStagenetPrefix = chain
141
- ? [Chain.THORChain, Chain.Maya].includes(chain) && isStagenet
142
- : false;
143
- const basePrefix = chain ? CosmosChainPrefixes[chain] : undefined;
144
-
145
- return useStagenetPrefix ? `s${basePrefix}` : basePrefix;
146
- }
147
-
148
- function getMinTransactionFee(chain: Chain) {
149
- return (
150
- {
151
- [Chain.Cosmos]: 0.007,
152
- [Chain.Kujira]: 0.02,
153
- [Chain.THORChain]: 0.02,
154
- [Chain.Maya]: 0.02,
155
- }[chain as CosmosChain] || 0
156
- );
157
- }
158
-
159
- function validateCosmosAddress({ prefix, address }: { prefix: string; address: string }) {
160
- if (!address.startsWith(prefix)) return false;
161
-
162
- try {
163
- const { prefix, words } = bech32.decode(address as `${string}1${string}`);
164
- const normalized = bech32.encode(prefix, words);
165
-
166
- return normalized === address.toLocaleLowerCase();
167
- } catch (_error) {
168
- return false;
169
- }
170
- }
171
-
172
- function cosmosTransfer(rpcUrl: string) {
173
- return async function transfer({
174
- from,
175
- recipient,
176
- assetValue,
177
- memo = "",
178
- fee = DEFAULT_COSMOS_FEE_MAINNET,
179
- signer,
180
- }: TransferParams) {
181
- if (!signer) {
182
- throw new SwapKitError("toolbox_cosmos_signer_not_defined");
183
- }
184
-
185
- const signingClient = await createSigningStargateClient(rpcUrl, signer);
186
- const message = [
187
- {
188
- denom: getMsgSendDenom(`u${assetValue.symbol}`).toLowerCase(),
189
- amount: assetValue.getBaseValue("string"),
190
- },
191
- ];
192
-
193
- const { transactionHash } = await signingClient.sendTokens(from, recipient, message, fee, memo);
194
-
195
- return transactionHash;
196
- };
197
- }
198
-
199
- function cosmosBalanceDenomsGetter(rpcUrl: string) {
200
- return async function getCosmosBalanceDenoms(address: string) {
201
- const client = await createStargateClient(rpcUrl);
202
- const allBalances = await client.getAllBalances(address);
203
-
204
- const balances = allBalances.map((balance) => ({
205
- ...balance,
206
- denom: balance.denom.includes("/") ? balance.denom.toUpperCase() : balance.denom,
207
- }));
208
-
209
- return balances;
210
- };
211
- }
212
-
213
- function cosmosBalanceGetter({ chain, rpcUrl }: { chain: Chain; rpcUrl: string }) {
214
- return async function getCosmosBalance(
215
- address: string,
216
- // filterFunc?: (params: { denom: string; amount: string }) => boolean,
217
- ) {
218
- const denomBalances = await cosmosBalanceDenomsGetter(rpcUrl)(address);
219
-
220
- const balances = denomBalances
221
- .filter(({ denom }) => denom && !denom.includes("IBC/"))
222
- .map(({ denom, amount }) => {
223
- const fullDenom =
224
- [Chain.THORChain, Chain.Maya].includes(chain) && denom.includes("/")
225
- ? `${chain}.${denom}`
226
- : denom;
227
- return getAssetFromDenom(fullDenom, amount);
228
- });
229
-
230
- return balances;
231
- };
232
- }
233
-
234
- function cosmosAccountGetter(prefix: string) {
235
- return async function getCosmosAccount({
236
- phrase,
237
- derivationPath,
238
- }: { phrase: string; derivationPath: string }) {
239
- const { Secp256k1HdWallet } = await import("@cosmjs/amino");
240
- const { stringToPath } = await import("@cosmjs/crypto");
241
- const wallet = await Secp256k1HdWallet.fromMnemonic(phrase, {
242
- prefix,
243
- hdPaths: [stringToPath(derivationPath)],
244
- });
245
-
246
- const [account] = await wallet.getAccounts();
247
-
248
- if (!account) {
249
- throw new SwapKitError("toolbox_cosmos_no_accounts_found");
250
- }
251
-
252
- return account;
253
- };
254
- }
@@ -1,39 +0,0 @@
1
- import { BaseDecimal, Chain, ChainId, SwapKitNumber } from "@swapkit/helpers";
2
-
3
- import type { TransferParams } from "../types";
4
-
5
- import { buildNativeTransferTx } from "../util";
6
- import { BaseCosmosToolbox, getFeeRateFromThorswap } from "./BaseCosmosToolbox";
7
-
8
- export function GaiaToolbox() {
9
- const cosmosToolbox = BaseCosmosToolbox({ chain: Chain.Cosmos });
10
-
11
- async function getFees() {
12
- const baseFee = await getFeeRateFromThorswap(ChainId.Cosmos, 500);
13
- return {
14
- type: "base",
15
- average: SwapKitNumber.fromBigInt(BigInt(baseFee), BaseDecimal.GAIA),
16
- fast: SwapKitNumber.fromBigInt((BigInt(baseFee) * 15n) / 10n, BaseDecimal.GAIA),
17
- fastest: SwapKitNumber.fromBigInt(BigInt(baseFee) * 2n, BaseDecimal.GAIA),
18
- };
19
- }
20
-
21
- async function transfer(params: TransferParams) {
22
- const gasFees = await getFees();
23
-
24
- return cosmosToolbox.transfer({
25
- ...params,
26
- fee: params.fee || {
27
- amount: [
28
- {
29
- denom: "uatom",
30
- amount: gasFees[params.feeOptionKey || "fast"].getBaseValue("string") || "1000",
31
- },
32
- ],
33
- gas: "200000",
34
- },
35
- });
36
- }
37
-
38
- return { ...cosmosToolbox, getFees, transfer, buildTransferTx: buildNativeTransferTx };
39
- }
@@ -1,29 +0,0 @@
1
- import { Chain } from "@swapkit/helpers";
2
-
3
- import { GaiaToolbox } from "./gaia";
4
- import { KujiraToolbox } from "./kujira";
5
- import { MayaToolbox, ThorchainToolbox } from "./thorchain";
6
-
7
- export type CosmosToolboxType = {
8
- THOR: typeof ThorchainToolbox;
9
- GAIA: typeof GaiaToolbox;
10
- KUJI: typeof KujiraToolbox;
11
- MAYA: typeof MayaToolbox;
12
- };
13
-
14
- export const getToolboxByChain = <T extends keyof CosmosToolboxType>(
15
- chain: T,
16
- ): CosmosToolboxType[T] => {
17
- switch (chain) {
18
- case Chain.Kujira:
19
- return KujiraToolbox as CosmosToolboxType[T];
20
- case Chain.Maya:
21
- return MayaToolbox as CosmosToolboxType[T];
22
- case Chain.THORChain:
23
- return ThorchainToolbox as CosmosToolboxType[T];
24
- case Chain.Cosmos:
25
- return GaiaToolbox as CosmosToolboxType[T];
26
- default:
27
- throw new Error(`Chain ${chain} is not supported`);
28
- }
29
- };
@@ -1,61 +0,0 @@
1
- import { BaseDecimal, Chain, ChainId, SwapKitNumber } from "@swapkit/helpers";
2
-
3
- import { USK_KUJIRA_FACTORY_DENOM, YUM_KUJIRA_FACTORY_DENOM } from "../index";
4
- import type { TransferParams } from "../types";
5
- import { buildNativeTransferTx, getAssetFromDenom } from "../util";
6
-
7
- import { BaseCosmosToolbox, getFeeRateFromThorswap } from "./BaseCosmosToolbox";
8
-
9
- async function getFees() {
10
- const baseFee = await getFeeRateFromThorswap(ChainId.Kujira, 1000);
11
- return {
12
- type: "base",
13
- average: SwapKitNumber.fromBigInt(BigInt(baseFee), BaseDecimal.KUJI),
14
- fast: SwapKitNumber.fromBigInt(BigInt(Math.floor(baseFee * 1.5)), BaseDecimal.KUJI),
15
- fastest: SwapKitNumber.fromBigInt(BigInt(Math.floor(baseFee * 2)), BaseDecimal.KUJI),
16
- };
17
- }
18
-
19
- export const KujiraToolbox = () => {
20
- const cosmosToolbox = BaseCosmosToolbox({ chain: Chain.Kujira });
21
-
22
- return {
23
- ...cosmosToolbox,
24
- getFees,
25
- buildTransferTx: buildNativeTransferTx,
26
- getBalance: async (address: string, _potentialScamFilter?: boolean) => {
27
- const denomBalances = await cosmosToolbox.getBalanceAsDenoms(address);
28
-
29
- const balances = await Promise.all(
30
- denomBalances
31
- .filter(({ denom }) => {
32
- if (!denom || denom.includes("IBC/")) return false;
33
-
34
- return (
35
- [USK_KUJIRA_FACTORY_DENOM, YUM_KUJIRA_FACTORY_DENOM].includes(denom) ||
36
- !denom.startsWith("FACTORY")
37
- );
38
- })
39
- .map(({ denom, amount }) => getAssetFromDenom(denom, amount)),
40
- );
41
-
42
- return balances;
43
- },
44
- transfer: async (params: TransferParams) => {
45
- const gasFees = await getFees();
46
-
47
- return cosmosToolbox.transfer({
48
- ...params,
49
- fee: params.fee || {
50
- gas: "200000",
51
- amount: [
52
- {
53
- denom: "ukuji",
54
- amount: gasFees[params.feeOptionKey || "fast"].getBaseValue("string") || "1000",
55
- },
56
- ],
57
- },
58
- });
59
- },
60
- };
61
- };
@@ -1,6 +0,0 @@
1
- import { type EVMChain, SKConfig } from "@swapkit/helpers";
2
- import { JsonRpcProvider } from "ethers";
3
-
4
- export const getProvider = (chain: EVMChain, customUrl?: string) => {
5
- return new JsonRpcProvider(customUrl || SKConfig.get("rpcUrls")[chain]);
6
- };