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

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 +1,47 @@
1
1
  import type { Pubkey, Secp256k1HdWallet } from "@cosmjs/amino";
2
- import type { Account } from "@cosmjs/stargate";
3
2
  import { base64 } from "@scure/base";
4
3
  import {
5
4
  BaseDecimal,
6
5
  Chain,
7
- type ChainId,
8
6
  CosmosChainPrefixes,
9
- DerivationPath,
10
7
  FeeOption,
8
+ type GenericTransferParams,
9
+ NetworkDerivationPath,
11
10
  RequestClient,
12
11
  SKConfig,
13
- SwapKitError,
14
12
  SwapKitNumber,
13
+ derivationPathToString,
14
+ updateDerivationPath,
15
15
  } from "@swapkit/helpers";
16
16
 
17
+ import { P, match } from "ts-pattern";
17
18
  import {
18
19
  buildAminoMsg,
19
- buildDepositTx,
20
20
  buildEncodedTxBody,
21
- buildTransferTx,
22
21
  convertToSignable,
23
22
  createDefaultAminoTypes,
24
23
  createDefaultRegistry,
24
+ getCreateTransaction,
25
25
  parseAminoMessageForDirectSigning,
26
- } from "../thorchainUtils/index";
27
- import type {
28
- DepositParam,
29
- ThorchainConstantsResponse,
30
- } from "../thorchainUtils/types/client-types";
31
- import type { Signer, TransferParams } from "../types";
26
+ } from "../thorchainUtils";
27
+ import type { ThorchainConstantsResponse } from "../thorchainUtils/types/client-types";
28
+ import type { MultisigTx } from "../types";
29
+ import type { CosmosToolboxParams, MultiSigSigner } from "../types";
32
30
  import {
33
31
  createOfflineStargateClient,
34
32
  createSigningStargateClient,
35
33
  createStargateClient,
36
34
  getDefaultChainFee,
37
35
  } from "../util";
38
- import { BaseCosmosToolbox } from "./BaseCosmosToolbox";
36
+ import { createCosmosToolbox } from "./cosmos";
39
37
 
