Skip to content

Commit 1327a06

Browse files
committed
use assert instead of throwing errors
1 parent 55054a3 commit 1327a06

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

src/utils/calldata/apiDataEncoder.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: To be implemented later in this PR

src/utils/calldata/calldataDecoder.ts

+11-25
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
StructAbi,
1010
} from '../../types';
1111
import { CairoUint256 } from '../cairoDataTypes/uint256';
12-
import { byteArrayFromString } from './byteArray';
1312
import {
1413
isTypeFelt,
1514
getArrayType,
@@ -30,7 +29,7 @@ import {
3029
} from './enum';
3130
import extractTupleMemberTypes from './tuple';
3231
import { decodeShortString } from '../shortString';
33-
import { hexToBytes } from '../num';
32+
import assert from '../assert';
3433

3534
/**
3635
* Decode a base type from calldata.
@@ -42,18 +41,14 @@ import { hexToBytes } from '../num';
4241
function decodeBaseType(type: string, calldata: string | string[]): BigNumberish | CairoUint256 {
4342
switch (true) {
4443
case CairoUint256.isAbiType(type):
45-
if (!Array.isArray(calldata) || calldata.length !== 2) {
46-
throw new Error('Expected calldata for CairoUint256 as an array of two strings.');
47-
}
44+
assert(Array.isArray(calldata) && calldata.length === 2, 'Expected calldata for CairoUint256 as an array of two strings.')
4845
return CairoUint256.fromCalldata([calldata[0], calldata[1]]);
4946

5047
case isTypeBytes31(type):
5148
return decodeShortString(calldata as string);
5249

5350
case isTypeFelt(type):
54-
if (typeof calldata !== 'string') {
55-
throw new Error('Expected string calldata for base type decoding.');
56-
}
51+
assert(typeof calldata === 'string', 'Expected string calldata for base type decoding.')
5752
return BigInt(calldata);
5853

5954
default:
@@ -198,10 +193,9 @@ function decodeCalldataValue(
198193
// CairoOption decoding
199194
if (isTypeOption(type)) {
200195
const match = type.match(/Option<(.*)>/);
201-
if (!match) {
202-
throw new Error(`Type "${type}" is not a valid Option type.`);
203-
}
204-
const innerType = match[1];
196+
assert(match !== null, `Type "${type}" is not a valid Option type.`);
197+
198+
const innerType = match![1];
205199
return decodeCairoOption(
206200
Array.isArray(calldata) ? calldata : [calldata],
207201
innerType,
@@ -213,9 +207,7 @@ function decodeCalldataValue(
213207
// CairoResult decoding
214208
if (isTypeResult(type)) {
215209
const matches = type.match(/Result<(.+),\s*(.+)>/);
216-
if (!matches || matches.length < 3) {
217-
throw new Error(`Type "${type}" is not a valid Option type.`);
218-
}
210+
assert(matches !== null && matches.length > 2, `Type "${type}" is not a valid Option type.`)
219211

220212
const okType = matches[1];
221213
const errType = matches[2];
@@ -273,9 +265,7 @@ function decodeStruct(
273265
enums: AbiEnums
274266
): ParsedStruct {
275267
const structAbi: StructAbi = structs[structName];
276-
if (!structAbi) {
277-
throw new Error(`Struct with name ${structName} not found.`);
278-
}
268+
assert(structAbi !== null, `Struct with name ${structName} not found.`);
279269

280270
let index = 0;
281271
const result: ParsedStruct = {};
@@ -300,14 +290,10 @@ function decodeStruct(
300290
*/
301291
function decodeEnum(calldataValues: string[], enumName: string, enums: AbiEnums): CairoEnum {
302292
const enumDefinition = enums[enumName];
303-
if (!enumDefinition) {
304-
throw new Error(`Enum with name ${enumName} not found.`);
305-
}
293+
assert(enumDefinition !== null, `Enum with name ${enumName} not found.`);
306294

307295
const variantIndex = parseInt(calldataValues[0], 10);
308-
if (variantIndex < 0 || variantIndex >= enumDefinition.variants.length) {
309-
throw new Error(`Variant index ${variantIndex} out of range for enum ${enumName}.`);
310-
}
296+
assert(variantIndex >=0 && variantIndex < enumDefinition.variants.length, `Variant index ${variantIndex} out of range for enum ${enumName}.`);
311297

312298
const variant = enumDefinition.variants[variantIndex];
313299

@@ -405,7 +391,7 @@ function getExpectedCalldataLengthForEnum(
405391
enums: AbiEnums
406392
): number {
407393
const enumDefinition = enums[enumName];
408-
if (!enumDefinition) throw new Error(`Enum with name ${enumName} not found.`);
394+
assert(enumDefinition, `Enum with name ${enumName} not found.`);
409395

410396
const variantIndex = parseInt(variantIndexCalldata, 10);
411397
const variant = enumDefinition.variants[variantIndex];

0 commit comments

Comments
 (0)