@keplr-wallet/hooks 0.9.18 → 0.10.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.
Files changed (77) hide show
  1. package/build/ibc/amount.d.ts +4 -4
  2. package/build/ibc/amount.js +5 -5
  3. package/build/ibc/amount.js.map +1 -1
  4. package/build/ibc/channel.d.ts +1 -1
  5. package/build/ibc/channel.js +4 -1
  6. package/build/ibc/channel.js.map +1 -1
  7. package/build/ibc/gas.d.ts +22 -5
  8. package/build/ibc/gas.js +8 -14
  9. package/build/ibc/gas.js.map +1 -1
  10. package/build/ibc/send-ibc-transfer.d.ts +10 -5
  11. package/build/ibc/send-ibc-transfer.js +6 -6
  12. package/build/ibc/send-ibc-transfer.js.map +1 -1
  13. package/build/ibc/types.d.ts +1 -1
  14. package/build/sign-doc/amount.d.ts +10 -6
  15. package/build/sign-doc/amount.js +8 -17
  16. package/build/sign-doc/amount.js.map +1 -1
  17. package/build/tx/amount.d.ts +5 -7
  18. package/build/tx/amount.js +14 -19
  19. package/build/tx/amount.js.map +1 -1
  20. package/build/tx/delegate-tx.d.ts +35 -6
  21. package/build/tx/delegate-tx.js +39 -9
  22. package/build/tx/delegate-tx.js.map +1 -1
  23. package/build/tx/errors.d.ts +3 -0
  24. package/build/tx/errors.js +9 -1
  25. package/build/tx/errors.js.map +1 -1
  26. package/build/tx/fee.d.ts +5 -7
  27. package/build/tx/fee.js +13 -19
  28. package/build/tx/fee.js.map +1 -1
  29. package/build/tx/gas.d.ts +1 -1
  30. package/build/tx/gas.js +7 -1
  31. package/build/tx/gas.js.map +1 -1
  32. package/build/tx/memo.d.ts +1 -1
  33. package/build/tx/memo.js +1 -1
  34. package/build/tx/memo.js.map +1 -1
  35. package/build/tx/recipient.d.ts +1 -1
  36. package/build/tx/recipient.js +4 -1
  37. package/build/tx/recipient.js.map +1 -1
  38. package/build/tx/redelegate-tx.d.ts +36 -4
  39. package/build/tx/redelegate-tx.js +38 -6
  40. package/build/tx/redelegate-tx.js.map +1 -1
  41. package/build/tx/send-gas.d.ts +6 -7
  42. package/build/tx/send-gas.js +41 -15
  43. package/build/tx/send-gas.js.map +1 -1
  44. package/build/tx/send-tx.d.ts +3 -5
  45. package/build/tx/send-tx.js +4 -4
  46. package/build/tx/send-tx.js.map +1 -1
  47. package/build/tx/send-types.d.ts +24 -0
  48. package/build/tx/send-types.js +3 -0
  49. package/build/tx/send-types.js.map +1 -0
  50. package/build/tx/staked-amount.d.ts +11 -6
  51. package/build/tx/staked-amount.js +14 -19
  52. package/build/tx/staked-amount.js.map +1 -1
  53. package/build/tx/types.d.ts +5 -5
  54. package/build/tx/undelegate-tx.d.ts +36 -4
  55. package/build/tx/undelegate-tx.js +38 -6
  56. package/build/tx/undelegate-tx.js.map +1 -1
  57. package/package.json +11 -11
  58. package/src/ibc/amount.ts +7 -15
  59. package/src/ibc/channel.ts +3 -2
  60. package/src/ibc/gas.ts +22 -19
  61. package/src/ibc/send-ibc-transfer.ts +20 -10
  62. package/src/ibc/types.ts +1 -1
  63. package/src/sign-doc/amount.ts +15 -16
  64. package/src/tx/amount.ts +18 -22
  65. package/src/tx/delegate-tx.ts +72 -15
  66. package/src/tx/errors.ts +8 -0
  67. package/src/tx/fee.ts +15 -20
  68. package/src/tx/gas.ts +4 -2
  69. package/src/tx/memo.ts +1 -1
  70. package/src/tx/recipient.ts +2 -1
  71. package/src/tx/redelegate-tx.ts +71 -10
  72. package/src/tx/send-gas.ts +48 -28
  73. package/src/tx/send-tx.ts +9 -16
  74. package/src/tx/send-types.ts +25 -0
  75. package/src/tx/staked-amount.ts +17 -19
  76. package/src/tx/types.ts +5 -5
  77. package/src/tx/undelegate-tx.ts +71 -10
