diff --git a/src/contract/contractFactory.ts b/src/contract/contractFactory.ts index 1ef8cb514..d92d9c380 100644 --- a/src/contract/contractFactory.ts +++ b/src/contract/contractFactory.ts @@ -1,5 +1,5 @@ import { AccountInterface } from '../account'; -import { Abi, ArgsOrCalldataWithOptions, CompiledContract } from '../types'; +import { Abi, ArgsOrCalldataWithOptions, CompiledContract, ValidateType } from '../types'; import assert from '../utils/assert'; import { CallData } from '../utils/calldata'; import { Contract, getCalldata, splitArgsAndOptions } from './default'; @@ -40,7 +40,7 @@ export class ContractFactory { const constructorCalldata = getCalldata(param, () => { if (options.parseRequest) { - this.CallData.validate('DEPLOY', 'constructor', param); + this.CallData.validate(ValidateType.DEPLOY, 'constructor', param); return this.CallData.compile('constructor', param); } // eslint-disable-next-line no-console diff --git a/src/contract/default.ts b/src/contract/default.ts index 69a33a493..eabf46ea9 100644 --- a/src/contract/default.ts +++ b/src/contract/default.ts @@ -17,6 +17,7 @@ import { RawArgs, Result, StructAbi, + ValidateType, } from '../types'; import assert from '../utils/assert'; import { CallData, cairo } from '../utils/calldata'; @@ -225,7 +226,7 @@ export class Contract implements ContractInterface { const calldata = getCalldata(args, () => { if (parseRequest) { - this.callData.validate('CALL', method, args); + this.callData.validate(ValidateType.CALL, method, args); return this.callData.compile(method, args); } // eslint-disable-next-line no-console @@ -262,7 +263,7 @@ export class Contract implements ContractInterface { const calldata = getCalldata(args, () => { if (parseRequest) { - this.callData.validate('INVOKE', method, args); + this.callData.validate(ValidateType.INVOKE, method, args); return this.callData.compile(method, args); } // eslint-disable-next-line no-console @@ -301,7 +302,7 @@ export class Contract implements ContractInterface { assert(this.address !== null, 'contract is not connected to an address'); if (!getCalldata(args, () => false)) { - this.callData.validate('INVOKE', method, args); + this.callData.validate(ValidateType.INVOKE, method, args); } const invocation = this.populate(method, args); diff --git a/src/provider/sequencer.ts b/src/provider/sequencer.ts index 8df1da64d..2a3a140ea 100644 --- a/src/provider/sequencer.ts +++ b/src/provider/sequencer.ts @@ -6,6 +6,7 @@ import { AccountInvocations, BigNumberish, BlockIdentifier, + BlockTag, CairoAssembly, Call, CallContractResponse, @@ -68,7 +69,7 @@ function isEmptyQueryObject(obj?: Record): obj is undefined { const defaultOptions = { network: NetworkName.SN_GOERLI2, - blockIdentifier: 'pending', + blockIdentifier: BlockTag.pending, }; export class SequencerProvider implements ProviderInterface { diff --git a/src/provider/utils.ts b/src/provider/utils.ts index 806f77b3f..1cef1233a 100644 --- a/src/provider/utils.ts +++ b/src/provider/utils.ts @@ -1,5 +1,11 @@ /* eslint-disable max-classes-per-file */ -import type { BigNumberish, BlockIdentifier, BlockNumber, SequencerIdentifier } from '../types'; +import { + BigNumberish, + BlockIdentifier, + BlockNumber, + BlockTag, + SequencerIdentifier, +} from '../types'; import { isHex, toHex } from '../utils/num'; /** @deprecated prefer importing from 'types' over 'provider/utils' */ @@ -32,7 +38,7 @@ export function txIdentifier(txHash?: BigNumberish, txId?: BigNumberish): string return `transactionHash=${hashString}`; } -export const validBlockTags = ['latest', 'pending']; +export const validBlockTags = Object.values(BlockTag); export class Block { hash: BlockIdentifier = null; @@ -48,11 +54,14 @@ export class Block { this.hash = toHex(__identifier); } else if (typeof __identifier === 'number') { this.number = __identifier; - } else if (typeof __identifier === 'string' && validBlockTags.includes(__identifier)) { + } else if ( + typeof __identifier === 'string' && + validBlockTags.includes(__identifier as BlockTag) + ) { this.tag = __identifier; } else { // default - this.tag = 'pending'; + this.tag = BlockTag.pending; } } diff --git a/src/types/index.ts b/src/types/index.ts index e62c87eed..ae41cbca2 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -6,3 +6,4 @@ export * from './signer'; export * from './typedData'; export * from './api/sequencer'; export * from './api/rpc'; +export * from './calldata'; diff --git a/src/types/lib/index.ts b/src/types/lib/index.ts index a361fe0a5..dcef835f9 100644 --- a/src/types/lib/index.ts +++ b/src/types/lib/index.ts @@ -142,7 +142,11 @@ export enum BlockStatus { REJECTED = 'REJECTED', } -export type BlockTag = 'pending' | 'latest'; +export enum BlockTag { + pending = 'pending', + latest = 'latest', +} + export type BlockNumber = BlockTag | null | number; /** diff --git a/src/utils/calldata/cairo.ts b/src/utils/calldata/cairo.ts index 342d31a92..76148a8bd 100644 --- a/src/utils/calldata/cairo.ts +++ b/src/utils/calldata/cairo.ts @@ -1,17 +1,8 @@ -import { Abi, AbiStructs, BigNumberish, Uint256 } from '../../types'; +import { Abi, AbiStructs, BigNumberish, Uint, Uint256 } from '../../types'; import { isBigInt, isHex, isStringWholeNumber } from '../num'; import { encodeShortString, isShortString, isText } from '../shortString'; import { UINT_128_MAX, isUint256 } from '../uint256'; -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 -} - export const isLen = (name: string) => /_len$/.test(name); export const isTypeFelt = (type: string) => type === 'felt' || type === 'core::felt252'; export const isTypeArray = (type: string) => diff --git a/src/utils/calldata/index.ts b/src/utils/calldata/index.ts index d31564b9a..02cea47b3 100644 --- a/src/utils/calldata/index.ts +++ b/src/utils/calldata/index.ts @@ -11,6 +11,7 @@ import { RawArgs, RawArgsArray, Result, + ValidateType, } from '../../types'; import assert from '../assert'; import { isBigInt, toHex } from '../num'; @@ -37,29 +38,29 @@ export class CallData { /** * Validate arguments passed to the method as corresponding to the ones in the abi - * @param type string - type of the method + * @param type ValidateType - type of the method * @param method string - name of the method * @param args ArgsOrCalldata - arguments that are passed to the method */ - public validate(type: 'INVOKE' | 'CALL' | 'DEPLOY', method: string, args: ArgsOrCalldata = []) { + public validate(type: ValidateType, method: string, args: ArgsOrCalldata = []) { // ensure provided method of type exists - if (type !== 'DEPLOY') { + if (type !== ValidateType.DEPLOY) { const invocableFunctionNames = this.abi .filter((abi) => { if (abi.type !== 'function') return false; const isView = abi.stateMutability === 'view' || abi.state_mutability === 'view'; - return type === 'INVOKE' ? !isView : isView; + return type === ValidateType.INVOKE ? !isView : isView; }) .map((abi) => abi.name); assert( invocableFunctionNames.includes(method), - `${type === 'INVOKE' ? 'invocable' : 'viewable'} method not found in abi` + `${type === ValidateType.INVOKE ? 'invocable' : 'viewable'} method not found in abi` ); } // get requested method from abi const abiMethod = this.abi.find((abi) => - type === 'DEPLOY' + type === ValidateType.DEPLOY ? abi.name === method && abi.type === method : abi.name === method && abi.type === 'function' ) as FunctionAbi; diff --git a/src/utils/calldata/propertyOrder.ts b/src/utils/calldata/propertyOrder.ts index 502190766..1c09ccaa7 100644 --- a/src/utils/calldata/propertyOrder.ts +++ b/src/utils/calldata/propertyOrder.ts @@ -1,4 +1,4 @@ -import { AbiEntry, AbiStructs, RawArgsObject } from '../../types'; +import { AbiEntry, AbiStructs, RawArgsObject, Uint } from '../../types'; import { getArrayType, isCairo1Type, @@ -80,7 +80,7 @@ export default function orderPropsByAbi( switch (true) { case typeInArray in structs: return myArray.map((myObj) => orderStruct(myObj, structs[typeInArray].members)); - case typeInArray === 'core::integer::u256': + case typeInArray === Uint.u256: return myArray.map((u256) => { if (typeof u256 !== 'object') { return u256; diff --git a/src/utils/calldata/validate.ts b/src/utils/calldata/validate.ts index 51cd94bd2..d8912c10f 100644 --- a/src/utils/calldata/validate.ts +++ b/src/utils/calldata/validate.ts @@ -2,13 +2,12 @@ * Validate cairo contract method arguments * Flow: Determine type from abi and than validate against parameter */ -import { AbiEntry, AbiStructs, BigNumberish, FunctionAbi } from '../../types'; +import { AbiEntry, AbiStructs, BigNumberish, FunctionAbi, Uint } from '../../types'; import assert from '../assert'; import { toBigInt } from '../num'; import { isLongText } from '../shortString'; import { uint256ToBN } from '../uint256'; import { - Uint, getArrayType, isLen, isTypeArray,