Skip to content

Commit

Permalink
fix: restore GetCode to common interface for backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Aug 30, 2022
1 parent 67d87fc commit 52f8e61
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
3 changes: 1 addition & 2 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('defaultProvider', () => {
test(`getBlock(blockHash=${exampleBlockHash}, blockNumber=undefined)`, () => {
return expect(testProvider.getBlock(exampleBlockHash)).resolves.not.toThrow();
});

test('getBlock(blockIdentifier=latest)', async () => {
expect(exampleBlock).not.toBeNull();

Expand Down Expand Up @@ -250,13 +251,11 @@ describe('defaultProvider', () => {
});

describe('Contract methods', () => {
// let contractAddress: string;
let deployResponse: DeployContractResponse;
let declareResponse: DeclareContractResponse;

beforeAll(async () => {
deployResponse = await provider.deployContract({ contract: compiledErc20 });
// contractAddress = deployResponse.contract_address;
declareResponse = await provider.declareContract({ contract: compiledErc20 });
await Promise.all([
provider.waitForTransaction(deployResponse.transaction_hash),
Expand Down
2 changes: 1 addition & 1 deletion __tests__/sequencerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getTestProvider,
} from './fixtures';

// This run only if Devnet Sequencer ?
// Run only if Devnet Sequencer
describeIfSequencer('SequencerProvider', () => {
let provider: SequencerProvider;
let customSequencerProvider: Provider;
Expand Down
8 changes: 8 additions & 0 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
DeployContractResponse,
EstimateFeeResponse,
GetBlockResponse,
GetCodeResponse,
GetTransactionReceiptResponse,
GetTransactionResponse,
Invocation,
Expand Down Expand Up @@ -102,6 +103,13 @@ export class Provider implements ProviderInterface {
return this.provider.declareContract(payload);
}

public async getCode(
contractAddress: string,
blockIdentifier?: BlockIdentifier
): Promise<GetCodeResponse> {
return this.provider.getCode(contractAddress, blockIdentifier);
}

public async waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void> {
return this.provider.waitForTransaction(txHash, retryInterval);
}
Expand Down
9 changes: 9 additions & 0 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
DeployContractResponse,
EstimateFeeResponse,
GetBlockResponse,
GetCodeResponse,
GetTransactionReceiptResponse,
GetTransactionResponse,
Invocation,
Expand Down Expand Up @@ -41,6 +42,14 @@ export abstract class ProviderInterface {
*/
public abstract getBlock(blockIdentifier: BlockIdentifier): Promise<GetBlockResponse>;

/**
* @deprecated The method should not be used
*/
public abstract getCode(
contractAddress: string,
blockIdentifier?: BlockIdentifier
): Promise<GetCodeResponse>;

/**
* Gets the contract class of the deployed contract.
*
Expand Down
12 changes: 10 additions & 2 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DeployContractResponse,
EstimateFeeResponse,
GetBlockResponse,
GetCodeResponse,
GetTransactionReceiptResponse,
GetTransactionResponse,
Invocation,
Expand Down Expand Up @@ -91,7 +92,7 @@ export class RpcProvider implements ProviderInterface {

public async getBlockWithTxHashes(
blockIdentifier: BlockIdentifier = 'pending'
): Promise<RPC.getBlockWithTxHashesResponse> {
): Promise<RPC.GetBlockWithTxHashesResponse> {
const blockIdentifierGetter = new BlockIdentifierClass(blockIdentifier);
return this.fetchEndpoint('starknet_getBlockWithTxHashes', [
blockIdentifierGetter.getIdentifier(),
Expand All @@ -100,7 +101,7 @@ export class RpcProvider implements ProviderInterface {

public async getBlockWithTxs(
blockIdentifier: BlockIdentifier = 'pending'
): Promise<RPC.getBlockWithTxs> {
): Promise<RPC.GetBlockWithTxs> {
const blockIdentifierGetter = new BlockIdentifierClass(blockIdentifier);
return this.fetchEndpoint('starknet_getBlockWithTxs', [blockIdentifierGetter.getIdentifier()]);
}
Expand Down Expand Up @@ -236,6 +237,13 @@ export class RpcProvider implements ProviderInterface {
return this.responseParser.parseCallContractResponse(result);
}

public async getCode(
_contractAddress: string,
_blockIdentifier?: BlockIdentifier
): Promise<GetCodeResponse> {
throw new Error('RPC 0.1.0 does not implement getCode function');
}

public async waitForTransaction(txHash: BigNumberish, retryInterval: number = 8000) {
let onchain = false;
let retries = 100;
Expand Down
5 changes: 5 additions & 0 deletions src/types/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export interface GetBlockResponse {
transactions: Array<string>;
}

export interface GetCodeResponse {
bytecode: string[];
// abi: string; // is not consistent between rpc and sequencer (is it?), therefore not included in the provider interface
}

export type GetTransactionResponse = InvokeTransactionResponse & DeclareTransactionResponse;

export interface CommonTransactionResponse {
Expand Down

0 comments on commit 52f8e61

Please sign in to comment.