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

WIP: Loss StarknetChainID #1223

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
11 changes: 6 additions & 5 deletions src/channel/rpc_0_6.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NetworkName, StarknetChainId } from '../constants';
import { NetworkName } from '../constants';
import { LibraryError } from '../provider/errors';
import {
AccountInvocationItem,
Expand All @@ -7,6 +7,7 @@ import {
BlockIdentifier,
BlockTag,
Call,
ChainId,
DeclareContractTransaction,
DeployAccountContractTransaction,
Invocation,
Expand Down Expand Up @@ -47,7 +48,7 @@ export class RpcChannel {

readonly blockIdentifier: BlockIdentifier;

private chainId?: StarknetChainId;
private chainId?: ChainId;

private specVersion?: string;

Expand Down Expand Up @@ -98,7 +99,7 @@ export class RpcChannel {
return this.transactionRetryIntervalFallback ?? 5000;
}

public setChainId(chainId: StarknetChainId) {
public setChainId(chainId: ChainId) {
this.chainId = chainId;
}

Expand Down Expand Up @@ -158,12 +159,12 @@ export class RpcChannel {
}

public async getChainId() {
this.chainId ??= (await this.fetchEndpoint('starknet_chainId')) as StarknetChainId;
this.chainId ??= (await this.fetchEndpoint('starknet_chainId')) as ChainId;
return this.chainId;
}

public async getSpecVersion() {
this.specVersion ??= (await this.fetchEndpoint('starknet_specVersion')) as StarknetChainId;
this.specVersion ??= (await this.fetchEndpoint('starknet_specVersion')) as ChainId;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.specVersion ??= (await this.fetchEndpoint('starknet_specVersion')) as ChainId;
this.specVersion ??= (await this.fetchEndpoint('starknet_specVersion'));

This looks like the cast can be removed. Also for rpc_0_7.

return this.specVersion;
}

Expand Down
11 changes: 6 additions & 5 deletions src/channel/rpc_0_7.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NetworkName, StarknetChainId } from '../constants';
import { NetworkName } from '../constants';
import { LibraryError } from '../provider/errors';
import {
AccountInvocationItem,
Expand All @@ -7,6 +7,7 @@ import {
BlockIdentifier,
BlockTag,
Call,
ChainId,
DeclareContractTransaction,
DeployAccountContractTransaction,
Invocation,
Expand Down Expand Up @@ -47,7 +48,7 @@ export class RpcChannel {

readonly blockIdentifier: BlockIdentifier;

private chainId?: StarknetChainId;
private chainId?: ChainId;

private specVersion?: string;

Expand Down Expand Up @@ -98,7 +99,7 @@ export class RpcChannel {
return this.transactionRetryIntervalFallback ?? 5000;
}

public setChainId(chainId: StarknetChainId) {
public setChainId(chainId: ChainId) {
this.chainId = chainId;
}

Expand Down Expand Up @@ -158,12 +159,12 @@ export class RpcChannel {
}

public async getChainId() {
this.chainId ??= (await this.fetchEndpoint('starknet_chainId')) as StarknetChainId;
this.chainId ??= (await this.fetchEndpoint('starknet_chainId')) as ChainId;
return this.chainId;
}

public async getSpecVersion() {
this.specVersion ??= (await this.fetchEndpoint('starknet_specVersion')) as StarknetChainId;
this.specVersion ??= (await this.fetchEndpoint('starknet_specVersion')) as ChainId;
return this.specVersion;
}

Expand Down
4 changes: 2 additions & 2 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { RPC06, RPC07 } from '../channel';
import { StarknetChainId } from '../constants';
import type {
AccountInvocations,
BigNumberish,
Block,
BlockIdentifier,
Call,
CallContractResponse,
ChainId,
ContractClassResponse,
ContractVersion,
DeclareContractResponse,
Expand Down Expand Up @@ -41,7 +41,7 @@ export abstract class ProviderInterface {
*
* @returns the chain Id
*/
public abstract getChainId(): Promise<StarknetChainId>;
public abstract getChainId(): Promise<ChainId>;

/**
* Calls a function on the Starknet contract.
Expand Down
5 changes: 3 additions & 2 deletions src/types/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { StarknetChainId } from '../../constants';
import { weierstrass } from '../../utils/ec';
import { EDataAvailabilityMode, ResourceBounds } from '../api';
import { CairoEnum } from '../cairoEnum';
Expand Down Expand Up @@ -159,7 +158,7 @@ export type Details = {
nonce: BigNumberish;
maxFee: BigNumberish;
version: BigNumberish;
chainId: StarknetChainId;
chainId: ChainId;
};

export type InvocationsDetailsWithNonce =
Expand Down Expand Up @@ -313,4 +312,6 @@ export type ContractVersion = {
compiler: CompilerVersion;
};

export type ChainId = '0x534e5f4d41494e' | '0x534e5f5345504f4c4941' | (`0x${string}` & {});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do this in a more verbose way:

Suggested change
export type ChainId = '0x534e5f4d41494e' | '0x534e5f5345504f4c4941' | (`0x${string}` & {});
export type StarknetChainId = ValuesType<{
SN_MAIN: '0x534e5f4d41494e'; // encodeShortString('SN_MAIN'),
SN_SEPOLIA: '0x534e5f5345504f4c4941'; // encodeShortString('SN_SEPOLIA')
}>;
export type ChainId = StarknetChainId | (`0x${string}` & {});

And also mark the existing StarknetChainId enum in src/constants.ts with /** @deprecated */.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea agree on extraction of values from type, but regarding deprecation, dev can still use enum to initialise known Starknet Chain Id's, so I would leave the enum (const) part of it.


export * from './contract';
6 changes: 3 additions & 3 deletions src/types/provider/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NetworkName, StarknetChainId } from '../../constants';
import { BlockIdentifier } from '../lib';
import { NetworkName } from '../../constants';
import { BlockIdentifier, ChainId } from '../lib';

export interface ProviderOptions extends RpcProviderOptions {}

Expand All @@ -9,7 +9,7 @@ export type RpcProviderOptions = {
transactionRetryIntervalFallback?: number;
headers?: object;
blockIdentifier?: BlockIdentifier;
chainId?: StarknetChainId;
chainId?: ChainId;
specVersion?: string;
default?: boolean;
waitMode?: boolean;
Expand Down
14 changes: 7 additions & 7 deletions src/types/signer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { StarknetChainId } from '../constants';
import { ETransactionVersion, ETransactionVersion2, ETransactionVersion3 } from './api';
import {
BigNumberish,
CairoVersion,
ChainId,
DeployAccountContractPayload,
InvocationsDetails,
V3TransactionDetails,
Expand All @@ -16,7 +16,7 @@ export type InvocationsSignerDetails = (V2InvocationsSignerDetails | V3Invocatio
export type V2InvocationsSignerDetails = {
walletAddress: string;
cairoVersion: CairoVersion;
chainId: StarknetChainId;
chainId: ChainId;
nonce: BigNumberish;
maxFee: BigNumberish;
version: `${ETransactionVersion2}`;
Expand All @@ -25,7 +25,7 @@ export type V2InvocationsSignerDetails = {
export type V3InvocationsSignerDetails = V3TransactionDetails & {
walletAddress: string;
cairoVersion: CairoVersion;
chainId: StarknetChainId;
chainId: ChainId;
version: `${ETransactionVersion3}`;
};

Expand All @@ -37,15 +37,15 @@ export type V2DeclareSignerDetails = Required<InvocationsDetails> & {
classHash: string;
compiledClassHash?: string;
senderAddress: string;
chainId: StarknetChainId;
chainId: ChainId;
version: `${ETransactionVersion2}`;
};

export type V3DeclareSignerDetails = V3TransactionDetails & {
classHash: string;
compiledClassHash: string;
senderAddress: string;
chainId: StarknetChainId;
chainId: ChainId;
version: `${ETransactionVersion3}`;
};

Expand All @@ -56,13 +56,13 @@ export type DeployAccountSignerDetails =
export type V2DeployAccountSignerDetails = Required<DeployAccountContractPayload> &
Required<InvocationsDetails> & {
contractAddress: BigNumberish;
chainId: StarknetChainId;
chainId: ChainId;
version: `${ETransactionVersion2}`;
};

export type V3DeployAccountSignerDetails = Required<DeployAccountContractPayload> &
V3TransactionDetails & {
contractAddress: BigNumberish;
chainId: StarknetChainId;
chainId: ChainId;
version: `${ETransactionVersion3}`;
};
3 changes: 1 addition & 2 deletions src/utils/calldata/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const guard = {
isBN: (data: Record<string, any>, type: Record<string, any>, key: string) => {
if (!isBigInt(data[key]))
throw new Error(
`Data and formatter mismatch on ${key}:${type[key]}, expected response data ${key}:${
data[key]
`Data and formatter mismatch on ${key}:${type[key]}, expected response data ${key}:${data[key]
} to be BN instead it is ${typeof data[key]}`
);
},
Expand Down
15 changes: 7 additions & 8 deletions src/utils/hash/transactionHash/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* Transaction Hash based on Transaction Version
*/

import { StarknetChainId } from '../../../constants';
import { BigNumberish, Calldata } from '../../../types';
import { BigNumberish, Calldata, ChainId } from '../../../types';
import {
EDAMode,
ETransactionVersion,
Expand Down Expand Up @@ -38,15 +37,15 @@ type CalcV2InvokeTxHashArgs = {
version: `${ETransactionVersion2}`;
compiledCalldata: Calldata;
maxFee: BigNumberish;
chainId: StarknetChainId;
chainId: ChainId;
nonce: BigNumberish;
};

type CalcV3InvokeTxHashArgs = {
senderAddress: BigNumberish;
version: `${ETransactionVersion3}`;
compiledCalldata: Calldata;
chainId: StarknetChainId;
chainId: ChainId;
nonce: BigNumberish;
accountDeploymentData: BigNumberish[];
nonceDataAvailabilityMode: EDAMode;
Expand Down Expand Up @@ -96,7 +95,7 @@ type CalcV2DeclareTxHashArgs = {
senderAddress: BigNumberish;
version: `${ETransactionVersion2}`;
maxFee: BigNumberish;
chainId: StarknetChainId;
chainId: ChainId;
nonce: BigNumberish;
compiledClassHash?: string;
};
Expand All @@ -106,7 +105,7 @@ type CalcV3DeclareTxHashArgs = {
compiledClassHash: string;
senderAddress: BigNumberish;
version: `${ETransactionVersion3}`;
chainId: StarknetChainId;
chainId: ChainId;
nonce: BigNumberish;
accountDeploymentData: BigNumberish[];
nonceDataAvailabilityMode: EDAMode;
Expand Down Expand Up @@ -164,7 +163,7 @@ type CalcV2DeployAccountTxHashArgs = {
salt: BigNumberish;
version: `${ETransactionVersion2}`;
maxFee: BigNumberish;
chainId: StarknetChainId;
chainId: ChainId;
nonce: BigNumberish;
};

Expand All @@ -174,7 +173,7 @@ type CalcV3DeployAccountTxHashArgs = {
compiledConstructorCalldata: Calldata;
salt: BigNumberish;
version: `${ETransactionVersion3}`;
chainId: StarknetChainId;
chainId: ChainId;
nonce: BigNumberish;
nonceDataAvailabilityMode: EDAMode;
feeDataAvailabilityMode: EDAMode;
Expand Down
10 changes: 5 additions & 5 deletions src/utils/hash/transactionHash/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* eslint-disable no-param-reassign */
/* eslint-disable import/extensions */
import { StarknetChainId, TransactionHashPrefix } from '../../../constants';
import { BigNumberish, RawCalldata } from '../../../types';
import { BigNumberish, ChainId, RawCalldata } from '../../../types';
import { starkCurve } from '../../ec';
import { toBigInt } from '../../num';
import { getSelector } from '../selector';
Expand Down Expand Up @@ -33,7 +33,7 @@ export function calculateTransactionHashCommon(
entryPointSelector: BigNumberish,
calldata: RawCalldata,
maxFee: BigNumberish,
chainId: StarknetChainId,
chainId: ChainId,
additionalData: BigNumberish[] = []
): string {
const calldataHash = computeHashOnElements(calldata);
Expand Down Expand Up @@ -61,7 +61,7 @@ export function calculateDeclareTransactionHash(
senderAddress: BigNumberish,
version: BigNumberish,
maxFee: BigNumberish,
chainId: StarknetChainId,
chainId: ChainId,
nonce: BigNumberish,
compiledClassHash?: string
): string {
Expand All @@ -88,7 +88,7 @@ export function calculateDeployAccountTransactionHash(
salt: BigNumberish,
version: BigNumberish,
maxFee: BigNumberish,
chainId: StarknetChainId,
chainId: ChainId,
nonce: BigNumberish
) {
const calldata = [classHash, salt, ...constructorCalldata];
Expand All @@ -114,7 +114,7 @@ export function calculateTransactionHash(
version: BigNumberish,
calldata: RawCalldata,
maxFee: BigNumberish,
chainId: StarknetChainId,
chainId: ChainId,
nonce: BigNumberish
): string {
return calculateTransactionHashCommon(
Expand Down
12 changes: 6 additions & 6 deletions src/utils/hash/transactionHash/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import { poseidonHashMany } from '@scure/starknet';

import { StarknetChainId, TransactionHashPrefix } from '../../../constants';
import { BigNumberish, Calldata } from '../../../types';
import { TransactionHashPrefix } from '../../../constants';
import { BigNumberish, Calldata, ChainId } from '../../../types';
import { EDAMode, ResourceBounds } from '../../../types/api';
import { toHex } from '../../num';
import { encodeShortString } from '../../shortString';
Expand Down Expand Up @@ -42,7 +42,7 @@ export function calculateTransactionHashCommon(
txHashPrefix: TransactionHashPrefix,
version: BigNumberish,
senderAddress: BigNumberish,
chainId: StarknetChainId,
chainId: ChainId,
nonce: BigNumberish,
tip: BigNumberish,
paymasterData: BigNumberish[],
Expand Down Expand Up @@ -77,7 +77,7 @@ export function calculateDeployAccountTransactionHash(
compiledConstructorCalldata: Calldata,
salt: BigNumberish,
version: BigNumberish,
chainId: StarknetChainId,
chainId: ChainId,
nonce: BigNumberish,
nonceDataAvailabilityMode: EDAMode,
feeDataAvailabilityMode: EDAMode,
Expand Down Expand Up @@ -109,7 +109,7 @@ export function calculateDeclareTransactionHash(
compiledClassHash: string,
senderAddress: BigNumberish,
version: BigNumberish,
chainId: StarknetChainId,
chainId: ChainId,
nonce: BigNumberish,
accountDeploymentData: BigNumberish[],
nonceDataAvailabilityMode: EDAMode,
Expand Down Expand Up @@ -141,7 +141,7 @@ export function calculateInvokeTransactionHash(
senderAddress: BigNumberish,
version: BigNumberish,
compiledCalldata: Calldata,
chainId: StarknetChainId,
chainId: ChainId,
nonce: BigNumberish,
accountDeploymentData: BigNumberish[],
nonceDataAvailabilityMode: EDAMode,
Expand Down
Loading