diff --git a/.changeset/grumpy-crabs-exist.md b/.changeset/grumpy-crabs-exist.md new file mode 100644 index 00000000000..a845151cc84 --- /dev/null +++ b/.changeset/grumpy-crabs-exist.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/apps/demo-fuels/fuels.config.full.ts b/apps/demo-fuels/fuels.config.full.ts index 92c7cf944ed..13898554404 100644 --- a/apps/demo-fuels/fuels.config.full.ts +++ b/apps/demo-fuels/fuels.config.full.ts @@ -91,8 +91,6 @@ export default createConfig({ // #endregion onBuild // #region onDeploy - // #import { DeployedData, FuelsConfig }; - onDeploy: (config: FuelsConfig, data: DeployedData) => { console.log('fuels:onDeploy', { config, data }); }, diff --git a/apps/docs-snippets2/src/scripts/initialising-scripts.ts b/apps/docs-snippets2/src/scripts/initialising-scripts.ts new file mode 100644 index 00000000000..97befd1800d --- /dev/null +++ b/apps/docs-snippets2/src/scripts/initialising-scripts.ts @@ -0,0 +1,40 @@ +// #region script-init +import type { BigNumberish } from 'fuels'; +import { arrayify, Provider, ReceiptType, ScriptRequest, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { CallTestScript } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const script = new CallTestScript(wallet); + +type MyStruct = { + arg_one: boolean; + arg_two: BigNumberish; +}; + +const scriptRequest = new ScriptRequest( + CallTestScript.bytecode, + (myStruct: MyStruct) => { + const encoded = script.interface.functions.main.encodeArguments([myStruct]); + + return arrayify(encoded); + }, + (scriptResult) => { + if (scriptResult.returnReceipt.type === ReceiptType.Revert) { + throw new Error('Reverted'); + } + if (scriptResult.returnReceipt.type !== ReceiptType.ReturnData) { + throw new Error('fail'); + } + + const [decodedResult] = script.interface.functions.main.decodeOutput( + scriptResult.returnReceipt.data + ); + return decodedResult; + } +); +// #endregion script-init + +console.log('Script request should be defined', scriptRequest); diff --git a/apps/docs-snippets2/src/scripts/script-with-main-args.ts b/apps/docs-snippets2/src/scripts/script-with-main-args.ts new file mode 100644 index 00000000000..5096d50f47d --- /dev/null +++ b/apps/docs-snippets2/src/scripts/script-with-main-args.ts @@ -0,0 +1,20 @@ +// #region full +import { bn, Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { ScriptMainArgs } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const foo = 3; + +const scriptInstance = new ScriptMainArgs(wallet); + +const { waitForResult } = await scriptInstance.functions.main(foo).call(); + +const { value, logs } = await waitForResult(); +// #endregion full + +console.log('value', value?.toString() === bn(foo).toString()); +console.log('logs', JSON.stringify(logs) === JSON.stringify(['u8 foo', 3])); diff --git a/apps/docs-snippets2/sway/Forc.toml b/apps/docs-snippets2/sway/Forc.toml index a8a0b44e2ca..1d01417c0c3 100644 --- a/apps/docs-snippets2/sway/Forc.toml +++ b/apps/docs-snippets2/sway/Forc.toml @@ -1,6 +1,7 @@ [workspace] members = [ "bytecode-input", + "call-test-script", "configurable-pin", "counter", "echo-asset-id", @@ -26,6 +27,7 @@ members = [ "script-signing", "script-sum", "script-transfer-to-contract", + "script-main-args", "simple-predicate", "simple-token", "simple-token-abi", diff --git a/apps/docs-snippets2/sway/call-test-script/Forc.toml b/apps/docs-snippets2/sway/call-test-script/Forc.toml new file mode 100644 index 00000000000..43314a18ef3 --- /dev/null +++ b/apps/docs-snippets2/sway/call-test-script/Forc.toml @@ -0,0 +1,6 @@ +[project] +authors = ["Fuel Labs "] +license = "Apache-2.0" +name = "call-test-script" + +[dependencies] diff --git a/apps/docs-snippets2/sway/call-test-script/src/main.sw b/apps/docs-snippets2/sway/call-test-script/src/main.sw new file mode 100644 index 00000000000..421880956b4 --- /dev/null +++ b/apps/docs-snippets2/sway/call-test-script/src/main.sw @@ -0,0 +1,27 @@ +script; + +fn log(v: T) { + asm(r1: v) { + log r1 zero zero zero; + } +} + +fn logd(v: T) { + asm(r1: v, r2: __size_of::()) { + logd zero zero r1 r2; + } +} + +struct MyStruct { + arg_one: bool, + arg_two: u64, +} + +fn main(my_struct: MyStruct) -> MyStruct { + log(my_struct.arg_one); + log(my_struct.arg_two); + MyStruct { + arg_one: my_struct.arg_one, + arg_two: my_struct.arg_two, + } +} diff --git a/apps/docs-snippets2/sway/script-main-args/Forc.toml b/apps/docs-snippets2/sway/script-main-args/Forc.toml new file mode 100644 index 00000000000..7b8c60d4b30 --- /dev/null +++ b/apps/docs-snippets2/sway/script-main-args/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "script-main-args" + +[dependencies] diff --git a/apps/docs-snippets2/sway/script-main-args/src/main.sw b/apps/docs-snippets2/sway/script-main-args/src/main.sw new file mode 100644 index 00000000000..8cc6a229c79 --- /dev/null +++ b/apps/docs-snippets2/sway/script-main-args/src/main.sw @@ -0,0 +1,11 @@ +// #region script-with-main-args +script; + +use std::logging::log; + +fn main(foo: u8) -> u8 { + log(__to_str_array("u8 foo")); + log(foo); + foo +} +// #endregion script-with-main-args diff --git a/apps/docs/src/guide/scripts/instantiating-a-script.md b/apps/docs/src/guide/scripts/instantiating-a-script.md index 9f1e304579a..eb0bb9a59a2 100644 --- a/apps/docs/src/guide/scripts/instantiating-a-script.md +++ b/apps/docs/src/guide/scripts/instantiating-a-script.md @@ -10,6 +10,6 @@ Similar to contracts and predicates, once you've written a script in Sway and compiled it with `forc build` (read here for more on how to work with Sway), you'll get the script binary. Using the binary, you can instantiate a `script` as shown in the code snippet below: -<<< @/../../../packages/script/src/script.test.ts#script-init{ts:line-numbers} +<<< @/../../docs-snippets2/src/scripts/initialising-scripts.ts#script-init{ts:line-numbers} In the [next section](./running-scripts.md), we show how to run a script. diff --git a/apps/docs/src/guide/scripts/running-scripts.md b/apps/docs/src/guide/scripts/running-scripts.md index 48e0f9b7674..2918ec26412 100644 --- a/apps/docs/src/guide/scripts/running-scripts.md +++ b/apps/docs/src/guide/scripts/running-scripts.md @@ -2,8 +2,8 @@ Suppose your Sway script `main` function is written using the arguments passed to the `main` function like so: -<<< @/../../../packages/fuel-gauge/test/fixtures/forc-projects/script-main-args/src/main.sw#script-with-main-args{rust:line-numbers} +<<< @/../../../apps/docs-snippets2/sway/script-main-args/src/main.sw#script-with-main-args{rust:line-numbers} You can still hand code out a solution wrapper using `callScript` utility to call your script with data. However, if you prefer to use the ABI generated from your script, you can use the `ScriptFactory` helper: -<<< @/../../../packages/fuel-gauge/src/script-main-args.test.ts#script-call-factory{ts:line-numbers} +<<< @/../../../apps/docs-snippets2/src/scripts/script-with-main-args.ts#full{ts:line-numbers} diff --git a/packages/account/src/wallet/wallet-unlocked.test.ts b/packages/account/src/wallet/wallet-unlocked.test.ts index e77f07c101e..b21576af0da 100644 --- a/packages/account/src/wallet/wallet-unlocked.test.ts +++ b/packages/account/src/wallet/wallet-unlocked.test.ts @@ -54,9 +54,6 @@ describe('WalletUnlocked', () => { using launched = await setupTestProviderAndWallets(); const { provider } = launched; - // #region wallet-transaction-signing - // #import { Provider, Wallet, Signer }; - const wallet = Wallet.fromPrivateKey(PRIVATE_KEY, provider); const signedTransaction = await wallet.signTransaction(SCRIPT_TX_REQUEST); const chainId = wallet.provider.getChainId(); @@ -64,7 +61,7 @@ describe('WalletUnlocked', () => { SCRIPT_TX_REQUEST.getTransactionId(chainId), signedTransaction ); - // #endregion wallet-transaction-signing + expect(signedTransaction).toEqual(SIGNED_TX); expect(verifiedAddress).toEqual(wallet.address); }); diff --git a/packages/fuel-gauge/src/script-main-args.test.ts b/packages/fuel-gauge/src/script-main-args.test.ts index c74673262c7..8395e0fb66f 100644 --- a/packages/fuel-gauge/src/script-main-args.test.ts +++ b/packages/fuel-gauge/src/script-main-args.test.ts @@ -19,7 +19,6 @@ describe('Script Coverage', () => { wallets: [wallet], } = launched; - // #region script-call-factory const foo = 33; const scriptInstance = new Script( ScriptMainArgs.bytecode, @@ -30,7 +29,6 @@ describe('Script Coverage', () => { const { waitForResult } = await scriptInstance.functions.main(foo).call(); const { value, logs } = await waitForResult(); - // #endregion script-call-factory expect(value?.toString()).toEqual(bn(foo).toString()); expect(logs).toEqual(['u8 foo', 33]); diff --git a/packages/script/src/script.test.ts b/packages/script/src/script.test.ts index fb4b1601621..069c02c0429 100644 --- a/packages/script/src/script.test.ts +++ b/packages/script/src/script.test.ts @@ -50,10 +50,6 @@ const callScript = async ( return { transactionResult, result, response }; }; -// #region script-init -// #import { ScriptRequest, arrayify }; -// #context const scriptBin = readFileSync(join(__dirname, './path/to/script-binary.bin')); - type MyStruct = { arg_one: boolean; arg_two: BigNumberish; @@ -86,7 +82,6 @@ describe('Script', () => { } ); }); - // #endregion script-init it('can call a script', async () => { using launched = await setupTestProviderAndWallets();