-
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: improve
cookbook/transaction-request
(#3440)
- Loading branch information
1 parent
260274a
commit 2c60078
Showing
16 changed files
with
214 additions
and
91 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,4 @@ | ||
--- | ||
--- | ||
|
||
docs: improve `cookbook/transaction-request` |
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
26 changes: 26 additions & 0 deletions
26
apps/docs/src/guide/transactions/snippets/transaction-request/add-output-coin.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,26 @@ | ||
import { Provider, ScriptTransactionRequest, Wallet } from 'fuels'; | ||
import { TestAssetId } from 'fuels/test-utils'; | ||
|
||
import { | ||
LOCAL_NETWORK_URL, | ||
WALLET_PVT_KEY, | ||
WALLET_PVT_KEY_2, | ||
} from '../../../../env'; | ||
import { ScriptSum } from '../../../../typegend'; | ||
|
||
// #region transaction-request-3 | ||
const provider = await Provider.create(LOCAL_NETWORK_URL); | ||
|
||
const recipient1 = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
const recipient2 = Wallet.fromPrivateKey(WALLET_PVT_KEY_2, provider); | ||
|
||
const baseAssetId = provider.getBaseAssetId(); | ||
const assetA = TestAssetId.A.value; | ||
|
||
const transactionRequest = new ScriptTransactionRequest({ | ||
script: ScriptSum.bytecode, | ||
}); | ||
|
||
transactionRequest.addCoinOutput(recipient1.address, 1000, baseAssetId); | ||
transactionRequest.addCoinOutput(recipient2.address, 500, assetA); | ||
// #endregion transaction-request-3 |
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
32 changes: 14 additions & 18 deletions
32
apps/docs/src/guide/transactions/snippets/transaction-request/add-witness.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 |
---|---|---|
@@ -1,38 +1,34 @@ | ||
import type { Account } from 'fuels'; | ||
import { | ||
Provider, | ||
ScriptTransactionRequest, | ||
WalletUnlocked, | ||
ZeroBytes32, | ||
} from 'fuels'; | ||
|
||
import { LOCAL_NETWORK_URL } from '../../../../env'; | ||
import { Provider, ScriptTransactionRequest, Wallet } from 'fuels'; | ||
|
||
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../../../env'; | ||
import { ScriptSum } from '../../../../typegend'; | ||
|
||
// #region transaction-request-10 | ||
const provider = await Provider.create(LOCAL_NETWORK_URL); | ||
const witness = ZeroBytes32; | ||
const accountA = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
const accountB = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
|
||
const transactionRequest = new ScriptTransactionRequest({ | ||
script: ScriptSum.bytecode, | ||
}); | ||
|
||
// #region transaction-request-6 | ||
// Add a witness directly | ||
transactionRequest.addWitness(witness); | ||
// Add a witness signature directly | ||
const signature = await accountA.signTransaction(transactionRequest); | ||
transactionRequest.addWitness(signature); | ||
|
||
// Add a witness using an account | ||
const account: Account = WalletUnlocked.generate({ provider }); | ||
await transactionRequest.addAccountWitnesses(account); | ||
// #endregion transaction-request-6 | ||
// Or add multiple via `addAccountWitnesses` | ||
await transactionRequest.addAccountWitnesses([accountB]); | ||
// #endregion transaction-request-10 | ||
|
||
// #region transaction-request-7 | ||
// #region transaction-request-11 | ||
// Get the chain ID | ||
const chainId = provider.getChainId(); | ||
|
||
// Get the transaction ID using the Chain ID | ||
const transactionId = transactionRequest.getTransactionId(chainId); | ||
// TX ID: 0x420f6... | ||
// #endregion transaction-request-7 | ||
// #endregion transaction-request-11 | ||
|
||
console.log('transactionId', transactionId); | ||
console.log('witnesses', transactionRequest.witnesses.length === 2); |
22 changes: 22 additions & 0 deletions
22
apps/docs/src/guide/transactions/snippets/transaction-request/estimate-and-fund.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,22 @@ | ||
import { Provider, ScriptTransactionRequest, Wallet } from 'fuels'; | ||
|
||
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../../../env'; | ||
import { ScriptSum } from '../../../../typegend'; | ||
|
||
const provider = await Provider.create(LOCAL_NETWORK_URL); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
|
||
// #region transaction-request-4 | ||
const transactionRequest = new ScriptTransactionRequest({ | ||
script: ScriptSum.bytecode, | ||
}); | ||
|
||
const cost = await wallet.getTransactionCost(transactionRequest); | ||
|
||
transactionRequest.gasLimit = cost.gasUsed; | ||
transactionRequest.maxFee = cost.maxFee; | ||
|
||
await wallet.fund(transactionRequest, cost); | ||
|
||
await wallet.sendTransaction(transactionRequest); | ||
// #endregion transaction-request-4 |
22 changes: 22 additions & 0 deletions
22
apps/docs/src/guide/transactions/snippets/transaction-request/fetch-coins.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,22 @@ | ||
import { Provider, ScriptTransactionRequest, Wallet } from 'fuels'; | ||
|
||
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../../../env'; | ||
import { ScriptSum } from '../../../../typegend'; | ||
|
||
const provider = await Provider.create(LOCAL_NETWORK_URL); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
const baseAssetId = provider.getBaseAssetId(); | ||
|
||
const transactionRequest = new ScriptTransactionRequest({ | ||
script: ScriptSum.bytecode, | ||
}); | ||
|
||
// #region transaction-request-6 | ||
// Fetching coins | ||
const { coins } = await wallet.getCoins(baseAssetId); | ||
const { messages } = await wallet.getMessages(); | ||
|
||
// Adding a specific coin or message | ||
transactionRequest.addCoinInput(coins[0]); | ||
transactionRequest.addMessageInput(messages[0]); | ||
// #endregion transaction-request-6 |
37 changes: 37 additions & 0 deletions
37
apps/docs/src/guide/transactions/snippets/transaction-request/fetch-resources.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,37 @@ | ||
import type { CoinQuantity } from 'fuels'; | ||
import { bn, Provider, ScriptTransactionRequest, Wallet } from 'fuels'; | ||
import { TestAssetId } from 'fuels/test-utils'; | ||
|
||
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../../../env'; | ||
import { ScriptSum } from '../../../../typegend'; | ||
|
||
const provider = await Provider.create(LOCAL_NETWORK_URL); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
|
||
// #region transaction-request-5 | ||
// Instantiate the transaction request | ||
const transactionRequest = new ScriptTransactionRequest({ | ||
script: ScriptSum.bytecode, | ||
}); | ||
|
||
const baseAssetId = provider.getBaseAssetId(); | ||
const assetA = TestAssetId.A.value; | ||
|
||
// Define the quantities to fetch | ||
const quantities: CoinQuantity[] = [ | ||
{ | ||
amount: bn(10000), | ||
assetId: baseAssetId, | ||
}, | ||
{ | ||
amount: bn(100), | ||
assetId: assetA, | ||
}, | ||
]; | ||
|
||
// Fetching resources | ||
const resources = await wallet.getResourcesToSpend(quantities); | ||
|
||
// Adding resources (coins or messages) | ||
transactionRequest.addResources(resources); | ||
// #endregion transaction-request-5 |
59 changes: 0 additions & 59 deletions
59
apps/docs/src/guide/transactions/snippets/transaction-request/fund-request.ts
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
apps/docs/src/guide/transactions/snippets/transaction-request/input-contract.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,20 @@ | ||
import { Provider, ScriptTransactionRequest, Wallet } from 'fuels'; | ||
|
||
import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../../../env'; | ||
import { CounterFactory, ScriptSum } from '../../../../typegend'; | ||
|
||
const provider = await Provider.create(LOCAL_NETWORK_URL); | ||
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); | ||
|
||
// #region transaction-request-8 | ||
const deploy = await CounterFactory.deploy(wallet); | ||
const { contract } = await deploy.waitForResult(); | ||
|
||
const transactionRequest = new ScriptTransactionRequest({ | ||
script: ScriptSum.bytecode, | ||
scriptData: contract.id.toB256(), | ||
}); | ||
|
||
// Add the contract input and output using the contract ID | ||
transactionRequest.addContractInputAndOutput(contract.id); | ||
// #endregion transaction-request-8 |
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,7 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "script-call-contract" | ||
|
||
[dependencies] |
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,12 @@ | ||
library; | ||
|
||
abi CounterAbi { | ||
#[storage(read)] | ||
fn get_count() -> u64; | ||
|
||
#[storage(write, read)] | ||
fn increment_count(amount: u64) -> u64; | ||
|
||
#[storage(write, read)] | ||
fn decrement_count(amount: u64) -> u64; | ||
} |
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,11 @@ | ||
script; | ||
|
||
mod counter; | ||
|
||
// #region transaction-request-7 | ||
use counter::CounterAbi; | ||
fn main(contract_id: ContractId) -> u64 { | ||
let counter_contract = abi(CounterAbi, contract_id.into()); | ||
counter_contract.get_count() | ||
} | ||
// #endregion transaction-request-7 |