Skip to content

Commit

Permalink
Merge branch 'master' into ps/chore/add-full-abi-coder-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad authored Nov 21, 2024
2 parents 7903a83 + 2cef020 commit 1c4744c
Show file tree
Hide file tree
Showing 32 changed files with 472 additions and 270 deletions.
3 changes: 2 additions & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"template-nextjs",
"create-fuels-counter-guide",
"demo-wallet-sdk-react",
"template-vite"
"template-vite",
"docs-api"
]
}
7 changes: 7 additions & 0 deletions .changeset/cyan-panthers-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fuel-ts/abi-typegen": patch
"@fuel-ts/contract": patch
"fuels": patch
---

fix: typegen and storage slots integration
4 changes: 4 additions & 0 deletions .changeset/quiet-cups-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

chore: ship API documentation
3 changes: 2 additions & 1 deletion .knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"templates/**",
"/apps/docs-snippets/**",
"/apps/docs-snippets2/**/*.test.ts",
"apps/create-fuels-counter-guide/**"
"apps/create-fuels-counter-guide/**",
"apps/docs-api/**"
],
"ignoreDependencies": [
"autocannon",
Expand Down
1 change: 1 addition & 0 deletions apps/docs-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/api
28 changes: 28 additions & 0 deletions apps/docs-api/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/FuelLabs/fuels-ts/master/apps/docs/src/public/fuels-ts-logo-dark.png">
<img alt="Fuels-ts SDK logo" width="400px" src="https://raw.githubusercontent.com/FuelLabs/fuels-ts/master/apps/docs/src/public/fuels-ts-logo-light.png">
</picture>

**fuels-ts** is a library for interacting with **Fuel v2**.

[![test](https://github.com/FuelLabs/fuels-ts/actions/workflows/test.yaml/badge.svg)](https://github.com/FuelLabs/fuels-ts/actions/workflows/test.yaml)
[![npm](https://img.shields.io/npm/v/fuels)](https://www.npmjs.com/package/fuels)
[![docs](https://img.shields.io/badge/docs-fuels.ts-brightgreen.svg?style=flat)](https://docs.fuel.network/docs/fuels-ts/)
[![discord](https://img.shields.io/badge/chat%20on-discord-orange?&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/xfpK4Pe)

# Modules

- [abi-coder](./modules/_fuel_ts_abi_coder.html)
- [abi-typegen](./modules/_fuel_ts_abi_typegen.html)
- [account](./modules/_fuel_ts_account.html)
- [address](./modules/_fuel_ts_address.html)
- [crypto](./modules/_fuel_ts_crypto.html)
- [errors](./modules/_fuel_ts_errors.html)
- [hasher](./modules/_fuel_ts_hasher.html)
- [interfaces](./modules/_fuel_ts_interfaces.html)
- [math](./modules/_fuel_ts_math.html)
- [predicate](./modules/_fuel_ts_predicate.html)
- [program](./modules/_fuel_ts_program.html)
- [script](./modules/_fuel_ts_script.html)
- [transactions](./modules/_fuel_ts_transactions.html)
- [utils](./modules/_fuel_ts_utils.html)
17 changes: 17 additions & 0 deletions apps/docs-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"private": true,
"name": "docs-api",
"version": "0.0.1",
"description": "",
"type": "module",
"scripts": {
"build": "typedoc"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"fuels": "workspace:*",
"typedoc": "^0.26.3"
}
}
28 changes: 28 additions & 0 deletions apps/docs-api/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "https://typedoc.org/schema.json",
"entryPointStrategy": "packages",
"entryPoints": [
"../../packages/abi-coder",
"../../packages/abi-typegen",
"../../packages/address",
"../../packages/interfaces",
"../../packages/predicate",
"../../packages/account",
"../../packages/program",
"../../packages/contract",
"../../packages/script",
"../../packages/utils",
"../../packages/crypto",
"../../packages/errors",
"../../packages/hasher",
"../../packages/math",
"../../packages/transactions"
],
"out": "src/api",
"readme": "./index.md",
"name": "Fuels TS SDK API Documentation",
"navigationLinks": {
"Docs": "https://docs.fuel.network/docs/intro/what-is-fuel/",
"GitHub": "https://github.com/FuelLabs/fuels-ts"
}
}
4 changes: 4 additions & 0 deletions apps/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ Using the SDK you can:
## Version

This documentation was generated using Fuels `v{{fuels}}`, Fuel Core `v{{fuelCore}}`, Sway `v{{forc}}`, and Forc `v{{forc}}`.

## API Documentation

The Complete API documentation for the SDK is available [here](https://fuels-ts-docs-api.vercel.app/).
2 changes: 1 addition & 1 deletion internal/benchmarks/src/cost-estimation.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Cost Estimation Benchmarks', () => {
const wallet = new WalletUnlocked(process.env.DEVNET_WALLET_PVT_KEY as string, provider);

const contractFactory = new CallTestContractFactory(wallet);
const { waitForResult } = await contractFactory.deploy<CallTestContract>();
const { waitForResult } = await contractFactory.deploy();
const { contract: deployedContract } = await waitForResult();
contract = deployedContract;
} else {
Expand Down
6 changes: 6 additions & 0 deletions packages/abi-coder/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://typedoc.org/schema.json",
"extends": ["../../typedoc.base.json"],
"entryPoints": ["src/index.ts"],
"readme": "none"
}
26 changes: 11 additions & 15 deletions packages/abi-typegen/src/templates/contract/factory.hbs
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
{{header}}

import { Contract, ContractFactory, decompressBytecode } from "fuels";
import type { Provider, Account, DeployContractOptions, DeployContractResult } from "fuels";
import { ContractFactory, decompressBytecode } from "fuels";
import type { Provider, Account, DeployContractOptions } from "fuels";

import { {{capitalizedName}} } from "./{{capitalizedName}}";

const bytecode = decompressBytecode("{{compressedBytecode}}");

export class {{capitalizedName}}Factory extends ContractFactory {
export class {{capitalizedName}}Factory extends ContractFactory<{{capitalizedName}}> {

static readonly bytecode = bytecode;

constructor(accountOrProvider: Account | Provider) {
super(bytecode, {{capitalizedName}}.abi, accountOrProvider);
super(
bytecode,
{{capitalizedName}}.abi,
accountOrProvider,
{{capitalizedName}}.storageSlots
);
}

override deploy<TContract extends Contract = Contract>(
deployOptions?: DeployContractOptions
): Promise<DeployContractResult<TContract>> {
return super.deploy({
storageSlots: {{capitalizedName}}.storageSlots,
...deployOptions,
});
}

static async deploy (
static deploy (
wallet: Account,
options: DeployContractOptions = {}
): Promise<DeployContractResult<{{capitalizedName}}>> {
) {
const factory = new {{capitalizedName}}Factory(wallet);
return factory.deploy(options);
}
Expand Down
26 changes: 11 additions & 15 deletions packages/abi-typegen/test/fixtures/templates/contract/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,30 @@
Fuel-Core version: 33.33.33
*/

import { Contract, ContractFactory, decompressBytecode } from "fuels";
import type { Provider, Account, DeployContractOptions, DeployContractResult } from "fuels";
import { ContractFactory, decompressBytecode } from "fuels";
import type { Provider, Account, DeployContractOptions } from "fuels";

import { MyContract } from "./MyContract";

const bytecode = decompressBytecode("0x-bytecode-here");

export class MyContractFactory extends ContractFactory {
export class MyContractFactory extends ContractFactory<MyContract> {

static readonly bytecode = bytecode;

constructor(accountOrProvider: Account | Provider) {
super(bytecode, MyContract.abi, accountOrProvider);
super(
bytecode,
MyContract.abi,
accountOrProvider,
MyContract.storageSlots
);
}

override deploy<TContract extends Contract = Contract>(
deployOptions?: DeployContractOptions
): Promise<DeployContractResult<TContract>> {
return super.deploy({
storageSlots: MyContract.storageSlots,
...deployOptions,
});
}

static async deploy (
static deploy (
wallet: Account,
options: DeployContractOptions = {}
): Promise<DeployContractResult<MyContract>> {
) {
const factory = new MyContractFactory(wallet);
return factory.deploy(options);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/abi-typegen/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://typedoc.org/schema.json",
"extends": ["../../typedoc.base.json"],
"entryPoints": ["src/index.ts"],
"readme": "none"
}
3 changes: 2 additions & 1 deletion packages/account/typedoc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://typedoc.org/schema.json",
"extends": ["../../typedoc.base.json"],
"entryPoints": ["src/index.ts"]
"entryPoints": ["src/index.ts"],
"readme": "none"
}
3 changes: 2 additions & 1 deletion packages/address/typedoc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://typedoc.org/schema.json",
"extends": ["../../typedoc.base.json"],
"entryPoints": ["src/index.ts"]
"entryPoints": ["src/index.ts"],
"readme": "none"
}
36 changes: 21 additions & 15 deletions packages/contract/src/contract-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ export type DeployContractResult<TContract extends Contract = Contract> = {
/**
* `ContractFactory` provides utilities for deploying and configuring contracts.
*/
export default class ContractFactory {
export default class ContractFactory<TContract extends Contract = Contract> {
bytecode: BytesLike;
interface: Interface;
provider!: Provider | null;
account!: Account | null;
storageSlots: StorageSlot[];

/**
* Create a ContractFactory instance.
Expand All @@ -68,7 +69,8 @@ export default class ContractFactory {
constructor(
bytecode: BytesLike,
abi: JsonAbi | Interface,
accountOrProvider: Account | Provider | null = null
accountOrProvider: Account | Provider | null = null,
storageSlots: StorageSlot[] = []
) {
// Force the bytecode to be a byte array
this.bytecode = arrayify(bytecode);
Expand Down Expand Up @@ -99,6 +101,8 @@ export default class ContractFactory {
this.provider = accountOrProvider;
this.account = null;
}

this.storageSlots = storageSlots;
}

/**
Expand All @@ -118,17 +122,19 @@ export default class ContractFactory {
* @returns The CreateTransactionRequest object for deploying the contract.
*/
createTransactionRequest(deployOptions?: DeployContractOptions & { bytecode?: BytesLike }) {
const storageSlots = deployOptions?.storageSlots
?.map(({ key, value }) => ({
const storageSlots = (deployOptions?.storageSlots ?? [])
.concat(this.storageSlots)
.map(({ key, value }) => ({
key: hexlifyWithPrefix(key),
value: hexlifyWithPrefix(value),
}))
.filter((el, index, self) => self.findIndex((s) => s.key === el.key) === index)
.sort(({ key: keyA }, { key: keyB }) => keyA.localeCompare(keyB));

const options = {
salt: randomBytes(32),
...deployOptions,
storageSlots: storageSlots || [],
...(deployOptions ?? {}),
storageSlots,
};

if (!this.provider) {
Expand Down Expand Up @@ -192,16 +198,16 @@ export default class ContractFactory {
* @param deployOptions - Options for deploying the contract.
* @returns A promise that resolves to the deployed contract instance.
*/
async deploy<TContract extends Contract = Contract>(
async deploy<T extends Contract = TContract>(
deployOptions: DeployContractOptions = {}
): Promise<DeployContractResult<TContract>> {
): Promise<DeployContractResult<T>> {
const account = this.getAccount();
const { consensusParameters } = account.provider.getChain();
const maxContractSize = consensusParameters.contractParameters.contractMaxSize.toNumber();

return this.bytecode.length > maxContractSize
? this.deployAsBlobTx(deployOptions)
: this.deployAsCreateTx(deployOptions);
: this.deployAsCreateTx<T>(deployOptions);
}

/**
Expand All @@ -210,9 +216,9 @@ export default class ContractFactory {
* @param deployOptions - Options for deploying the contract.
* @returns A promise that resolves to the deployed contract instance.
*/
async deployAsCreateTx<TContract extends Contract = Contract>(
async deployAsCreateTx<T extends Contract = TContract>(
deployOptions: DeployContractOptions = {}
): Promise<DeployContractResult<TContract>> {
): Promise<DeployContractResult<T>> {
const account = this.getAccount();
const { consensusParameters } = account.provider.getChain();
const maxContractSize = consensusParameters.contractParameters.contractMaxSize.toNumber();
Expand All @@ -230,7 +236,7 @@ export default class ContractFactory {

const waitForResult = async () => {
const transactionResult = await transactionResponse.waitForResult<TransactionType.Create>();
const contract = new Contract(contractId, this.interface, account) as TContract;
const contract = new Contract(contractId, this.interface, account) as T;

return { contract, transactionResult };
};
Expand All @@ -248,11 +254,11 @@ export default class ContractFactory {
* @param deployOptions - Options for deploying the contract.
* @returns A promise that resolves to the deployed contract instance.
*/
async deployAsBlobTx<TContract extends Contract = Contract>(
async deployAsBlobTx<T extends Contract = TContract>(
deployOptions: DeployContractOptions = {
chunkSizeMultiplier: CHUNK_SIZE_MULTIPLIER,
}
): Promise<DeployContractResult<TContract>> {
): Promise<DeployContractResult<T>> {
const account = this.getAccount();
const { configurableConstants, chunkSizeMultiplier } = deployOptions;
if (configurableConstants) {
Expand Down Expand Up @@ -362,7 +368,7 @@ export default class ContractFactory {
txIdResolver(createRequest.getTransactionId(account.provider.getChainId()));
const transactionResponse = await account.sendTransaction(createRequest);
const transactionResult = await transactionResponse.waitForResult<TransactionType.Create>();
const contract = new Contract(contractId, this.interface, account) as TContract;
const contract = new Contract(contractId, this.interface, account) as T;

return { contract, transactionResult };
};
Expand Down
3 changes: 2 additions & 1 deletion packages/contract/typedoc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://typedoc.org/schema.json",
"extends": ["../../typedoc.base.json"],
"entryPoints": ["src/index.ts", "src/test-utils.ts"]
"entryPoints": ["src/index.ts", "src/test-utils.ts"],
"readme": "none"
}
6 changes: 6 additions & 0 deletions packages/crypto/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://typedoc.org/schema.json",
"extends": ["../../typedoc.base.json"],
"entryPoints": ["src/index.ts"],
"readme": "none"
}
6 changes: 6 additions & 0 deletions packages/errors/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://typedoc.org/schema.json",
"extends": ["../../typedoc.base.json"],
"entryPoints": ["src/index.ts"],
"readme": "none"
}
Loading

0 comments on commit 1c4744c

Please sign in to comment.