Skip to content

Commit

Permalink
Add signAndSendTransactionAsync interface
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsonevv committed Nov 19, 2024
1 parent e5aa0c5 commit 3f94ddf
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/docs/api/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions packages/core/docs/api/wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Action>`): 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**
Expand Down
21 changes: 21 additions & 0 deletions packages/core/docs/guides/custom-wallets.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ const MyWallet: WalletBehaviourFactory<BrowserWallet> = ({
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.
Expand Down Expand Up @@ -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<void>` instead of the usual `Promise<FinalExecutionOutcome>`.
### `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<void>` instead of the usual `Uint8Array`.
### `signAndSendTransactions`

This method is similar to `signAndSendTransaction` but instead sends a batch of Transactions.
Expand All @@ -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<void>` instead of the usual `Promise<SignedDelegateWithHash>`.
8 changes: 8 additions & 0 deletions packages/core/src/lib/wallet/wallet.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ interface BaseWalletBehaviour {
signAndSendTransaction(
params: SignAndSendTransactionParams
): Promise<providers.FinalExecutionOutcome>;
/**
* 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.
Expand Down Expand Up @@ -325,6 +330,9 @@ export type BrowserWalletBehaviour = Modify<
signAndSendTransaction(
params: BrowserWalletSignAndSendTransactionParams
): Promise<FinalExecutionOutcome | void>;
signAndSendTransactionAsync(
params: BrowserWalletSignAndSendTransactionParams
): Uint8Array | void;
signAndSendTransactions(
params: BrowserWalletSignAndSendTransactionsParams
): Promise<void>;
Expand Down

0 comments on commit 3f94ddf

Please sign in to comment.