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

chore: adding type checking #3046

Merged
merged 20 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions .changeset/odd-seahorses-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

chore: adding type checking
arboleya marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 3 additions & 2 deletions apps/docs-snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "",
"private": true,
"scripts": {
"pretest": "pnpm build:forc",
"build:forc": "pnpm fuels build"
"pretest": "run-s build:forc type:check",
"build:forc": "pnpm fuels build",
"type:check": "tsc --noEmit"
nedsalk marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@fuel-ts/account": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions apps/docs-snippets/src/guide/types/arrays.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('Arrays Types', () => {
contracts: [contract],
} = launched;
try {
// @ts-expect-error Will throw error because the array length is not 2
// #region arrays-3
// will throw error because the array length is not 2
await contract.functions.echo_u64_array([10000000]).simulate();
Expand Down
16 changes: 11 additions & 5 deletions apps/docs-snippets/src/guide/types/contract-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { Address } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import { InputOutputTypesFactory } from '../../../test/typegen';
import type {
IdentityOutput,
AddressOutput,
ContractIdOutput,
} from '../../../test/typegen/contracts/InputOutputTypes';

/**
* @group node
Expand Down Expand Up @@ -87,9 +92,9 @@ describe('Contract Types', () => {

// #region identity-address-output
// #import { Address };
const identityFromOutput1 = callResponse1.value;
const addressStringFromOutput = identityFromOutput1.Address.bits;
const addressFromOutput: Address = Address.fromB256(addressStringFromOutput);
const identityFromOutput1: IdentityOutput = callResponse1.value;
const addressStringFromOutput: AddressOutput = identityFromOutput1.Address as AddressOutput;
const addressFromOutput: Address = Address.fromB256(addressStringFromOutput.bits);
// #endregion identity-address-output

// #region identity-contract-input
Expand All @@ -100,8 +105,9 @@ describe('Contract Types', () => {
const callResponse2 = await contract.functions.identity(contractIdentityInput).simulate();

// #region identity-contract-output
const identityFromOutput2 = callResponse2.value;
const contractIdFromOutput: string = identityFromOutput2.ContractId.bits;
const identityFromOutput2: IdentityOutput = callResponse2.value;
const contractIdOutput: ContractIdOutput = identityFromOutput2.ContractId as ContractIdOutput;
const contractIdFromOutput: string = contractIdOutput.bits;
// #endregion identity-contract-output

expect(addressFromOutput).toEqual(address);
Expand Down
3 changes: 3 additions & 0 deletions apps/docs-snippets/src/guide/types/enums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('Enums Types', () => {
const emumValue: number = 1;

await expectToThrowFuelError(
// @ts-expect-error number is not a valid type
() => contract.functions.echo_state_error_enum(emumValue).simulate(),
new FuelError(FuelError.CODES.INVALID_DECODE_VALUE, 'A field for the case must be provided.')
);
Expand All @@ -122,6 +123,7 @@ describe('Enums Types', () => {
const emumValue = 'NotStateEnumValue';

await expectToThrowFuelError(
// @ts-expect-error NotStateEnumValue is not a valid value
() => contract.functions.echo_state_error_enum(emumValue).simulate(),
new FuelError(FuelError.CODES.INVALID_DECODE_VALUE, 'Only one field must be provided.')
);
Expand All @@ -146,6 +148,7 @@ describe('Enums Types', () => {
const enumParam = { UnknownKey: 'Completed' };

await expectToThrowFuelError(
// @ts-expect-error UnknownKey is not a valid key
() => contract.functions.echo_error_enum(enumParam).simulate(),
new FuelError(
FuelError.CODES.INVALID_DECODE_VALUE,
Expand Down
2 changes: 1 addition & 1 deletion apps/docs-snippets/src/guide/types/vector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Vector Types', () => {
// #import { arrayify };
// #context import { BytecodeInputFactory } from '../path/to/typegen';

const bytecodeAsVecU8 = arrayify(BytecodeInputFactory.bytecode);
const bytecodeAsVecU8 = Array.from(arrayify(BytecodeInputFactory.bytecode));
maschad marked this conversation as resolved.
Show resolved Hide resolved

const { waitForResult } = await bytecodeContract.functions
.compute_bytecode_root(bytecodeAsVecU8)
Expand Down
7 changes: 5 additions & 2 deletions apps/docs-snippets/src/guide/wallets/connectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ class WalletConnector extends FuelConnector {
// #region fuel-connector-events-abis
const abis: Array<FuelABI> = [
{
encoding: '1',
types: [],
encodingVersion: '1',
specVersion: '1',
programType: 'contract',
concreteTypes: [],
metadataTypes: [],
loggedTypes: [],
functions: [],
messagesTypes: [],
Expand Down
5 changes: 5 additions & 0 deletions apps/docs-snippets/src/jsonc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// informs TS about `.jsonc` file extension
declare module '*.jsonc' {
const value: string;
export default value;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { EncodingVersion } from '../../utils/constants';
import { ENCODING_V1 } from '../../utils/constants';

import { getCoderForEncoding } from './getCoderForEncoding';
Expand All @@ -17,6 +18,8 @@ describe('getEncodingStrategy', () => {
});

it('throws for an unsupported encoding version', () => {
expect(() => getCoderForEncoding('2')).toThrowError('Encoding version 2 is unsupported.');
expect(() => getCoderForEncoding('2' as EncodingVersion)).toThrowError(
'Encoding version 2 is unsupported.'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('parseTypeArguments.ts', () => {
/*
Test helpers
*/
function bundleTypes(rawTypes = defautRawTypes) {
function bundleTypes(rawTypes: readonly JsonAbiType[] = defautRawTypes) {
const types = rawTypes.map((rawAbiType) => makeType({ rawAbiType }));
return types;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/abi-typegen/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src", "test"]
"include": ["src", "test"],
"exclude": ["**/__temp__/**"]
}
2 changes: 1 addition & 1 deletion packages/contract/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src", "src/**/*.json"]
"include": ["src", "src/**/*.json", "test"]
}
4 changes: 3 additions & 1 deletion packages/create-fuels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
},
"license": "Apache-2.0",
"scripts": {
"build": "tsup && tsc --noEmit",
"build": "run-s build:source type:check",
"build:source": "tsup",
"type:check": "tsc --noEmit",
"prepublishOnly": "tsx ./scripts/prepublish.ts"
},
"dependencies": {
Expand Down
12 changes: 11 additions & 1 deletion packages/crypto/test/hmac.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import type { BytesLike } from '@fuel-ts/interfaces';
import { arrayify } from '@fuel-ts/utils';

import { computeHmac } from '..';
import { computeHmac as computeHmacNode } from '..';

interface ComputeHmacNode {
(algorithm: 'sha256' | 'sha512', key: string, data: string): string;
register(func: (algorithm: 'sha256' | 'sha512', key: string, data: string) => BytesLike): void;
lock(): void;
}

/**
* @group node
*/
describe('computeHmac node', () => {
const computeHmac = computeHmacNode as ComputeHmacNode;
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved

it('should use the registered HMAC function', () => {
const key = '0x0102030405060708090a0b0c0d0e0f10';
const data = '0x11121314151617181920212223242526';
Expand Down
26 changes: 24 additions & 2 deletions packages/crypto/test/pbkdf2.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import { bufferFromString, pbkdf2 } from '..';
import { bufferFromString, pbkdf2 as pbkdf2Node } from '..';

interface PBKDF2Node {
(
password: Uint8Array,
salt: Uint8Array,
iterations: number,
keylen: number,
algo: string
): string;
register(
fn: (
password: Uint8Array,
salt: Uint8Array,
iterations: number,
keylen: number,
algo: string
) => string
maschad marked this conversation as resolved.
Show resolved Hide resolved
): void;
lock(): void;
}

/**
* @group node
*/
describe('pbkdf2 node', () => {
const pbkdf2 = pbkdf2Node as PBKDF2Node;

it('should use the registered function for PBKDF2 computation', () => {
const expectedResult = '0x90eceedd899d5cdcdfd9b315ad6e2c3391bf95cc131b6f0f016339db5ee60494';
const passwordBuffer = bufferFromString(String('password123').normalize('NFKC'), 'utf-8');
Expand All @@ -28,7 +50,7 @@ describe('pbkdf2 node', () => {
pbkdf2.lock();

expect(() => {
pbkdf2.register(() => {});
pbkdf2.register(() => '');
maschad marked this conversation as resolved.
Show resolved Hide resolved
}).toThrowError('pbkdf2 is locked');
});

Expand Down
5 changes: 3 additions & 2 deletions packages/fuel-gauge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"description": "",
"author": "Fuel Labs <[email protected]> (https://fuel.network/)",
"scripts": {
"pretest": "run-s build:forc build:process-predicates",
"pretest": "run-s build:forc build:process-predicates type:check",
"build:forc": "pnpm fuels build",
"build:process-predicates": "tsx ./scripts/process-predicates.ts"
"build:process-predicates": "tsx ./scripts/process-predicates.ts",
"type:check": "tsc --noEmit"
},
"license": "Apache-2.0",
"dependencies": {
Expand Down
13 changes: 4 additions & 9 deletions packages/fuel-gauge/src/bytecode-sway-lib.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Predicate, arrayify } from 'fuels';
import { arrayify } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import { defaultPredicateAbi } from '../test/fixtures/abi/predicate';
import { defaultPredicateBytecode } from '../test/fixtures/bytecode/predicate';
import { PredicateTrue } from '../test/typegen';
import { BytecodeSwayLibFactory } from '../test/typegen/contracts';

import { launchTestContract } from './utils';
Expand Down Expand Up @@ -58,16 +57,12 @@ describe('bytecode computations', () => {
provider,
} = launched;

const predicate = new Predicate({
bytecode: defaultPredicateBytecode,
abi: defaultPredicateAbi,
provider,
});
const predicate = new PredicateTrue({ provider });

const address = predicate.address;

const { waitForResult } = await contract.functions
.compute_predicate_address(Array.from(arrayify(defaultPredicateBytecode)))
.compute_predicate_address(Array.from(arrayify(PredicateTrue.bytecode)))
.call();

const { value } = await waitForResult();
Expand Down
4 changes: 3 additions & 1 deletion packages/fuel-gauge/src/coverage-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ describe('Coverage Contract', { timeout: 15_000 }, () => {
const INPUT = 1;

// adds the three values together, but only first param value is supplied
const { waitForResult } = await contractInstance.functions.echo_option_three_u8(INPUT).call();
const { waitForResult } = await contractInstance.functions
.echo_option_three_u8(INPUT, undefined, undefined)
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
.call();
const { value: Some } = await waitForResult();

// we receive the result of adding whatever was passed
Expand Down
17 changes: 9 additions & 8 deletions packages/fuel-gauge/src/vectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { BigNumberish, BN } from 'fuels';

import { VectorsFactory } from '../test/typegen/contracts';
import { SmallEnumInput } from '../test/typegen/contracts/CoverageContract';
import type { Vec } from '../test/typegen/contracts/common';

import { launchTestContract } from './utils';

Expand Down Expand Up @@ -119,7 +120,7 @@ describe('Vector Tests', () => {
it('should test (u8, u8) vector input/output', async () => {
using contractInstance = await setupContract();

const INPUT: Array<[BigNumberish, BigNumberish]> = [
const INPUT: Vec<[BigNumberish, BigNumberish]> = [
nedsalk marked this conversation as resolved.
Show resolved Hide resolved
[1, 2],
[3, 4],
[5, 6],
Expand All @@ -136,7 +137,7 @@ describe('Vector Tests', () => {
it('should test (u64, u64) vector input/output', async () => {
using contractInstance = await setupContract();

const INPUT: [BigNumberish, BigNumberish][] = [
const INPUT: Vec<[BigNumberish, BigNumberish]> = [
[111, 2222],
[333, 4445],
[5555, 6],
Expand Down Expand Up @@ -167,7 +168,7 @@ describe('Vector Tests', () => {
it('should test [u64; 5] vector input/output', async () => {
using contractInstance = await setupContract();

const INPUT: Array<[BigNumberish, BigNumberish, BigNumberish, BigNumberish, BigNumberish]> = [
const INPUT: Vec<[BigNumberish, BigNumberish, BigNumberish, BigNumberish, BigNumberish]> = [
[1, 2, 3, 4, 5],
[500, 600, 700, 9000, 9999],
[11500, 22600, 33700, 55000, 669999],
Expand All @@ -182,7 +183,7 @@ describe('Vector Tests', () => {
it('should test [bool; 2] vector input/output', async () => {
using contractInstance = await setupContract();

const INPUT: Array<[boolean, boolean]> = [
const INPUT: Vec<[boolean, boolean]> = [
[true, true],
[true, false],
[false, true],
Expand Down Expand Up @@ -282,7 +283,7 @@ describe('Vector Tests', () => {
it('should test SmallEnum vector input/output', async () => {
using contractInstance = await setupContract();

const INPUT = [
const INPUT: Vec<SmallEnumInput> = [
SmallEnumInput.Empty,
SmallEnumInput.Empty,
SmallEnumInput.Empty,
Expand Down Expand Up @@ -403,7 +404,7 @@ describe('Vector Tests', () => {
it('should test Vec<u8> and b256 tuple input/output', async () => {
using contractInstance = await setupContract();

const INPUT: [Array<number>, string] = [[1, 8, 3, 2, 55, 215], hexlify(randomBytes(32))];
const INPUT: [Vec<BigNumberish>, string] = [[1, 8, 3, 2, 55, 215], hexlify(randomBytes(32))];

const { waitForResult } = await contractInstance.functions
.echo_vector_and_b256_tuple(...INPUT)
Expand All @@ -416,7 +417,7 @@ describe('Vector Tests', () => {
it('should test two vectors tuple input/output', async () => {
using contractInstance = await setupContract();

const INPUT: [Array<number>, Array<number>] = [
const INPUT: [Vec<BigNumberish>, Vec<BigNumberish>] = [
[219, 229],
[1, 254, 55],
];
Expand All @@ -432,7 +433,7 @@ describe('Vector Tests', () => {
it('should test u32 and three different vectors tuple input/output', async () => {
using contractInstance = await setupContract();

const INPUT: [number, boolean[], number[], number[]] = [
const INPUT: [BigNumberish, Vec<boolean>, Vec<BigNumberish>, Vec<BigNumberish>] = [
91000,
[true, true, false],
[95000, 153333],
Expand Down
Loading