Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
Browse files Browse the repository at this point in the history
…/chore/manual-proxy-contracts
  • Loading branch information
danielbate committed Oct 9, 2024
2 parents 73b1bb1 + 04578ed commit 191054d
Show file tree
Hide file tree
Showing 112 changed files with 1,983 additions and 360 deletions.
10 changes: 10 additions & 0 deletions .changeset/afraid-pets-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@internal/forc": patch
"@internal/fuel-core": patch
"@fuel-ts/versions": patch
"@fuel-ts/account": patch
"fuels": patch
"create-fuels": patch
---

chore: `[email protected]`, `[email protected]`, and `create-fuels` toolchains
4 changes: 0 additions & 4 deletions .changeset/angry-buses-jam.md

This file was deleted.

6 changes: 6 additions & 0 deletions .changeset/beige-bears-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@internal/forc": patch
"@fuel-ts/versions": patch
---

chore: update to `[email protected]`
5 changes: 0 additions & 5 deletions .changeset/dull-toys-dream.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/fluffy-bulldogs-lay.md

This file was deleted.

4 changes: 0 additions & 4 deletions .changeset/honest-toes-punch.md

This file was deleted.

4 changes: 4 additions & 0 deletions .changeset/itchy-schools-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

chore: add blob script deploy stress test
4 changes: 0 additions & 4 deletions .changeset/neat-pumas-dream.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/ninety-carpets-watch.md

This file was deleted.

3 changes: 0 additions & 3 deletions .changeset/sour-actors-tease.md

This file was deleted.

8 changes: 8 additions & 0 deletions .changeset/strange-glasses-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@internal/fuel-core": patch
"@fuel-ts/versions": patch
"@fuel-ts/account": patch
"fuels": patch
---

chore: update `fuel-core` to `0.38.0`
6 changes: 0 additions & 6 deletions .changeset/tall-lemons-explode.md

This file was deleted.

4 changes: 0 additions & 4 deletions .changeset/thin-waves-care.md

This file was deleted.

4 changes: 4 additions & 0 deletions .changeset/thirty-parrots-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

docs: predicate and script deploys
5 changes: 5 additions & 0 deletions .changeset/twenty-islands-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-typegen": patch
---

fix: typegen factory template
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ _site/
Forc.lock

# Ignore forc builds
**/out/debug
**/out/release
**/out
**/test-predicate-*/index.ts

## Ignore perf test files
Expand Down
3 changes: 3 additions & 0 deletions .knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"/apps/docs/*",
"/packages/abi-typegen/test/**",
"templates/**",
"/apps/docs-snippets/**",
"/apps/docs-snippets2/**/*.test.ts",
"apps/create-fuels-counter-guide/**"
],
Expand All @@ -28,6 +29,8 @@
"eslint-plugin-react",
"eslint-plugin-react-hooks",
"dotenv",
"kill",
"lsof",
"memfs",
"open",
"textlint",
Expand Down
2 changes: 1 addition & 1 deletion apps/demo-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"pretest": "pnpm original:build"
},
"dependencies": {
"@fuels/vm-asm": "0.57.1",
"@fuels/vm-asm": "0.58.0",
"@types/node": "^22.5.5",
"@types/react-dom": "^18.3",
"@types/react": "^18.3.10",
Expand Down
2 changes: 1 addition & 1 deletion apps/demo-react-cra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.29",
"private": true,
"dependencies": {
"@fuels/vm-asm": "0.57.1",
"@fuels/vm-asm": "0.58.0",
"@testing-library/react": "^16.0.1",
"@types/node": "^22.5.5",
"@types/react": "^18.3.10",
Expand Down
2 changes: 1 addition & 1 deletion apps/demo-react-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"pretest": "pnpm original:build"
},
"dependencies": {
"@fuels/vm-asm": "0.57.1",
"@fuels/vm-asm": "0.58.0",
"fuels": "workspace:*",
"react-dom": "^18.3.1",
"react": "^18.3.1"
Expand Down
4 changes: 1 addition & 3 deletions apps/docs-snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"description": "",
"private": true,
"scripts": {
"pretest": "run-s build:forc type:check",
"build:forc": "pnpm fuels build",
"type:check": "tsc --noEmit"
"pretest": "./scripts/pretest.sh"
},
"devDependencies": {
"@fuel-ts/account": "workspace:*",
Expand Down
17 changes: 17 additions & 0 deletions apps/docs-snippets/scripts/pretest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Kill anything running on port 4000
lsof -t -i:4000 | xargs -r kill

# Runs a node at port 4000
pnpm fuels node > /dev/null 2>&1 &

# Builds projects
pnpm fuels build

# Deploys projects (needed for loader bytecode)
pnpm fuels deploy

# Kills the node
lsof -t -i:4000 | xargs -r kill

# Checks for type errors
pnpm tsc --noEmit
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('Resubmitting Failed Transactions', () => {
}
}
// #endregion resubmitting-failed-transactions-3
}).rejects.toThrow('Transaction is not inserted. UTXO does not exist: 0x');
}).rejects.toThrowError(/Transaction input validation failed: UTXO \(id: .*\) does not exist/);

