Skip to content

Commit

Permalink
[dapp-kit] do a little polishing on the hooks for inferring the activ…
Browse files Browse the repository at this point in the history
…e chain / exposing errors / etc. (MystenLabs#14181)

## Description 

This PR does a little polish on the signing hooks for a few things I
noticed were off. The most notable change here is that we're making the
`chain` arg optional as we should just infer the active chain from the
account. This keeps parity with the wallet-kit implementation

## Test Plan 
- CI

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
williamrobertson13 authored Oct 10, 2023
1 parent b482893 commit c7e12c9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-turtles-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/dapp-kit': minor
---

Infer the active chain when signing transactions and expose some more descriptive errors
4 changes: 3 additions & 1 deletion sdk/dapp-kit/src/hooks/wallet/useDisconnectWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { WalletNotConnectedError } from '../../errors/walletErrors.js';
import { useCurrentWallet } from './useCurrentWallet.js';
import { useWalletStore } from './useWalletStore.js';

type UseDisconnectWalletError = WalletNotConnectedError | Error;

type UseDisconnectWalletMutationOptions = Omit<
UseMutationOptions<void, Error, void, unknown>,
UseMutationOptions<void, UseDisconnectWalletError, void, unknown>,
'mutationFn'
>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { useCurrentWallet } from './useCurrentWallet.js';

type UseSignAndExecuteTransactionBlockArgs = PartialBy<
SuiSignAndExecuteTransactionBlockInput,
'account'
'account' | 'chain'
>;

type UseSignAndExecuteTransactionBlockResult = SuiSignAndExecuteTransactionBlockOutput;
Expand Down Expand Up @@ -61,7 +61,7 @@ export function useSignAndExecuteTransactionBlock({
const signerAccount = signAndExecuteTransactionBlockArgs.account ?? currentAccount;
if (!signerAccount) {
throw new WalletNoAccountSelectedError(
'No wallet account is selected to sign the personal message with.',
'No wallet account is selected to sign and execute the transaction block with.',
);
}

Expand All @@ -75,6 +75,7 @@ export function useSignAndExecuteTransactionBlock({
return await walletFeature.signAndExecuteTransactionBlock({
...signAndExecuteTransactionBlockArgs,
account: signerAccount,
chain: signAndExecuteTransactionBlockArgs.chain ?? signerAccount.chains[0],
});
},
...mutationOptions,
Expand Down
5 changes: 3 additions & 2 deletions sdk/dapp-kit/src/hooks/wallet/useSignTransactionBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { PartialBy } from '../../types/utilityTypes.js';
import { useCurrentAccount } from './useCurrentAccount.js';
import { useCurrentWallet } from './useCurrentWallet.js';

type UseSignTransactionBlockArgs = PartialBy<SuiSignTransactionBlockInput, 'account'>;
type UseSignTransactionBlockArgs = PartialBy<SuiSignTransactionBlockInput, 'account' | 'chain'>;

type UseSignTransactionBlockResult = SuiSignTransactionBlockOutput;

Expand Down Expand Up @@ -58,7 +58,7 @@ export function useSignTransactionBlock({
const signerAccount = signTransactionBlockArgs.account ?? currentAccount;
if (!signerAccount) {
throw new WalletNoAccountSelectedError(
'No wallet account is selected to sign the personal message with.',
'No wallet account is selected to sign the transaction block with.',
);
}

Expand All @@ -72,6 +72,7 @@ export function useSignTransactionBlock({
return await walletFeature.signTransactionBlock({
...signTransactionBlockArgs,
account: signerAccount,
chain: signTransactionBlockArgs.chain ?? signerAccount.chains[0],
});
},
...mutationOptions,
Expand Down
9 changes: 3 additions & 6 deletions sdk/dapp-kit/src/hooks/wallet/useSwitchAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ type SwitchAccountArgs = {

type SwitchAccountResult = void;

type UseSwitchAccountError = WalletNotConnectedError | WalletAccountNotFoundError | Error;

type UseSwitchAccountMutationOptions = Omit<
UseMutationOptions<
SwitchAccountResult,
WalletNotConnectedError | WalletAccountNotFoundError | Error,
SwitchAccountArgs,
unknown
>,
UseMutationOptions<SwitchAccountResult, UseSwitchAccountError, SwitchAccountArgs, unknown>,
'mutationFn'
>;

Expand Down

0 comments on commit c7e12c9

Please sign in to comment.