-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
sendAndAwaitStatus
subscription (#3541)
* chore: rebuild * chore: rebuild * feat: implement `sendTransactionAndAwaitStatus` * chore: alter snippet region * chore: changeset * chore: add spellcheck work * chore: add spellcheck work * chore: changeset * Update apps/docs/src/guide/transactions/snippets/transaction-subscriptions/contract-call.ts * Update apps/docs/src/guide/transactions/snippets/transaction-subscriptions/transaction-request.ts * Update apps/docs/src/guide/transactions/transaction-subscriptions.md * Update apps/docs/src/guide/transactions/transaction-subscriptions.md * Update packages/account/src/providers/provider.ts * docs: improvement Co-authored-by: Nedim Salkić <[email protected]> * test: remove node args * feat: use waitForResult * chore: remove test on;y * docs: update docs and tests * chore: update spellcheck * chore: lint * chore: rebuild * chore: remove redundant method * chore: code style Co-authored-by: Nedim Salkić <[email protected]> * chore: update docs * Revert "chore: code style" This reverts commit fc5ce61. * chore: cleaner doc Co-authored-by: Nedim Salkić <[email protected]> * chor: update test * chore: update and fix tests * chore: lint --------- Co-authored-by: Nedim Salkić <[email protected]> Co-authored-by: Anderson Arboleya <[email protected]> Co-authored-by: Peter Smith <[email protected]>
- Loading branch information
1 parent
75fae34
commit 40d9960
Showing
8 changed files
with
170 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fuel-ts/account": patch | ||
--- | ||
|
||
feat: implement `sendAndAwaitStatus` subscription |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -344,4 +344,7 @@ Workspaces | |
WSL | ||
XOR | ||
XORs | ||
YAML | ||
YAML | ||
TransactionRequest | ||
TransactionResponse | ||
frictionless |
31 changes: 31 additions & 0 deletions
31
apps/docs/src/guide/transactions/snippets/transaction-subscriptions/contract-call.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Provider, Wallet } from 'fuels'; | ||
|
||
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../../../env'; | ||
import { CounterFactory } from '../../../../typegend'; | ||
|
||
const provider = new Provider(LOCAL_NETWORK_URL); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
|
||
const deploy = await CounterFactory.deploy(wallet); | ||
const { contract } = await deploy.waitForResult(); | ||
|
||
// #region main | ||
// Create a new transaction request from a contract call | ||
const txRequest = await contract.functions | ||
.increment_count(1) | ||
.getTransactionRequest(); | ||
|
||
// Fund the transaction | ||
await txRequest.autoCost(wallet); | ||
|
||
// Sign the transaction | ||
const txSignature = await wallet.signTransaction(txRequest); | ||
txRequest.updateWitnessByOwner(wallet.address, txSignature); | ||
|
||
// Send the transaction and await it's result via the opened subscription | ||
const result = await provider.sendTransactionAndAwaitStatus(txRequest); | ||
// #endregion main | ||
|
||
console.log('transactionId', result.id); | ||
console.log('status', result.status); | ||
console.log('receipts', result.receipts); |
31 changes: 31 additions & 0 deletions
31
apps/docs/src/guide/transactions/snippets/transaction-subscriptions/transaction-request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Provider, ScriptTransactionRequest, Wallet } from 'fuels'; | ||
|
||
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../../../env'; | ||
|
||
const provider = new Provider(LOCAL_NETWORK_URL); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
const recipient = Wallet.generate(); | ||
|
||
// #region main | ||
// Create a new transaction request | ||
const txRequest = new ScriptTransactionRequest(); | ||
txRequest.addCoinOutput( | ||
recipient.address, | ||
1_000, | ||
await provider.getBaseAssetId() | ||
); | ||
|
||
// Fund the transaction | ||
await txRequest.autoCost(wallet); | ||
|
||
// Sign the transaction | ||
const txSignature = await wallet.signTransaction(txRequest); | ||
txRequest.updateWitnessByOwner(wallet.address, txSignature); | ||
|
||
// Send the transaction and await it's result via the opened subscription | ||
const result = await provider.sendTransactionAndAwaitStatus(txRequest); | ||
// #endregion main | ||
|
||
console.log('transactionId', result.id); | ||
console.log('status', result.status); | ||
console.log('receipts', result.receipts); |
16 changes: 16 additions & 0 deletions
16
apps/docs/src/guide/transactions/transaction-subscriptions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Transaction Subscriptions | ||
|
||
When submitting transactions via the SDK, usually this is done by calling a method in a [script](../scripts/running-scripts.md) or [contract](../contracts/methods.md#call), or sending a transaction via a [predicate](../predicates/methods.md#sendtransaction) or [wallet](../wallets/index.md). These methods submit the transaction and then return a [TransactionResponse](./transaction-response.md) that allows you to view the result of a transaction at your convenience, leaving the rest of your app processing unblocked. | ||
|
||
However, if you want to send a transaction and wait until it's processed, for convenience the SDK also exposes the `sendTransactionAndAwaitStatus` available on a [Provider](../provider/index.md) which behaves the same as `sendTransaction` but waits until the transaction is processed by the node and then returns the transaction result. | ||
|
||
This functionality can be used like so: | ||
|
||
<<< @./snippets/transaction-subscriptions/transaction-request.ts#main{ts:line-numbers} | ||
|
||
Or used with a contract call like so: | ||
|
||
<<< @./snippets/transaction-subscriptions/contract-call.ts#main{ts:line-numbers} | ||
|
||
> [!NOTE] Note | ||
> This is a blocking call, which means the rest of your app logic could be blocked for several seconds until the transaction is processed. If this is a problem for your app then we recommend using the previous submission methods mentioned in this guide. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters