Skip to content

Commit

Permalink
feat: support configurable constants on Scripts (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf authored May 18, 2023
1 parent 4199c3e commit 8c42ba2
Show file tree
Hide file tree
Showing 42 changed files with 485 additions and 149 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-kings-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/script": minor
---

add support for configurable constants on scripts
3 changes: 2 additions & 1 deletion apps/docs-snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"build": "pnpm forc build -p projects"
},
"devDependencies": {
"fuels": "workspace:*"
"fuels": "workspace:*",
"@fuel-ts/utils": "workspace:*"
},
"keywords": [],
"author": "",
Expand Down
1 change: 1 addition & 0 deletions apps/docs-snippets/projects/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"counter",
"echo-enum",
"log-values",
"sum-script",
"echo-values",
"simple-token",
"sum-option-u8",
Expand Down
24 changes: 5 additions & 19 deletions apps/docs-snippets/projects/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { readFileSync } from 'fs';
import { getForcProject } from '@fuel-ts/utils/test';
import type { JsonFlatAbi } from 'fuels';
import { join } from 'path';

export enum SnippetProjectEnum {
COUNTER = 'counter',
ECHO_ENUM = 'echo-enum',
LOG_VALUES = 'log-values',
SUM_SCRIPT = 'sum-script',
ECHO_VALUES = 'echo-values',
SIMPLE_TOKEN = 'simple-token',
SUM_OPTION_U8 = 'sum-option-u8',
Expand All @@ -17,21 +19,5 @@ export enum SnippetProjectEnum {
WHITELISTED_ADDRESS_PREDICATE = 'whitelisted-address-predicate',
}

const getSnippetContractPath = (project: SnippetProjectEnum) =>
join(__dirname, project, 'out', 'debug');

const getSnippetContractAbiPath = (project: SnippetProjectEnum) =>
join(getSnippetContractPath(project), `${project}-abi.json`);

const getSnippetContractBinPath = (project: SnippetProjectEnum) =>
join(getSnippetContractPath(project), `${project}.bin`);

export const getSnippetContractArtifacts = (project: SnippetProjectEnum) => {
const abi = JSON.parse(readFileSync(getSnippetContractAbiPath(project), 'utf-8'));
const bin = readFileSync(getSnippetContractBinPath(project));

return {
abi,
bin,
};
};
export const getSnippetProjectArtifacts = (project: SnippetProjectEnum) =>
getForcProject<JsonFlatAbi>(join(__dirname, project));
7 changes: 7 additions & 0 deletions apps/docs-snippets/projects/sum-script/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["FuelLabs"]
entry = "main.sw"
license = "Apache-2.0"
name = "sum-script"

[dependencies]
11 changes: 11 additions & 0 deletions apps/docs-snippets/projects/sum-script/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// #region script-with-configurable-contants-1
script;

configurable {
AMOUNT: u8 = 10
}

fn main(inpputed_amount: u8) -> u8 {
inpputed_amount + AMOUNT
}
// #endregion script-with-configurable-contants-1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #region predicates-with-configurable-constants-1
// #region predicate-with-configurable-constants-1
predicate;

configurable {
Expand All @@ -8,4 +8,4 @@ configurable {
fn main(address: b256) -> bool {
WHITELISTED == address
}
// #endregion predicates-with-configurable-constants-1
// #endregion predicate-with-configurable-constants-1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Contract } from 'fuels';
import { BN, NativeAssetId, ContractFactory } from 'fuels';

import { getSnippetContractArtifacts, SnippetProjectEnum } from '../../../projects';
import { getSnippetProjectArtifacts, SnippetProjectEnum } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
Expand All @@ -10,9 +10,11 @@ describe(__filename, () => {
beforeAll(async () => {
const wallet = await getTestWallet();

const { abi, bin } = getSnippetContractArtifacts(SnippetProjectEnum.RETURN_CONTEXT);
const { abiContents, binHelixfied } = getSnippetProjectArtifacts(
SnippetProjectEnum.RETURN_CONTEXT
);

const factory = new ContractFactory(bin, abi, wallet);
const factory = new ContractFactory(binHelixfied, abiContents, wallet);

contract = await factory.deployContract();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Contract } from 'fuels';
import { Provider, WalletUnlocked, ContractFactory } from 'fuels';

import { SnippetProjectEnum, getSnippetContractArtifacts } from '../../../projects';
import { SnippetProjectEnum, getSnippetProjectArtifacts } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
Expand All @@ -10,9 +10,11 @@ describe(__filename, () => {
beforeAll(async () => {
const wallet = await getTestWallet();

const { abi, bin } = getSnippetContractArtifacts(SnippetProjectEnum.RETURN_CONTEXT);
const { abiContents, binHelixfied } = getSnippetProjectArtifacts(
SnippetProjectEnum.RETURN_CONTEXT
);

const contractFactory = new ContractFactory(bin, abi, wallet);
const contractFactory = new ContractFactory(binHelixfied, abiContents, wallet);

deployedContract = await contractFactory.deployContract();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { WalletUnlocked } from 'fuels';
import { ContractFactory } from 'fuels';

import { getSnippetContractArtifacts, SnippetProjectEnum } from '../../../projects';
import { getSnippetProjectArtifacts, SnippetProjectEnum } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
let wallet: WalletUnlocked;

const { abi, bin } = getSnippetContractArtifacts(SnippetProjectEnum.ECHO_CONFIGURABLES);
const { abiContents: abi, binHelixfied: bin } = getSnippetProjectArtifacts(
SnippetProjectEnum.ECHO_CONFIGURABLES
);

const defaultValues = {
age: 25,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Contract } from 'fuels';
import { Wallet, BN, ContractFactory, NativeAssetId } from 'fuels';

import { getSnippetContractArtifacts, SnippetProjectEnum } from '../../../projects';
import { getSnippetProjectArtifacts, SnippetProjectEnum } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
Expand All @@ -10,9 +10,11 @@ describe(__filename, () => {
beforeAll(async () => {
const wallet = await getTestWallet();

const { abi, bin } = getSnippetContractArtifacts(SnippetProjectEnum.TRANSFER_TO_ADDRESS);
const { abiContents, binHelixfied } = getSnippetProjectArtifacts(
SnippetProjectEnum.TRANSFER_TO_ADDRESS
);

const factory = new ContractFactory(bin, abi, wallet);
const factory = new ContractFactory(binHelixfied, abiContents, wallet);

contract = await factory.deployContract();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Contract } from 'fuels';
import { ContractFactory, NativeAssetId } from 'fuels';

import { SnippetProjectEnum, getSnippetContractArtifacts } from '../../../projects';
import { SnippetProjectEnum, getSnippetProjectArtifacts } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
Expand All @@ -10,9 +10,11 @@ describe(__filename, () => {
beforeAll(async () => {
const wallet = await getTestWallet();

const { abi, bin } = getSnippetContractArtifacts(SnippetProjectEnum.RETURN_CONTEXT);
const { abiContents, binHelixfied } = getSnippetProjectArtifacts(
SnippetProjectEnum.RETURN_CONTEXT
);

const contractFactory = new ContractFactory(bin, abi, wallet);
const contractFactory = new ContractFactory(binHelixfied, abiContents, wallet);

contract = await contractFactory.deployContract();
});
Expand Down
8 changes: 5 additions & 3 deletions apps/docs-snippets/src/guide/contracts/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Contract } from 'fuels';
import { ContractFactory } from 'fuels';

import { SnippetProjectEnum, getSnippetContractArtifacts } from '../../../projects';
import { SnippetProjectEnum, getSnippetProjectArtifacts } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
Expand All @@ -10,9 +10,11 @@ describe(__filename, () => {
beforeAll(async () => {
const wallet = await getTestWallet();

const { abi, bin } = getSnippetContractArtifacts(SnippetProjectEnum.ECHO_VALUES);
const { abiContents, binHelixfied } = getSnippetProjectArtifacts(
SnippetProjectEnum.ECHO_VALUES
);

const factory = new ContractFactory(bin, abi, wallet);
const factory = new ContractFactory(binHelixfied, abiContents, wallet);

contract = await factory.deployContract();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Contract, WalletUnlocked } from 'fuels';
import { BN, ContractFactory } from 'fuels';

import { getSnippetContractArtifacts, SnippetProjectEnum } from '../../../projects';
import { getSnippetProjectArtifacts, SnippetProjectEnum } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
Expand All @@ -13,18 +13,18 @@ describe(__filename, () => {
beforeAll(async () => {
wallet = await getTestWallet();

const tokenArtifacts = getSnippetContractArtifacts(SnippetProjectEnum.SIMPLE_TOKEN);
const depositorArtifacts = getSnippetContractArtifacts(SnippetProjectEnum.TOKEN_DEPOSITOR);
const tokenArtifacts = getSnippetProjectArtifacts(SnippetProjectEnum.SIMPLE_TOKEN);
const depositorArtifacts = getSnippetProjectArtifacts(SnippetProjectEnum.TOKEN_DEPOSITOR);

simpleToken = await new ContractFactory(
tokenArtifacts.bin,
tokenArtifacts.abi,
tokenArtifacts.binHelixfied,
tokenArtifacts.abiContents,
wallet
).deployContract();

tokenDepositor = await new ContractFactory(
depositorArtifacts.bin,
depositorArtifacts.abi,
depositorArtifacts.binHelixfied,
depositorArtifacts.abiContents,
wallet
).deployContract();
});
Expand Down
6 changes: 3 additions & 3 deletions apps/docs-snippets/src/guide/contracts/logs.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Contract } from 'fuels';
import { BN, ContractFactory } from 'fuels';

import { getSnippetContractArtifacts, SnippetProjectEnum } from '../../../projects';
import { getSnippetProjectArtifacts, SnippetProjectEnum } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
Expand All @@ -10,9 +10,9 @@ describe(__filename, () => {
beforeAll(async () => {
const wallet = await getTestWallet();

const { abi, bin } = getSnippetContractArtifacts(SnippetProjectEnum.LOG_VALUES);
const { abiContents, binHelixfied } = getSnippetProjectArtifacts(SnippetProjectEnum.LOG_VALUES);

const factory = new ContractFactory(bin, abi, wallet);
const factory = new ContractFactory(binHelixfied, abiContents, wallet);

contract = await factory.deployContract();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { AbstractAddress, WalletUnlocked } from 'fuels';
import { ContractFactory, Contract } from 'fuels';

import { SnippetProjectEnum, getSnippetContractArtifacts } from '../../../projects';
import { SnippetProjectEnum, getSnippetProjectArtifacts } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
let contract: Contract;
let contractId: AbstractAddress;
let wallet: WalletUnlocked;
const { abi, bin } = getSnippetContractArtifacts(SnippetProjectEnum.ECHO_VALUES);
const { abiContents: abi, binHelixfied: bin } = getSnippetProjectArtifacts(
SnippetProjectEnum.ECHO_VALUES
);

beforeAll(async () => {
wallet = await getTestWallet();
Expand Down
28 changes: 20 additions & 8 deletions apps/docs-snippets/src/guide/contracts/multicalls.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Contract } from 'fuels';
import { NativeAssetId, BN, ContractFactory } from 'fuels';

import { getSnippetContractArtifacts, SnippetProjectEnum } from '../../../projects';
import { getSnippetProjectArtifacts, SnippetProjectEnum } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
Expand All @@ -12,13 +12,25 @@ describe(__filename, () => {
beforeAll(async () => {
const wallet = await getTestWallet();

const counterArtifacts = getSnippetContractArtifacts(SnippetProjectEnum.COUNTER);
const echoArtifacts = getSnippetContractArtifacts(SnippetProjectEnum.ECHO_VALUES);
const contextArtifacts = getSnippetContractArtifacts(SnippetProjectEnum.RETURN_CONTEXT);

const factory1 = new ContractFactory(echoArtifacts.bin, echoArtifacts.abi, wallet);
const factory2 = new ContractFactory(counterArtifacts.bin, counterArtifacts.abi, wallet);
const factory3 = new ContractFactory(contextArtifacts.bin, contextArtifacts.abi, wallet);
const counterArtifacts = getSnippetProjectArtifacts(SnippetProjectEnum.COUNTER);
const echoArtifacts = getSnippetProjectArtifacts(SnippetProjectEnum.ECHO_VALUES);
const contextArtifacts = getSnippetProjectArtifacts(SnippetProjectEnum.RETURN_CONTEXT);

const factory1 = new ContractFactory(
echoArtifacts.binHelixfied,
echoArtifacts.abiContents,
wallet
);
const factory2 = new ContractFactory(
counterArtifacts.binHelixfied,
counterArtifacts.abiContents,
wallet
);
const factory3 = new ContractFactory(
contextArtifacts.binHelixfied,
contextArtifacts.abiContents,
wallet
);

echoContract = await factory1.deployContract();
counterContract = await factory2.deployContract();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Contract } from 'fuels';
import { ContractFactory } from 'fuels';

import { SnippetProjectEnum, getSnippetContractArtifacts } from '../../../projects';
import { SnippetProjectEnum, getSnippetProjectArtifacts } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
Expand All @@ -10,9 +10,11 @@ describe(__filename, () => {
beforeAll(async () => {
const wallet = await getTestWallet();

const { abi, bin } = getSnippetContractArtifacts(SnippetProjectEnum.ECHO_VALUES);
const { abiContents, binHelixfied } = getSnippetProjectArtifacts(
SnippetProjectEnum.ECHO_VALUES
);

const factory = new ContractFactory(bin, abi, wallet);
const factory = new ContractFactory(binHelixfied, abiContents, wallet);

contract = await factory.deployContract();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Contract } from 'fuels';
import { BN, ContractFactory } from 'fuels';

import { SnippetProjectEnum, getSnippetContractArtifacts } from '../../../projects';
import { SnippetProjectEnum, getSnippetProjectArtifacts } from '../../../projects';
import { getTestWallet } from '../../utils';

describe(__filename, () => {
Expand All @@ -10,9 +10,9 @@ describe(__filename, () => {
beforeAll(async () => {
const wallet = await getTestWallet();

const { abi, bin } = getSnippetContractArtifacts(SnippetProjectEnum.COUNTER);
const { abiContents, binHelixfied } = getSnippetProjectArtifacts(SnippetProjectEnum.COUNTER);

const factory = new ContractFactory(bin, abi, wallet);
const factory = new ContractFactory(binHelixfied, abiContents, wallet);

contract = await factory.deployContract();
});
Expand Down
Loading

0 comments on commit 8c42ba2

Please sign in to comment.