Skip to content

Commit

Permalink
Chore/replace enums with companion pattern approach (#1083)
Browse files Browse the repository at this point in the history
* test: test setup refactor & remove rpc sequencer

* fix: enable sequencer tests to run on goerli

* chore: format markdowns

* fix: remove unnecessary goerli describeIf comment

* chore: remove setup verifier & simplify envs check logic

* test: naming improvements

* test: update test setup data log for testnet

* test: change from goerli to sepolia

* test: rename from rpc testnet to testnet

* test: remove `TEST_PROVIDER_BASE_URL`

* chore: replace enums with companion pattern

* chore: convert caior option & result enums to consts

* chore: add `ValuesType<T>` type helper

* test: Improve tests performance (#1121)

* test: fix transaction retry interval fallback for devnet tests

* test: remove test.only

* chore: revert from describeIfNot to describeIf

* Update _test.yml (#1126)

* chore(release): 6.9.0 [skip ci]

# [6.9.0](v6.8.0...v6.9.0) (2024-05-21)

### Bug Fixes

* cannot infer ts2742 types from starknet-types@0.7 ([#1098](#1098)) ([f1c3b8e](f1c3b8e))
* remove [warning] from typedoc for external usage ([#1095](#1095)) ([195186f](195186f)), closes [#1121](#1121) [#1126](#1126)

### Features

* add type coverage ([#1120](#1120)) ([eceda5d](eceda5d))
* provider.getL1MessageHash ([#1123](#1123)) ([1489cf2](1489cf2))

### Reverts

* Revert "chore: add examples to JsDoc for transaction.ts file (#1105)" (#1108) ([59eb01e](59eb01e)), closes [#1105](#1105) [#1108](#1108)

* fix: use proper constants

---------

Co-authored-by: Luka Saric <luka.saric@scayle.com>
Co-authored-by: Ivan Pavičić <ivan.pavicic@live.com>
Co-authored-by: Toni Tabak <tabaktoni@gmail.com>
Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
  • Loading branch information
5 people authored May 31, 2024
1 parent 12fd0f8 commit 70a6dab
Show file tree
Hide file tree
Showing 26 changed files with 350 additions and 246 deletions.
28 changes: 23 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
"node": true,
"jest": true
},
"extends": ["airbnb-base", "airbnb-typescript/base", "prettier", "plugin:prettier/recommended"],
"ignorePatterns": ["tsup.config.ts"],
"extends": [
"airbnb-base",
"airbnb-typescript/base",
"prettier",
"plugin:prettier/recommended"
],
"ignorePatterns": [
"tsup.config.ts"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
Expand All @@ -17,13 +24,24 @@
"sourceType": "module",
"project": "./tsconfig.eslint.json"
},
"plugins": ["@typescript-eslint"],
"plugins": [
"@typescript-eslint"
],
"rules": {
"class-methods-use-this": 0,
"import/prefer-default-export": 0,
"@typescript-eslint/no-redeclare": 0,
"@typescript-eslint/naming-convention": 0,
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"@typescript-eslint/no-use-before-define": ["error", "nofunc"],
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_"
}
],
"@typescript-eslint/no-use-before-define": [
"error",
"nofunc"
],
"func-names": 0
}
}
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# [6.9.0](https://github.com/starknet-io/starknet.js/compare/v6.8.0...v6.9.0) (2024-05-21)

### Bug Fixes

- cannot infer ts2742 types from starknet-types@0.7 ([#1098](https://github.com/starknet-io/starknet.js/issues/1098)) ([f1c3b8e](https://github.com/starknet-io/starknet.js/commit/f1c3b8e3aeb96f6efb7e512ac3ba689253004c9d))
- remove [warning] from typedoc for external usage ([#1095](https://github.com/starknet-io/starknet.js/issues/1095)) ([195186f](https://github.com/starknet-io/starknet.js/commit/195186fc2974ab0d164b1a48c68f7bf026329df5)), closes [#1121](https://github.com/starknet-io/starknet.js/issues/1121) [#1126](https://github.com/starknet-io/starknet.js/issues/1126)

### Features

- add type coverage ([#1120](https://github.com/starknet-io/starknet.js/issues/1120)) ([eceda5d](https://github.com/starknet-io/starknet.js/commit/eceda5dc1c39e472e1105e07797e76aaac3c1531))
- provider.getL1MessageHash ([#1123](https://github.com/starknet-io/starknet.js/issues/1123)) ([1489cf2](https://github.com/starknet-io/starknet.js/commit/1489cf25e7e8598ab161cecc62c82495f64daa33))

### Reverts

- Revert "chore: add examples to JsDoc for transaction.ts file (#1105)" (#1108) ([59eb01e](https://github.com/starknet-io/starknet.js/commit/59eb01e451cf64dfdacd6d34b2a709e0a1029f15)), closes [#1105](https://github.com/starknet-io/starknet.js/issues/1105) [#1108](https://github.com/starknet-io/starknet.js/issues/1108)

# [6.8.0](https://github.com/starknet-io/starknet.js/compare/v6.7.0...v6.8.0) (2024-04-23)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion __tests__/account.starknetId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,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
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
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "starknet",
"version": "6.8.0",
"version": "6.9.0",
"description": "JavaScript library for Starknet",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
6 changes: 4 additions & 2 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,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
74 changes: 44 additions & 30 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,43 +25,56 @@ 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_SEPOLIA = 'https://alpha-sepolia.starknet.io',
}

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

export enum StarknetChainId {
SN_MAIN = '0x534e5f4d41494e', // encodeShortString('SN_MAIN'),
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_SEPOLIA: 'https://alpha-sepolia.starknet.io',
} as const;

export type BaseUrl = ValuesType<typeof BaseUrl>;

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

export type NetworkName = ValuesType<typeof NetworkName>;

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

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;

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;

export type FeeMarginPercentage = ValuesType<typeof FeeMarginPercentage>;

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

export type UDC = ValuesType<typeof UDC>;

export const RPC_DEFAULT_VERSION = 'v0_7';

Expand All @@ -73,4 +87,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;
8 changes: 4 additions & 4 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ export class RpcProvider implements ProviderInterface {
blockIdentifier: BlockIdentifier = 'pending',
retryInterval: number = 5000
) {
if (blockIdentifier === BlockTag.latest) return;
if (blockIdentifier === BlockTag.LATEST) return;
const currentBlock = await this.getBlockNumber();
const targetBlock =
blockIdentifier === BlockTag.pending
blockIdentifier === BlockTag.PENDING
? currentBlock + 1
: Number(toHex(blockIdentifier as BigNumberish));
if (targetBlock <= currentBlock) return;
Expand Down Expand Up @@ -205,11 +205,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
8 changes: 4 additions & 4 deletions src/types/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,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
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

0 comments on commit 70a6dab

Please sign in to comment.