Skip to content

Commit

Permalink
Merge branch 'st/chore/add-missing-panic-reasons-to-constant' of gith…
Browse files Browse the repository at this point in the history
…ub.com:FuelLabs/fuels-ts into st/chore/add-missing-panic-reasons-to-constant
  • Loading branch information
Torres-ssf committed Aug 6, 2024
2 parents 809be2e + 0475790 commit a4e004f
Show file tree
Hide file tree
Showing 160 changed files with 2,841 additions and 1,665 deletions.
9 changes: 9 additions & 0 deletions .changeset/empty-lemons-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@fuel-ts/abi-typegen": minor
"@fuel-ts/account": minor
"@fuel-ts/contract": minor
"create-fuels": minor
"fuels": minor
---

feat!: prettify `typegen` api
7 changes: 3 additions & 4 deletions apps/create-fuels-counter-guide/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";

import type { TestContractAbi } from "@/sway-api";
import { TestContractAbi__factory } from "@/sway-api";
import { TestContract } from "@/sway-api";
import contractIds from "@/sway-api/contract-ids.json";
import { FuelLogo } from "@/components/FuelLogo";
import { bn } from "fuels";
Expand All @@ -22,7 +21,7 @@ const contractId =

export default function Home() {
const { wallet, walletBalance, refreshWalletBalance } = useActiveWallet();
const [contract, setContract] = useState<TestContractAbi>();
const [contract, setContract] = useState<TestContract>();
const [counter, setCounter] = useState<number>();

/**
Expand All @@ -32,7 +31,7 @@ export default function Home() {
useAsync(async () => {
if (wallet) {
// Create a new instance of the contract
const testContract = TestContractAbi__factory.connect(contractId, wallet);
const testContract = new TestContract(contractId, wallet);
setContract(testContract);

// Read the current value of the counter
Expand Down
24 changes: 14 additions & 10 deletions apps/create-fuels-counter-guide/src/app/predicate/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { FuelLogo } from "@/components/FuelLogo";
import { Input } from "@/components/Input";
import { Link } from "@/components/Link";
import { useActiveWallet } from "@/hooks/useActiveWallet";
import { TestPredicate } from "@/sway-api/predicates/index";
import { FAUCET_LINK } from '@/lib';
import { TestPredicateAbi__factory } from "@/sway-api/predicates/index";
import { BN, InputValue, Predicate } from "fuels";
import { bn } from "fuels";
import { useState } from "react";
Expand All @@ -28,9 +28,9 @@ export default function PredicateExample() {
if (wallet) {
baseAssetId = wallet.provider.getBaseAssetId();
// Initialize a new predicate instance
const predicate = TestPredicateAbi__factory.createInstance(
wallet.provider,
);
const predicate = new TestPredicate({
provider: wallet.provider
});
setPredicate(predicate);
setPredicateBalance(await predicate.getBalance());
}
Expand Down Expand Up @@ -76,10 +76,10 @@ export default function PredicateExample() {
}

// Initialize a new predicate instance with the entered pin
const reInitializePredicate = TestPredicateAbi__factory.createInstance(
wallet.provider,
[bn(pin)],
);
const reInitializePredicate = new TestPredicate({
provider: wallet.provider,
data: [bn(pin)],
});

if (!reInitializePredicate) {
return toast.error("Failed to initialize predicate");
Expand Down Expand Up @@ -133,9 +133,13 @@ export default function PredicateExample() {
return toast.error("Please enter a pin");
}

const configurable = { PIN: bn(pin) };
const configurableConstants = { PIN: bn(pin) };
// instantiate predicate with configurable constants
const reInitializePredicate = TestPredicateAbi__factory.createInstance(wallet.provider, [bn(configurable.PIN)], configurable);
const reInitializePredicate = new TestPredicate({
provider: wallet.provider,
data: [configurableConstants.PIN],
configurableConstants,
});

if (!reInitializePredicate) {
return toast.error("Failed to initialize predicate");
Expand Down
4 changes: 2 additions & 2 deletions apps/create-fuels-counter-guide/src/app/script/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FuelLogo } from "@/components/FuelLogo";
import { Input } from "@/components/Input";
import { Link } from "@/components/Link";
import { useActiveWallet } from "@/hooks/useActiveWallet";
import { TestScriptAbi__factory } from "@/sway-api";
import { TestScript } from "@/sway-api";
import { BN, BigNumberish, Script, bn } from "fuels";
import { useState } from "react";
import toast from "react-hot-toast";
Expand All @@ -21,7 +21,7 @@ export default function ScriptExample() {
useAsync(async () => {
if (wallet) {
// Initialize script instance
const script = TestScriptAbi__factory.createInstance(wallet);
const script = new TestScript(wallet);
setScript(script);
}
}, [wallet]);
Expand Down
35 changes: 17 additions & 18 deletions apps/demo-bun-fuels/src/bun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* It ensures that built code is fully working.
*/

import { ContractFactory, Provider, toHex, Wallet, FUEL_NETWORK_URL } from 'fuels';
import { Provider, toHex, Wallet, FUEL_NETWORK_URL } from 'fuels';
import { generateTestWallet, safeExec } from 'fuels/test-utils';

import { SampleAbi__factory } from './sway-programs-api';
import bytecode from './sway-programs-api/contracts/SampleAbi.hex';
import { Sample, SampleFactory } from './sway-programs-api';

let baseAssetId: string;

Expand All @@ -27,8 +26,8 @@ describe('ExampleContract', () => {
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);

// Deploy
const factory = new ContractFactory(bytecode, SampleAbi__factory.abi, wallet);
const { waitForResult } = await factory.deployContract();
const factory = new SampleFactory(wallet);
const { waitForResult } = await factory.deploy();
const { contract } = await waitForResult();

// Call
Expand All @@ -41,20 +40,20 @@ describe('ExampleContract', () => {
expect(value.toHex()).toEqual(toHex(1337));

// You can also make a call using the factory
const contractInstance = SampleAbi__factory.connect(contract.id, wallet);
const contractInstance = new Sample(contract.id, wallet);
const call2 = await contractInstance.functions.return_input(1337).call();

// Wait for result
const { value: v2 } = await call2.waitForResult();
expect(v2.toHex()).toBe(toHex(1337));
});

it('deployContract method', async () => {
it('deploy method', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);

// Deploy
const deploy = await SampleAbi__factory.deployContract(bytecode, wallet);
const deploy = await SampleFactory.deploy(wallet);
const { contract } = await deploy.waitForResult();

// Call
Expand All @@ -72,10 +71,10 @@ describe('ExampleContract', () => {
const fundedWallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
const unfundedWallet = Wallet.generate({ provider });

const factory = new ContractFactory(bytecode, SampleAbi__factory.abi, fundedWallet);
const { waitForResult } = await factory.deployContract();
const { contract } = await waitForResult();
const contractInstance = SampleAbi__factory.connect(contract.id, unfundedWallet);
const deploy = await SampleFactory.deploy(fundedWallet);
const { contract } = await deploy.waitForResult();

const contractInstance = new Sample(contract.id, unfundedWallet);

const { error } = await safeExec(() =>
contractInstance.functions.return_input(1337).simulate()
Expand All @@ -89,24 +88,24 @@ describe('ExampleContract', () => {
const fundedWallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
const unfundedWallet = Wallet.generate({ provider });

const factory = new ContractFactory(bytecode, SampleAbi__factory.abi, fundedWallet);
const { waitForResult } = await factory.deployContract();
const { contract } = await waitForResult();
const contractInstance = SampleAbi__factory.connect(contract.id, unfundedWallet);
const deploy = await SampleFactory.deploy(fundedWallet);
const { contract } = await deploy.waitForResult();

const contractInstance = new Sample(contract.id, unfundedWallet);

await expect(contractInstance.functions.return_input(1337).dryRun()).resolves.not.toThrow();
});

it('should demo how to use generated files just fine', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
const { waitForResult } = await SampleAbi__factory.deployContract(bytecode, wallet);
const { waitForResult } = await SampleFactory.deploy(wallet);
const { contract: depoloyed } = await waitForResult();
const contractsIds = {
sample: depoloyed.id,
};

const contract = SampleAbi__factory.connect(contractsIds.sample, wallet);
const contract = new Sample(contractsIds.sample, wallet);

const { value } = await contract.functions.return_input(1337).dryRun();

Expand Down
7 changes: 4 additions & 3 deletions apps/demo-fuels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"description": "Simple demo using Fuels CLI",
"author": "Fuel Labs <contact@fuel.sh> (https://fuel.network/)",
"scripts": {
"pretest": "pnpm fuels build"
"build": "pnpm fuels build",
"pretest": "pnpm build",
"test": "cd ../.. && pnpm run test:filter apps/demo-fuels"
},
"license": "Apache-2.0",
"dependencies": {
"@fuel-ts/errors": "workspace:^",
"@fuel-ts/account": "workspace:^",
"fuels": "workspace:*"
},
"version": "0.0.0"
}
}
35 changes: 16 additions & 19 deletions apps/demo-fuels/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* It ensures that built code is fully working.
*/

import { ContractFactory, Provider, toHex, Wallet, FUEL_NETWORK_URL } from 'fuels';
import { Provider, toHex, Wallet, FUEL_NETWORK_URL } from 'fuels';
import { generateTestWallet, safeExec } from 'fuels/test-utils';

import { SampleAbi__factory } from './sway-programs-api';
import bytecode from './sway-programs-api/contracts/SampleAbi.hex';
import { SampleFactory, Sample } from './sway-programs-api';

let baseAssetId: string;

Expand All @@ -27,8 +26,7 @@ describe('ExampleContract', () => {
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);

// Deploy
const factory = new ContractFactory(bytecode, SampleAbi__factory.abi, wallet);
const deploy = await factory.deployContract();
const deploy = await SampleFactory.deploy(wallet);
const { contract } = await deploy.waitForResult();

// Call
Expand All @@ -39,18 +37,18 @@ describe('ExampleContract', () => {
expect(value.toHex()).toEqual(toHex(1337));

// You can also make a call using the factory
const contractInstance = SampleAbi__factory.connect(contract.id, wallet);
const contractInstance = new Sample(contract.id, wallet);
const call2 = await contractInstance.functions.return_input(1337).call();
const { value: v2 } = await call2.waitForResult();
expect(v2.toHex()).toBe(toHex(1337));
});

it('deployContract method', async () => {
it('should deploy contract', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);

// Deploy
const deploy = await SampleAbi__factory.deployContract(bytecode, wallet);
const deploy = await SampleFactory.deploy(wallet);
const { contract } = await deploy.waitForResult();

// Call
Expand All @@ -66,10 +64,10 @@ describe('ExampleContract', () => {
const fundedWallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
const unfundedWallet = Wallet.generate({ provider });

const factory = new ContractFactory(bytecode, SampleAbi__factory.abi, fundedWallet);
const { waitForResult } = await factory.deployContract();
const { contract } = await waitForResult();
const contractInstance = SampleAbi__factory.connect(contract.id, unfundedWallet);
const deploy = await SampleFactory.deploy(fundedWallet);
const { contract } = await deploy.waitForResult();

const contractInstance = new Sample(contract.id, unfundedWallet);

const { error } = await safeExec(() =>
contractInstance.functions.return_input(1337).simulate()
Expand All @@ -83,25 +81,24 @@ describe('ExampleContract', () => {
const fundedWallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
const unfundedWallet = Wallet.generate({ provider });

const factory = new ContractFactory(bytecode, SampleAbi__factory.abi, fundedWallet);
const { waitForResult } = await factory.deployContract();
const { waitForResult } = await SampleFactory.deploy(fundedWallet);
const { contract } = await waitForResult();
const contractInstance = SampleAbi__factory.connect(contract.id, unfundedWallet);
const contractInstance = new Sample(contract.id, unfundedWallet);

await expect(contractInstance.functions.return_input(1337).dryRun()).resolves.not.toThrow();
});

it('should demo how to use generated files just fine', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
const { waitForResult } = await SampleAbi__factory.deployContract(bytecode, wallet);
const { contract: depoloyed } = await waitForResult();
const deploy = await SampleFactory.deploy(wallet);
const { contract: depoloyed } = await deploy.waitForResult();
const contractsIds = {
sample: depoloyed.id,
};

// #region using-generated-files
// #context import { SampleAbi__factory } from './sway-programs-api';
// #context import { Sample } from './sway-programs-api';
// #context import contractsIds from './sway-programs-api/contract-ids.json';

// #context /**
Expand All @@ -110,7 +107,7 @@ describe('ExampleContract', () => {
// #context */

// #context const wallet = new Wallet.fromPrivateKey(process.env.PRIVATE_KEY);
const contract = SampleAbi__factory.connect(contractsIds.sample, wallet);
const contract = new Sample(contractsIds.sample, wallet);

const { value } = await contract.functions.return_input(1337).dryRun();

Expand Down
7 changes: 7 additions & 0 deletions apps/demo-fuels/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src"]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "demo-contract"

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "script"
name = "demo-predicate"

[dependencies]
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "predicate"
name = "demo-script"

[dependencies]
File renamed without changes.
15 changes: 7 additions & 8 deletions apps/demo-typegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"scripts": {
"pretest": "run-s build:forc build:types",
"build:forc": "run-p forc:*",
"forc:contract": "pnpm fuels-forc build -p contract --release",
"forc:script": "pnpm fuels-forc build -p script --release",
"forc:predicate": "pnpm fuels-forc build -p predicate --release",
"forc:contract": "pnpm fuels-forc build -p demo-contract --release",
"forc:script": "pnpm fuels-forc build -p demo-script --release",
"forc:predicate": "pnpm fuels-forc build -p demo-predicate --release",
"build:types": "run-p types:*",
"types:contract": "pnpm fuels typegen -i contract/out/release/demo-contract-abi.json -o src/contract-types",
"types:script": "pnpm fuels typegen -i script/out/release/script-abi.json -o src/script-types --script",
"types:predicate": "pnpm fuels typegen -i predicate/out/release/predicate-abi.json -o src/predicate-types --predicate"
"types:contract": "pnpm fuels typegen -i demo-contract/out/release/demo-contract-abi.json -o src/contract-types",
"types:script": "pnpm fuels typegen -i demo-script/out/release/demo-script-abi.json -o src/script-types --script",
"types:predicate": "pnpm fuels typegen -i demo-predicate/out/release/demo-predicate-abi.json -o src/predicate-types --predicate"
},
"license": "Apache-2.0",
"dependencies": {
Expand All @@ -22,6 +22,5 @@
"@fuel-ts/account": "workspace:*",
"@fuel-ts/errors": "workspace:*",
"@internal/forc": "workspace:*"
},
"version": "0.0.0"
}
}
Loading

0 comments on commit a4e004f

Please sign in to comment.