Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: predicate and script deploys #3266

Merged
merged 22 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 1 addition & 7 deletions apps/docs-snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
"description": "",
"private": true,
"scripts": {
"pretest": "run-s fuels:build type:check",
"xpretest": "run-s kill-node fuels:build fuels:node fuels:deploy kill-node type:check",
"kill-node": "lsof -t -i:4000 | xargs -r kill",
"fuels:deploy": "pnpm fuels deploy",
"fuels:node": "pnpm fuels node > /dev/null 2>&1 &",
"fuels:build": "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
@@ -1,17 +1,16 @@
import { readFileSync } from 'fs';
import { ContractFactory, Predicate, Provider, Wallet, hexlify } from 'fuels';
import { ContractFactory, Provider, Wallet, bn, hexlify } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';
import { join } from 'path';

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

/**
* @group browser
* @group node
*
* TODO: enable the test and reintroduce the docs
*/
describe.skip('Deploying Predicates', () => {
describe('Deploying Predicates', () => {
it('deploys a predicate via loader and calls', async () => {
using launched = await launchTestNode();

Expand All @@ -33,49 +32,48 @@ describe.skip('Deploying Predicates', () => {
const { waitForResult: waitForDeploy } = await factory.deployAsBlobTxForScript();
await waitForDeploy();

const loaderBytecode = hexlify(
readFileSync(
join(
__dirname,
'../../../test/fixtures/forc-projects/configurable-pin/out/release/configurable-pin.deployed.bin'
)
)
);

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

// First, we will need the loader bytecode that is generated by `fuels deploy`
// #context const loaderBytecode = hexlify(readFileSync('path/to/forc/build/outputs')));
// #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();

// Then we will instantiate the predicate using both the scripts bytecode and it's loader bytecode,
// now we are free to interact with the predicate as we would normally, such as overriding the configurables
const predicate = new Predicate({
bytecode: loaderBytecode,
abi: TypegenPredicate.abi,
data: [1337],
// 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: [bn(23)],
provider,
configurableConstants: {
PIN: 23,
},
});

// First, let's fund the predicate
const { waitForResult: waitForFund } = await wallet.transfer(predicate.address, 100_000);
// Now, let's fund the predicate
const { waitForResult: waitForFund } = await wallet.transfer(
predicate.address,
100_000,
baseAssetId
);
await waitForFund();

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

const anotherPredicate = new Predicate({
bytecode: TypegenPredicate.bytecode,
abi: TypegenPredicate.abi,
data: [1337],
const anotherPredicate = new TypegenPredicate({
data: [23],
provider,
configurableConstants: {
PIN: 23,
},
});

const { waitForResult: waitForAnotherFund } = await wallet.transfer(
Expand All @@ -91,6 +89,6 @@ describe.skip('Deploying Predicates', () => {
const { gasUsed: anotherGasUsed } = await waitForAnotherTransfer();

expect(recieverInitialBalance.toNumber()).toBeLessThan(recieverInitialBalance.toNumber());
expect(gasUsed.toNumber()).toBeLessThan(anotherGasUsed.toNumber());
expect(gasUsed.toNumber()).toBeLessThanOrEqual(anotherGasUsed.toNumber());
});
});
39 changes: 13 additions & 26 deletions apps/docs-snippets/src/guide/scripts/deploying-scripts.test.ts
danielbate marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { readFileSync } from 'fs';
import { ContractFactory, Provider, Script, Wallet, hexlify } from 'fuels';
import { ContractFactory, Provider, Wallet, hexlify } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';
import { join } from 'path';

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

/**
* @group browser
* @group node
*
* TODO: enable the test and reintroduce the docs
*/
describe.skip('Deploying Scripts', () => {
describe('Deploying Scripts', () => {
it('deploys a script via loader and calls', async () => {
using launched = await launchTestNode();

Expand All @@ -27,29 +26,17 @@ describe.skip('Deploying Scripts', () => {
const { waitForResult: waitForDeploy } = await factory.deployAsBlobTxForScript();
await waitForDeploy();

const loaderBytecode = hexlify(
readFileSync(
join(
__dirname,
'../../../test/fixtures/forc-projects/sum-script/out/release/sum-script-loader.bin'
)
)
);

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

// First, we will need the loader bytecode that is generated by `fuels deploy`
// #context const loaderBytecode = hexlify(readFileSync('path/to/forc/build/outputs')));
// #context import { TypegenScriptLoader } from 'path/to/typegen/outputs';

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

// Then we will instantiate the script using both the scripts bytecode and it's loader bytecode
const script = new Script(loaderBytecode, TypegenScript.abi, wallet);
// 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 = {
Expand All @@ -61,13 +48,13 @@ describe.skip('Deploying Scripts', () => {
const { value, gasUsed } = await waitForResult();
// #endregion deploying-scripts

danielbate marked this conversation as resolved.
Show resolved Hide resolved
const scriptWithoutLoader = new Script(TypegenScript.bytecode, TypegenScript.abi, wallet);
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()).toBeLessThan(anotherGasUsed.toNumber());
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#fuels-deploy).

Check failure on line 3 in apps/docs/src/guide/predicates/deploying-predicates.md

View workflow job for this annotation

GitHub Actions / Lint

commands does not exist

By deploying the predicate, it's bytecode is stored on chain as a blob. The SDK will then produce bytecode that can load the blob on demand that can execute the predicate. This far reduces the repeat execution cost of the predicate.
danielbate marked this conversation as resolved.
Show resolved Hide resolved

## 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#fuels-deploy).

Check failure on line 9 in apps/docs/src/guide/predicates/deploying-predicates.md

View workflow job for this annotation

GitHub Actions / Lint

commands does not exist
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved

This will perform the following actions:

1. Compile the script using your `forc` version
1. Deploys the built script binary to the chain as a blob
1. Generates a script that loads the blob that can be used to execute the script
1. Generates types for both the script and the loader that you can use in your application
danielbate marked this conversation as resolved.
Show resolved Hide resolved

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#fuels-deploy).

Check failure on line 3 in apps/docs/src/guide/scripts/deploying-scripts.md

View workflow job for this annotation

GitHub Actions / Lint

commands does not exist

By deploying the script, it's bytecode is stored on chain as a blob. The SDK will then produce bytecode that can load the blob on demand that can execute the script. This far reduces the repeat execution cost of the script.
danielbate marked this conversation as resolved.
Show resolved Hide resolved

## 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#fuels-deploy).

Check failure on line 9 in apps/docs/src/guide/scripts/deploying-scripts.md

View workflow job for this annotation

GitHub Actions / Lint

commands does not exist
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved

This will perform the following actions:

1. Compile the script using your `forc` version
1. Deploys the built script binary to the chain as a blob
1. Generates a script that loads the blob that can be used to execute the script
1. Generates types for both the script and the loader that you can use in your application
danielbate marked this conversation as resolved.
Show resolved Hide resolved

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