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:
// #import { ScriptRequest, arrayify };
const scriptBin = readFileSync(join(__dirname, './path/to/script-binary.bin'));
type MyStruct = {
arg_one: boolean;
arg_two: BigNumberish;
};
/**
* @group node
*/
describe('Script', () => {
let scriptRequest: ScriptRequest<MyStruct, MyStruct>;
beforeAll(() => {
const abiInterface = new Interface(scriptJsonAbi);
scriptRequest = new ScriptRequest(
scriptBin,
(myStruct: MyStruct) => {
const encoded = abiInterface.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 decoded = abiInterface.functions.main.decodeOutput(scriptResult.returnReceipt.data);
return (decoded as any)[0];
}
);
});
In the next section , we show how to run a script.