From cf36fa3ad643a061ab372d3357e23ff9465cf7ca Mon Sep 17 00:00:00 2001 From: Dhaiwat Date: Sat, 4 Nov 2023 03:44:50 +0530 Subject: [PATCH] docs: purge hardcoded snippets on 'using typegen' page --- .changeset/odd-kids-buy.md | 2 + apps/demo-typegen/package.json | 10 ++- apps/demo-typegen/predicate/.gitignore | 2 + apps/demo-typegen/predicate/Forc.toml | 7 ++ apps/demo-typegen/predicate/src/main.sw | 7 ++ apps/demo-typegen/script/.gitignore | 2 + apps/demo-typegen/script/Forc.toml | 7 ++ apps/demo-typegen/script/src/main.sw | 5 ++ .../src/contract-types/DemoContractAbi.d.ts | 38 +++++++++ .../src/contract-types/DemoContractAbi.hex.ts | 12 +++ .../factories/DemoContractAbi__factory.ts | 82 +++++++++++++++++++ apps/demo-typegen/src/contract-types/index.ts | 14 ++++ apps/demo-typegen/src/demo.test.ts | 77 ++++++++++++++++- .../src/predicate-types/PredicateAbi.d.ts | 36 ++++++++ .../src/predicate-types/PredicateAbi.hex.ts | 12 +++ .../factories/PredicateAbi__factory.ts | 78 ++++++++++++++++++ .../demo-typegen/src/predicate-types/index.ts | 12 +++ .../src/script-types/ScriptAbi.d.ts | 36 ++++++++ .../src/script-types/ScriptAbi.hex.ts | 12 +++ .../factories/ScriptAbi__factory.ts | 67 +++++++++++++++ apps/demo-typegen/src/script-types/index.ts | 12 +++ .../abi-typegen/using-generated-types.md | 81 ++---------------- 22 files changed, 531 insertions(+), 80 deletions(-) create mode 100644 .changeset/odd-kids-buy.md create mode 100644 apps/demo-typegen/predicate/.gitignore create mode 100644 apps/demo-typegen/predicate/Forc.toml create mode 100644 apps/demo-typegen/predicate/src/main.sw create mode 100644 apps/demo-typegen/script/.gitignore create mode 100644 apps/demo-typegen/script/Forc.toml create mode 100644 apps/demo-typegen/script/src/main.sw create mode 100644 apps/demo-typegen/src/contract-types/DemoContractAbi.d.ts create mode 100644 apps/demo-typegen/src/contract-types/DemoContractAbi.hex.ts create mode 100644 apps/demo-typegen/src/contract-types/factories/DemoContractAbi__factory.ts create mode 100644 apps/demo-typegen/src/contract-types/index.ts create mode 100644 apps/demo-typegen/src/predicate-types/PredicateAbi.d.ts create mode 100644 apps/demo-typegen/src/predicate-types/PredicateAbi.hex.ts create mode 100644 apps/demo-typegen/src/predicate-types/factories/PredicateAbi__factory.ts create mode 100644 apps/demo-typegen/src/predicate-types/index.ts create mode 100644 apps/demo-typegen/src/script-types/ScriptAbi.d.ts create mode 100644 apps/demo-typegen/src/script-types/ScriptAbi.hex.ts create mode 100644 apps/demo-typegen/src/script-types/factories/ScriptAbi__factory.ts create mode 100644 apps/demo-typegen/src/script-types/index.ts diff --git a/.changeset/odd-kids-buy.md b/.changeset/odd-kids-buy.md new file mode 100644 index 00000000000..a845151cc84 --- /dev/null +++ b/.changeset/odd-kids-buy.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/apps/demo-typegen/package.json b/apps/demo-typegen/package.json index cc347f406b1..75231eacb1c 100644 --- a/apps/demo-typegen/package.json +++ b/apps/demo-typegen/package.json @@ -5,8 +5,14 @@ "author": "Fuel Labs (https://fuel.network/)", "scripts": { "pretest": "run-s build:forc build:types", - "build:forc": "pnpm fuels-forc build -p contract", - "build:types": "pnpm fuels typegen -i contract/out/debug/demo-contract-abi.json -o src/generated-types" + "build:forc": "pnpm forc:contract && pnpm forc:script && pnpm forc:predicate", + "forc:contract": "pnpm fuels-forc build -p contract", + "forc:script": "pnpm fuels-forc build -p script", + "forc:predicate": "pnpm fuels-forc build -p predicate", + "build:types": "pnpm types:contract && pnpm types:script && pnpm types:predicate", + "types:contract": "pnpm fuels typegen -i contract/out/debug/demo-contract-abi.json -o src/contract-types", + "types:script": "pnpm fuels typegen -i script/out/debug/script-abi.json -o src/script-types --script", + "types:predicate": "pnpm fuels typegen -i predicate/out/debug/predicate-abi.json -o src/predicate-types --predicate" }, "license": "Apache-2.0", "dependencies": { diff --git a/apps/demo-typegen/predicate/.gitignore b/apps/demo-typegen/predicate/.gitignore new file mode 100644 index 00000000000..77d3844f58c --- /dev/null +++ b/apps/demo-typegen/predicate/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/apps/demo-typegen/predicate/Forc.toml b/apps/demo-typegen/predicate/Forc.toml new file mode 100644 index 00000000000..2cd7d9544ee --- /dev/null +++ b/apps/demo-typegen/predicate/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Dhaiwat Pandya"] +entry = "main.sw" +license = "Apache-2.0" +name = "predicate" + +[dependencies] diff --git a/apps/demo-typegen/predicate/src/main.sw b/apps/demo-typegen/predicate/src/main.sw new file mode 100644 index 00000000000..689896544bd --- /dev/null +++ b/apps/demo-typegen/predicate/src/main.sw @@ -0,0 +1,7 @@ +predicate; + +fn main(input: u8) -> bool { + let answer: u8 = 10; + + input == answer +} \ No newline at end of file diff --git a/apps/demo-typegen/script/.gitignore b/apps/demo-typegen/script/.gitignore new file mode 100644 index 00000000000..77d3844f58c --- /dev/null +++ b/apps/demo-typegen/script/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/apps/demo-typegen/script/Forc.toml b/apps/demo-typegen/script/Forc.toml new file mode 100644 index 00000000000..f6098806485 --- /dev/null +++ b/apps/demo-typegen/script/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Dhaiwat Pandya"] +entry = "main.sw" +license = "Apache-2.0" +name = "script" + +[dependencies] diff --git a/apps/demo-typegen/script/src/main.sw b/apps/demo-typegen/script/src/main.sw new file mode 100644 index 00000000000..93cb6bc581d --- /dev/null +++ b/apps/demo-typegen/script/src/main.sw @@ -0,0 +1,5 @@ +script; + +fn main() -> u8 { + 10 +} diff --git a/apps/demo-typegen/src/contract-types/DemoContractAbi.d.ts b/apps/demo-typegen/src/contract-types/DemoContractAbi.d.ts new file mode 100644 index 00000000000..9e04e668aaf --- /dev/null +++ b/apps/demo-typegen/src/contract-types/DemoContractAbi.d.ts @@ -0,0 +1,38 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +import type { + BigNumberish, + BN, + BytesLike, + Contract, + DecodedValue, + FunctionFragment, + Interface, + InvokeFunction, +} from 'fuels'; + +interface DemoContractAbiInterface extends Interface { + functions: { + return_input: FunctionFragment; + }; + + encodeFunctionData(functionFragment: 'return_input', values: [BigNumberish]): Uint8Array; + + decodeFunctionData(functionFragment: 'return_input', data: BytesLike): DecodedValue; +} + +export class DemoContractAbi extends Contract { + interface: DemoContractAbiInterface; + functions: { + return_input: InvokeFunction<[input: BigNumberish], BN>; + }; +} diff --git a/apps/demo-typegen/src/contract-types/DemoContractAbi.hex.ts b/apps/demo-typegen/src/contract-types/DemoContractAbi.hex.ts new file mode 100644 index 00000000000..51ae4163785 --- /dev/null +++ b/apps/demo-typegen/src/contract-types/DemoContractAbi.hex.ts @@ -0,0 +1,12 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +export default '0x740000034700000000000000000000445dfcc00110fff3005d4060495d47f000134904407648000272f0007b36f000001aec5000910000005d40604a2440000047000000000000002d8ccb5b' \ No newline at end of file diff --git a/apps/demo-typegen/src/contract-types/factories/DemoContractAbi__factory.ts b/apps/demo-typegen/src/contract-types/factories/DemoContractAbi__factory.ts new file mode 100644 index 00000000000..730524e28b2 --- /dev/null +++ b/apps/demo-typegen/src/contract-types/factories/DemoContractAbi__factory.ts @@ -0,0 +1,82 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +import { Interface, Contract, ContractFactory } from "fuels"; +import type { Provider, Account, AbstractAddress, BytesLike, DeployContractOptions, StorageSlot } from "fuels"; +import type { DemoContractAbi, DemoContractAbiInterface } from "../DemoContractAbi"; + +const _abi = { + "types": [ + { + "typeId": 0, + "type": "u64", + "components": null, + "typeParameters": null + } + ], + "functions": [ + { + "inputs": [ + { + "name": "input", + "type": 0, + "typeArguments": null + } + ], + "name": "return_input", + "output": { + "name": "", + "type": 0, + "typeArguments": null + }, + "attributes": null + } + ], + "loggedTypes": [], + "messagesTypes": [], + "configurables": [] +}; + +const _storageSlots: StorageSlot[] = []; + +export class DemoContractAbi__factory { + static readonly abi = _abi; + + static readonly storageSlots = _storageSlots; + + static createInterface(): DemoContractAbiInterface { + return new Interface(_abi) as unknown as DemoContractAbiInterface + } + + static connect( + id: string | AbstractAddress, + accountOrProvider: Account | Provider + ): DemoContractAbi { + return new Contract(id, _abi, accountOrProvider) as unknown as DemoContractAbi + } + + static async deployContract( + bytecode: BytesLike, + wallet: Account, + options: DeployContractOptions = {} + ): Promise { + const factory = new ContractFactory(bytecode, _abi, wallet); + + const { storageSlots } = DemoContractAbi__factory; + + const contract = await factory.deployContract({ + storageSlots, + ...options, + }); + + return contract as unknown as DemoContractAbi; + } +} diff --git a/apps/demo-typegen/src/contract-types/index.ts b/apps/demo-typegen/src/contract-types/index.ts new file mode 100644 index 00000000000..bbf3c0426ee --- /dev/null +++ b/apps/demo-typegen/src/contract-types/index.ts @@ -0,0 +1,14 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +export type { DemoContractAbi } from './DemoContractAbi'; + +export { DemoContractAbi__factory } from './factories/DemoContractAbi__factory'; diff --git a/apps/demo-typegen/src/demo.test.ts b/apps/demo-typegen/src/demo.test.ts index af461841472..ba308386a7e 100644 --- a/apps/demo-typegen/src/demo.test.ts +++ b/apps/demo-typegen/src/demo.test.ts @@ -4,8 +4,12 @@ import { generateTestWallet } from '@fuel-ts/wallet/test-utils'; import type { BN } from 'fuels'; import { ContractFactory, Provider, toHex, BaseAssetId, Wallet, FUEL_NETWORK_URL } from 'fuels'; -import { DemoContractAbi__factory } from './generated-types'; -import bytecode from './generated-types/DemoContractAbi.hex'; +import storageSlots from '../contract/out/debug/demo-contract-storage_slots.json'; + +import { DemoContractAbi__factory } from './contract-types'; +import bytecode from './contract-types/DemoContractAbi.hex'; +import { PredicateAbi__factory } from './predicate-types'; +import { ScriptAbi__factory } from './script-types'; let gasPrice: BN; describe('ExampleContract', () => { @@ -13,6 +17,21 @@ describe('ExampleContract', () => { const provider = await Provider.create(FUEL_NETWORK_URL); ({ minGasPrice: gasPrice } = provider.getGasConfig()); }); + it('with imported storage slots', async () => { + const provider = await Provider.create(FUEL_NETWORK_URL); + const wallet = await generateTestWallet(provider, [[500_000, BaseAssetId]]); + + // #region typegen-demo-contract-storage-slots + // #context import storageSlots from './contract/out/debug/demo-contract-storage_slots.json'; + + const contract = await DemoContractAbi__factory.deployContract(bytecode, wallet, { + storageSlots, + gasPrice, + }); + // #endregion typegen-demo-contract-storage-slots + + expect(contract.id).toBeTruthy(); + }); it('should return the input', async () => { const provider = await Provider.create(FUEL_NETWORK_URL); const wallet = await generateTestWallet(provider, [[500_000, BaseAssetId]]); @@ -20,6 +39,7 @@ describe('ExampleContract', () => { // Deploy const factory = new ContractFactory(bytecode, DemoContractAbi__factory.abi, wallet); const contract = await factory.deployContract({ gasPrice }); + const contractId = contract.id; // Call const { value } = await contract.functions.return_input(1337).txParams({ gasPrice }).call(); @@ -28,11 +48,15 @@ describe('ExampleContract', () => { expect(value.toHex()).toEqual(toHex(1337)); // You can also make a call using the factory - const contractInstance = DemoContractAbi__factory.connect(contract.id, wallet); + // #region typegen-demo-contract-factory-connect + // #context import { DemoContractAbi__factory } from './types'; + + const contractInstance = DemoContractAbi__factory.connect(contractId, wallet); const { value: v2 } = await contractInstance.functions .return_input(1337) .txParams({ gasPrice }) .call(); + // #endregion typegen-demo-contract-factory-connect expect(v2.toHex()).toBe(toHex(1337)); }); @@ -40,8 +64,13 @@ describe('ExampleContract', () => { const provider = await Provider.create(FUEL_NETWORK_URL); const wallet = await generateTestWallet(provider, [[500_000, BaseAssetId]]); + // #region typegen-demo-contract-factory-deploy + // #context import { DemoContractAbi__factory } from './types'; + // #context import bytecode from './types/DemoContractAbi.hex'; + // Deploy const contract = await DemoContractAbi__factory.deployContract(bytecode, wallet, { gasPrice }); + // #endregion typegen-demo-contract-factory-deploy // Call const { value } = await contract.functions.return_input(1337).txParams({ gasPrice }).call(); @@ -79,3 +108,45 @@ it('should throw when dry running via contract factory with wallet with no resou expect((error).message).toMatch('not enough coins to fit the target'); }); + +test('Example script', async () => { + const provider = await Provider.create(FUEL_NETWORK_URL); + const wallet = await generateTestWallet(provider, [[500_000, BaseAssetId]]); + + // #region typegen-demo-script + // #context import { ScriptAbi__factory } from './types'; + + const script = ScriptAbi__factory.createInstance(wallet); + const { value } = await script.functions + .main() + .txParams({ + gasPrice: provider.getGasConfig().minGasPrice, + }) + .call(); + // #endregion typegen-demo-script + // @ts-expect-error TODO: investitage - typegen is expecting value to be a number but the value being returned is the string '0xa' + expect(value.toNumber()).toBe(10); +}); + +test('Example predicate', async () => { + // #region typegen-demo-predicate + // #context import { PredicateAbi__factory } from './types'; + const provider = await Provider.create(FUEL_NETWORK_URL); + const wallet1 = await generateTestWallet(provider, [[500_000, BaseAssetId]]); + const wallet2 = await generateTestWallet(provider, [[500_000, BaseAssetId]]); + + const predicate = PredicateAbi__factory.createInstance(provider); + + const tx = await wallet1.transfer(predicate.address, 100_000, BaseAssetId, { + gasPrice: provider.getGasConfig().minGasPrice, + }); + await tx.wait(); + + const tx2 = await predicate.setData(10).transfer(wallet2.address, 50_000, BaseAssetId, { + gasPrice: provider.getGasConfig().minGasPrice, + }); + await tx2.wait(); + + expect((await wallet2.getBalance()).toNumber()).toEqual(550_000); + // #endregion typegen-demo-predicate +}); diff --git a/apps/demo-typegen/src/predicate-types/PredicateAbi.d.ts b/apps/demo-typegen/src/predicate-types/PredicateAbi.d.ts new file mode 100644 index 00000000000..1e5df31e3d4 --- /dev/null +++ b/apps/demo-typegen/src/predicate-types/PredicateAbi.d.ts @@ -0,0 +1,36 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +import type { + BytesLike, + Contract, + DecodedValue, + FunctionFragment, + Interface, + InvokeFunction, +} from 'fuels'; + +interface PredicateAbiInterface extends Interface { + functions: { + main: FunctionFragment; + }; + + encodeFunctionData(functionFragment: 'main', values: [string]): Uint8Array; + + decodeFunctionData(functionFragment: 'main', data: BytesLike): DecodedValue; +} + +export class PredicateAbi extends Contract { + interface: PredicateAbiInterface; + functions: { + main: InvokeFunction<[input_address: string], boolean>; + }; +} diff --git a/apps/demo-typegen/src/predicate-types/PredicateAbi.hex.ts b/apps/demo-typegen/src/predicate-types/PredicateAbi.hex.ts new file mode 100644 index 00000000000..9c8c7dfaadb --- /dev/null +++ b/apps/demo-typegen/src/predicate-types/PredicateAbi.hex.ts @@ -0,0 +1,12 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +export default '0x740000034700000000000000000000605dfcc00110fff3001aec5000910000007144000361491101764800026141110d74000007724c0002134924c05a492001764800026141111f74000001240000005d47f00410451300a141046024400000fc05c23a8f7f66222377170ddcbfea9c543dff0dd2d2ba4d0478a4521423a9d40000000000000060' \ No newline at end of file diff --git a/apps/demo-typegen/src/predicate-types/factories/PredicateAbi__factory.ts b/apps/demo-typegen/src/predicate-types/factories/PredicateAbi__factory.ts new file mode 100644 index 00000000000..0a1544f7c76 --- /dev/null +++ b/apps/demo-typegen/src/predicate-types/factories/PredicateAbi__factory.ts @@ -0,0 +1,78 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +import { + BigNumberish, + Predicate, + Provider, +} from 'fuels'; + +export type PredicateAbiConfigurables = { +}; + +type PredicateAbiInputs = [input: BigNumberish]; + +const _abi = { + "types": [ + { + "typeId": 0, + "type": "bool", + "components": null, + "typeParameters": null + }, + { + "typeId": 1, + "type": "u8", + "components": null, + "typeParameters": null + } + ], + "functions": [ + { + "inputs": [ + { + "name": "input", + "type": 1, + "typeArguments": null + } + ], + "name": "main", + "output": { + "name": "", + "type": 0, + "typeArguments": null + }, + "attributes": null + } + ], + "loggedTypes": [], + "messagesTypes": [], + "configurables": [] +} + +const _bin = '0x740000034700000000000000000000645dfcc00110fff3001aec5000910000007144000361491101764800026141110d74000007724c0002134924c05a492001764800026141111f74000001240000005d4100005d47f000134104402440000047000000000000000000000a' + +export class PredicateAbi__factory { + + static readonly abi = _abi + static readonly bin = _bin; + + static createInstance(provider: Provider, configurables?: PredicateAbiConfigurables) { + + const { abi, bin } = PredicateAbi__factory + + const predicate = new Predicate(bin, provider, abi, configurables); + + return predicate; + + } + +} diff --git a/apps/demo-typegen/src/predicate-types/index.ts b/apps/demo-typegen/src/predicate-types/index.ts new file mode 100644 index 00000000000..62abb4b1e84 --- /dev/null +++ b/apps/demo-typegen/src/predicate-types/index.ts @@ -0,0 +1,12 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +export { PredicateAbi__factory } from './factories/PredicateAbi__factory'; diff --git a/apps/demo-typegen/src/script-types/ScriptAbi.d.ts b/apps/demo-typegen/src/script-types/ScriptAbi.d.ts new file mode 100644 index 00000000000..7e25b908019 --- /dev/null +++ b/apps/demo-typegen/src/script-types/ScriptAbi.d.ts @@ -0,0 +1,36 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +import type { + BytesLike, + Contract, + DecodedValue, + FunctionFragment, + Interface, + InvokeFunction, +} from 'fuels'; + +interface ScriptAbiInterface extends Interface { + functions: { + main: FunctionFragment; + }; + + encodeFunctionData(functionFragment: 'main', values: []): Uint8Array; + + decodeFunctionData(functionFragment: 'main', data: BytesLike): DecodedValue; +} + +export class ScriptAbi extends Contract { + interface: ScriptAbiInterface; + functions: { + main: InvokeFunction<[], boolean>; + }; +} diff --git a/apps/demo-typegen/src/script-types/ScriptAbi.hex.ts b/apps/demo-typegen/src/script-types/ScriptAbi.hex.ts new file mode 100644 index 00000000000..6e0e8771f4e --- /dev/null +++ b/apps/demo-typegen/src/script-types/ScriptAbi.hex.ts @@ -0,0 +1,12 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +export default '0x740000034700000000000000000000245dfcc00110fff3001aec50009100000024040000' \ No newline at end of file diff --git a/apps/demo-typegen/src/script-types/factories/ScriptAbi__factory.ts b/apps/demo-typegen/src/script-types/factories/ScriptAbi__factory.ts new file mode 100644 index 00000000000..5e062dbf48e --- /dev/null +++ b/apps/demo-typegen/src/script-types/factories/ScriptAbi__factory.ts @@ -0,0 +1,67 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +import { + Account, + BigNumberish, + Script, +} from 'fuels'; + +type ScriptAbiInputs = []; +type ScriptAbiOutput = number; + +const _abi = { + "types": [ + { + "typeId": 0, + "type": "u8", + "components": null, + "typeParameters": null + } + ], + "functions": [ + { + "inputs": [], + "name": "main", + "output": { + "name": "", + "type": 0, + "typeArguments": null + }, + "attributes": null + } + ], + "loggedTypes": [], + "messagesTypes": [], + "configurables": [] +} + +const _bin = '0x7400000347000000000000000000002c5dfcc00110fff3001aec5000910000005d43f0002440000047000000000000000000000a' + +export class ScriptAbi__factory { + + static readonly abi = _abi + static readonly bin = _bin + + static createInstance(wallet: Account) { + + const { abi, bin } = ScriptAbi__factory + + const script = new Script< + ScriptAbiInputs, + ScriptAbiOutput + >(bin, abi, wallet); + + return script; + + } + +} diff --git a/apps/demo-typegen/src/script-types/index.ts b/apps/demo-typegen/src/script-types/index.ts new file mode 100644 index 00000000000..dbed88b210c --- /dev/null +++ b/apps/demo-typegen/src/script-types/index.ts @@ -0,0 +1,12 @@ +/* Autogenerated file. Do not edit manually. */ + +/* tslint:disable */ +/* eslint-disable */ + +/* + Fuels version: 0.65.0 + Forc version: 0.46.1 + Fuel-Core version: 0.20.8 +*/ + +export { ScriptAbi__factory } from './factories/ScriptAbi__factory'; diff --git a/apps/docs/src/guide/abi-typegen/using-generated-types.md b/apps/docs/src/guide/abi-typegen/using-generated-types.md index e201d20f881..6766ea1bd7e 100644 --- a/apps/docs/src/guide/abi-typegen/using-generated-types.md +++ b/apps/docs/src/guide/abi-typegen/using-generated-types.md @@ -10,49 +10,19 @@ yarn exec fuels typegen -i ./abis/*-abi.json -o ./types We can use these files like so: - - -```ts -import { Wallet } from "fuels"; -import { MyContract__factory } from "./types"; - -const contractId = "0x..."; -const wallet = Wallet.fromAddress("..."); -const contract = MyContract__factory.connect(contractId, wallet); - -// All contract methods are available under functions with the correct types -const { transactionId, value } = await contract.functions.my_fn(1).call(); - -console.log(transactionId, value); -``` +<<< @/../../demo-typegen/src/demo.test.ts#typegen-demo-contract-factory-connect{ts:line-numbers} ## Contract Let's use the Contract class to deploy a contract: -```ts -import { Wallet } from "fuels"; -import { MyContract__factory } from "./types"; -import bytecode from "./types/MyContract.hex.ts"; - -const wallet = Wallet.fromAddress("..."); - -const contract = await MyContract__factory.deployContract(bytecode, wallet); - -console.log(contract.id); -``` +<<< @/../../demo-typegen/src/demo.test.ts#typegen-demo-contract-factory-deploy{ts:line-numbers} ### Autoloading of Storage Slots Typegen tries to resolve, auto-load, and embed the [Storage Slots](../contracts//storage-slots.md) for your Contract within the `MyContract__factory` class. Still, you can override it alongside other options from [`DeployContractOptions`](https://github.com/FuelLabs/fuels-ts/blob/a64b67b9fb2d7f764ab9151a21d2266bf2df3643/packages/contract/src/contract-factory.ts#L19-L24), when calling the `deployContract` method: -```ts -import storageSlots from "../contract/out/debug/storage-slots.json"; - -const contract = await MyContract__factory.deployContract(bytecode, wallet, { - storageSlots, -}); -``` +<<< @/../../demo-typegen/src/demo.test.ts#typegen-demo-contract-storage-slots{ts:line-numbers} ## Script @@ -64,27 +34,11 @@ yarn exec fuels typegen -i ./abis/*-abi.json -o ./types --script We can use these files like so: - - -```ts -import { Wallet } from "fuels"; -import { MyScript__factory } from "./types"; - -const wallet = Wallet.fromAddress("..."); -const script = ScriptAbi__factory.createInstance(wallet); - -const { value, logs } = await script.functions.main(1).call(); - -console.log({ value, logs }); -``` +<<< @/../../demo-typegen/src/demo.test.ts#typegen-demo-script{ts:line-numbers} ## Predicate -Consider the following predicate: - -<<< @/../../../packages/fuel-gauge/fixtures/forc-projects/predicate-main-args-struct/src/main.sw#Predicate-main-args{ts:line-numbers} - -Now, after generating types via: +After generating types via: ```console yarn exec fuels typegen -i ./abis/*-abi.json -o ./types --predicate @@ -92,30 +46,7 @@ yarn exec fuels typegen -i ./abis/*-abi.json -o ./types --predicate We can use these files like so: - - -```ts -import { Wallet } from "fuels"; -import { MyPredicate__factory } from "./types"; - -const wallet = Wallet.fromAddress("..."); -const predicate = MyPredicate__factory.createInstance(); - -await predicate - .setData({ - has_account: true, - total_complete: 100, - }) - .transfer(wallet.address, ); - -const walletBalance = await wallet.getBalance(); -const predicateBalance = await predicate.getBalance(); - -console.log({ - walletBalance, - predicateBalance, -}); -``` +<<< @/../../demo-typegen/src/demo.test.ts#typegen-demo-predicate{ts:line-numbers} See also: