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/replace enums with companion pattern approach #1083

Merged
merged 26 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6d057f7
test: test setup refactor & remove rpc sequencer
Mar 26, 2024
2992590
fix: enable sequencer tests to run on goerli
Mar 27, 2024
109fe15
chore: format markdowns
Mar 27, 2024
9cc532e
fix: remove unnecessary goerli describeIf comment
Mar 27, 2024
415f152
chore: remove setup verifier & simplify envs check logic
Mar 28, 2024
f8ddeef
Merge branch 'develop' into chore/remove-sequencer-and-test-setup-ref…
Mar 28, 2024
76eeb82
Merge branch 'develop' into chore/remove-sequencer-and-test-setup-ref…
Apr 3, 2024
652e6bb
test: naming improvements
Apr 3, 2024
8f83e3a
test: update test setup data log for testnet
Apr 3, 2024
55d36ad
test: change from goerli to sepolia
Apr 4, 2024
d94a6b3
test: rename from rpc testnet to testnet
Apr 5, 2024
6481eb0
test: remove `TEST_PROVIDER_BASE_URL`
Apr 5, 2024
04f6885
Merge branch 'next-version' into chore/remove-sequencer-and-test-setu…
ivpavici Apr 8, 2024
8b0d4bc
chore: replace enums with companion pattern
Apr 17, 2024
4115475
chore: convert caior option & result enums to consts
Apr 17, 2024
bbb76b0
chore: add `ValuesType<T>` type helper
Apr 17, 2024
f1293bf
Merge branch 'develop' into chore/replace-enums-with-companion-patter…
Apr 23, 2024
a5c3d0b
Merge branch 'develop' into chore/replace-enums-with-companion-patter…
lukasaric May 5, 2024
d002bae
test: Improve tests performance (#1121)
lukasaric May 6, 2024
e7e817a
Merge branch 'develop' into chore/replace-enums-with-companion-patter…
lukasaric May 6, 2024
daf6867
chore: revert from describeIfNot to describeIf
lukasaric May 7, 2024
37526cf
Update _test.yml (#1126)
ivpavici May 15, 2024
fe24214
Merge pull request #1110 from starknet-io/next-version
tabaktoni May 21, 2024
66a5c03
chore(release): 6.9.0 [skip ci]
semantic-release-bot May 21, 2024
5d550da
Merge branch 'develop' into chore/replace-enums-with-companion-patter…
lukasaric May 23, 2024
879590a
fix: use proper constants
lukasaric May 23, 2024
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
2 changes: 1 addition & 1 deletion __tests__/account.starknetId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('deploy and test Wallet', () => {

test('Get the stark name of the account (using starknet.id)', async () => {
const address = await account.getAddressFromStarkName('fricoben.stark', namingAddress);
expect(hexToDecimalString(address as string)).toEqual(hexToDecimalString(account.address));
expect(hexToDecimalString(address)).toEqual(hexToDecimalString(account.address));
});

test('Get the account from a stark name of the account (using starknet.id)', async () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/cairo1v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ describe('Cairo 1', () => {

describe('Cairo2.6.0 Sierra1.5.0', () => {
test('declare Sierra 1.5.0', async () => {
const declare260Response = await account.declare({
const declare260Response = await account.declareIfNot({
contract: compiledC260,
casm: compiledC260Casm,
});
Expand Down
5 changes: 3 additions & 2 deletions __tests__/utils/starknetId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ function generateString(length: number, seed: number): string {
return result;
}

describe('Should tets StarknetId utils', () => {
describe('Should test StarknetId utils', () => {
test('Should test useEncoded and useDecoded hook with a random string', () => {
for (let index = 0; index < 2500; index += 1) {
const randomString = generateString(10, index);
expect(useDecoded([useEncoded(randomString)])).toBe(randomString.concat('.stark'));
const decoded = useDecoded([useEncoded(randomString)]);
expect(decoded).toBe(randomString.concat('.stark'));
}
});

Expand Down
6 changes: 4 additions & 2 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ export class Account extends Provider implements AccountInterface {

public cairoVersion: CairoVersion;

readonly transactionVersion: ETransactionVersion.V2 | ETransactionVersion.V3;
readonly transactionVersion: typeof ETransactionVersion.V2 | typeof ETransactionVersion.V3;

constructor(
providerOrOptions: ProviderOptions | ProviderInterface,
address: string,
pkOrSigner: Uint8Array | string | SignerInterface,
cairoVersion?: CairoVersion,
transactionVersion: ETransactionVersion.V2 | ETransactionVersion.V3 = ETransactionVersion.V2 // TODO: Discuss this, set to v2 for backward compatibility
transactionVersion:
| typeof ETransactionVersion.V2
| typeof ETransactionVersion.V3 = ETransactionVersion.V2 // TODO: Discuss this, set to v2 for backward compatibility
) {
super(providerOrOptions);
this.address = address.toLowerCase();
Expand Down
4 changes: 2 additions & 2 deletions src/channel/rpc_0_6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { getVersionsByType } from '../utils/transaction';

const defaultOptions = {
headers: { 'Content-Type': 'application/json' },
blockIdentifier: BlockTag.pending,
blockIdentifier: BlockTag.PENDING,
retries: 200,
};

Expand Down Expand Up @@ -93,7 +93,7 @@ export class RpcChannel {
if (rpcError) {
const { code, message, data } = rpcError;
throw new LibraryError(
`RPC: ${method} with params ${stringify(params, null, 2)}\n
`RPC: ${method} with params ${stringify(params, null, 2)}\n
${code}: ${message}: ${stringify(data)}`
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/channel/rpc_0_7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { getVersionsByType } from '../utils/transaction';

const defaultOptions = {
headers: { 'Content-Type': 'application/json' },
blockIdentifier: BlockTag.pending,
blockIdentifier: BlockTag.PENDING,
retries: 200,
};

Expand Down
86 changes: 53 additions & 33 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ETransactionVersion } from './types/api';
import { ValuesType } from './types/helpers/valuesType';

export { IS_BROWSER } from './utils/encode';

Expand All @@ -24,46 +25,65 @@ export const MAX_STORAGE_ITEM_SIZE = 256n;
export const ADDR_BOUND = 2n ** 251n - MAX_STORAGE_ITEM_SIZE;

const range = (min: bigint, max: bigint) => ({ min, max }) as const;

export const RANGE_FELT = range(ZERO, PRIME - 1n);
export const RANGE_I128 = range(-(2n ** 127n), 2n ** 127n - 1n);
export const RANGE_U128 = range(ZERO, 2n ** 128n - 1n);

export enum BaseUrl {
SN_MAIN = 'https://alpha-mainnet.starknet.io',
SN_GOERLI = 'https://alpha4.starknet.io',
SN_SEPOLIA = 'https://alpha-sepolia.starknet.io',
}

export enum NetworkName {
SN_MAIN = 'SN_MAIN',
SN_GOERLI = 'SN_GOERLI',
SN_SEPOLIA = 'SN_SEPOLIA',
}

export enum StarknetChainId {
SN_MAIN = '0x534e5f4d41494e', // encodeShortString('SN_MAIN'),
SN_GOERLI = '0x534e5f474f45524c49', // encodeShortString('SN_GOERLI')
SN_SEPOLIA = '0x534e5f5345504f4c4941', // encodeShortString('SN_SEPOLIA')
}

export enum TransactionHashPrefix {
DECLARE = '0x6465636c617265', // encodeShortString('declare'),
DEPLOY = '0x6465706c6f79', // encodeShortString('deploy'),
DEPLOY_ACCOUNT = '0x6465706c6f795f6163636f756e74', // encodeShortString('deploy_account'),
INVOKE = '0x696e766f6b65', // encodeShortString('invoke'),
L1_HANDLER = '0x6c315f68616e646c6572', // encodeShortString('l1_handler'),
}

export const enum feeMarginPercentage {
L1_BOUND_MAX_AMOUNT = 50,
L1_BOUND_MAX_PRICE_PER_UNIT = 50,
MAX_FEE = 50,
}
export const BaseUrl = {
SN_MAIN: 'https://alpha-mainnet.starknet.io',
SN_GOERLI: 'https://alpha4.starknet.io',
SN_SEPOLIA: 'https://alpha-sepolia.starknet.io',
} as const;

// eslint-disable-next-line @typescript-eslint/no-redeclare -- intentionally naming the variable the same as the type
lukasaric marked this conversation as resolved.
Show resolved Hide resolved
export type BaseUrl = ValuesType<typeof BaseUrl>;

export const NetworkName = {
SN_MAIN: 'SN_MAIN',
SN_GOERLI: 'SN_GOERLI',
SN_SEPOLIA: 'SN_SEPOLIA',
} as const;

// eslint-disable-next-line @typescript-eslint/no-redeclare -- intentionally naming the variable the same as the type
export type NetworkName = ValuesType<typeof NetworkName>;

export const StarknetChainId = {
SN_MAIN: '0x534e5f4d41494e', // encodeShortString('SN_MAIN'),
SN_GOERLI: '0x534e5f474f45524c49', // encodeShortString('SN_GOERLI')
SN_SEPOLIA: '0x534e5f5345504f4c4941', // encodeShortString('SN_SEPOLIA')
} as const;

// eslint-disable-next-line @typescript-eslint/no-redeclare -- intentionally naming the variable the same as the type
export type StarknetChainId = ValuesType<typeof StarknetChainId>;

export const TransactionHashPrefix = {
DECLARE: '0x6465636c617265', // encodeShortString('declare'),
DEPLOY: '0x6465706c6f79', // encodeShortString('deploy'),
DEPLOY_ACCOUNT: '0x6465706c6f795f6163636f756e74', // encodeShortString('deploy_account'),
INVOKE: '0x696e766f6b65', // encodeShortString('invoke'),
L1_HANDLER: '0x6c315f68616e646c6572', // encodeShortString('l1_handler'),
} as const;

// eslint-disable-next-line @typescript-eslint/no-redeclare -- intentionally naming the variable the same as the type
export type TransactionHashPrefix = ValuesType<typeof TransactionHashPrefix>;

export const FeeMarginPercentage = {
L1_BOUND_MAX_AMOUNT: 50,
L1_BOUND_MAX_PRICE_PER_UNIT: 50,
MAX_FEE: 50,
} as const;

// eslint-disable-next-line @typescript-eslint/no-redeclare -- intentionally naming the variable the same as the type
export type FeeMarginPercentage = ValuesType<typeof FeeMarginPercentage>;

export const UDC = {
ADDRESS: '0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf',
ENTRYPOINT: 'deployContract',
};
} as const;

// eslint-disable-next-line @typescript-eslint/no-redeclare -- intentionally naming the variable the same as the type
export type UDC = ValuesType<typeof UDC>;

export const RPC_DEFAULT_VERSION = 'v0_7';

Expand All @@ -80,4 +100,4 @@ export const RPC_NODES = {
`https://starknet-sepolia.public.blastapi.io/rpc/${RPC_DEFAULT_VERSION}`,
`https://free-rpc.nethermind.io/sepolia-juno/${RPC_DEFAULT_VERSION}`,
],
};
} as const;
4 changes: 2 additions & 2 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ export class RpcProvider implements ProviderInterface {

/**
* Return transactions from pending block
* @deprecated Instead use getBlock(BlockTag.pending); (will be removed in next minor version)
* @deprecated Instead use getBlock(BlockTag.PENDING); (will be removed in next minor version)
* Utility method, same result can be achieved using getBlockWithTxHashes(BlockTag.pending);
*/
public async getPendingTransactions() {
const { transactions } = await this.getBlockWithTxHashes(BlockTag.pending).then(
const { transactions } = await this.getBlockWithTxHashes(BlockTag.PENDING).then(
this.responseParser.parseGetBlockResponse
);
return Promise.all(transactions.map((it: any) => this.getTransactionByHash(it)));
Expand Down
20 changes: 12 additions & 8 deletions src/types/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EDataAvailabilityMode, ETransactionVersion, ResourceBounds } from './api';
import { ValuesType } from './helpers/valuesType';
import {
AllowArray,
BigNumberish,
Expand Down Expand Up @@ -76,26 +77,29 @@ export type SimulateTransactionDetails = {
skipExecute?: boolean;
} & Partial<V3TransactionDetails>;

export enum SIMULATION_FLAG {
SKIP_VALIDATE = 'SKIP_VALIDATE',
SKIP_EXECUTE = 'SKIP_EXECUTE',
}
export const SimulationFlag = {
SKIP_VALIDATE: 'SKIP_VALIDATE',
SKIP_EXECUTE: 'SKIP_EXECUTE',
} as const;

// eslint-disable-next-line @typescript-eslint/no-redeclare -- intentionally naming the variable the same as the type
export type SimulationFlag = ValuesType<typeof SimulationFlag>;

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
3 changes: 0 additions & 3 deletions src/types/api/rpcspec_0_6/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ type IMPL = {
interface_name: string;
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type EVENT_KIND = 'struct' | 'enum';

type EVENT_FIELD = {
name: string;
type: string;
Expand Down
Loading