Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/npm_and_yarn/memfs-4.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
arboleya authored Oct 20, 2024
2 parents 4d461c6 + 9ebd6c2 commit 46bb06d
Show file tree
Hide file tree
Showing 132 changed files with 3,481 additions and 786 deletions.
4 changes: 0 additions & 4 deletions .changeset/angry-buses-jam.md

This file was deleted.

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: 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.

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.

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
4 changes: 0 additions & 4 deletions apps/create-fuels-counter-guide/fuel-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
[toolchain]
channel = "testnet"

[components]
forc = "0.63.5"
fuel-core = "0.35.0"
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());
});
});
24 changes: 24 additions & 0 deletions apps/docs-snippets/src/guide/provider/querying-the-chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,28 @@ describe('querying the chain', () => {
expect(messageProof?.amount.toNumber()).toEqual(100);
expect(messageProof?.sender.toHexString()).toEqual(result.id);
});

it('get transactions', async () => {
using launched = await launchTestNode();
const {
provider: testProvider,
wallets: [wallet, receiver],
} = launched;

const tx = await wallet.transfer(receiver.address, 100);
await tx.waitForResult();
const FUEL_NETWORK_URL = testProvider.url;

// #region get-transactions
// #import { Provider };

const provider = await Provider.create(FUEL_NETWORK_URL);

const { transactions } = await provider.getTransactions();
// #endregion get-transactions

expect(transactions).toBeDefined();
expect(transactions.length).toBe(2);
// Includes base asset minting tx
});
});
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());
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Script With Configurable', () => {
const tx = script.functions.main(argument);

// Set the call parameters
tx.callParams({ gasLimit: 1500 });
tx.callParams({ gasLimit: 1700 });

// Get the entire transaction request prior to
const txRequest = await tx.getTransactionRequest();
Expand Down
3 changes: 2 additions & 1 deletion apps/docs-snippets/src/guide/types/address.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ describe('Address Types', () => {
it('should successfully create new address from bech32 string', () => {
// #region address-2
const ADDRESS_BECH32 = 'fuel1elnmzsav56dqnp95sx4e2pckq36cvae9ser44m5zlvgtwxw49fmqd7e42e';
const ADDRESS_CHECKSUM = '0xcfe7b143Aca69a0984b481ab950716047586772586475AEE82fb10B719d52A76';

const address = new Address(ADDRESS_BECH32);

expect(address.toString()).toEqual(ADDRESS_BECH32);
expect(address.toString()).toEqual(ADDRESS_CHECKSUM);
expect(address.bech32Address).toEqual(ADDRESS_BECH32);
// #endregion address-2
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const { contract } = await deploy.waitForResult();
// #region transaction-parameters-6
const txParams: TxParams = {
// #region transaction-parameters-1
gasLimit: bn(69242),
gasLimit: bn(70935),
// #endregion transaction-parameters-1
// #region transaction-parameters-2
maxFee: bn(69242),
Expand Down
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}
Loading

0 comments on commit 46bb06d

Please sign in to comment.