Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: final snippets #3397

Merged
merged 7 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/grumpy-crabs-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 0 additions & 2 deletions apps/demo-fuels/fuels.config.full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
},
Expand Down
40 changes: 40 additions & 0 deletions apps/docs-snippets2/src/scripts/initialising-scripts.ts
Original file line number Diff line number Diff line change
@@ -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);
20 changes: 20 additions & 0 deletions apps/docs-snippets2/src/scripts/script-with-main-args.ts
Original file line number Diff line number Diff line change
@@ -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]));
2 changes: 2 additions & 0 deletions apps/docs-snippets2/sway/Forc.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"bytecode-input",
"call-test-script",
"configurable-pin",
"counter",
"echo-asset-id",
Expand All @@ -26,6 +27,7 @@ members = [
"script-signing",
"script-sum",
"script-transfer-to-contract",
"script-main-args",
"simple-predicate",
"simple-token",
"simple-token-abi",
Expand Down
6 changes: 6 additions & 0 deletions apps/docs-snippets2/sway/call-test-script/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
license = "Apache-2.0"
name = "call-test-script"

[dependencies]
27 changes: 27 additions & 0 deletions apps/docs-snippets2/sway/call-test-script/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
script;

fn log<T>(v: T) {
asm(r1: v) {
log r1 zero zero zero;
}
}

fn logd<T>(v: T) {
asm(r1: v, r2: __size_of::<T>()) {
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,
}
}
7 changes: 7 additions & 0 deletions apps/docs-snippets2/sway/script-main-args/Forc.toml
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-main-args"

[dependencies]
11 changes: 11 additions & 0 deletions apps/docs-snippets2/sway/script-main-args/src/main.sw
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion apps/docs/src/guide/scripts/instantiating-a-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

Similar to contracts and predicates, once you've written a script in Sway and compiled it with `forc build` (read <a :href="url" target="_blank" rel="noreferrer">here</a> 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.
4 changes: 2 additions & 2 deletions apps/docs/src/guide/scripts/running-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
5 changes: 1 addition & 4 deletions packages/account/src/wallet/wallet-unlocked.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,14 @@ 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();
const verifiedAddress = Signer.recoverAddress(
SCRIPT_TX_REQUEST.getTransactionId(chainId),
signedTransaction
);
// #endregion wallet-transaction-signing

expect(signedTransaction).toEqual(SIGNED_TX);
expect(verifiedAddress).toEqual(wallet.address);
});
Expand Down
2 changes: 0 additions & 2 deletions packages/fuel-gauge/src/script-main-args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('Script Coverage', () => {
wallets: [wallet],
} = launched;

// #region script-call-factory
const foo = 33;
const scriptInstance = new Script<BigNumberish[], BigNumberish>(
ScriptMainArgs.bytecode,
Expand All @@ -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]);
Expand Down
5 changes: 0 additions & 5 deletions packages/script/src/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ const callScript = async <TData, TResult>(
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;
Expand Down Expand Up @@ -86,7 +82,6 @@ describe('Script', () => {
}
);
});
// #endregion script-init

it('can call a script', async () => {
using launched = await setupTestProviderAndWallets();
Expand Down