Skip to content
Merged
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
5 changes: 5 additions & 0 deletions __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ describeIfRpc('RPCProvider', () => {
expect(typeof count).toBe('number');
});

test('getStarknetVersion', async () => {
const version = await rpcProvider.getStarknetVersion('latest');
expect(typeof version).toBe('string');
});

test('getBlockHashAndNumber', async () => {
const blockHashAndNumber = await rpcProvider.getBlockLatestAccepted();
expect(blockHashAndNumber).toHaveProperty('block_hash');
Expand Down
8 changes: 4 additions & 4 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export class Account extends Provider implements AccountInterface {
const invocations = [
{
type: ETransactionType.DECLARE,
payload: extractContractHashes(payload, await this.channel.setUpSpecVersion()),
payload: extractContractHashes(payload, await this.channel.getStarknetVersion()),
},
];
const estimateBulk = await this.estimateFeeBulk(invocations, details);
Expand Down Expand Up @@ -400,7 +400,7 @@ export class Account extends Provider implements AccountInterface {
): Promise<DeclareContractResponse> {
const declareContractPayload = extractContractHashes(
payload,
await this.channel.setUpSpecVersion()
await this.channel.getStarknetVersion()
);
try {
await this.getClassByHash(declareContractPayload.classHash);
Expand All @@ -421,7 +421,7 @@ export class Account extends Provider implements AccountInterface {

const declareContractPayload = extractContractHashes(
payload,
await this.channel.setUpSpecVersion()
await this.channel.getStarknetVersion()
);
const detailsWithTip = await this.resolveDetailsWithTip(details);

Expand Down Expand Up @@ -791,7 +791,7 @@ export class Account extends Provider implements AccountInterface {
): Promise<DeclareContractTransaction> {
const { classHash, contract, compiledClassHash } = extractContractHashes(
payload,
await this.channel.setUpSpecVersion()
await this.channel.getStarknetVersion()
);
const compressedCompiledContract = parseContract(contract);

Expand Down
9 changes: 9 additions & 0 deletions src/channel/rpc_0_8_1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@ export class RpcChannel {
});
}

/**
* Helper method to get the starknet version from the block, default latest block
* @returns Starknet version
*/
public async getStarknetVersion(blockIdentifier: BlockIdentifier = this.blockIdentifier) {
const block = await this.getBlockWithTxHashes(blockIdentifier);
return block.starknet_version;
}

/**
* Get the most recent accepted block hash and number
*/
Expand Down
9 changes: 9 additions & 0 deletions src/channel/rpc_0_9_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,15 @@ export class RpcChannel {
});
}

/**
* Helper method to get the starknet version from the block, default latest block
* @returns Starknet version
*/
public async getStarknetVersion(blockIdentifier: BlockIdentifier = this.blockIdentifier) {
const block = await this.getBlockWithTxHashes(blockIdentifier);
return block.starknet_version;
}

/**
* Get the most recent accepted block hash and number
*/
Expand Down
6 changes: 5 additions & 1 deletion src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ export class RpcProvider implements ProviderInterface {
return this.channel.setUpSpecVersion();
}

public async getStarknetVersion(blockIdentifier?: BlockIdentifier) {
return this.channel.getStarknetVersion(blockIdentifier);
}

public async getNonceForAddress(
contractAddress: BigNumberish,
blockIdentifier?: BlockIdentifier
Expand Down Expand Up @@ -545,7 +549,7 @@ export class RpcProvider implements ProviderInterface {
if (!contractClassIdentifier.classHash && 'contract' in contractClassIdentifier) {
const hashes = extractContractHashes(
contractClassIdentifier,
await this.channel.setUpSpecVersion()
await this.channel.getStarknetVersion()
);
classHash = hashes.classHash;
} else if (contractClassIdentifier.classHash) {
Expand Down
5 changes: 2 additions & 3 deletions src/utils/contract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SupportedRpcVersion } from '../global/constants';
import { ContractClassResponse } from '../types';
import {
CairoContract,
Expand Down Expand Up @@ -50,13 +49,13 @@ export function isSierra(
*/
export function extractContractHashes(
payload: DeclareContractPayload,
specVersion?: SupportedRpcVersion
starknetVersion?: string
): CompleteDeclareContractPayload {
const response = { ...payload } as CompleteDeclareContractPayload;

if (isSierra(payload.contract)) {
if (!payload.compiledClassHash && payload.casm) {
response.compiledClassHash = computeCompiledClassHash(payload.casm, specVersion);
response.compiledClassHash = computeCompiledClassHash(payload.casm, starknetVersion);
}
if (!response.compiledClassHash)
throw new Error(
Expand Down
5 changes: 2 additions & 3 deletions src/utils/hash/classHash/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { isString } from '../../typed';
import { computeLegacyContractClassHash } from './pedersen';
import { computeCompiledClassHashPoseidon, computeSierraContractClassHash } from './poseidon';
import { computeCompiledClassHashBlake } from './blake';
import { SupportedRpcVersion } from '../../../global/constants';
import { compareVersions } from '../../resolve';

export * from './pedersen';
Expand Down Expand Up @@ -45,9 +44,9 @@ export function computeCompiledClassHash(
/**
* Used to determine which hashing algorithm to use
*/
specVersion?: SupportedRpcVersion
starknetVersion?: string
): string {
if (specVersion && compareVersions(specVersion, '0.10.0') >= 0) {
if (starknetVersion && compareVersions(starknetVersion, '0.14.1') >= 0) {
return computeCompiledClassHashBlake(casm);
}
return computeCompiledClassHashPoseidon(casm);
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class WalletAccount extends Account implements AccountInterface {
override async declare(payload: DeclareContractPayload) {
const declareContractPayload = extractContractHashes(
payload,
await this.channel.setUpSpecVersion()
await this.channel.getStarknetVersion()
);

// DISCUSS: HOTFIX: Adapt Abi format
Expand Down
Loading