From 3f94ddf607968651726f7a93e104dd2cbdda707b Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Tue, 19 Nov 2024 15:27:31 +0200 Subject: [PATCH] Add signAndSendTransactionAsync interface --- packages/core/docs/api/transactions.md | 2 +- packages/core/docs/api/wallet.md | 39 ++++++++++++++++++++ packages/core/docs/guides/custom-wallets.md | 21 +++++++++++ packages/core/src/lib/wallet/wallet.types.ts | 8 ++++ 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/packages/core/docs/api/transactions.md b/packages/core/docs/api/transactions.md index 6ae9e8d46..fe7333fef 100644 --- a/packages/core/docs/api/transactions.md +++ b/packages/core/docs/api/transactions.md @@ -14,7 +14,7 @@ interface Transaction { ### Actions -Below are the 8 supported NEAR Actions used by `signAndSendTransaction` and `signAndSendTransactions`: +Below are the 8 supported NEAR Actions used by `signAndSendTransaction`, `signAndSendTransactions` and `signAndSendTransactionAsync`: ```ts interface CreateAccountAction { diff --git a/packages/core/docs/api/wallet.md b/packages/core/docs/api/wallet.md index c2295e0bc..d182efd22 100644 --- a/packages/core/docs/api/wallet.md +++ b/packages/core/docs/api/wallet.md @@ -263,6 +263,45 @@ Signs one or more NEAR Actions before sending to the network. The user must be s })(); ``` +### `.signAndSendTransactionAsync(params)` + +**Parameters** + +- `params` (`object`) + - `signerId` (`string?`): Account ID used to sign the transaction. Defaults to the first account. + - `receiverId` (`string?`): Account ID to receive the transaction. Defaults to `contractId` defined in `.init`. + - `actions` (`Array`): NEAR Action(s) to sign and send to the network (e.g. `FunctionCall`). You can find more information on `Action` [here](./transactions.md). + - `callbackUrl` (`string?`): Applicable to browser wallets (e.g. MyNearWallet). This the callback url once the transaction is approved. + +**Returns** + +- `Uint8Array | void`: Browser wallets won't return the transaction outcome as they may need to redirect for signing. More details on this can be found [here](https://docs.near.org/api/rpc/transactions#send-transaction-await). + +**Description** + +Signs one or more NEAR Actions before sending to the network. The user must be signed in to call this method as there's at least charges for gas spent. + +> Note: Sender only supports `"FunctionCall"` action types right now. If you wish to use other NEAR Actions in your dApp, it's recommended to remove this wallet in your configuration. + +**Example** + +```ts +(async () => { + const wallet = await selector.wallet("sender"); + const txHash = wallet.signAndSendTransactionAsync({ + actions: [{ + type: "FunctionCall", + params: { + methodName: "addMessage", + args: { text: "Hello World!" }, + gas: "30000000000000", + deposit: "10000000000000000000000", + } + }] + }); +})(); +``` + ### `.signAndSendTransactions(params)` **Parameters** diff --git a/packages/core/docs/guides/custom-wallets.md b/packages/core/docs/guides/custom-wallets.md index 59d27f9d2..d88da4b49 100644 --- a/packages/core/docs/guides/custom-wallets.md +++ b/packages/core/docs/guides/custom-wallets.md @@ -58,6 +58,13 @@ const MyWallet: WalletBehaviourFactory = ({ return provider.sendTransaction(signedTx); }, + async signAndSendTransactionAsync({ signerId, receiverId, actions }) { + // Sign a list of NEAR Actions before sending via an RPC endpoint. + // An RPC provider is injected to make this process easier and configured based on options.network. + + return provider.sendTransaction(signedTx); + }, + async signAndSendTransactions({ transactions }) { // Sign a list of Transactions before sending via an RPC endpoint. // An RPC provider is injected to make this process easier and configured based on options.network. @@ -144,6 +151,12 @@ Where you might have to construct NEAR Transactions and send them yourself, you > Note: Browser wallets (i.e. MyNearWallet) are unable to return the transaction outcome as they can trigger a redirect. The return type in this case is `Promise` instead of the usual `Promise`. +### `signAndSendTransactionAsync` + +This method is similar to `signAndSendTransaction` but instead returns the transaction hash as a `Uint8Array` instead of a `FinalExecutionOutcome`, allowing the users to monitor the transaction success or failure. + +> Note: Browser wallets (i.e. MyNearWallet) are unable to return the transaction outcome as they can trigger a redirect. The return type in this case is `Promise` instead of the usual `Uint8Array`. + ### `signAndSendTransactions` This method is similar to `signAndSendTransaction` but instead sends a batch of Transactions. @@ -154,3 +167,11 @@ This method is similar to `signAndSendTransaction` but instead sends a batch of This method allows users to sign a message for a specific recipient using their NEAR account. Returns the `SignedMessage` based on the [NEP413](https://github.com/near/NEPs/blob/master/neps/nep-0413.md). + +### `signTransaction` + +This method is similar to `signMessage` but instead signs and returns a `SignedTransaction` which can be broadcasted to the network. + +This method composes and signs a SignedDelegate action to be executed in a transaction. Returns the `SignedDelegateWithHash` object. + +> Note: Browser wallets (i.e. MyNearWallet) are unable to return the transaction outcome as they can trigger a redirect. The return type in this case is `Promise` instead of the usual `Promise`. \ No newline at end of file diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 38bb8d599..496e6b4b0 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -217,6 +217,11 @@ interface BaseWalletBehaviour { signAndSendTransaction( params: SignAndSendTransactionParams ): Promise; + /** + * Signs one or more NEAR Actions before sending to the network. + * The user must be signed in to call this method as there's at least charges for gas spent. + */ + signAndSendTransactionAsync(params: SignAndSendTransactionParams): Uint8Array; /** * Signs one or more transactions before sending to the network. * The user must be signed in to call this method as there's at least charges for gas spent. @@ -325,6 +330,9 @@ export type BrowserWalletBehaviour = Modify< signAndSendTransaction( params: BrowserWalletSignAndSendTransactionParams ): Promise; + signAndSendTransactionAsync( + params: BrowserWalletSignAndSendTransactionParams + ): Uint8Array | void; signAndSendTransactions( params: BrowserWalletSignAndSendTransactionsParams ): Promise;