Skip to content

Commit

Permalink
docs: purge hardcoded snippets on 'using typegen' page
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhaiwat10 committed Nov 3, 2023
1 parent df3222d commit cf36fa3
Show file tree
Hide file tree
Showing 22 changed files with 531 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .changeset/odd-kids-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
10 changes: 8 additions & 2 deletions apps/demo-typegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
"author": "Fuel Labs <[email protected]> (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": {
Expand Down
2 changes: 2 additions & 0 deletions apps/demo-typegen/predicate/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
7 changes: 7 additions & 0 deletions apps/demo-typegen/predicate/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Dhaiwat Pandya"]
entry = "main.sw"
license = "Apache-2.0"
name = "predicate"

[dependencies]
7 changes: 7 additions & 0 deletions apps/demo-typegen/predicate/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
predicate;

fn main(input: u8) -> bool {
let answer: u8 = 10;

input == answer
}
2 changes: 2 additions & 0 deletions apps/demo-typegen/script/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
7 changes: 7 additions & 0 deletions apps/demo-typegen/script/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Dhaiwat Pandya"]
entry = "main.sw"
license = "Apache-2.0"
name = "script"

[dependencies]
5 changes: 5 additions & 0 deletions apps/demo-typegen/script/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
script;

fn main() -> u8 {
10
}
38 changes: 38 additions & 0 deletions apps/demo-typegen/src/contract-types/DemoContractAbi.d.ts
Original file line number Diff line number Diff line change
@@ -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>;
};
}
12 changes: 12 additions & 0 deletions apps/demo-typegen/src/contract-types/DemoContractAbi.hex.ts
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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<DemoContractAbi> {
const factory = new ContractFactory(bytecode, _abi, wallet);

const { storageSlots } = DemoContractAbi__factory;

const contract = await factory.deployContract({
storageSlots,
...options,
});

return contract as unknown as DemoContractAbi;
}
}
14 changes: 14 additions & 0 deletions apps/demo-typegen/src/contract-types/index.ts
Original file line number Diff line number Diff line change
@@ -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';
77 changes: 74 additions & 3 deletions apps/demo-typegen/src/demo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,42 @@ 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', () => {
beforeAll(async () => {
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]]);

// 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();
Expand All @@ -28,20 +48,29 @@ 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));
});

it('deployContract method', async () => {
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();
Expand Down Expand Up @@ -79,3 +108,45 @@ it('should throw when dry running via contract factory with wallet with no resou

expect((<Error>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
});
36 changes: 36 additions & 0 deletions apps/demo-typegen/src/predicate-types/PredicateAbi.d.ts
Original file line number Diff line number Diff line change
@@ -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>;
};
}
12 changes: 12 additions & 0 deletions apps/demo-typegen/src/predicate-types/PredicateAbi.hex.ts
Original file line number Diff line number Diff line change
@@ -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'
Loading

0 comments on commit cf36fa3

Please sign in to comment.