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

fix(utils): fix block identifier #1076

Merged
merged 49 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
dd7e48b
test: eth signer
PhilippeR26 Feb 27, 2024
f02f11f
Merge remote-tracking branch 'upstream/develop' into ETHsignerContract
PhilippeR26 Feb 27, 2024
3599be0
test: move secp256k1Point tests in a dedicated test file
PhilippeR26 Mar 11, 2024
648e4d6
Merge branch 'develop' into ETHsignerContract
PhilippeR26 Mar 11, 2024
656ce42
Merge branch 'develop' into ETHsignerContract
PhilippeR26 Mar 18, 2024
57d395b
feat: helper for transaction receipt
PhilippeR26 Mar 18, 2024
495512a
simplify extends for account class
gregoryguillou Mar 18, 2024
9365063
feat: handling of cairo u512 type
PhilippeR26 Mar 19, 2024
b4ea22a
Merge pull request #1021 from 0xknwn/feature/simplify-default-account…
tabaktoni Mar 19, 2024
ce13ede
refactor: change name of variable : GetTxReceiptResponseWithoutHelper
PhilippeR26 Mar 19, 2024
9f13a49
Merge branch 'develop' into txResponse3
PhilippeR26 Mar 20, 2024
d122e34
Merge branch 'develop' into ETHsignerContract
PhilippeR26 Mar 20, 2024
c7ca4e2
Merge branch 'develop' into u512
PhilippeR26 Mar 20, 2024
f273e70
fix: double lines for same imports
PhilippeR26 Mar 20, 2024
c98fae4
Merge branch 'develop' into next-version
tabaktoni Mar 22, 2024
24f82d4
fix: solve an error in validate.ts initiated by pr 1007
PhilippeR26 Mar 22, 2024
46ca53b
fix: correction of a word in guide
PhilippeR26 Mar 22, 2024
2262b5e
docs: validateChecksumAddress
ivpavici Mar 22, 2024
218632b
fix: jsdoc correction
PhilippeR26 Mar 22, 2024
6623ea5
Merge pull request #1022 from PhilippeR26/u512
tabaktoni Mar 22, 2024
60674d8
Merge pull request #1032 from starknet-io/ivpavici_1031
tabaktoni Mar 22, 2024
aefe139
docs: add tsdoc in utils/address.ts
PhilippeR26 Mar 22, 2024
ad6c552
Merge pull request #1034 from PhilippeR26/addAddressPaddingDoc
tabaktoni Mar 25, 2024
9847c88
test: add extra fees
PhilippeR26 Mar 25, 2024
8c10c4a
fix: estimateFeeBulk include skipValidate in accountInvocationsFactory
tabaktoni Mar 25, 2024
9610f2a
Merge pull request #1037 from starknet-io/fix/estimateFeeBulk-skipVal…
tabaktoni Mar 25, 2024
878ba0d
Merge branch 'next-version' into ETHsignerContract
tabaktoni Mar 25, 2024
ab9fbb8
Merge pull request #985 from PhilippeR26/ETHsignerContract
tabaktoni Mar 25, 2024
b656443
Merge pull request #1020 from PhilippeR26/txResponse3
tabaktoni Mar 25, 2024
587d9f6
feat: add type guard to receipt response status methods
tabaktoni Mar 25, 2024
9341385
fix: repair i128 typed data encoding and add typed data range checks
penovicp Mar 15, 2024
87147d5
chore: update left over StarkNet casing
penovicp Mar 15, 2024
4b2398a
Merge pull request #1038 from starknet-io/fix-transaction-receipt-suc…
tabaktoni Mar 25, 2024
e5c2b45
Merge branch 'develop' into next-version
tabaktoni Mar 26, 2024
99921b1
feat: bundle resolution, module, type import for walletacc
tabaktoni Mar 26, 2024
5265995
fix: estimateMessageFee - eth address format (#1040)
PhilippeR26 Mar 27, 2024
f121a75
docs: small guides cleanup (#1048)
ivpavici Mar 28, 2024
73e9bbf
fix(RpcProvider): allow client to provide `specVersion` in 0.7 provider
avimak Apr 1, 2024
2cf8ca0
fix: remove abis parameter from signer and account execute
PhilippeR26 Apr 1, 2024
80d7fa8
feat: configure u512 and Secp256k1Point for abiwan
haroune-mohammedi Mar 27, 2024
4b83943
chore: bump dependencies
penovicp Apr 1, 2024
abf87d1
chore: expose data gas consumed and data gas price for 0.7 rpc
dhruvkelawala Apr 2, 2024
173d7e7
refactor: create wallet namespace
PhilippeR26 Apr 3, 2024
f5c2488
chore: dep starknet-types
tabaktoni Apr 3, 2024
3132e50
Merge branch 'develop' into next-version
tabaktoni Apr 3, 2024
1a826e3
fix(utils): fix block identifier
tabaktoni Apr 11, 2024
44fcd95
docs: class Block docs
tabaktoni Apr 11, 2024
5656bdd
Merge branch 'next-version' into fix/blockIdentifier
tabaktoni Apr 11, 2024
612994e
Merge branch 'next-version' into fix/blockIdentifier
tabaktoni Apr 11, 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
133 changes: 133 additions & 0 deletions __tests__/utils/block.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { Block } from '../../src/utils/provider';

describe('new Block()', () => {
test('decimal number 0 BlockIdentifier', () => {
const block = new Block(0);
expect(block.identifier).toMatchObject({ block_number: 0 });
expect(block.queryIdentifier).toBe('blockNumber=0');
expect(block.hash).toBe(null);
expect(block.number).toBe(0);
expect(block.tag).toBe(null);
});

test('decimal number 631581 BlockIdentifier', () => {
const block1 = new Block(631581);
expect(block1.identifier).toMatchObject({ block_number: 631581 });
expect(block1.queryIdentifier).toBe('blockNumber=631581');
expect(block1.hash).toBe(null);
expect(block1.number).toBe(631581);
expect(block1.tag).toBe(null);
});

test('non-decimal number -1 BlockIdentifier', () => {
expect(() => {
const block = new Block(-1);
return block;
}).toThrow(TypeError);
});

test('decimal string `0` BlockIdentifier', () => {
const block = new Block('0');
expect(block.identifier).toMatchObject({ block_number: 0 });
expect(block.queryIdentifier).toBe('blockNumber=0');
expect(block.hash).toBe(null);
expect(block.number).toBe(0);
expect(block.tag).toBe(null);
});

test('decimal string `631581` BlockIdentifier', () => {
const block1 = new Block('631581');
expect(block1.identifier).toMatchObject({ block_number: 631581 });
expect(block1.queryIdentifier).toBe('blockNumber=631581');
expect(block1.hash).toBe(null);
expect(block1.number).toBe(631581);
expect(block1.tag).toBe(null);
});

test('non-decimal string `-1` BlockIdentifier', () => {
expect(() => {
const block = new Block('-1');
return block;
}).toThrow(TypeError);
});

test('(Irregular support) hex string `0x0` BlockIdentifier.', () => {
const block = new Block('0x0');
expect(block.identifier).toMatchObject({ block_hash: '0x0' });
expect(block.queryIdentifier).toBe('blockHash=0x0');
expect(block.hash).toBe('0x0');
expect(block.number).toBe(null);
expect(block.tag).toBe(null);
});

test('hex string `0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb` BlockIdentifier', () => {
const block = new Block('0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb');
expect(block.identifier).toMatchObject({
block_hash: '0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb',
});
expect(block.queryIdentifier).toBe(
'blockHash=0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb'
);
expect(block.hash).toBe('0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb');
expect(block.number).toBe(null);
expect(block.tag).toBe(null);
});

test('BigInt 1100871802642964430494835386862140987097292376415056243504467124241116103854n BlockIdentifier', () => {
const block = new Block(
1100871802642964430494835386862140987097292376415056243504467124241116103854n
);
expect(block.identifier).toMatchObject({
block_hash: '0x26f12449d649a5339d4891b312a381f23ebc1106792d169b42e6646e87304ae',
});
expect(block.queryIdentifier).toBe(
'blockHash=0x26f12449d649a5339d4891b312a381f23ebc1106792d169b42e6646e87304ae'
);
expect(block.hash).toBe('0x26f12449d649a5339d4891b312a381f23ebc1106792d169b42e6646e87304ae');
expect(block.number).toBe(null);
expect(block.tag).toBe(null);
});

test('String BigInt `1100871802642964430494835386862140987097292376415056243504467124241116103854n` BlockIdentifier', () => {
expect(() => {
const block = new Block(
'1100871802642964430494835386862140987097292376415056243504467124241116103854n'
);
return block;
}).toThrow(TypeError);
});

test('string `pending` BlockIdentifier', () => {
const block1 = new Block('pending');
expect(block1.identifier).toBe('pending');
expect(block1.queryIdentifier).toBe('blockNumber=pending');
expect(block1.hash).toBe(null);
expect(block1.number).toBe(null);
expect(block1.tag).toBe('pending');
});

test('string `latest` BlockIdentifier', () => {
const block1 = new Block('latest');
expect(block1.identifier).toBe('latest');
expect(block1.queryIdentifier).toBe('blockNumber=latest');
expect(block1.hash).toBe(null);
expect(block1.number).toBe(null);
expect(block1.tag).toBe('latest');
});

test('False string `supernova` BlockIdentifier', () => {
expect(() => {
const block = new Block('supernova');
return block;
}).toThrow(TypeError);
});

test('null BlockIdentifier', () => {
const block1 = new Block(null);
expect(block1.identifier).toBe('pending');
expect(block1.queryIdentifier).toBe('blockNumber=pending');
expect(block1.hash).toBe(null);
expect(block1.number).toBe(null);
expect(block1.tag).toBe('pending');
});
});
17 changes: 0 additions & 17 deletions __tests__/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as starkCurve from '@scure/starknet';

import { constants, ec, hash, num, stark } from '../../src';
import { Block } from '../../src/utils/provider';

const { IS_BROWSER } = constants;

Expand Down Expand Up @@ -115,19 +114,3 @@ describe('calculateContractAddressFromHash()', () => {
starkCurveSpy.mockRestore();
});
});

describe('new Block()', () => {
test('Block identifier and queryIdentifier', () => {
const blockA = new Block(0);
expect(blockA.identifier).toMatchObject({ block_number: 0 });
expect(blockA.queryIdentifier).toBe('blockNumber=0');

const blockB = new Block('latest');
expect(blockB.identifier).toBe('latest');
expect(blockB.queryIdentifier).toBe('blockNumber=latest');

const blockC = new Block('0x01');
expect(blockC.identifier).toMatchObject({ block_hash: '0x01' });
expect(blockC.queryIdentifier).toBe('blockHash=0x01');
});
});
8 changes: 6 additions & 2 deletions src/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ export enum BlockTag {
export type BlockNumber = BlockTag | null | number;

/**
* hex string and BN are detected as block hashes
* hex string and BigInt are detected as block hashes
*
* decimal string and number are detected as block numbers
* null appends nothing to the request url
*
* text string are detected as block tag
*
* null return 'pending' block tag
*/
export type BlockIdentifier = BlockNumber | BigNumberish;

Expand Down
18 changes: 10 additions & 8 deletions src/utils/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { isSierra } from './contract';
import { formatSpaces } from './hash';
import { parse, stringify } from './json';
import { isBigInt, isHex, isNumber, toHex } from './num';
import { isDecimalString, isString } from './shortString';
import { compressProgram } from './stark';
import type { GetTransactionReceiptResponse } from './transactionReceipt';
import { isString } from './shortString';

/**
* Helper - Async Sleep for 'delay' time
Expand Down Expand Up @@ -111,10 +111,14 @@ export class Block {

private setIdentifier(__identifier: BlockIdentifier) {
if (isString(__identifier)) {
if (isHex(__identifier)) {
if (isDecimalString(__identifier)) {
this.number = parseInt(__identifier, 10);
} else if (isHex(__identifier)) {
this.hash = __identifier;
} else if (validBlockTags.includes(__identifier as BlockTag)) {
this.tag = __identifier;
} else {
throw TypeError(`Block identifier unmanaged: ${__identifier}`);
}
} else if (isBigInt(__identifier)) {
this.hash = toHex(__identifier);
Expand All @@ -123,6 +127,10 @@ export class Block {
} else {
this.tag = BlockTag.pending;
}

if (isNumber(this.number) && this.number < 0) {
throw TypeError(`Block number (${this.number}) can't be negative`);
}
}

constructor(_identifier: BlockIdentifier) {
Expand Down Expand Up @@ -162,12 +170,6 @@ export class Block {
valueOf = () => this.number;

toString = () => this.hash;

/* get sequencerIdentifier(): SequencerIdentifier {
return this.hash !== null
? { blockHash: this.hash as string }
: { blockNumber: (this.number ?? this.tag) as BlockNumber };
} */
}

export function isV3Tx(details: InvocationsDetailsWithNonce): details is V3TransactionDetails {
Expand Down
Loading