await expect(
(async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { ContractFactory, Provider, Wallet, hexlify } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import {
ConfigurablePin as TypegenPredicate,
ConfigurablePinLoader as TypegenPredicateLoader,
} from '../../../test/typegen';

/**
* @group browser
* @group node
*/
describe('Deploying Predicates', () => {
it('deploys a predicate via loader and calls', async () => {
using launched = await launchTestNode();

const {
provider: testProvider,
wallets: [testWallet, receiver],
} = launched;

const recieverInitialBalance = await receiver.getBalance();

const providerUrl = testProvider.url;
const WALLET_PVT_KEY = hexlify(testWallet.privateKey);

const factory = new ContractFactory(
TypegenPredicate.bytecode,
TypegenPredicate.abi,
testWallet
);
const { waitForResult: waitForDeploy } = await factory.deployAsBlobTxForScript();
await waitForDeploy();

// #region deploying-predicates
// #import { Provider, Wallet };
// #context import { WALLET_PVT_KEY } from 'path/to/my/env/file';
// #context import { TypegenPredicateLoader } from 'path/to/typegen/outputs';

const provider = await Provider.create(providerUrl);
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);
const baseAssetId = provider.getBaseAssetId();

// First, we will need to instantiate the script via it's loader bytecode.
// This can be imported from the typegen outputs that were created on `fuels deploy`.
// Then we can use the predicate as we would normally, such as overriding the configurables.
const predicate = new TypegenPredicateLoader({
data: [23],
provider,
configurableConstants: {
PIN: 23,
},
});

// Now, let's fund the predicate
const fundTx = await wallet.transfer(predicate.address, 100_000, baseAssetId);
await fundTx.waitForResult();

// Then we'll execute the transfer and validate the predicate
const transferTx = await predicate.transfer(receiver.address, 1000, baseAssetId);
const { gasUsed } = await transferTx.waitForResult();
// #endregion deploying-predicates

const anotherPredicate = new TypegenPredicate({
data: [23],
provider,
configurableConstants: {
PIN: 23,
},
});

const anotherFundTx = await wallet.transfer(anotherPredicate.address, 100_000);
await anotherFundTx.waitForResult();

const anotherTransferTx = await anotherPredicate.transfer(receiver.address, 1000);
const { gasUsed: anotherGasUsed } = await anotherTransferTx.waitForResult();

expect(recieverInitialBalance.toNumber()).toBeLessThan(
(await receiver.getBalance()).toNumber()
);
expect(gasUsed.toNumber()).toBeLessThanOrEqual(anotherGasUsed.toNumber());
});
});
61 changes: 61 additions & 0 deletions apps/docs-snippets/src/guide/scripts/deploying-scripts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { ContractFactory, Provider, Wallet, hexlify } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import {
SumScript as TypegenScript,
SumScriptLoader as TypegenScriptLoader,
} from '../../../test/typegen';

/**
* @group browser
* @group node
*/
describe('Deploying Scripts', () => {
it('deploys a script via loader and calls', async () => {
using launched = await launchTestNode();

const {
provider: testProvider,
wallets: [testWallet],
} = launched;

const providerUrl = testProvider.url;
const WALLET_PVT_KEY = hexlify(testWallet.privateKey);

const factory = new ContractFactory(TypegenScript.bytecode, TypegenScript.abi, testWallet);
const { waitForResult: waitForDeploy } = await factory.deployAsBlobTxForScript();
await waitForDeploy();

// #region deploying-scripts
// #import { Provider, Wallet };
// #context import { WALLET_PVT_KEY } from 'path/to/my/env/file';
// #context import { TypegenScriptLoader } from 'path/to/typegen/outputs';

const provider = await Provider.create(providerUrl);
const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);

// First, we will need to instantiate the script via it's loader bytecode. This can be imported from the typegen outputs
// that were created on `fuels deploy`
const script = new TypegenScriptLoader(wallet);

// Now we are free to interact with the script as we would normally, such as overriding the configurables
const configurable = {
AMOUNT: 20,
};
script.setConfigurableConstants(configurable);

const { waitForResult } = await script.functions.main(10).call();
const { value, gasUsed } = await waitForResult();
// value: 10
// #endregion deploying-scripts

const scriptWithoutLoader = new TypegenScript(wallet);
scriptWithoutLoader.setConfigurableConstants(configurable);
const { waitForResult: waitForAnotherResult } = await script.functions.main(10).call();
const { value: anotherValue, gasUsed: anotherGasUsed } = await waitForAnotherResult();

expect(value).toBe(30);
expect(anotherValue).toBe(30);
expect(gasUsed.toNumber()).toBeLessThanOrEqual(anotherGasUsed.toNumber());
});
});
8 changes: 8 additions & 0 deletions apps/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ export default defineConfig({
text: 'Instantiating A Script',
link: '/guide/scripts/instantiating-a-script',
},
{
text: 'Deploying Scripts',
link: '/guide/scripts/deploying-scripts',
},
{
text: 'Configurable Constants',
link: '/guide/scripts/configurable-constants',
Expand All @@ -298,6 +302,10 @@ export default defineConfig({
text: 'Instantiating A Predicate',
link: '/guide/predicates/instantiating-a-predicate',
},
{
text: 'Deploying Predicates',
link: '/guide/predicates/deploying-predicates',
},
{
text: 'Configurable Constants',
link: '/guide/predicates/configurable-constants',
Expand Down
20 changes: 20 additions & 0 deletions apps/docs/src/guide/predicates/deploying-predicates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Deploying Predicates

In order to optimize the cost of your recurring predicate executions, we recommend first deploying your predicate. This can be done using the [Fuels CLI](../fuels-cli/index.md) and running the [deploy command](../fuels-cli/commands.md#fuels-deploy).

By deploying the predicate, its bytecode is stored on chain as a blob. The SDK will then produce bytecode that can load the blob on demand to execute the original predicate. This far reduces the repeat execution cost of the predicate.

## How to Deploy a Predicate

To deploy a predicate, we can use the [Fuels CLI](../fuels-cli/index.md) and execute the [deploy command](../fuels-cli/commands.md#fuels-deploy).

This will perform the following actions:

1. Compile the predicate using your `forc` version
1. Deploy the built predicate binary to the chain as a blob
1. Generate a new, smaller predicate that loads the deployed predicate's blob
1. Generate types for both the predicate and the loader that you can use in your application

We can then utilize the above generated types like so:

<<< @/../../docs-snippets/src/guide/predicates/deploying-predicates.test.ts#deploying-predicates{ts:line-numbers}
20 changes: 20 additions & 0 deletions apps/docs/src/guide/scripts/deploying-scripts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Deploying Scripts

In order to optimize the cost of your recurring script executions, we recommend first deploying your script. This can be done using the [Fuels CLI](../fuels-cli/index.md) and running the [deploy command](../fuels-cli/commands.md#fuels-deploy).

By deploying the script, its bytecode is stored on chain as a blob. The SDK will then produce bytecode that can load the blob on demand to execute the original script. This far reduces the repeat execution cost of the script.

## How to Deploy a Script

To deploy a script, we can use the [Fuels CLI](../fuels-cli/index.md) and execute the [deploy command](../fuels-cli/commands.md#fuels-deploy).

This will perform the following actions:

1. Compile the script using your `forc` version
1. Deploy the built script binary to the chain as a blob
1. Generate a script that loads the blob that can be used to execute the script
1. Generate types for both the script and the loader that you can use in your application

We can then utilize the above generated types like so:

<<< @/../../docs-snippets/src/guide/scripts/deploying-scripts.test.ts#deploying-scripts{ts:line-numbers}
Loading

0 comments on commit 191054d

Please sign in to comment.