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 5 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
12 changes: 12 additions & 0 deletions __tests__/utils/typed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { isUndefined } 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);
});
});
61 changes: 61 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 15 additions & 20 deletions src/account/default.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import type { SPEC } from 'starknet-types-07';
import { UDC, ZERO } from '../constants';
import { Provider, ProviderInterface } from '../provider';
import { Signer, SignerInterface } from '../signer';
Expand All @@ -23,6 +21,7 @@ import {
DeployContractUDCResponse,
DeployTransactionReceiptResponse,
EstimateFee,
UniversalSuggestedFee,
EstimateFeeAction,
EstimateFeeBulk,
Invocation,
Expand All @@ -40,11 +39,12 @@ import {
UniversalDeployerContractPayload,
UniversalDetails,
} from '../types';
import { ETransactionVersion, ETransactionVersion3, ResourceBounds } from '../types/api';
import { ETransactionVersion, ETransactionVersion3, type ResourceBounds } from '../types/api';
import { CallData } from '../utils/calldata';
import { extractContractHashes, isSierra } from '../utils/contract';
import { parseUDCEvent } from '../utils/events';
import { calculateContractAddressFromHash } from '../utils/hash';
import { isUndefined } from '../utils/typed';
import { toBigInt, toCairoBool } from '../utils/num';
import { parseContract } from '../utils/provider';
import { isString } from '../utils/shortString';
Expand Down Expand Up @@ -637,9 +637,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 @@ -656,28 +657,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 @@ -687,10 +685,7 @@ export class Account extends Provider implements AccountInterface {
data_gas_consumed: 0n,
data_gas_price: 0n,
};
break;
}

return feeEstimate;
}

public async buildInvocation(
Expand All @@ -716,7 +711,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 @@ -120,13 +120,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
13 changes: 9 additions & 4 deletions src/types/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import { DeclareTransactionReceiptResponse, EstimateFeeResponse } from './provid

export interface EstimateFee extends EstimateFeeResponse {}

export type UniversalSuggestedFee = {
maxFee: BigNumberish;
resourceBounds: ResourceBounds;
};

export type EstimateFeeBulk = Array<EstimateFee>;

// TODO: This is too wide generic with optional params
Expand Down Expand Up @@ -78,19 +83,19 @@ export type SimulateTransactionDetails = {

export type EstimateFeeAction =
| {
type: TransactionType.INVOKE;
type: typeof TransactionType.INVOKE;
payload: AllowArray<Call>;
}
| {
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;
};

Expand Down
24 changes: 11 additions & 13 deletions src/types/api/rpcspec_0_6/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ABI = Array<
FUNCTION | CONSTRUCTOR | L1_HANDLER | EVENT | STRUCT | ENUM | INTERFACE | IMPL
>;

export type FUNCTION = {
type FUNCTION = {
type: 'function';
name: string;
inputs: Array<{
Expand All @@ -24,7 +24,7 @@ export type FUNCTION = {
state_mutability: 'view' | 'external';
};

export type CONSTRUCTOR = {
type CONSTRUCTOR = {
type: 'constructor';
name: 'constructor';
inputs: Array<{
Expand All @@ -33,7 +33,7 @@ export type CONSTRUCTOR = {
}>;
};

export type L1_HANDLER = {
type L1_HANDLER = {
type: 'l1_handler';
name: string;
inputs: Array<{
Expand All @@ -46,22 +46,22 @@ export type L1_HANDLER = {
state_mutability: 'view' | 'external';
};

export type EVENT = {
type EVENT = {
type: 'event';
name: string;
} & (ENUM_EVENT | STRUCT_EVENT);

export type STRUCT_EVENT = {
type STRUCT_EVENT = {
kind: 'struct';
members: Array<EVENT_FIELD>;
};

export type ENUM_EVENT = {
type ENUM_EVENT = {
kind: 'enum';
variants: Array<EVENT_FIELD>;
};

export type STRUCT = {
type STRUCT = {
type: 'struct';
name: string;
members: Array<{
Expand All @@ -70,7 +70,7 @@ export type STRUCT = {
}>;
};

export type ENUM = {
type ENUM = {
type: 'enum';
name: string;
variants: Array<{
Expand All @@ -79,21 +79,19 @@ export type ENUM = {
}>;
};

export type INTERFACE = {
type INTERFACE = {
type: 'interface';
name: string;
items: Array<FUNCTION>;
};

export type IMPL = {
type IMPL = {
type: 'impl';
name: string;
interface_name: string;
};

export type EVENT_KIND = 'struct' | 'enum';

export type EVENT_FIELD = {
type EVENT_FIELD = {
name: string;
type: string;
kind: 'key' | 'data' | 'nested';
Expand Down
4 changes: 2 additions & 2 deletions src/types/api/rpcspec_0_6/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* version 0.6.0
*/

export { Methods } from './methods';
export { ABI } from './contract';
export * from './methods';
export * from './contract';
export * as Errors from './errors';
export * as SPEC from './components';
export * from './nonspec';
4 changes: 2 additions & 2 deletions src/types/api/rpcspec_0_6/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ import {
TransactionWithHash,
} from './nonspec';

export type Methods = ReadMethods & WriteMethods & TraceMethods;

type ReadMethods = {
// Returns the version of the Starknet JSON-RPC specification being used
starknet_specVersion: {
Expand Down Expand Up @@ -328,3 +326,5 @@ type TraceMethods = {
errors: Errors.BLOCK_NOT_FOUND | Errors.TRANSACTION_EXECUTION_ERROR;
};
};

export type Methods = ReadMethods & WriteMethods & TraceMethods;
Loading