40
38
  function secp256k1HdWalletFromMnemonic({
41
39
  prefix,
42
40
  derivationPath,
43
- }: { prefix: string; derivationPath: string }) {
41
+ }: {
42
+ prefix: string;
43
+ derivationPath?: string;
44
+ }) {
44
45
  return async function secp256k1HdWalletFromMnemonic(mnemonic: string, index = 0) {
45
46
  const { Secp256k1HdWallet } = await import("@cosmjs/amino");
46
47
  const { stringToPath } = await import("@cosmjs/crypto");
@@ -56,59 +57,59 @@ function exportSignature(signature: Uint8Array) {
56
57
  return base64.encode(signature);
57
58
  }
58
59
 
59
- async function signMultisigTx(
60
- wallet: Secp256k1HdWallet,
61
- tx: string,
62
- chain: Chain.THORChain | Chain.Maya,
63
- ) {
64
- const {
65
- msgs,
66
- accountNumber,
67
- sequence,
68
- chainId,
69
- fee,
70
- memo,
60
+ function signMultisigTx(chain: Chain.THORChain | Chain.Maya) {
61
+ return async function signMultisigTx({
62
+ wallet,
63
+ tx,
71
64
  }: {
72
- msgs: ReturnType<typeof buildAminoMsg>[];
73
- accountNumber: number;
74
- sequence: number;
75
- chainId: ChainId;
76
- fee: ReturnType<typeof getDefaultChainFee>;
77
- memo: string;
78
- } = JSON.parse(tx);
79
-
80
- const address = (await wallet.getAccounts())?.[0]?.address || "";
81
- const aminoTypes = await createDefaultAminoTypes(chain);
82
- const registry = await createDefaultRegistry();
83
- const signingClient = await createOfflineStargateClient(wallet, { registry, aminoTypes });
84
- const msgForSigning = [];
85
-
86
- for (const msg of msgs) {
87
- const signMsg = await convertToSignable(msg, chain);
88
- msgForSigning.push(signMsg);
89
- }
65
+ wallet: Secp256k1HdWallet;
66
+ tx: string | MultisigTx;
67
+ }) {
68
+ const { msgs, accountNumber, sequence, chainId, fee, memo } =
69
+ typeof tx === "string" ? JSON.parse(tx) : tx;
90
70
 
91
- const {
92
- signatures: [signature],
93
- } = await signingClient.sign(address, msgForSigning, fee, memo, {
94
- accountNumber,
95
- sequence,
96
- chainId,
97
- });
71
+ const address = (await wallet.getAccounts())?.[0]?.address || "";
72
+ const aminoTypes = await createDefaultAminoTypes(chain);
73
+ const registry = await createDefaultRegistry();
74
+ const signingClient = await createOfflineStargateClient(wallet, {
75
+ registry,
76
+ aminoTypes,
77
+ });
78
+ const msgForSigning = [];
98
79
 
99
- const bodyBytes = await buildEncodedTxBody({
100
- chain,
101
- memo,
102
- msgs: msgs.map(parseAminoMessageForDirectSigning),
103
- });
80
+ for (const msg of msgs) {
81
+ const signMsg = await convertToSignable(msg, chain);
82
+ msgForSigning.push(signMsg);
83
+ }
84
+
85
+ const {
86
+ signatures: [signature],
87
+ } = await signingClient.sign(address, msgForSigning, fee, memo, {
88
+ accountNumber,
89
+ sequence,
90
+ chainId,
91
+ });
92
+
93
+ const bodyBytes = await buildEncodedTxBody({
94
+ chain,
95
+ memo,
96
+ msgs: msgs.map(parseAminoMessageForDirectSigning),
97
+ });
104
98
 
105
- return { signature: exportSignature(signature as Uint8Array), bodyBytes };
99
+ return { signature: exportSignature(signature as Uint8Array), bodyBytes };
100
+ };
106
101
  }
107
102
 
108
- function broadcastMultisigTx({ prefix, rpcUrl }: { prefix: string; rpcUrl: string }) {
103
+ function broadcastMultisigTx({
104
+ prefix,
105
+ rpcUrl,
106
+ }: {
107
+ prefix: string;
108
+ rpcUrl: string;
109
+ }) {
109
110
  return async function broadcastMultisigTx(
110
111
  tx: string,
111
- signers: Signer[],
112
+ signers: MultiSigSigner[],
112
113
  membersPubKeys: string[],
113
114
  threshold: number,
114
115
  bodyBytes: Uint8Array,
@@ -156,51 +157,46 @@ function importSignature(signature: string) {
156
157
  async function signWithPrivateKey({
157
158
  privateKey,
158
159
  message,
159
- }: { privateKey: Uint8Array; message: string }) {
160
+ }: {
161
+ privateKey: Uint8Array;
162
+ message: string;
163
+ }) {
160
164
  const { Secp256k1 } = await import("@cosmjs/crypto");
161
165
 
162
166
  const signature = await Secp256k1.createSignature(base64.decode(message), privateKey);
163
167
  return base64.encode(Buffer.concat([signature.r(32), signature.s(32)]));
164
168
  }
165
169
 
166
- function verifySignature(getAccount: (address: string) => Promise<Account | null>) {
167
- return async function verifySignature({
168
- signature,
169
- message,
170
- address,
171
- }: {
172
- signature: string;
173
- message: string;
174
- address: string;
175
- }) {
176
- const account = await getAccount(address);
177
- if (!account?.pubkey) throw new SwapKitError("toolbox_cosmos_verify_signature_no_pubkey");
178
- const { Secp256k1Signature, Secp256k1 } = await import("@cosmjs/crypto");
179
-
180
- const secpSignature = Secp256k1Signature.fromFixedLength(base64.decode(signature));
181
- return Secp256k1.verifySignature(secpSignature, base64.decode(message), account.pubkey.value);
182
- };
183
- }
184
-
185
- export function BaseThorchainToolbox(chain: Chain.THORChain | Chain.Maya) {
170
+ export async function createThorchainToolbox({
171
+ chain,
172
+ ...toolboxParams
173
+ }: CosmosToolboxParams<Chain.THORChain | Chain.Maya>) {
186
174
  const nodeUrl = SKConfig.get("nodeUrls")[chain];
187
175
  const rpcUrl = SKConfig.get("rpcUrls")[chain];
188
176
  const { isStagenet } = SKConfig.get("envs");
189
-
190
177
  const isThorchain = chain === Chain.THORChain;
191
178
  const chainPrefix = `${isStagenet ? "s" : ""}${CosmosChainPrefixes[chain]}`;
192
- const derivationPath = DerivationPath[chain];
193
179
 
194
- const cosmosToolbox = BaseCosmosToolbox({ chain, derivationPath, prefix: chainPrefix });
195
- const defaultFee = getDefaultChainFee(chain);
180
+ const index = "index" in toolboxParams ? toolboxParams.index || 0 : 0;
196
181
 
197
- function loadAddressBalances(address: string) {
198
- try {
199
- return cosmosToolbox.getBalance(address);
200
- } catch (error) {
201
- return Promise.reject(error);
202
- }
203
- }
182
+ const derivationPath =
183
+ "derivationPath" in toolboxParams && toolboxParams.derivationPath
184
+ ? toolboxParams.derivationPath
185
+ : updateDerivationPath(NetworkDerivationPath[chain], { index });
186
+
187
+ const cosmosToolbox = await createCosmosToolbox({
188
+ chain,
189
+ ...toolboxParams,
190
+ });
191
+
192
+ const signer = await match(toolboxParams)
193
+ .with({ phrase: P.string }, ({ phrase }) =>
194
+ cosmosToolbox.getSignerFromPhrase({ phrase, derivationPath }),
195
+ )
196
+ .with({ signer: P.any }, ({ signer }) => signer)
197
+ .otherwise(() => undefined);
198
+
199
+ const defaultFee = getDefaultChainFee(chain);
204
200
 
205
201
  async function getFees() {
206
202
  let fee: SwapKitNumber;
@@ -218,21 +214,23 @@ export function BaseThorchainToolbox(chain: Chain.THORChain | Chain.Maya) {
218
214
 
219
215
  fee = new SwapKitNumber(nativeFee);
220
216
  } catch {
221
- fee = new SwapKitNumber({ value: isThorchain ? 0.02 : 1, decimal: BaseDecimal[chain] });
217
+ fee = new SwapKitNumber({
218
+ value: isThorchain ? 0.02 : 1,
219
+ decimal: BaseDecimal[chain],
220
+ });
222
221
  }
223
222
 
224
223
  return { [FeeOption.Average]: fee, [FeeOption.Fast]: fee, [FeeOption.Fastest]: fee };
225
224
  }
226
225
 
227
226
  async function transfer({
228
- from,
229
- recipient,
230
227
  assetValue,
231
228
  memo = "",
232
- signer,
233
- }: Omit<TransferParams, "recipient"> & { recipient?: string }) {
229
+ recipient,
230
+ }: Omit<GenericTransferParams, "recipient"> & { recipient?: string }) {
234
231
  const { TxRaw } = await import("cosmjs-types/cosmos/tx/v1beta1/tx");
235
- if (!signer) throw new Error("Signer not defined");
232
+ const sender = (await signer?.getAccounts())?.[0]?.address;
233
+ if (!(sender && signer)) throw new Error("Signer not defined");
236
234
 
237
235
  const isAminoSigner = "signAmino" in signer;
238
236
  const registry = await createDefaultRegistry();
@@ -242,13 +240,18 @@ export function BaseThorchainToolbox(chain: Chain.THORChain | Chain.Maya) {
242
240
  aminoTypes,
243
241
  });
244
242
 
245
- const aminoMessage = buildAminoMsg({ assetValue, from, recipient, memo, chain });
243
+ const aminoMessage = buildAminoMsg({
244
+ assetValue,
245
+ sender,
246
+ recipient,
247
+ memo,
248
+ });
246
249
 
247
250
  if (isAminoSigner) {
248
251
  const msgSign = await convertToSignable(aminoMessage, chain);
249
252
 
250
253
  const { signatures, authInfoBytes } = await signingClient.sign(
251
- from,
254
+ sender,
252
255
  [msgSign],
253
256
  defaultFee,
254
257
  memo,
@@ -271,51 +274,35 @@ export function BaseThorchainToolbox(chain: Chain.THORChain | Chain.Maya) {
271
274
 
272
275
  const preparedMessage = parseAminoMessageForDirectSigning(aminoMessage);
273
276
  const msgSign = await convertToSignable(preparedMessage, chain);
274
- const txResponse = await signingClient.signAndBroadcast(from, [msgSign], defaultFee, memo);
277
+ const txResponse = await signingClient.signAndBroadcast(sender, [msgSign], defaultFee, memo);
275
278
 
276
279
  return txResponse.transactionHash;
277
280
  }
278
281
 
279
282
  return {
280
283
  ...cosmosToolbox,
281
- deposit: (params: DepositParam & { from: string }) => transfer(params),
282
- pubkeyToAddress: async (pubkey: Pubkey) => {
283
- const { pubkeyToAddress } = await import("@cosmjs/amino");
284
- return pubkeyToAddress(pubkey, chainPrefix);
285
- },
286
- transfer,
287
- getFees,
284
+ broadcastMultisigTx: broadcastMultisigTx({ prefix: chainPrefix, rpcUrl }),
288
285
  buildAminoMsg,
289
- convertToSignable,
290
- buildDepositTx: buildDepositTx(rpcUrl),
291
- buildTransferTx: buildTransferTx(rpcUrl),
292
286
  buildEncodedTxBody,
293
- parseAminoMessageForDirectSigning,
287
+ convertToSignable,
294
288
  createDefaultAminoTypes: () => createDefaultAminoTypes(chain),
295
289
  createDefaultRegistry,
290
+ createMultisig,
291
+ createTransaction: getCreateTransaction(rpcUrl),
292
+ deposit: transfer,
293
+ getFees,
294
+ importSignature,
295
+ parseAminoMessageForDirectSigning,
296
296
  secp256k1HdWalletFromMnemonic: secp256k1HdWalletFromMnemonic({
297
- derivationPath,
297
+ derivationPath: derivationPathToString(derivationPath),
298
298
  prefix: chainPrefix,
299
299
  }),
300
- signMultisigTx: (wallet: Secp256k1HdWallet, tx: string) => signMultisigTx(wallet, tx, chain),
301
- broadcastMultisigTx: broadcastMultisigTx({ prefix: chainPrefix, rpcUrl }),
302
- createMultisig,
303
- importSignature,
304
- loadAddressBalances,
300
+ signMultisigTx: signMultisigTx(chain),
305
301
  signWithPrivateKey,
306
- verifySignature: verifySignature(cosmosToolbox.getAccount),
302
+ transfer,
303
+ pubkeyToAddress: async (pubkey: Pubkey) => {
304
+ const { pubkeyToAddress } = await import("@cosmjs/amino");
305
+ return pubkeyToAddress(pubkey, chainPrefix);
306
+ },
307
307
  };
308
308
  }
309
-
310
- export function ThorchainToolbox() {
311
- return BaseThorchainToolbox(Chain.THORChain);
312
- }
313
-
314
- export function MayaToolbox() {
315
- return BaseThorchainToolbox(Chain.Maya);
316
- }
317
-
318
- export type ThorchainWallet = Omit<ReturnType<typeof BaseThorchainToolbox>, "signMessage">;
319
- export type ThorchainWallets = {
320
- [chain in Chain.THORChain | Chain.Maya]: ThorchainWallet;
321
- };
@@ -1,6 +1,10 @@
1
- import type { OfflineAminoSigner, StdFee } from "@cosmjs/amino";
2
- import type { OfflineDirectSigner } from "@cosmjs/proto-signing";
3
- import type { AssetValue, ChainId, FeeOption } from "@swapkit/helpers";
1
+ import type { OfflineAminoSigner } from "@cosmjs/amino";
2
+ import type { DirectSecp256k1HdWallet, OfflineDirectSigner } from "@cosmjs/proto-signing";
3
+ import type { Chain, ChainId, CosmosChain, DerivationPathArray } from "@swapkit/helpers";
4
+ import type { buildAminoMsg } from "./thorchainUtils";
5
+ import type { createCosmosToolbox } from "./toolbox/cosmos";
6
+ import type { createThorchainToolbox } from "./toolbox/thorchain";
7
+ import type { getDefaultChainFee } from "./util";
4
8
 
5
9
  export type CosmosSDKClientParams = {
6
10
  server: string;
@@ -9,23 +13,38 @@ export type CosmosSDKClientParams = {
9
13
  stagenet?: boolean;
10
14
  };
11
15
 
12
- export type TransferParams = {
13
- assetValue: AssetValue;
14
- fee?: StdFee | number;
15
- feeOptionKey?: FeeOption;
16
- from: string;
17
- memo?: string;
18
- privkey?: Uint8Array;
19
- recipient: string;
20
- signer?: OfflineDirectSigner | OfflineAminoSigner;
16
+ export type MultiSigSigner = {
17
+ pubKey: string;
18
+ signature: string;
21
19
  };
22
20
 
23
- export type ToolboxParams = {
24
- rpcUrl?: string;
25
- prefix?: string;
21
+ export type MultisigTx = {
22
+ msgs: ReturnType<typeof buildAminoMsg>[];
23
+ accountNumber: number;
24
+ sequence: number;
25
+ chainId: ChainId;
26
+ fee: ReturnType<typeof getDefaultChainFee>;
27
+ memo: string;
26
28
  };
27
29
 
28
- export type Signer = {
29
- pubKey: string;
30
- signature: string;
30
+ export type CosmosSigner = DirectSecp256k1HdWallet | OfflineDirectSigner | OfflineAminoSigner;
31
+
32
+ export type CosmosToolboxParams<T = CosmosChain> = {
33
+ chain: T;
34
+ } & (
35
+ | { signer?: CosmosSigner }
36
+ | { phrase?: string; derivationPath?: DerivationPathArray; index?: number }
37
+ );
38
+
39
+ export type BaseCosmosToolboxType = ReturnType<typeof createCosmosToolbox>;
40
+ export type BaseCosmosWallet = Awaited<ReturnType<typeof createCosmosToolbox>>;
41
+ export type CosmosWallets = {
42
+ [chain in Chain.Cosmos | Chain.Kujira]: BaseCosmosWallet;
43
+ };
44
+
45
+ export type ThorchainWallet = Awaited<
46
+ Omit<ReturnType<typeof createThorchainToolbox>, "signMessage">
47
+ >;
48
+ export type ThorchainWallets = {
49
+ [chain in Chain.THORChain | Chain.Maya]: ThorchainWallet;
31
50
  };
@@ -1,16 +1,8 @@
1
1
  import type { OfflineSigner } from "@cosmjs/proto-signing";
2
2
  import type { SigningStargateClientOptions } from "@cosmjs/stargate";
3
- import {
4
- AssetValue,
5
- Chain,
6
- ChainId,
7
- type CosmosChain,
8
- SKConfig,
9
- defaultRequestHeaders,
10
- getGasAsset,
11
- } from "@swapkit/helpers";
12
-
13
- import type { CosmosNativeTransferTxParams } from "./thorchainUtils";
3
+ import { AssetValue, Chain, ChainId, type CosmosChain, SKConfig } from "@swapkit/helpers";
4
+
5
+ import type { CosmosCreateTransactionParams } from "./thorchainUtils";
14
6
 
15
7
  export const USK_KUJIRA_FACTORY_DENOM =
16
8
  "FACTORY/KUJIRA1QK00H5ATUTPSV900X202PXX42NPJR9THG58DNQPA72F2P7M2LUASE444A7/UUSK";
@@ -76,55 +68,10 @@ export const getDenomWithChain = ({ symbol, chain }: AssetValue) => {
76
68
  return getMsgSendDenom(symbol, false);
77
69
  };
78
70
 
79
- // TODO: figure out some better way to initialize from base value
80
- export const getAssetFromDenom = (denom: string, amount: string) => {
81
- switch (denom) {
82
- case "rune":
83
- return AssetValue.from({
84
- chain: Chain.THORChain,
85
- value: Number.parseInt(amount) / 1e8,
86
- });
87
- case "uatom":
88
- case "atom":
89
- return AssetValue.from({
90
- chain: Chain.Cosmos,
91
- value: Number.parseInt(amount) / 1e6,
92
- });
93
- case "cacao":
94
- return AssetValue.from({
95
- chain: Chain.Maya,
96
- value: Number.parseInt(amount) / 1e10,
97
- });
98
- case "maya":
99
- return AssetValue.from({
100
- asset: `${Chain.Maya}.${Chain.Maya}`,
101
- value: Number.parseInt(amount) / 1e4,
102
- });
103
- case "ukuji":
104
- case "kuji":
105
- return AssetValue.from({
106
- chain: Chain.Kujira,
107
- value: Number.parseInt(amount) / 1e6,
108
- });
109
- case USK_KUJIRA_FACTORY_DENOM:
110
- // USK on Kujira
111
- return AssetValue.from({
112
- asset: `${Chain.Kujira}.USK`,
113
- value: Number.parseInt(amount) / 1e6,
114
- });
115
-
116
- default:
117
- return AssetValue.from({
118
- asset: denom,
119
- value: Number.parseInt(amount) / 1e8,
120
- });
121
- }
122
- };
123
-
124
71
  export async function createStargateClient(url: string) {
125
72
  const { StargateClient } = await import("@cosmjs/stargate");
126
73
 
127
- return StargateClient.connect({ url, headers: defaultRequestHeaders });
74
+ return StargateClient.connect(url);
128
75
  }
129
76
 
130
77
  export async function createSigningStargateClient(
@@ -136,7 +83,7 @@ export async function createSigningStargateClient(
136
83
  const gasPrice = typeof optionsOrBaseGas === "string" ? optionsOrBaseGas : "0.0003uatom";
137
84
  const options = typeof optionsOrBaseGas === "string" ? {} : optionsOrBaseGas;
138
85
 
139
- return SigningStargateClient.connectWithSigner({ url, headers: defaultRequestHeaders }, signer, {
86
+ return SigningStargateClient.connectWithSigner(url, signer, {
140
87
  gasPrice: GasPrice.fromString(gasPrice),
141
88
  ...options,
142
89
  });
@@ -186,45 +133,48 @@ const getTransferMsgTypeByChain = (chain: CosmosChain) => {
186
133
  /**
187
134
  * Used to build tx for Cosmos and Kujira
188
135
  */
189
- export const buildNativeTransferTx = async ({
190
- fromAddress,
191
- toAddress,
136
+ export const cosmosCreateTransaction = async ({
137
+ sender,
138
+ recipient,
192
139
  assetValue,
193
140
  memo = "",
194
- fee,
195
- }: CosmosNativeTransferTxParams) => {
141
+ feeRate,
142
+ sequence,
143
+ accountNumber,
144
+ }: CosmosCreateTransactionParams) => {
196
145
  const { chain, chainId } = assetValue;
197
146
 
198
147
  const url = getRPC(chainId);
199
148
  const client = await createStargateClient(url);
200
- const accountOnChain = await client.getAccount(fromAddress);
149
+ const accountOnChain = await client.getAccount(sender);
201
150
 
202
151
  if (!accountOnChain) {
203
152
  throw new Error("Account does not exist");
204
153
  }
205
154
 
206
- const feeAsset = getMsgSendDenom(getGasAsset({ chain }).symbol);
155
+ const gasAsset = AssetValue.from({ chain });
156
+ const feeAsset = getMsgSendDenom(gasAsset.symbol);
207
157
  const defaultFee = getDefaultChainFee(chain as CosmosChain);
208
158
 
209
159
  const txFee =
210
- feeAsset && fee
211
- ? { amount: [{ denom: feeAsset, amount: fee }], gas: defaultFee.gas }
160
+ feeAsset && feeRate
161
+ ? { amount: [{ denom: feeAsset, amount: feeRate.toString() }], gas: defaultFee.gas }
212
162
  : defaultFee;
213
163
 
214
164
  const msgSend = {
215
- fromAddress,
216
- toAddress,
165
+ fromAddress: sender,
166
+ toAddress: recipient,
217
167
  amount: [
218
168
  { amount: assetValue.getBaseValue("string"), denom: getMsgSendDenom(assetValue.symbol) },
219
169
  ],
220
170
  };
221
171
 
222
172
  return {
223
- accountNumber: accountOnChain.accountNumber,
173
+ accountNumber: accountNumber ?? accountOnChain.accountNumber,
224
174
  chainId,
225
175
  fee: txFee,
226
176
  memo,
227
- sequence: accountOnChain.sequence,
177
+ sequence: sequence ?? accountOnChain.sequence,
228
178
  msgs: [{ typeUrl: getTransferMsgTypeByChain(chain as CosmosChain), value: msgSend }],
229
179
  };
230
180
  };