How do you instantiate or interact with a Sway Script?

How do you instantiate or interact with a Sway Script?

There seems to be 2 ways:

  1. Fuel TS SDK | Fuel Docs
  2. Fuel TS SDK | Fuel Docs

While for 2) how do you instantiate and interact with the script? In the docs it states to use callScript and ScriptHelper, but not much documentation or usage for both libraries.:

const foo = 33;
const scriptInstance = new Script<BigNumberish[], BigNumberish>(
  ScriptMainArgs.bytecode,
  ScriptMainArgs.abi,
  wallet
);
 
const { waitForResult } = await scriptInstance.functions.main(foo).call();
 
const { value, logs } = await waitForResult();

Hey @eesheng :wave:

Have you tried out create fuels yet.

It has and example of how to instantiate a script and might help you get up to speed quickly.

Please let me know if this doesn’t suffice and we can assist further.

1 Like

Hi @p.s, within the scripts folder src/sway-api/scripts/TestScript.ts

  1. How can we use const scriptBin = readFileSync(join(__dirname, './path/to/script-binary.bin')); to read the bytecode? I noticed that readFileSync seems to be depreciated.

  2. Is there a way to get the ABI by file without copy and pasting the whole json?

  3. Why the ABI generated so different from mine:

{
  "encoding": "1",
  "types": [
    {
      "typeId": 0,
      "type": "()",
      "components": [],
      "typeParameters": null
    },

Generated in Example:

const abi = {
  "programType": "script",
  "specVersion": "1",
  "encodingVersion": "1",
  "concreteTypes": [
    {
      "type": "u64",
      "concreteTypeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0"
    }
  ],
  "metadataTypes": [],
  "functions": [
    {
      "inputs": [
        {
          "name": "input",
          "concreteTypeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0"
        }
      ],
      "name": "main",
      "output": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0",
      "attributes": null
    }
  ],
  "loggedTypes": [],
  "messagesTypes": [],
  "configurables": []
};

Hi friends, for anyone who faced the following problems, it is because we need to interact with generated types. For those working with NextJS, do ensure u checkout this thread to fix your issues.

Update: Doesn’t Work for Vanilla NextJS (I.e. didn’t run the command pnpm create fuels)

1 Like

The issue will be tracked here: BUG on pnpm fuels typegen for NextJS / npm install - #3 by eesheng

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.