Skip to content

Commit

Permalink
docs: migrated script-init
Browse files Browse the repository at this point in the history
  • Loading branch information
petertonysmith94 committed Nov 13, 2024
1 parent 7fbb526 commit 2a0be14
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
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/src/snippets/scripts/initialising-scripts.ts#script-init{ts:line-numbers}

In the [next section](./running-scripts.md), we show how to run a script.
40 changes: 40 additions & 0 deletions apps/docs/src/snippets/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);
1 change: 1 addition & 0 deletions apps/docs/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 Down
6 changes: 6 additions & 0 deletions apps/docs/sway/call-test-script/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "call-test-script"

[dependencies]
27 changes: 27 additions & 0 deletions apps/docs/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,
}
}
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

0 comments on commit 2a0be14

Please sign in to comment.