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: Types & utils improvements #1212

Merged
merged 20 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
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
3 changes: 2 additions & 1 deletion __tests__/cairo1v2_typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
types,
} from '../src';
import { hexToDecimalString } from '../src/utils/num';
import { encodeShortString, isString } from '../src/utils/shortString';
import { encodeShortString } from '../src/utils/shortString';
import { isString } from '../src/utils/typed';
import {
TEST_TX_VERSION,
compiledC1Account,
Expand Down
2 changes: 1 addition & 1 deletion __tests__/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import componentSchemas from '../schemas/component.json';
import libSchemas from '../schemas/lib.json';
import providerSchemas from '../schemas/provider.json';
import rpcSchemas from '../schemas/rpc.json';
import { isBigInt } from '../../src/utils/num';
import { isBigInt } from '../../src/utils/typed';

const matcherSchemas = [accountSchemas, libSchemas, providerSchemas, rpcSchemas];
const starknetSchemas = [
Expand Down
15 changes: 15 additions & 0 deletions __tests__/utils/assert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assert from '../../src/utils/assert';

describe('assert', () => {
test('should throw an error if condition is not true', () => {
expect(() => assert(false)).toThrow(new Error('Assertion failure'));
});

test('should throw an error with a specific message', () => {
expect(() => assert(false, 'Error message')).toThrow(new Error('Error message'));
});

test('should not throw an error if condition is true', () => {
expect(() => assert(true)).toBeTruthy();
});
});
53 changes: 0 additions & 53 deletions __tests__/utils/num.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
isHex,
toBigInt,
isBigInt,
toHex,
hexToDecimalString,
cleanHex,
Expand All @@ -15,8 +14,6 @@ import {
toCairoBool,
hexToBytes,
addPercent,
isNumber,
isBoolean,
} from '../../src/utils/num';
import { num } from '../../src';

Expand Down Expand Up @@ -49,23 +46,6 @@ describe('toBigInt', () => {
});
});

describe('isBigInt', () => {
test('should return true for big integers', () => {
expect(isBigInt(BigInt(10))).toBe(true);
expect(isBigInt(BigInt('9007199254740991'))).toBe(true);
});

test('should return false for non-big integers', () => {
expect(isBigInt(10)).toBe(false);
expect(isBigInt('10')).toBe(false);
expect(isBigInt(undefined)).toBe(false);
expect(isBigInt(null)).toBe(false);
expect(isBigInt({})).toBe(false);
expect(isBigInt([])).toBe(false);
expect(isBigInt(true)).toBe(false);
});
});

