-
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.
docs!: optimizing frontend apps (#3573)
* chore: update tx page titles * docs: optimizing FE apps * docs: add final comment * Revert "feat: implement `sendAndAwaitStatus` subscription (#3541)" This reverts commit 40d9960. * feat: `autoCost` -> `estimateAndFund` * chore: fix docs links * feat: autoCost -> fundWithRequiredCoins * docs: add react advanced example * docs: actually push the advanced example * chore: lint * chore: changeset * docs: use other wallet gen in react example * chore: changeset is breaking * chore: fix test * chore: lint * chore: update react example Co-authored-by: Peter Smith <[email protected]> * chore: remove dust Co-authored-by: Peter Smith <[email protected]> * docs: optimizing frontend apps touchups (#3574) * Adding linebreaks and touchups * Overall page touichups * Update apps/docs/src/guide/transactions/snippets/transaction-speed/transaction-speed-optimized.ts Co-authored-by: Peter Smith <[email protected]> --------- Co-authored-by: Peter Smith <[email protected]> * chore: lintfix * chore: remove deploy from docs dev command * docs: advanced react example and language updats * chore: reintroduce deploy flag in docs dev command * docs: add link file endings * docs: fix relative links --------- Co-authored-by: Peter Smith <[email protected]> Co-authored-by: Anderson Arboleya <[email protected]>
- Loading branch information
1 parent
f293646
commit 13977df
Showing
51 changed files
with
299 additions
and
206 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,7 @@ | ||
--- | ||
"@fuel-ts/account": minor | ||
"@fuel-ts/program": minor | ||
"@fuel-ts/script": patch | ||
--- | ||
|
||
docs!: optimizing frontend apps |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -343,7 +343,4 @@ Workspaces | |
WSL | ||
XOR | ||
XORs | ||
YAML | ||
TransactionRequest | ||
TransactionResponse | ||
frictionless | ||
YAML |
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
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,69 @@ | ||
# Optimized React Example | ||
|
||
This example implements the strategies outlined in [Optimizing Frontend Apps](../transactions/optimizing-frontend-apps.md) and demonstrates how to improve the perceived speed of transactions in a React application. | ||
|
||
```tsx | ||
import { Provider, Wallet, ScriptTransactionRequest } from "fuels"; | ||
import { useEffect, useState } from "react"; | ||
|
||
import { TestContract } from "./typegend"; | ||
import contractIds from "./typegend/contract-ids.json"; | ||
|
||
function App() { | ||
const [request, setRequest] = useState<ScriptTransactionRequest | null>(null); | ||
|
||
// Initialize the provider and wallet | ||
const NETWORK_URL = "https://mainnet.fuel.network/v1/graphql"; | ||
const provider = new Provider(NETWORK_URL); | ||
const wallet = Wallet.fromAddress("0x...", provider); | ||
|
||
/** | ||
* Here we'll prepare our transaction upfront on page load, so that | ||
* by the time the user interacts with your app (i.e. clicking a btn), | ||
* the transaction is ready to be submitted | ||
*/ | ||
useEffect(() => { | ||
const onPageLoad = async () => { | ||
// 1. Connect to the contract | ||
const contractInstance = new TestContract( | ||
contractIds.testContract, | ||
wallet, | ||
); | ||
|
||
// 2. Invoke the contract function whilst estimating and funding the | ||
// call, which gives us the transaction request | ||
const preparedRequest = await contractInstance.functions | ||
.increment_counter(1) | ||
.fundWithRequiredCoins(); | ||
|
||
setRequest(preparedRequest); | ||
}; | ||
|
||
onPageLoad(); | ||
}, []); | ||
|
||
/** | ||
* By the time user user clicks the submit button, we only need to | ||
* submit the transaction to the network | ||
*/ | ||
const handleSubmit = async () => { | ||
if (!request) return; | ||
|
||
// 1. Submit the transaction to the network | ||
const response = await wallet.sendTransaction(request); | ||
|
||
// 2. Wait for the transaction to settle and get the result | ||
const result = await response.waitForResult(); | ||
|
||
console.log("result", result); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button onClick={handleSubmit}>Submit</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default App; | ||
``` |
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
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
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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Preparing a Script Transaction | ||
|
||
Akin to Contracts, we can configure the [call parameters](../contracts/call-parameters.md) and [transaction parameters](../transactions/transaction-parameters.md) for Scripts, as well as retrieve the entire transaction request or transaction ID prior to submission. | ||
Akin to Contracts, we can configure the [call parameters](../contracts/call-parameters.md) and [transaction parameters](../transactions/adding-parameters.md) for Scripts, as well as retrieve the entire transaction request or transaction ID prior to submission. | ||
|
||
<<< @./snippets/script-with-configurable.ts#preparing-scripts{ts:line-numbers} |
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
4 changes: 2 additions & 2 deletions
4
...de/transactions/transaction-parameters.md → ...c/guide/transactions/adding-parameters.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
4 changes: 2 additions & 2 deletions
4
...uide/transactions/transaction-policies.md → ...src/guide/transactions/adding-policies.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
File renamed without changes.
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
39 changes: 39 additions & 0 deletions
39
apps/docs/src/guide/transactions/optimizing-frontend-apps.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,39 @@ | ||
# Optimizing Frontend Apps | ||
|
||
Your application must perform a series of operations to estimate, submit and receive the result of a transaction. However, the flow in which it performs these actions can be organized or performed optimistically, increasing it's perceived speed. | ||
|
||
## Use Case | ||
|
||
In a frontend application, imagine we have a button that executes a contract call: | ||
|
||
```tsx | ||
<Button onClick={handleSubmit}>Submit</Button> | ||
``` | ||
|
||
The handler would be implemented as follows: | ||
|
||
<<< @./snippets/transaction-speed/transaction-speed-init.ts#main{ts:line-numbers} | ||
|
||
Once the user clicks the button, multiple sequential calls are made to the network, which can take a while because the transaction must be: | ||
|
||
1. Estimated | ||
1. Funded | ||
1. Submitted | ||
|
||
## Optimization Strategy | ||
|
||
With a few optimizations, the flow can be organized as follows: | ||
|
||
<<< @./snippets/transaction-speed/transaction-speed-optimized.ts#main{ts:line-numbers} | ||
|
||
## Conclusion | ||
|
||
Finally, when users click the button, they only need to submit the transaction, which vastly improves the perceived speed of the transaction because many of the necessary requests were done upfront, under the hood. | ||
|
||
Just remember: | ||
|
||
- _After preparation, any changes made to the transaction request will require it to be re-estimated and re-funded before it can be signed and submitted._ | ||
|
||
# See Also | ||
|
||
- Check a full example at [Optimized React Example](../cookbook/optimized-react-example.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
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
Oops, something went wrong.