@@ -1,39 +1,25 @@
1
1
  import { GasConfig } from "./gas";
2
2
  import { DenomHelper } from "@keplr-wallet/common";
3
- import {
4
- ChainGetter,
5
- CosmosMsgOpts,
6
- SecretMsgOpts,
7
- CosmwasmMsgOpts,
8
- } from "@keplr-wallet/stores";
3
+ import { ChainGetter } from "@keplr-wallet/stores";
9
4
  import { IAmountConfig } from "./types";
10
5
  import { useState } from "react";
11
- import { action, makeObservable, observable } from "mobx";
12
-
13
- type MsgOpts = CosmosMsgOpts & SecretMsgOpts & CosmwasmMsgOpts;
6
+ import { makeObservable, override } from "mobx";
7
+ import { AccountStore } from "./send-types";
8
+ import { UnknownCurrencyError } from "./errors";
14
9
 
15
10
  export class SendGasConfig extends GasConfig {
16
- @observable.ref
17
- protected sendMsgOpts: MsgOpts["send"];
18
-
19
11
  constructor(
20
12
  chainGetter: ChainGetter,
13
+ protected readonly accountStore: AccountStore,
21
14
  initialChainId: string,
22
- protected readonly amountConfig: IAmountConfig,
23
- sendMsgOpts: MsgOpts["send"]
15
+ protected readonly amountConfig: IAmountConfig
24
16
  ) {
25
17
  super(chainGetter, initialChainId);
26
18
 
27
- this.sendMsgOpts = sendMsgOpts;
28
-
29
19
  makeObservable(this);
30
20
  }
31
21
 
32
- @action
33
- setSendMsgOpts(opts: MsgOpts["send"]) {
34
- this.sendMsgOpts = opts;
35
- }
36
-
22
+ @override
37
23
  get gas(): number {
38
24
  // If gas not set manually, assume that the tx is for MsgSend.
39
25
  // And, set the default gas according to the currency type.
@@ -42,31 +28,65 @@ export class SendGasConfig extends GasConfig {
42
28
  this.amountConfig.sendCurrency.coinMinimalDenom
43
29
  );
44
30
 
31
+ const account = this.accountStore.getAccount(this.chainId);
32
+
45
33
  switch (denomHelper.type) {
46
34
  case "secret20":
47
- return this.sendMsgOpts.secret20.gas;
35
+ return account.secret?.msgOpts.send.secret20.gas ?? 0;
48
36
  case "cw20":
49
- return this.sendMsgOpts.cw20.gas;
37
+ return account.cosmwasm?.msgOpts.send.cw20.gas ?? 0;
50
38
  default:
51
- return this.sendMsgOpts.native.gas;
39
+ return account.cosmos?.msgOpts.send.native.gas ?? 0;
52
40
  }
53
41
  }
54
42
 
55
43
  return super.gas;
56
44
  }
45
+
46
+ @override
47
+ get error(): Error | undefined {
48
+ if (this.amountConfig.sendCurrency) {
49
+ const denomHelper = new DenomHelper(
50
+ this.amountConfig.sendCurrency.coinMinimalDenom
51
+ );
52
+
53
+ const account = this.accountStore.getAccount(this.chainId);
54
+
55
+ switch (denomHelper.type) {
56
+ case "secret20": {
57
+ if (!account.secret?.msgOpts.send.secret20.gas) {
58
+ return new UnknownCurrencyError("Unknown currency");
59
+ }
60
+ break;
61
+ }
62
+ case "cw20": {
63
+ if (!account.cosmwasm?.msgOpts.send.cw20.gas) {
64
+ return new UnknownCurrencyError("Unknown currency");
65
+ }
66
+ break;
67
+ }
68
+ default: {
69
+ if (!account.cosmos?.msgOpts.send.native.gas) {
70
+ return new UnknownCurrencyError("Unknown currency");
71
+ }
72
+ }
73
+ }
74
+ }
75
+
76
+ return super.error;
77
+ }
57
78
  }
58
79
 
59
80
  export const useSendGasConfig = (
60
81
  chainGetter: ChainGetter,
82
+ accountStore: AccountStore,
61
83
  chainId: string,
62
- amountConfig: IAmountConfig,
63
- sendMsgOpts: MsgOpts["send"]
84
+ amountConfig: IAmountConfig
64
85
  ) => {
65
86
  const [gasConfig] = useState(
66
- () => new SendGasConfig(chainGetter, chainId, amountConfig, sendMsgOpts)
87
+ () => new SendGasConfig(chainGetter, accountStore, chainId, amountConfig)
67
88
  );
68
89
  gasConfig.setChain(chainId);
69
- gasConfig.setSendMsgOpts(sendMsgOpts);
70
90
 
71
91
  return gasConfig;
72
92
  };
package/src/tx/send-tx.ts CHANGED
@@ -1,43 +1,36 @@
1
- import {
2
- ChainGetter,
3
- CosmosMsgOpts,
4
- CosmwasmMsgOpts,
5
- SecretMsgOpts,
6
- } from "@keplr-wallet/stores";
7
- import { ObservableQueryBalances } from "@keplr-wallet/stores/build/query/balances";
1
+ import { ChainGetter, IQueriesStore } from "@keplr-wallet/stores";
8
2
  import { useFeeConfig, useMemoConfig, useRecipientConfig } from "./index";
9
3
  import { useSendGasConfig } from "./send-gas";
10
4
  import { useAmountConfig } from "./amount";
11
-
12
- type MsgOpts = CosmosMsgOpts & SecretMsgOpts & CosmwasmMsgOpts;
5
+ import { AccountStore } from "./send-types";
13
6
 
14
7
  export const useSendTxConfig = (
15
8
  chainGetter: ChainGetter,
9
+ queriesStore: IQueriesStore,
10
+ accountStore: AccountStore,
16
11
  chainId: string,
17
- sendMsgOpts: MsgOpts["send"],
18
12
  sender: string,
19
- queryBalances: ObservableQueryBalances,
20
13
  ensEndpoint?: string
21
14
  ) => {
22
15
  const amountConfig = useAmountConfig(
23
16
  chainGetter,
17
+ queriesStore,
24
18
  chainId,
25
- sender,
26
- queryBalances
19
+ sender
27
20
  );
28
21
 
29
22
  const memoConfig = useMemoConfig(chainGetter, chainId);
30
23
  const gasConfig = useSendGasConfig(
31
24
  chainGetter,
25
+ accountStore,
32
26
  chainId,
33
- amountConfig,
34
- sendMsgOpts
27
+ amountConfig
35
28
  );
36
29
  const feeConfig = useFeeConfig(
37
30
  chainGetter,
31
+ queriesStore,
38
32
  chainId,
39
33
  sender,
40
- queryBalances,
41
34
  amountConfig,
42
35
  gasConfig
43
36
  );
@@ -0,0 +1,25 @@
1
+ import { IAccountStore, MsgOpt } from "@keplr-wallet/stores";
2
+
3
+ export type AccountStore = IAccountStore<{
4
+ cosmos?: {
5
+ readonly msgOpts: {
6
+ readonly send: {
7
+ readonly native: MsgOpt;
8
+ };
9
+ };
10
+ };
11
+ cosmwasm?: {
12
+ readonly msgOpts: {
13
+ readonly send: {
14
+ readonly cw20: Pick<MsgOpt, "gas">;
15
+ };
16
+ };
17
+ };
18
+ secret?: {
19
+ readonly msgOpts: {
20
+ readonly send: {
21
+ readonly secret20: Pick<MsgOpt, "gas">;
22
+ };
23
+ };
24
+ };
25
+ }>;
@@ -3,7 +3,8 @@ import { TxChainSetter } from "./chain";
3
3
  import {
4
4
  ChainGetter,
5
5
  CoinPrimitive,
6
- ObservableQueryDelegations,
6
+ CosmosQueriesImpl,
7
+ IQueriesStore,
7
8
  } from "@keplr-wallet/stores";
8
9
  import { action, computed, makeObservable, observable } from "mobx";
9
10
  import { AppCurrency } from "@keplr-wallet/types";
@@ -18,9 +19,6 @@ import { Dec, DecUtils } from "@keplr-wallet/unit";
18
19
  import { useState } from "react";
19
20
 
20
21
  export class StakedAmountConfig extends TxChainSetter implements IAmountConfig {
21
- @observable.ref
22
- protected queryDelegations: ObservableQueryDelegations;
23
-
24
22
  @observable
25
23
  protected _sender: string;
26
24
 
@@ -35,15 +33,16 @@ export class StakedAmountConfig extends TxChainSetter implements IAmountConfig {
35
33
 
36
34
  constructor(
37
35
  chainGetter: ChainGetter,
36
+ protected readonly queriesStore: IQueriesStore<{
37
+ cosmos: Pick<CosmosQueriesImpl, "queryDelegations">;
38
+ }>,
38
39
  initialChainId: string,
39
40
  sender: string,
40
- queryDelegations: ObservableQueryDelegations,
41
41
  initialValidatorAddress: string
42
42
  ) {
43
43
  super(chainGetter, initialChainId);
44
44
 
45
45
  this._sender = sender;
46
- this.queryDelegations = queryDelegations;
47
46
  this._amount = "";
48
47
  this._validatorAddress = initialValidatorAddress;
49
48
 
@@ -59,11 +58,6 @@ export class StakedAmountConfig extends TxChainSetter implements IAmountConfig {
59
58
  return this._validatorAddress;
60
59
  }
61
60
 
62
- @action
63
- setQueryDelegations(queryDelegations: ObservableQueryDelegations) {
64
- this.queryDelegations = queryDelegations;
65
- }
66
-
67
61
  @action
68
62
  setSender(sender: string) {
69
63
  this._sender = sender;
@@ -116,8 +110,9 @@ export class StakedAmountConfig extends TxChainSetter implements IAmountConfig {
116
110
  @computed
117
111
  get amount(): string {
118
112
  if (this.fraction != null) {
119
- const result = this.queryDelegations
120
- .getQueryBech32Address(this.sender)
113
+ const result = this.queriesStore
114
+ .get(this.chainId)
115
+ .cosmos.queryDelegations.getQueryBech32Address(this.sender)
121
116
  .getDelegationTo(this.validatorAddress);
122
117
 
123
118
  if (result.toDec().lte(new Dec(0))) {
@@ -171,7 +166,8 @@ export class StakedAmountConfig extends TxChainSetter implements IAmountConfig {
171
166
  return [this.chainInfo.stakeCurrency];
172
167
  }
173
168
 
174
- getError(): Error | undefined {
169
+ @computed
170
+ get error(): Error | undefined {
175
171
  const sendCurrency = this.sendCurrency;
176
172
  if (!sendCurrency) {
177
173
  return new Error("Currency to send not set");
@@ -195,8 +191,9 @@ export class StakedAmountConfig extends TxChainSetter implements IAmountConfig {
195
191
  return new NegativeAmountError("Amount is negative");
196
192
  }
197
193
 
198
- const balance = this.queryDelegations
199
- .getQueryBech32Address(this.sender)
194
+ const balance = this.queriesStore
195
+ .get(this.chainId)
196
+ .cosmos.queryDelegations.getQueryBech32Address(this.sender)
200
197
  .getDelegationTo(this.validatorAddress);
201
198
  const balanceDec = balance.toDec();
202
199
  if (dec.gt(balanceDec)) {
@@ -209,23 +206,24 @@ export class StakedAmountConfig extends TxChainSetter implements IAmountConfig {
209
206
 
210
207
  export const useStakedAmountConfig = (
211
208
  chainGetter: ChainGetter,
209
+ queriesStore: IQueriesStore<{
210
+ cosmos: Pick<CosmosQueriesImpl, "queryDelegations">;
211
+ }>,
212
212
  chainId: string,
213
213
  sender: string,
214
- queryDelegations: ObservableQueryDelegations,
215
214
  validatorAddress: string
216
215
  ) => {
217
216
  const [txConfig] = useState(
218
217
  () =>
219
218
  new StakedAmountConfig(
220
219
  chainGetter,
220
+ queriesStore,
221
221
  chainId,
222
222
  sender,
223
- queryDelegations,
224
223
  validatorAddress
225
224
  )
226
225
  );
227
226
  txConfig.setChain(chainId);
228
- txConfig.setQueryDelegations(queryDelegations);
229
227
  txConfig.setSender(sender);
230
228
  txConfig.setValidatorAddress(validatorAddress);
231
229
 
package/src/tx/types.ts CHANGED
@@ -12,7 +12,7 @@ export interface IMemoConfig extends ITxChainSetter {
12
12
  memo: string;
13
13
  setMemo(memo: string): void;
14
14
 
15
- getError(): Error | undefined;
15
+ error: Error | undefined;
16
16
  }
17
17
 
18
18
  export interface IGasConfig extends ITxChainSetter {
@@ -23,7 +23,7 @@ export interface IGasConfig extends ITxChainSetter {
23
23
  gasRaw: string;
24
24
  setGas(gas: number | string): void;
25
25
 
26
- getError(): Error | undefined;
26
+ error: Error | undefined;
27
27
  }
28
28
 
29
29
  export interface IFeeConfig extends ITxChainSetter {
@@ -36,7 +36,7 @@ export interface IFeeConfig extends ITxChainSetter {
36
36
  getFeeTypePretty(feeType: FeeType): CoinPretty;
37
37
  getFeePrimitive(): CoinPrimitive | undefined;
38
38
  isManual: boolean;
39
- getError(): Error | undefined;
39
+ error: Error | undefined;
40
40
  }
41
41
 
42
42
  export interface IRecipientConfig extends ITxChainSetter {
@@ -44,7 +44,7 @@ export interface IRecipientConfig extends ITxChainSetter {
44
44
  rawRecipient: string;
45
45
  setRawRecipient(recipient: string): void;
46
46
 
47
- getError(): Error | undefined;
47
+ error: Error | undefined;
48
48
  }
49
49
 
50
50
  export interface IAmountConfig extends ITxChainSetter {
@@ -76,7 +76,7 @@ export interface IAmountConfig extends ITxChainSetter {
76
76
  fraction: number | undefined;
77
77
  setFraction(value: number | undefined): void;
78
78
 
79
- getError(): Error | undefined;
79
+ error: Error | undefined;
80
80
  }
81
81
 
82
82
  export const DefaultGasPriceStep: {
@@ -1,38 +1,99 @@
1
- import { ChainGetter, ObservableQueryDelegations } from "@keplr-wallet/stores";
2
- import { ObservableQueryBalances } from "@keplr-wallet/stores/build/query/balances";
3
1
  import {
2
+ ChainGetter,
3
+ IQueriesStore,
4
+ CosmosQueriesImpl,
5
+ IAccountStore,
6
+ MsgOpt,
7
+ } from "@keplr-wallet/stores";
8
+ import {
9
+ GasConfig,
4
10
  useFeeConfig,
5
- useGasConfig,
6
11
  useMemoConfig,
7
12
  useRecipientConfig,
8
13
  } from "./index";
9
14
  import { useStakedAmountConfig } from "./staked-amount";
15
+ import { makeObservable, override } from "mobx";
16
+ import { useState } from "react";
17
+
18
+ export class UndelegateGasConfig extends GasConfig {
19
+ constructor(
20
+ chainGetter: ChainGetter,
21
+ protected readonly accountStore: IAccountStore<{
22
+ cosmos: {
23
+ readonly msgOpts: {
24
+ readonly undelegate: MsgOpt;
25
+ };
26
+ };
27
+ }>,
28
+ initialChainId: string
29
+ ) {
30
+ super(chainGetter, initialChainId);
31
+
32
+ makeObservable(this);
33
+ }
34
+
35
+ @override
36
+ get gas(): number {
37
+ // If gas not set manually, assume that the tx is for MsgTransfer.
38
+ if (this._gasRaw == null) {
39
+ return this.accountStore.getAccount(this.chainId).cosmos.msgOpts
40
+ .undelegate.gas;
41
+ }
42
+
43
+ return super.gas;
44
+ }
45
+ }
46
+
47
+ export const useUndelegateGasConfig = (
48
+ chainGetter: ChainGetter,
49
+ accountStore: IAccountStore<{
50
+ cosmos: {
51
+ readonly msgOpts: {
52
+ readonly undelegate: MsgOpt;
53
+ };
54
+ };
55
+ }>,
56
+ chainId: string
57
+ ) => {
58
+ const [gasConfig] = useState(
59
+ () => new UndelegateGasConfig(chainGetter, accountStore, chainId)
60
+ );
61
+ gasConfig.setChain(chainId);
62
+
63
+ return gasConfig;
64
+ };
10
65
 
11
66
  export const useUndelegateTxConfig = (
12
67
  chainGetter: ChainGetter,
68
+ queriesStore: IQueriesStore<{
69
+ cosmos: Pick<CosmosQueriesImpl, "queryDelegations">;
70
+ }>,
71
+ accountStore: IAccountStore<{
72
+ cosmos: {
73
+ readonly msgOpts: {
74
+ readonly undelegate: MsgOpt;
75
+ };
76
+ };
77
+ }>,
13
78
  chainId: string,
14
- gas: number,
15
79
  sender: string,
16
- queryBalances: ObservableQueryBalances,
17
- queryDelegations: ObservableQueryDelegations,
18
80
  validatorAddress: string
19
81
  ) => {
20
82
  const amountConfig = useStakedAmountConfig(
21
83
  chainGetter,
84
+ queriesStore,
22
85
  chainId,
23
86
  sender,
24
- queryDelegations,
25
87
  validatorAddress
26
88
  );
27
89
 
28
90
  const memoConfig = useMemoConfig(chainGetter, chainId);
29
- const gasConfig = useGasConfig(chainGetter, chainId, gas);
30
- gasConfig.setGas(gas);
91
+ const gasConfig = useUndelegateGasConfig(chainGetter, accountStore, chainId);
31
92
  const feeConfig = useFeeConfig(
32
93
  chainGetter,
94
+ queriesStore,
33
95
  chainId,
34
96
  sender,
35
- queryBalances,
36
97
  amountConfig,
37
98
  gasConfig,
38
99
  false