diff --git a/.eslintrc b/.eslintrc index ae2cb6131..06718be35 100644 --- a/.eslintrc +++ b/.eslintrc @@ -5,8 +5,15 @@ "node": true, "jest": true }, - "extends": ["airbnb-base", "airbnb-typescript/base", "prettier", "plugin:prettier/recommended"], - "ignorePatterns": ["tsup.config.ts"], + "extends": [ + "airbnb-base", + "airbnb-typescript/base", + "prettier", + "plugin:prettier/recommended" + ], + "ignorePatterns": [ + "tsup.config.ts" + ], "globals": { "Atomics": "readonly", "SharedArrayBuffer": "readonly" @@ -17,13 +24,24 @@ "sourceType": "module", "project": "./tsconfig.eslint.json" }, - "plugins": ["@typescript-eslint"], + "plugins": [ + "@typescript-eslint" + ], "rules": { "class-methods-use-this": 0, "import/prefer-default-export": 0, + "@typescript-eslint/no-redeclare": 0, "@typescript-eslint/naming-convention": 0, - "@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }], - "@typescript-eslint/no-use-before-define": ["error", "nofunc"], + "@typescript-eslint/no-unused-vars": [ + "error", + { + "argsIgnorePattern": "^_" + } + ], + "@typescript-eslint/no-use-before-define": [ + "error", + "nofunc" + ], "func-names": 0 } } diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ab41b55b..ec0d4d2ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,19 @@ +# [6.9.0](https://github.com/starknet-io/starknet.js/compare/v6.8.0...v6.9.0) (2024-05-21) + +### Bug Fixes + +- cannot infer ts2742 types from starknet-types@0.7 ([#1098](https://github.com/starknet-io/starknet.js/issues/1098)) ([f1c3b8e](https://github.com/starknet-io/starknet.js/commit/f1c3b8e3aeb96f6efb7e512ac3ba689253004c9d)) +- remove [warning] from typedoc for external usage ([#1095](https://github.com/starknet-io/starknet.js/issues/1095)) ([195186f](https://github.com/starknet-io/starknet.js/commit/195186fc2974ab0d164b1a48c68f7bf026329df5)), closes [#1121](https://github.com/starknet-io/starknet.js/issues/1121) [#1126](https://github.com/starknet-io/starknet.js/issues/1126) + +### Features + +- add type coverage ([#1120](https://github.com/starknet-io/starknet.js/issues/1120)) ([eceda5d](https://github.com/starknet-io/starknet.js/commit/eceda5dc1c39e472e1105e07797e76aaac3c1531)) +- provider.getL1MessageHash ([#1123](https://github.com/starknet-io/starknet.js/issues/1123)) ([1489cf2](https://github.com/starknet-io/starknet.js/commit/1489cf25e7e8598ab161cecc62c82495f64daa33)) + +### Reverts + +- Revert "chore: add examples to JsDoc for transaction.ts file (#1105)" (#1108) ([59eb01e](https://github.com/starknet-io/starknet.js/commit/59eb01e451cf64dfdacd6d34b2a709e0a1029f15)), closes [#1105](https://github.com/starknet-io/starknet.js/issues/1105) [#1108](https://github.com/starknet-io/starknet.js/issues/1108) + # [6.8.0](https://github.com/starknet-io/starknet.js/compare/v6.7.0...v6.8.0) (2024-04-23) ### Bug Fixes diff --git a/__tests__/account.starknetId.test.ts b/__tests__/account.starknetId.test.ts index 2529c6199..6c08eb19e 100644 --- a/__tests__/account.starknetId.test.ts +++ b/__tests__/account.starknetId.test.ts @@ -93,7 +93,7 @@ describe('deploy and test Wallet', () => { test('Get the stark name of the account (using starknet.id)', async () => { const address = await account.getAddressFromStarkName('fricoben.stark', namingAddress); - expect(hexToDecimalString(address as string)).toEqual(hexToDecimalString(account.address)); + expect(hexToDecimalString(address)).toEqual(hexToDecimalString(account.address)); }); test('Get the account from a stark name of the account (using starknet.id)', async () => { diff --git a/__tests__/utils/starknetId.test.ts b/__tests__/utils/starknetId.test.ts index 43e8727cd..83bf80e08 100644 --- a/__tests__/utils/starknetId.test.ts +++ b/__tests__/utils/starknetId.test.ts @@ -18,11 +18,12 @@ function generateString(length: number, seed: number): string { return result; } -describe('Should tets StarknetId utils', () => { +describe('Should test StarknetId utils', () => { test('Should test useEncoded and useDecoded hook with a random string', () => { for (let index = 0; index < 2500; index += 1) { const randomString = generateString(10, index); - expect(useDecoded([useEncoded(randomString)])).toBe(randomString.concat('.stark')); + const decoded = useDecoded([useEncoded(randomString)]); + expect(decoded).toBe(randomString.concat('.stark')); } }); diff --git a/package-lock.json b/package-lock.json index 508eb0203..190283481 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "starknet", - "version": "6.8.0", + "version": "6.9.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "starknet", - "version": "6.8.0", + "version": "6.9.0", "license": "MIT", "dependencies": { "@noble/curves": "~1.4.0", diff --git a/package.json b/package.json index 9b19070b9..272b1d9e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "starknet", - "version": "6.8.0", + "version": "6.9.0", "description": "JavaScript library for Starknet", "main": "dist/index.js", "module": "dist/index.mjs", diff --git a/src/account/default.ts b/src/account/default.ts index 2bd428e95..e3820dca9 100644 --- a/src/account/default.ts +++ b/src/account/default.ts @@ -67,14 +67,16 @@ export class Account extends Provider implements AccountInterface { public cairoVersion: CairoVersion; - readonly transactionVersion: ETransactionVersion.V2 | ETransactionVersion.V3; + readonly transactionVersion: typeof ETransactionVersion.V2 | typeof ETransactionVersion.V3; constructor( providerOrOptions: ProviderOptions | ProviderInterface, address: string, pkOrSigner: Uint8Array | string | SignerInterface, cairoVersion?: CairoVersion, - transactionVersion: ETransactionVersion.V2 | ETransactionVersion.V3 = ETransactionVersion.V2 // TODO: Discuss this, set to v2 for backward compatibility + transactionVersion: + | typeof ETransactionVersion.V2 + | typeof ETransactionVersion.V3 = ETransactionVersion.V2 // TODO: Discuss this, set to v2 for backward compatibility ) { super(providerOrOptions); this.address = address.toLowerCase(); diff --git a/src/channel/rpc_0_6.ts b/src/channel/rpc_0_6.ts index 3074c82d0..44be230d7 100644 --- a/src/channel/rpc_0_6.ts +++ b/src/channel/rpc_0_6.ts @@ -31,7 +31,7 @@ import { getVersionsByType } from '../utils/transaction'; const defaultOptions = { headers: { 'Content-Type': 'application/json' }, - blockIdentifier: BlockTag.pending, + blockIdentifier: BlockTag.PENDING, retries: 200, }; @@ -93,7 +93,7 @@ export class RpcChannel { if (rpcError) { const { code, message, data } = rpcError; throw new LibraryError( - `RPC: ${method} with params ${stringify(params, null, 2)}\n + `RPC: ${method} with params ${stringify(params, null, 2)}\n ${code}: ${message}: ${stringify(data)}` ); } diff --git a/src/channel/rpc_0_7.ts b/src/channel/rpc_0_7.ts index 00e875bd4..6d0f9ba9a 100644 --- a/src/channel/rpc_0_7.ts +++ b/src/channel/rpc_0_7.ts @@ -31,7 +31,7 @@ import { getVersionsByType } from '../utils/transaction'; const defaultOptions = { headers: { 'Content-Type': 'application/json' }, - blockIdentifier: BlockTag.pending, + blockIdentifier: BlockTag.PENDING, retries: 200, }; diff --git a/src/constants.ts b/src/constants.ts index 0b2d9ef85..ace6b2956 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,4 +1,5 @@ import { ETransactionVersion } from './types/api'; +import { ValuesType } from './types/helpers/valuesType'; export { IS_BROWSER } from './utils/encode'; @@ -24,43 +25,56 @@ export const MAX_STORAGE_ITEM_SIZE = 256n; export const ADDR_BOUND = 2n ** 251n - MAX_STORAGE_ITEM_SIZE; const range = (min: bigint, max: bigint) => ({ min, max }) as const; + export const RANGE_FELT = range(ZERO, PRIME - 1n); export const RANGE_I128 = range(-(2n ** 127n), 2n ** 127n - 1n); export const RANGE_U128 = range(ZERO, 2n ** 128n - 1n); -export enum BaseUrl { - SN_MAIN = 'https://alpha-mainnet.starknet.io', - SN_SEPOLIA = 'https://alpha-sepolia.starknet.io', -} - -export enum NetworkName { - SN_MAIN = 'SN_MAIN', - SN_SEPOLIA = 'SN_SEPOLIA', -} - -export enum StarknetChainId { - SN_MAIN = '0x534e5f4d41494e', // encodeShortString('SN_MAIN'), - SN_SEPOLIA = '0x534e5f5345504f4c4941', // encodeShortString('SN_SEPOLIA') -} - -export enum TransactionHashPrefix { - DECLARE = '0x6465636c617265', // encodeShortString('declare'), - DEPLOY = '0x6465706c6f79', // encodeShortString('deploy'), - DEPLOY_ACCOUNT = '0x6465706c6f795f6163636f756e74', // encodeShortString('deploy_account'), - INVOKE = '0x696e766f6b65', // encodeShortString('invoke'), - L1_HANDLER = '0x6c315f68616e646c6572', // encodeShortString('l1_handler'), -} - -export const enum feeMarginPercentage { - L1_BOUND_MAX_AMOUNT = 50, - L1_BOUND_MAX_PRICE_PER_UNIT = 50, - MAX_FEE = 50, -} +export const BaseUrl = { + SN_MAIN: 'https://alpha-mainnet.starknet.io', + SN_SEPOLIA: 'https://alpha-sepolia.starknet.io', +} as const; + +export type BaseUrl = ValuesType; + +export const NetworkName = { + SN_MAIN: 'SN_MAIN', + SN_SEPOLIA: 'SN_SEPOLIA', +} as const; + +export type NetworkName = ValuesType; + +export const StarknetChainId = { + SN_MAIN: '0x534e5f4d41494e', // encodeShortString('SN_MAIN'), + SN_SEPOLIA: '0x534e5f5345504f4c4941', // encodeShortString('SN_SEPOLIA') +} as const; + +export type StarknetChainId = ValuesType; + +export const TransactionHashPrefix = { + DECLARE: '0x6465636c617265', // encodeShortString('declare'), + DEPLOY: '0x6465706c6f79', // encodeShortString('deploy'), + DEPLOY_ACCOUNT: '0x6465706c6f795f6163636f756e74', // encodeShortString('deploy_account'), + INVOKE: '0x696e766f6b65', // encodeShortString('invoke'), + L1_HANDLER: '0x6c315f68616e646c6572', // encodeShortString('l1_handler'), +} as const; + +export type TransactionHashPrefix = ValuesType; + +export const FeeMarginPercentage = { + L1_BOUND_MAX_AMOUNT: 50, + L1_BOUND_MAX_PRICE_PER_UNIT: 50, + MAX_FEE: 50, +} as const; + +export type FeeMarginPercentage = ValuesType; export const UDC = { ADDRESS: '0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf', ENTRYPOINT: 'deployContract', -}; +} as const; + +export type UDC = ValuesType; export const RPC_DEFAULT_VERSION = 'v0_7'; @@ -73,4 +87,4 @@ export const RPC_NODES = { `https://starknet-sepolia.public.blastapi.io/rpc/${RPC_DEFAULT_VERSION}`, `https://free-rpc.nethermind.io/sepolia-juno/${RPC_DEFAULT_VERSION}`, ], -}; +} as const; diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index 9d4ded36c..97bf1dd4a 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -124,10 +124,10 @@ export class RpcProvider implements ProviderInterface { blockIdentifier: BlockIdentifier = 'pending', retryInterval: number = 5000 ) { - if (blockIdentifier === BlockTag.latest) return; + if (blockIdentifier === BlockTag.LATEST) return; const currentBlock = await this.getBlockNumber(); const targetBlock = - blockIdentifier === BlockTag.pending + blockIdentifier === BlockTag.PENDING ? currentBlock + 1 : Number(toHex(blockIdentifier as BigNumberish)); if (targetBlock <= currentBlock) return; @@ -205,11 +205,11 @@ export class RpcProvider implements ProviderInterface { /** * Return transactions from pending block - * @deprecated Instead use getBlock(BlockTag.pending); (will be removed in next minor version) + * @deprecated Instead use getBlock(BlockTag.PENDING); (will be removed in next minor version) * Utility method, same result can be achieved using getBlockWithTxHashes(BlockTag.pending); */ public async getPendingTransactions() { - const { transactions } = await this.getBlockWithTxHashes(BlockTag.pending).then( + const { transactions } = await this.getBlockWithTxHashes(BlockTag.PENDING).then( this.responseParser.parseGetBlockResponse ); return Promise.all(transactions.map((it: any) => this.getTransactionByHash(it))); diff --git a/src/types/account.ts b/src/types/account.ts index 4da859a92..fec5c0754 100644 --- a/src/types/account.ts +++ b/src/types/account.ts @@ -78,19 +78,19 @@ export type SimulateTransactionDetails = { export type EstimateFeeAction = | { - type: TransactionType.INVOKE; + type: typeof TransactionType.INVOKE; payload: AllowArray; } | { - type: TransactionType.DECLARE; + type: typeof TransactionType.DECLARE; payload: DeclareContractPayload; } | { - type: TransactionType.DEPLOY_ACCOUNT; + type: typeof TransactionType.DEPLOY_ACCOUNT; payload: DeployAccountContractPayload; } | { - type: TransactionType.DEPLOY; + type: typeof TransactionType.DEPLOY; payload: UniversalDeployerContractPayload; }; diff --git a/src/types/api/rpcspec_0_6/contract.ts b/src/types/api/rpcspec_0_6/contract.ts index 5cb22cad4..e822c3fff 100644 --- a/src/types/api/rpcspec_0_6/contract.ts +++ b/src/types/api/rpcspec_0_6/contract.ts @@ -91,9 +91,6 @@ type IMPL = { interface_name: string; }; -// eslint-disable-next-line @typescript-eslint/no-unused-vars -type EVENT_KIND = 'struct' | 'enum'; - type EVENT_FIELD = { name: string; type: string; diff --git a/src/types/api/rpcspec_0_6/nonspec.ts b/src/types/api/rpcspec_0_6/nonspec.ts index f3453b43b..8a9bb7e42 100644 --- a/src/types/api/rpcspec_0_6/nonspec.ts +++ b/src/types/api/rpcspec_0_6/nonspec.ts @@ -39,6 +39,7 @@ import { TXN_RECEIPT, TXN_STATUS, } from './components'; +import { ValuesType } from '../../helpers/valuesType'; // METHOD RESPONSES // response starknet_getClass @@ -107,83 +108,106 @@ export type NonceUpdates = NONCE_UPDATE[]; export type ReplacedClasses = REPLACED_CLASS[]; // Enums Derived From Spec Types (require manual check for changes) -export enum ETransactionType { - DECLARE = 'DECLARE', - DEPLOY = 'DEPLOY', - DEPLOY_ACCOUNT = 'DEPLOY_ACCOUNT', - INVOKE = 'INVOKE', - L1_HANDLER = 'L1_HANDLER', -} - -export enum ESimulationFlag { - SKIP_VALIDATE = 'SKIP_VALIDATE', - SKIP_FEE_CHARGE = 'SKIP_FEE_CHARGE', -} - -export enum ETransactionStatus { - RECEIVED = 'RECEIVED', - REJECTED = 'REJECTED', - ACCEPTED_ON_L2 = 'ACCEPTED_ON_L2', - ACCEPTED_ON_L1 = 'ACCEPTED_ON_L1', -} - -export enum ETransactionFinalityStatus { - ACCEPTED_ON_L2 = 'ACCEPTED_ON_L2', - ACCEPTED_ON_L1 = 'ACCEPTED_ON_L1', -} -export enum ETransactionExecutionStatus { - SUCCEEDED = 'SUCCEEDED', - REVERTED = 'REVERTED', -} - -export enum EBlockTag { - LATEST = 'latest', - PENDING = 'pending', -} +export const ETransactionType = { + DECLARE: 'DECLARE', + DEPLOY: 'DEPLOY', + DEPLOY_ACCOUNT: 'DEPLOY_ACCOUNT', + INVOKE: 'INVOKE', + L1_HANDLER: 'L1_HANDLER', +} as const; + +export type ETransactionType = ValuesType; + +export const ESimulationFlag = { + SKIP_VALIDATE: 'SKIP_VALIDATE', + SKIP_FEE_CHARGE: 'SKIP_FEE_CHARGE', +} as const; + +export type ESimulationFlag = ValuesType; + +export const ETransactionStatus = { + RECEIVED: 'RECEIVED', + REJECTED: 'REJECTED', + ACCEPTED_ON_L2: 'ACCEPTED_ON_L2', + ACCEPTED_ON_L1: 'ACCEPTED_ON_L1', +} as const; + +export type ETransactionStatus = ValuesType; + +export const ETransactionFinalityStatus = { + ACCEPTED_ON_L2: 'ACCEPTED_ON_L2', + ACCEPTED_ON_L1: 'ACCEPTED_ON_L1', +} as const; + +export type ETransactionFinalityStatus = ValuesType; + +export const ETransactionExecutionStatus = { + SUCCEEDED: 'SUCCEEDED', + REVERTED: 'REVERTED', +} as const; + +export type ETransactionExecutionStatus = ValuesType; + +export const EBlockTag = { + PENDING: 'pending', + LATEST: 'latest', +} as const; + +export type EBlockTag = ValuesType; // 'L1' | 'L2' -export enum EDataAvailabilityMode { - L1 = 'L1', - L2 = 'L2', -} +export const EDataAvailabilityMode = { + L1: 'L1', + L2: 'L2', +} as const; + +export type EDataAvailabilityMode = ValuesType; // 0 | 1 -export enum EDAMode { - L1, - L2, -} +export const EDAMode = { + L1: 0, + L2: 1, +} as const; + +export type EDAMode = ValuesType; /** * V_ Transaction versions HexString * F_ Fee Transaction Versions HexString (2 ** 128 + TRANSACTION_VERSION) */ -export enum ETransactionVersion { - V0 = '0x0', - V1 = '0x1', - V2 = '0x2', - V3 = '0x3', - F0 = '0x100000000000000000000000000000000', - F1 = '0x100000000000000000000000000000001', - F2 = '0x100000000000000000000000000000002', - F3 = '0x100000000000000000000000000000003', -} +export const ETransactionVersion = { + V0: '0x0', + V1: '0x1', + V2: '0x2', + V3: '0x3', + F0: '0x100000000000000000000000000000000', + F1: '0x100000000000000000000000000000001', + F2: '0x100000000000000000000000000000002', + F3: '0x100000000000000000000000000000003', +} as const; + +export type ETransactionVersion = ValuesType; /** * Old Transaction Versions */ -export enum ETransactionVersion2 { - V0 = '0x0', - V1 = '0x1', - V2 = '0x2', - F0 = '0x100000000000000000000000000000000', - F1 = '0x100000000000000000000000000000001', - F2 = '0x100000000000000000000000000000002', -} +export const ETransactionVersion2 = { + V0: '0x0', + V1: '0x1', + V2: '0x2', + F0: '0x100000000000000000000000000000000', + F1: '0x100000000000000000000000000000001', + F2: '0x100000000000000000000000000000002', +} as const; + +export type ETransactionVersion2 = ValuesType; /** * V3 Transaction Versions */ -export enum ETransactionVersion3 { - V3 = '0x3', - F3 = '0x100000000000000000000000000000003', -} +export const ETransactionVersion3 = { + V3: '0x3', + F3: '0x100000000000000000000000000000003', +} as const; + +export type ETransactionVersion3 = ValuesType; diff --git a/src/types/calldata.ts b/src/types/calldata.ts index 4a572f2bf..75729969b 100644 --- a/src/types/calldata.ts +++ b/src/types/calldata.ts @@ -1,21 +1,29 @@ -export enum ValidateType { - DEPLOY = 'DEPLOY', - CALL = 'CALL', - INVOKE = 'INVOKE', -} +import { ValuesType } from './helpers/valuesType'; -export enum Uint { - u8 = 'core::integer::u8', - u16 = 'core::integer::u16', - u32 = 'core::integer::u32', - u64 = 'core::integer::u64', - u128 = 'core::integer::u128', - u256 = 'core::integer::u256', // This one is struct - u512 = 'core::integer::u512', // This one is struct -} +export const ValidateType = { + DEPLOY: 'DEPLOY', + CALL: 'CALL', + INVOKE: 'INVOKE', +} as const; -export enum Literal { - ClassHash = 'core::starknet::class_hash::ClassHash', - ContractAddress = 'core::starknet::contract_address::ContractAddress', - Secp256k1Point = 'core::starknet::secp256k1::Secp256k1Point', -} +export type ValidateType = ValuesType; + +export const Uint = { + u8: 'core::integer::u8', + u16: 'core::integer::u16', + u32: 'core::integer::u32', + u64: 'core::integer::u64', + u128: 'core::integer::u128', + u256: 'core::integer::u256', // This one is struct + u512: 'core::integer::u512', // This one is struct +} as const; + +export type Uint = ValuesType; + +export const Literal = { + ClassHash: 'core::starknet::class_hash::ClassHash', + ContractAddress: 'core::starknet::contract_address::ContractAddress', + Secp256k1Point: 'core::starknet::secp256k1::Secp256k1Point', +} as const; + +export type Literal = ValuesType; diff --git a/src/types/helpers/valuesType.ts b/src/types/helpers/valuesType.ts new file mode 100644 index 000000000..400e961c3 --- /dev/null +++ b/src/types/helpers/valuesType.ts @@ -0,0 +1,8 @@ +export type ValuesType | ArrayLike | Record> = + T extends ReadonlyArray + ? T[number] + : T extends ArrayLike + ? T[number] + : T extends object + ? T[keyof T] + : never; diff --git a/src/types/lib/contract/abi.ts b/src/types/lib/contract/abi.ts index 1b23e1b47..ab265cc9f 100644 --- a/src/types/lib/contract/abi.ts +++ b/src/types/lib/contract/abi.ts @@ -6,11 +6,7 @@ export type AbiEntry = { name: string; type: 'felt' | 'felt*' | string }; export type EventEntry = { name: string; type: 'felt' | 'felt*' | string; kind: 'key' | 'data' }; -enum FunctionAbiType { - 'function', - 'l1_handler', - 'constructor', -} +type FunctionAbiType = 'function' | 'l1_handler' | 'constructor'; // Sub elements export type FunctionAbi = { diff --git a/src/types/lib/contract/index.ts b/src/types/lib/contract/index.ts index a773f636d..317edda65 100644 --- a/src/types/lib/contract/index.ts +++ b/src/types/lib/contract/index.ts @@ -1,3 +1,4 @@ +import { ValuesType } from '../../helpers/valuesType'; import { LegacyCompiledContract, LegacyContractClass } from './legacy'; import { CompiledSierra, SierraContractClass } from './sierra'; @@ -20,11 +21,13 @@ export type CompiledContract = LegacyCompiledContract | CompiledSierra; export type CairoContract = ContractClass | CompiledContract; // Basic elements -export enum EntryPointType { - EXTERNAL = 'EXTERNAL', - L1_HANDLER = 'L1_HANDLER', - CONSTRUCTOR = 'CONSTRUCTOR', -} +export const EntryPointType = { + EXTERNAL: 'EXTERNAL', + L1_HANDLER: 'L1_HANDLER', + CONSTRUCTOR: 'CONSTRUCTOR', +} as const; + +export type EntryPointType = ValuesType; export * from './abi'; export * from './legacy'; diff --git a/src/types/lib/index.ts b/src/types/lib/index.ts index d96583f4a..c5f4d8a34 100644 --- a/src/types/lib/index.ts +++ b/src/types/lib/index.ts @@ -1,3 +1,4 @@ +import { ValuesType } from '../helpers/valuesType'; import { StarknetChainId } from '../../constants'; import { weierstrass } from '../../utils/ec'; import { EDataAvailabilityMode, ResourceBounds } from '../api'; @@ -157,55 +158,65 @@ export type Details = { }; export type InvocationsDetailsWithNonce = - | (InvocationsDetails & { - nonce: BigNumberish; - }) + | (InvocationsDetails & { nonce: BigNumberish }) | V3TransactionDetails; -export enum TransactionType { - DECLARE = 'DECLARE', - DEPLOY = 'DEPLOY', - DEPLOY_ACCOUNT = 'DEPLOY_ACCOUNT', - INVOKE = 'INVOKE_FUNCTION', -} +export const TransactionType = { + DECLARE: 'DECLARE', + DEPLOY: 'DEPLOY', + DEPLOY_ACCOUNT: 'DEPLOY_ACCOUNT', + INVOKE: 'INVOKE_FUNCTION', +} as const; + +export type TransactionType = ValuesType; /** * new statuses are defined by props: finality_status and execution_status * to be #deprecated */ -export enum TransactionStatus { - NOT_RECEIVED = 'NOT_RECEIVED', - RECEIVED = 'RECEIVED', - ACCEPTED_ON_L2 = 'ACCEPTED_ON_L2', - ACCEPTED_ON_L1 = 'ACCEPTED_ON_L1', - REJECTED = 'REJECTED', - REVERTED = 'REVERTED', -} - -export enum TransactionFinalityStatus { - NOT_RECEIVED = 'NOT_RECEIVED', - RECEIVED = 'RECEIVED', - ACCEPTED_ON_L2 = 'ACCEPTED_ON_L2', - ACCEPTED_ON_L1 = 'ACCEPTED_ON_L1', -} - -export enum TransactionExecutionStatus { - REJECTED = 'REJECTED', - REVERTED = 'REVERTED', - SUCCEEDED = 'SUCCEEDED', -} - -export enum BlockStatus { - PENDING = 'PENDING', - ACCEPTED_ON_L1 = 'ACCEPTED_ON_L1', - ACCEPTED_ON_L2 = 'ACCEPTED_ON_L2', - REJECTED = 'REJECTED', -} - -export enum BlockTag { - pending = 'pending', - latest = 'latest', -} +export const TransactionStatus = { + NOT_RECEIVED: 'NOT_RECEIVED', + RECEIVED: 'RECEIVED', + ACCEPTED_ON_L2: 'ACCEPTED_ON_L2', + ACCEPTED_ON_L1: 'ACCEPTED_ON_L1', + REJECTED: 'REJECTED', + REVERTED: 'REVERTED', +} as const; + +export type TransactionStatus = ValuesType; + +export const TransactionFinalityStatus = { + NOT_RECEIVED: 'NOT_RECEIVED', + RECEIVED: 'RECEIVED', + ACCEPTED_ON_L2: 'ACCEPTED_ON_L2', + ACCEPTED_ON_L1: 'ACCEPTED_ON_L1', +} as const; + +export type TransactionFinalityStatus = ValuesType; + +export const TransactionExecutionStatus = { + REJECTED: 'REJECTED', + REVERTED: 'REVERTED', + SUCCEEDED: 'SUCCEEDED', +} as const; + +export type TransactionExecutionStatus = ValuesType; + +export const BlockStatus = { + PENDING: 'PENDING', + ACCEPTED_ON_L1: 'ACCEPTED_ON_L1', + ACCEPTED_ON_L2: 'ACCEPTED_ON_L2', + REJECTED: 'REJECTED', +} as const; + +export type BlockStatus = ValuesType; + +export const BlockTag = { + PENDING: 'pending', + LATEST: 'latest', +} as const; + +export type BlockTag = ValuesType; export type BlockNumber = BlockTag | null | number; @@ -224,9 +235,9 @@ export type BlockIdentifier = BlockNumber | BigNumberish; * items used by AccountInvocations */ export type AccountInvocationItem = ( - | ({ type: TransactionType.DECLARE } & DeclareContractTransaction) - | ({ type: TransactionType.DEPLOY_ACCOUNT } & DeployAccountContractTransaction) - | ({ type: TransactionType.INVOKE } & Invocation) + | ({ type: typeof TransactionType.DECLARE } & DeclareContractTransaction) + | ({ type: typeof TransactionType.DEPLOY_ACCOUNT } & DeployAccountContractTransaction) + | ({ type: typeof TransactionType.INVOKE } & Invocation) ) & InvocationsDetailsWithNonce; @@ -239,12 +250,14 @@ export type AccountInvocations = AccountInvocationItem[]; * Invocations array user provide to bulk method (simulate) */ export type Invocations = Array< - | ({ type: TransactionType.DECLARE } & OptionalPayload) - | ({ type: TransactionType.DEPLOY } & OptionalPayload< + | ({ type: typeof TransactionType.DECLARE } & OptionalPayload) + | ({ type: typeof TransactionType.DEPLOY } & OptionalPayload< AllowArray >) - | ({ type: TransactionType.DEPLOY_ACCOUNT } & OptionalPayload) - | ({ type: TransactionType.INVOKE } & OptionalPayload>) + | ({ + type: typeof TransactionType.DEPLOY_ACCOUNT; + } & OptionalPayload) + | ({ type: typeof TransactionType.INVOKE } & OptionalPayload>) >; export type Tupled = { element: any; type: string }; diff --git a/src/types/provider/response.ts b/src/types/provider/response.ts index a91534778..e4cd70411 100644 --- a/src/types/provider/response.ts +++ b/src/types/provider/response.ts @@ -71,7 +71,7 @@ export interface MessageToL1 { export type RevertedTransactionReceiptResponse = { type?: TransactionType | any; // RPC only // any due to RPC Spec issue - execution_status: TransactionExecutionStatus.REVERTED | any; // any due to RPC Spec issue + execution_status: typeof TransactionExecutionStatus.REVERTED | any; // any due to RPC Spec issue finality_status: TransactionFinalityStatus | any; status?: TransactionStatus; // SEQ only actual_fee: string; @@ -85,7 +85,7 @@ export type RevertedTransactionReceiptResponse = { }; export type RejectedTransactionReceiptResponse = { - status: `${TransactionStatus.REJECTED}`; + status: typeof TransactionStatus.REJECTED; transaction_failure_reason: { code: string; error_message: string; diff --git a/src/utils/calldata/enum/CairoOption.ts b/src/utils/calldata/enum/CairoOption.ts index 13a8df94d..53e83ba6a 100644 --- a/src/utils/calldata/enum/CairoOption.ts +++ b/src/utils/calldata/enum/CairoOption.ts @@ -1,7 +1,11 @@ -export enum CairoOptionVariant { - Some = 0, - None = 1, -} +import { ValuesType } from '../../../types/helpers/valuesType'; + +export const CairoOptionVariant = { + Some: 0, + None: 1, +} as const; + +export type CairoOptionVariant = ValuesType; /** * Class to handle Cairo Option @@ -18,8 +22,8 @@ export class CairoOption { readonly None?: boolean; - constructor(variant: CairoOptionVariant, someContent?: T) { - if (!(variant in CairoOptionVariant)) { + constructor(variant: CairoOptionVariant | number, someContent?: T) { + if (!(variant in Object.values(CairoOptionVariant))) { throw new Error('Wrong variant : should be CairoOptionVariant.Some or .None.'); } if (variant === CairoOptionVariant.Some) { diff --git a/src/utils/calldata/enum/CairoResult.ts b/src/utils/calldata/enum/CairoResult.ts index f6fae9467..c09a0b46a 100644 --- a/src/utils/calldata/enum/CairoResult.ts +++ b/src/utils/calldata/enum/CairoResult.ts @@ -1,7 +1,11 @@ -export enum CairoResultVariant { - Ok = 0, - Err = 1, -} +import { ValuesType } from '../../../types/helpers/valuesType'; + +export const CairoResultVariant = { + Ok: 0, + Err: 1, +} as const; + +export type CairoResultVariant = ValuesType; /** * Class to handle Cairo Result @@ -18,8 +22,8 @@ export class CairoResult { readonly Err?: U; - constructor(variant: CairoResultVariant, resultContent: T | U) { - if (!(variant in CairoResultVariant)) { + constructor(variant: CairoResultVariant | number, resultContent: T | U) { + if (!(variant in Object.values(CairoResultVariant))) { throw new Error('Wrong variant : should be CairoResultVariant.Ok or .Err.'); } if (variant === CairoResultVariant.Ok) { diff --git a/src/utils/hash/transactionHash/index.ts b/src/utils/hash/transactionHash/index.ts index af8c0083f..cc0a2e3cf 100644 --- a/src/utils/hash/transactionHash/index.ts +++ b/src/utils/hash/transactionHash/index.ts @@ -26,10 +26,10 @@ import { * INVOKE TX HASH */ +type Version = typeof ETransactionVersion.V3 | typeof ETransactionVersion.F3; + function isV3InvokeTx(args: CalcInvokeTxHashArgs): args is CalcV3InvokeTxHashArgs { - return [ETransactionVersion.V3, ETransactionVersion.F3].includes( - args.version as ETransactionVersion - ); + return [ETransactionVersion.V3, ETransactionVersion.F3].includes(args.version as Version); } type CalcV2InvokeTxHashArgs = { @@ -87,9 +87,7 @@ export function calculateInvokeTransactionHash(args: CalcInvokeTxHashArgs) { * DECLARE TX HASH */ function isV3DeclareTx(args: CalcDeclareTxHashArgs): args is CalcV3DeclareTxHashArgs { - return [ETransactionVersion.V3, ETransactionVersion.F3].includes( - args.version as ETransactionVersion - ); + return [ETransactionVersion.V3, ETransactionVersion.F3].includes(args.version as Version); } type CalcV2DeclareTxHashArgs = { @@ -155,9 +153,7 @@ export function calculateDeclareTransactionHash(args: CalcDeclareTxHashArgs) { function isV3DeployAccountTx( args: CalcDeployAccountTxHashArgs ): args is CalcV3DeployAccountTxHashArgs { - return [ETransactionVersion.V3, ETransactionVersion.F3].includes( - args.version as ETransactionVersion - ); + return [ETransactionVersion.V3, ETransactionVersion.F3].includes(args.version as Version); } type CalcV2DeployAccountTxHashArgs = { diff --git a/src/utils/provider.ts b/src/utils/provider.ts index 757189824..1fe6a62ee 100644 --- a/src/utils/provider.ts +++ b/src/utils/provider.ts @@ -216,7 +216,7 @@ export class Block { } else if (isNumber(__identifier)) { this.number = __identifier; } else { - this.tag = BlockTag.pending; + this.tag = BlockTag.PENDING; } if (isNumber(this.number) && this.number < 0) { diff --git a/src/utils/stark.ts b/src/utils/stark.ts index fc9671366..5bf89bbb2 100644 --- a/src/utils/stark.ts +++ b/src/utils/stark.ts @@ -3,7 +3,7 @@ import type { SPEC } from 'starknet-types-07'; import { getStarkKey, utils } from '@scure/starknet'; import { gzip, ungzip } from 'pako'; -import { ZERO, feeMarginPercentage } from '../constants'; +import { ZERO, FeeMarginPercentage } from '../constants'; import { ArraySignatureType, BigNumberish, @@ -174,7 +174,7 @@ export function signatureToHexArray(sig?: Signature): ArraySignatureType { */ export function estimatedFeeToMaxFee( estimatedFee: BigNumberish, - overhead: number = feeMarginPercentage.MAX_FEE + overhead: number = FeeMarginPercentage.MAX_FEE ): bigint { return addPercent(estimatedFee, overhead); } @@ -204,8 +204,8 @@ const result = stark.estimateFeeToBounds(feeEstimated, 70, 50); */ export function estimateFeeToBounds( estimate: FeeEstimate | 0n, - amountOverhead: number = feeMarginPercentage.L1_BOUND_MAX_AMOUNT, - priceOverhead: number = feeMarginPercentage.L1_BOUND_MAX_PRICE_PER_UNIT + amountOverhead: number = FeeMarginPercentage.L1_BOUND_MAX_AMOUNT, + priceOverhead: number = FeeMarginPercentage.L1_BOUND_MAX_PRICE_PER_UNIT ): ResourceBounds { if (isBigInt(estimate)) { return { diff --git a/src/utils/starknetId.ts b/src/utils/starknetId.ts index 73b595cac..30e6f5fae 100644 --- a/src/utils/starknetId.ts +++ b/src/utils/starknetId.ts @@ -124,10 +124,10 @@ export function useEncoded(decoded: string): bigint { return encoded; } -export const enum StarknetIdContract { - MAINNET = '0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678', - TESTNET_SEPOLIA = '0x0707f09bc576bd7cfee59694846291047e965f4184fe13dac62c56759b3b6fa7', -} +export const StarknetIdContract = { + MAINNET: '0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678', + TESTNET_SEPOLIA: '0x0707f09bc576bd7cfee59694846291047e965f4184fe13dac62c56759b3b6fa7', +} as const; /** * Returns the Starknet ID contract address based on the provided chain ID. @@ -154,10 +154,10 @@ export function getStarknetIdContract(chainId: StarknetChainId): string { } } -export const enum StarknetIdIdentityContract { - MAINNET = '0x05dbdedc203e92749e2e746e2d40a768d966bd243df04a6b712e222bc040a9af', - TESTNET_SEPOLIA = '0x070DF8B4F5cb2879f8592849fA8f3134da39d25326B8558cc9C8FE8D47EA3A90', -} +export const StarknetIdIdentityContract = { + MAINNET: '0x05dbdedc203e92749e2e746e2d40a768d966bd243df04a6b712e222bc040a9af', + TESTNET_SEPOLIA: '0x070DF8B4F5cb2879f8592849fA8f3134da39d25326B8558cc9C8FE8D47EA3A90', +} as const; /** * Returns the Starknet ID identity contract address for the given chain ID. @@ -214,10 +214,10 @@ export function getStarknetIdMulticallContract(chainId: StarknetChainId): string } } -export const enum StarknetIdVerifierContract { - MAINNET = '0x07d14dfd8ee95b41fce179170d88ba1f0d5a512e13aeb232f19cfeec0a88f8bf', - TESTNET_SEPOLIA = '0x0182EcE8173C216A395f4828e1523541b7e3600bf190CB252E1a1A0cE219d184', -} +export const StarknetIdVerifierContract = { + MAINNET: '0x07d14dfd8ee95b41fce179170d88ba1f0d5a512e13aeb232f19cfeec0a88f8bf', + TESTNET_SEPOLIA: '0x0182EcE8173C216A395f4828e1523541b7e3600bf190CB252E1a1A0cE219d184', +} as const; /** * Returns the address of the Starknet ID Verifier contract based on the specified chain ID. @@ -244,10 +244,10 @@ export function getStarknetIdVerifierContract(chainId: StarknetChainId): string } } -export const enum StarknetIdPfpContract { - MAINNET = '0x070aaa20ec4a46da57c932d9fd89ca5e6bb9ca3188d3df361a32306aff7d59c7', - TESTNET_SEPOLIA = '0x058061bb6bdc501eE215172c9f87d557C1E0f466dC498cA81b18f998Bf1362b2', -} +export const StarknetIdPfpContract = { + MAINNET: '0x070aaa20ec4a46da57c932d9fd89ca5e6bb9ca3188d3df361a32306aff7d59c7', + TESTNET_SEPOLIA: '0x058061bb6bdc501eE215172c9f87d557C1E0f466dC498cA81b18f998Bf1362b2', +} as const; /** * Retrieves the contract address of the Starknet.id profile picture verifier contract based on the given chain ID. @@ -276,10 +276,10 @@ export function getStarknetIdPfpContract(chainId: StarknetChainId): string { } } -export const enum StarknetIdPopContract { - MAINNET = '0x0293eb2ba9862f762bd3036586d5755a782bd22e6f5028320f1d0405fd47bff4', - TESTNET_SEPOLIA = '0x0023FE3b845ed5665a9eb3792bbB17347B490EE4090f855C1298d03BB5F49B49', -} +export const StarknetIdPopContract = { + MAINNET: '0x0293eb2ba9862f762bd3036586d5755a782bd22e6f5028320f1d0405fd47bff4', + TESTNET_SEPOLIA: '0x0023FE3b845ed5665a9eb3792bbB17347B490EE4090f855C1298d03BB5F49B49', +} as const; /** * Retrieves the Starknet ID Proof of Personhood (IdPop) verifier contract address for the given chain ID.