Skip to content

Commit

Permalink
fix: eradicate static strings
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Jun 29, 2023
1 parent 8908a90 commit 597d2b5
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/contract/contractFactory.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/contract/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
RawArgs,
Result,
StructAbi,
ValidateType,
} from '../types';
import assert from '../utils/assert';
import { CallData, cairo } from '../utils/calldata';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AccountInvocations,
BigNumberish,
BlockIdentifier,
BlockTag,
CairoAssembly,
Call,
CallContractResponse,
Expand Down Expand Up @@ -68,7 +69,7 @@ function isEmptyQueryObject(obj?: Record<any, any>): obj is undefined {

const defaultOptions = {
network: NetworkName.SN_GOERLI2,
blockIdentifier: 'pending',
blockIdentifier: BlockTag.pending,
};

export class SequencerProvider implements ProviderInterface {
Expand Down
17 changes: 13 additions & 4 deletions src/provider/utils.ts
Original file line number Diff line number Diff line change
@@ -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' */
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './signer';
export * from './typedData';
export * from './api/sequencer';
export * from './api/rpc';
export * from './calldata';
6 changes: 5 additions & 1 deletion src/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
11 changes: 1 addition & 10 deletions src/utils/calldata/cairo.ts
Original file line number Diff line number Diff line change
@@ -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) =>
Expand Down
13 changes: 7 additions & 6 deletions src/utils/calldata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
RawArgs,
RawArgsArray,
Result,
ValidateType,
} from '../../types';
import assert from '../assert';
import { isBigInt, toHex } from '../num';
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/calldata/propertyOrder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbiEntry, AbiStructs, RawArgsObject } from '../../types';
import { AbiEntry, AbiStructs, RawArgsObject, Uint } from '../../types';
import {
getArrayType,
isCairo1Type,
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/utils/calldata/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 597d2b5

Please sign in to comment.