describe('toHex', () => {
test('should properly convert to hex-string', () => {
expect(toHex(100)).toBe('0x64');
Expand Down Expand Up @@ -177,39 +157,6 @@ describe('addPercent', () => {
});
});

describe('isNumber', () => {
test('should correctly determine if value is a number', () => {
expect(isNumber(0)).toBe(true);
expect(isNumber(123)).toBe(true);
expect(isNumber(-123)).toBe(true);

expect(isNumber(123n)).toBe(false);
expect(isNumber('')).toBe(false);
expect(isNumber('123')).toBe(false);
expect(isNumber(true)).toBe(false);
expect(isNumber(false)).toBe(false);
expect(isNumber(null)).toBe(false);
expect(isBoolean([])).toBe(false);
expect(isBoolean({})).toBe(false);
});
});

describe('isBoolean', () => {
test('should correctly determine if value is a boolean', () => {
expect(isBoolean(true)).toBe(true);
expect(isBoolean(false)).toBe(true);

expect(isBoolean(0)).toBe(false);
expect(isBoolean(1)).toBe(false);
expect(isBoolean('')).toBe(false);
expect(isBoolean('true')).toBe(false);
expect(isBoolean('false')).toBe(false);
expect(isBoolean(null)).toBe(false);
expect(isBoolean([])).toBe(false);
expect(isBoolean({})).toBe(false);
});
});

describe('stringToSha256ToArrayBuff4', () => {
test('should correctly hash&encode an utf8 string', () => {
const buff = num.stringToSha256ToArrayBuff4('LedgerW');
Expand Down
17 changes: 0 additions & 17 deletions __tests__/utils/shortString.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
encodeShortString,
isDecimalString,
isShortString,
isString,
} from '../../src/utils/shortString';

describe('shortString', () => {
Expand Down Expand Up @@ -110,22 +109,6 @@ describe('shortString', () => {
).toBe('');
});

describe('isString', () => {
test('should return true for strings', () => {
expect(isString('test')).toBe(true);
expect(isString('')).toBe(true);
});

test('should return false for non-string values', () => {
expect(isString(10)).toBe(false);
expect(isString({})).toBe(false);
expect(isString(null)).toBe(false);
expect(isString(undefined)).toBe(false);
expect(isString([])).toBe(false);
expect(isString(true)).toBe(false);
});
});

describe('isShortString', () => {
test('should return true for short strings', () => {
const shortStr = '1234567890123456789012345678901';
Expand Down
100 changes: 100 additions & 0 deletions __tests__/utils/typed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {
isUndefined,
isBigInt,
isBoolean,
isNumber,
isString,
isObject,
} from '../../src/utils/typed';

describe('isUndefined', () => {
test('should return true if value is undefined', () => {
expect(isUndefined(undefined)).toBe(true);
});

test('should return false if value is not undefined', () => {
const value = 'existing value';
expect(isUndefined(value)).toBe(false);
});
});

describe('isNumber', () => {
test('should correctly determine if value is a number', () => {
expect(isNumber(0)).toBe(true);
expect(isNumber(123)).toBe(true);
expect(isNumber(-123)).toBe(true);

expect(isNumber(123n)).toBe(false);
expect(isNumber('')).toBe(false);
expect(isNumber('123')).toBe(false);
expect(isNumber(true)).toBe(false);
expect(isNumber(false)).toBe(false);
expect(isNumber(null)).toBe(false);
expect(isBoolean([])).toBe(false);
expect(isBoolean({})).toBe(false);
});
});

describe('isBoolean', () => {
test('should correctly determine if value is a boolean', () => {
expect(isBoolean(true)).toBe(true);
expect(isBoolean(false)).toBe(true);

expect(isBoolean(0)).toBe(false);
expect(isBoolean(1)).toBe(false);
expect(isBoolean('')).toBe(false);
expect(isBoolean('true')).toBe(false);
expect(isBoolean('false')).toBe(false);
expect(isBoolean(null)).toBe(false);
expect(isBoolean([])).toBe(false);
expect(isBoolean({})).toBe(false);
});
});

describe('isBigInt', () => {
test('should return true for big integers', () => {
expect(isBigInt(BigInt(10))).toBe(true);
expect(isBigInt(BigInt('9007199254740991'))).toBe(true);
});

test('should return false for non-big integers', () => {
expect(isBigInt(10)).toBe(false);
expect(isBigInt('10')).toBe(false);
expect(isBigInt(undefined)).toBe(false);
expect(isBigInt(null)).toBe(false);
expect(isBigInt({})).toBe(false);
expect(isBigInt([])).toBe(false);
expect(isBigInt(true)).toBe(false);
});
});

describe('isString', () => {
test('should return true for strings', () => {
expect(isString('test')).toBe(true);
expect(isString('')).toBe(true);
});

test('should return false for non-string values', () => {
expect(isString(10)).toBe(false);
expect(isString({})).toBe(false);
expect(isString(null)).toBe(false);
expect(isString(undefined)).toBe(false);
expect(isString([])).toBe(false);
expect(isString(true)).toBe(false);
});
});

describe('isObject', () => {
test('should return true if value is object', () => {
expect(isObject({ test: 'test' })).toEqual(true);
expect(isObject({})).toEqual(true);
});

test('should return false if value is not object', () => {
expect(isObject(10)).toBe(false);
expect(isObject(null)).toBe(false);
expect(isObject(undefined)).toBe(false);
expect(isObject([])).toBe(false);
expect(isObject(true)).toBe(false);
});
});
39 changes: 16 additions & 23 deletions src/account/default.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { SPEC } from 'starknet-types-07';

import {
OutsideExecutionCallerAny,
SNIP9_V1_INTERFACE_ID,
Expand Down Expand Up @@ -30,6 +27,7 @@ import {
DeployContractUDCResponse,
DeployTransactionReceiptResponse,
EstimateFee,
UniversalSuggestedFee,
EstimateFeeAction,
EstimateFeeBulk,
Invocation,
Expand All @@ -47,7 +45,7 @@ import {
UniversalDeployerContractPayload,
UniversalDetails,
} from '../types';
import { ETransactionVersion, ETransactionVersion3, ResourceBounds } from '../types/api';
import { ETransactionVersion, ETransactionVersion3, type ResourceBounds } from '../types/api';
import {
OutsideExecutionVersion,
type OutsideExecution,
Expand All @@ -58,14 +56,14 @@ import { CallData } from '../utils/calldata';
import { extractContractHashes, isSierra } from '../utils/contract';
import { parseUDCEvent } from '../utils/events';
import { calculateContractAddressFromHash } from '../utils/hash';
import { isUndefined, isString } from '../utils/typed';
import { isHex, toBigInt, toCairoBool, toHex } from '../utils/num';
import {
buildExecuteFromOutsideCallData,
getOutsideCall,
getTypedData,
} from '../utils/outsideExecution';
import { parseContract } from '../utils/provider';
import { isString } from '../utils/shortString';
import { supportsInterface } from '../utils/src5';
import {
estimateFeeToBounds,
Expand Down Expand Up @@ -678,7 +676,7 @@ export class Account extends Provider implements AccountInterface {
* const call1: Call = { contractAddress: ethAddress, entrypoint: 'transfer', calldata: {
* recipient: recipientAccount.address, amount: cairo.uint256(100) } };
* const outsideTransaction1: OutsideTransaction = await signerAccount.getOutsideTransaction(callOptions, call3);
* // result = {
* // result = {
* // outsideExecution: {
* // caller: '0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691',
* // nonce: '0x28a612590dbc36927933c8ee0f357eee639c8b22b3d3aa86949eed3ada4ac55',
Expand Down Expand Up @@ -784,9 +782,10 @@ export class Account extends Provider implements AccountInterface {
version: ETransactionVersion,
{ type, payload }: EstimateFeeAction,
details: UniversalDetails
) {
): Promise<UniversalSuggestedFee> {
let maxFee: BigNumberish = 0;
let resourceBounds: ResourceBounds = estimateFeeToBounds(ZERO);

if (version === ETransactionVersion.V3) {
resourceBounds =
details.resourceBounds ??
Expand All @@ -803,28 +802,25 @@ export class Account extends Provider implements AccountInterface {
};
}

public async getSuggestedFee({ type, payload }: EstimateFeeAction, details: UniversalDetails) {
let feeEstimate: EstimateFee;

public async getSuggestedFee(
{ type, payload }: EstimateFeeAction,
details: UniversalDetails
): Promise<EstimateFee> {
switch (type) {
case TransactionType.INVOKE:
feeEstimate = await this.estimateInvokeFee(payload, details);
break;
return this.estimateInvokeFee(payload, details);

case TransactionType.DECLARE:
feeEstimate = await this.estimateDeclareFee(payload, details);
break;
return this.estimateDeclareFee(payload, details);

case TransactionType.DEPLOY_ACCOUNT:
feeEstimate = await this.estimateAccountDeployFee(payload, details);
break;
return this.estimateAccountDeployFee(payload, details);

case TransactionType.DEPLOY:
feeEstimate = await this.estimateDeployFee(payload, details);
break;
return this.estimateDeployFee(payload, details);

default:
feeEstimate = {
return {
gas_consumed: 0n,
gas_price: 0n,
overall_fee: ZERO,
Expand All @@ -834,10 +830,7 @@ export class Account extends Provider implements AccountInterface {
data_gas_consumed: 0n,
data_gas_price: 0n,
};
break;
}

return feeEstimate;
}

public async buildInvocation(
Expand All @@ -863,7 +856,7 @@ export class Account extends Provider implements AccountInterface {
const compressedCompiledContract = parseContract(contract);

if (
typeof compiledClassHash === 'undefined' &&
isUndefined(compiledClassHash) &&
(details.version === ETransactionVersion3.F3 || details.version === ETransactionVersion3.V3)
) {
throw Error('V3 Transaction work with Cairo1 Contracts and require compiledClassHash');
Expand Down
4 changes: 2 additions & 2 deletions src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ export abstract class AccountInterface extends ProviderInterface {
/**
* Estimate Fee for executing a UDC DEPLOY transaction on starknet
* This is different from the normal DEPLOY transaction as it goes through the Universal Deployer Contract (UDC)

* @param deployContractPayload array or singular
* - classHash: computed class hash of compiled contract
* - salt: address salt
* - unique: bool if true ensure unique salt
* - constructorCalldata: constructor calldata
*
*
* @param estimateFeeDetails -
* - blockIdentifier?
* - nonce?
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export * from './utils/responseParser';
export * from './utils/cairoDataTypes/uint256';
export * from './utils/cairoDataTypes/uint512';
export * from './utils/address';
export * from './utils/url';
export * from './utils/calldata';
export * from './utils/calldata/enum';
export * from './utils/contract';
Expand Down
Loading