Skip to content

Commit

Permalink
fix: getClassAt and Test Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Aug 29, 2022
1 parent c622913 commit 548cf6e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 57 deletions.
29 changes: 11 additions & 18 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,8 @@ describe('defaultProvider', () => {
expect(latestBlock).toHaveProperty('parent_hash');
expect(latestBlock).toHaveProperty('block_number');
expect(latestBlock).toHaveProperty('status');
expect(latestBlock).toHaveProperty('sequencer');
expect(latestBlock).toHaveProperty('new_root');
expect(latestBlock).toHaveProperty('old_root');
expect(latestBlock).toHaveProperty('accepted_time');
expect(latestBlock).toHaveProperty('gas_price');
expect(latestBlock).toHaveProperty('timestamp');
expect(latestBlock).toHaveProperty('transactions');
expect(Array.isArray(latestBlock.transactions)).toBe(true);
});
Expand All @@ -164,11 +161,8 @@ describe('defaultProvider', () => {
expect(block).toHaveProperty('parent_hash');
expect(block).toHaveProperty('block_number');
expect(block).toHaveProperty('status');
expect(block).toHaveProperty('sequencer');
expect(block).toHaveProperty('new_root');
expect(block).toHaveProperty('old_root');
expect(block).toHaveProperty('accepted_time');
expect(block).toHaveProperty('gas_price');
expect(block).toHaveProperty('timestamp');
expect(block).toHaveProperty('transactions');
expect(Array.isArray(block.transactions)).toBe(true);
});
Expand All @@ -179,11 +173,8 @@ describe('defaultProvider', () => {
expect(block).toHaveProperty('parent_hash');
expect(block).toHaveProperty('block_number');
expect(block).toHaveProperty('status');
expect(block).toHaveProperty('sequencer');
expect(block).toHaveProperty('new_root');
expect(block).toHaveProperty('old_root');
expect(block).toHaveProperty('accepted_time');
expect(block).toHaveProperty('gas_price');
expect(block).toHaveProperty('timestamp');
expect(block).toHaveProperty('transactions');
expect(Array.isArray(block.transactions)).toBe(true);
});
Expand All @@ -193,15 +184,15 @@ describe('defaultProvider', () => {
test('pending', async () => {
const storage = await provider.getStorageAt(
'0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
0
'0'
);
expect(typeof storage).toBe('string');
});

test('Block Hash 0x7104702055c2a5773a870ceada9552ec659d69c18053b14078983f07527dea8', async () => {
const storage = await provider.getStorageAt(
'0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
0,
'0',
'0x7225762c7ff5e7e5f0867f0a8e73594df4f44f05a65375339a76398e8ae3e64'
);
expect(typeof storage).toBe('string');
Expand Down Expand Up @@ -239,7 +230,6 @@ describe('defaultProvider', () => {
expect(transaction.max_fee).toBeTruthy();
expect(transaction.transaction_hash).toBeTruthy();
expect(transaction).toHaveProperty('nonce');
expect(transaction).toHaveProperty('sender_address');
expect(transaction).toHaveProperty('version');
});
});
Expand All @@ -260,13 +250,13 @@ describe('defaultProvider', () => {
});

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

beforeAll(async () => {
deployResponse = await provider.deployContract({ contract: compiledErc20 });
contractAddress = deployResponse.contract_address;
// contractAddress = deployResponse.contract_address;
declareResponse = await provider.declareContract({ contract: compiledErc20 });
await Promise.all([
provider.waitForTransaction(deployResponse.transaction_hash),
Expand All @@ -290,7 +280,10 @@ describe('defaultProvider', () => {

describe('getClassAt', () => {
test('response', async () => {
const classResponse = await provider.getClassAt(contractAddress);
const classResponse = await provider.getClassAt(
'0x075c4CEe7e88010008c1aA8777D798073D5Db63B685A033140cE5AF144EA0283',
'0x673c337dd17b50628e7b2a070e2c599a4fed54a2e7c1ff10e84115325c5b37e'
);

expect(classResponse).toHaveProperty('program');
expect(classResponse).toHaveProperty('entry_points_by_type');
Expand Down
29 changes: 21 additions & 8 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,22 @@ export class RpcProvider implements ProviderInterface {
]);
}

// common interface
public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
return this.fetchEndpoint('starknet_getTransactionByHash', [txHash]).then(
this.responseParser.parseGetTransactionResponse
);
return this.getTransactionByHash(txHash).then(this.responseParser.parseGetTransactionResponse);
}

public async getTransactionByHash(
txHash: BigNumberish
): Promise<RPC.GetTransactionByHashResponse> {
return this.fetchEndpoint('starknet_getTransactionByHash', [txHash]);
}

public async getTransactionByBlockIdAndIndex(
blockIdentifier: BlockIdentifier,
index: number
): Promise<RPC.GetTransactionByBlockIdAndIndex> {
return this.fetchEndpoint('starknet_getTransactionByHash', [blockIdentifier, index]);
}

public async getTransactionReceipt(txHash: BigNumberish): Promise<GetTransactionReceiptResponse> {
Expand All @@ -135,11 +147,12 @@ export class RpcProvider implements ProviderInterface {
);
}

public async getClassAt(
contractAddress: string,
_blockIdentifier: BlockIdentifier = 'pending'
): Promise<any> {
return this.fetchEndpoint('starknet_getClassAt', [contractAddress]);
public async getClassAt(contractAddress: string, blockIdentifier: BlockIdentifier): Promise<any> {
const blockIdentifierGetter = new BlockIdentifierClass(blockIdentifier);
return this.fetchEndpoint('starknet_getClassAt', [
blockIdentifierGetter.getIdentifier(),
contractAddress,
]);
}

public async getEstimateFee(
Expand Down
25 changes: 22 additions & 3 deletions src/types/api/openrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* Starknet RPC version 0.1.0
* starknet_api_openrpc version 0.31.0
*
* TypeScript Representation of OpenRpc protocol types | responses
* TypeScript Representation of OpenRpc protocol types | results
* errors are not implemented here only results
*/

/**
Expand Down Expand Up @@ -142,8 +143,26 @@ type PENDING_BLOCK_WITH_TXS = BLOCK_BODY_WITH_TXS & {
parent_hash: BLOCK_HASH;
};

type CONTRACT_CLASS = {
program: string; // A base64 representation of the compressed program code
entry_points_by_type: {
CONSTRUCTOR: CONTRACT_ENTRY_POINT_LIST;
EXTERNAL: CONTRACT_ENTRY_POINT_LIST;
L1_HANDLER: CONTRACT_ENTRY_POINT_LIST;
};
};

type CONTRACT_ENTRY_POINT_LIST = Array<CONTRACT_ENTRY_POINT>;
type CONTRACT_ENTRY_POINT = {
offset: NUM_AS_HEX;
selector: FELT;
};

export namespace OPENRPC {
export type getBlockWithTxHashesResponse = BLOCK_WITH_TX_HASHES | PENDING_BLOCK_WITH_TX_HASHES;
export type getBlockWithTxs = BLOCK_WITH_TXS | PENDING_BLOCK_WITH_TXS;
export type GetBlockWithTxHashesResponse = BLOCK_WITH_TX_HASHES | PENDING_BLOCK_WITH_TX_HASHES;
export type GetBlockWithTxs = BLOCK_WITH_TXS | PENDING_BLOCK_WITH_TXS;
export type GetStorageAtResponse = FELT;
export type GetTransactionByHashResponse = TXN;
export type GetTransactionByBlockIdAndIndex = TXN;
export type GetClassResponse = CONTRACT_CLASS;
}
31 changes: 7 additions & 24 deletions src/types/api/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,8 @@ export namespace RPC {
gas_price: number;
};

export type getBlockWithTxHashesResponse = OPENRPC.getBlockWithTxHashesResponse;
export type getBlockWithTxs = OPENRPC.getBlockWithTxs;
/* {
block_hash: string;
parent_hash: string;
block_number: number;
status: Status;
sequencer: string;
new_root: string;
old_root: string;
timestamp: number;
gas_price: string;
transactions: string[];
}; */

export type getBlockWithTxHashesResponse = OPENRPC.GetBlockWithTxHashesResponse;
export type getBlockWithTxs = OPENRPC.GetBlockWithTxs;
export type GetStorageAtResponse = OPENRPC.GetStorageAtResponse;

export type GetTransactionReceiptResponse = {
Expand Down Expand Up @@ -80,7 +67,8 @@ export namespace RPC {
sender_address?: string;
}

export type GetTransactionResponse = InvokeTransactionResponse & DeclareTransactionResponse;
export type GetTransactionByHashResponse = OPENRPC.GetTransactionByHashResponse;
export type GetTransactionByBlockIdAndIndex = OPENRPC.GetTransactionByBlockIdAndIndex;

export type GetTransactionCountResponse = number;

Expand Down Expand Up @@ -165,17 +153,12 @@ export namespace RPC {
starknet_getTransactionByHash: {
QUERY: never;
REQUEST: any[];
RESPONSE: GetTransactionResponse;
};
starknet_getTransactionByBlockHashAndIndex: {
QUERY: never;
REQUEST: any[];
RESPONSE: GetTransactionResponse;
RESPONSE: GetTransactionByHashResponse;
};
starknet_getTransactionByBlockNumberAndIndex: {
starknet_getTransactionByBlockIdAndIndex: {
QUERY: never;
REQUEST: any[];
RESPONSE: GetTransactionResponse;
RESPONSE: GetTransactionByBlockIdAndIndex;
};
starknet_getTransactionReceipt: {
QUERY: never;
Expand Down
10 changes: 6 additions & 4 deletions src/utils/responseParser/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type RpcGetBlockResponse = RPC.getBlockWithTxHashesResponse & {
[key: string]: any;
};

type GetTransactionByHashResponse = RPC.GetTransactionByHashResponse & {
[key: string]: any;
};

export class RPCResponseParser extends ResponseParser {
public parseGetBlockResponse(res: RpcGetBlockResponse): GetBlockResponse {
return {
Expand All @@ -33,17 +37,15 @@ export class RPCResponseParser extends ResponseParser {
};
}

public parseGetTransactionResponse(res: RPC.GetTransactionResponse): GetTransactionResponse {
public parseGetTransactionResponse(res: GetTransactionByHashResponse): GetTransactionResponse {
return {
calldata: res.calldata || [],
contract_address: res.contract_address,
contract_class: res.contract_class,
entry_point_selector: res.entry_point_selector,
max_fee: res.max_fee,
nonce: res.nonce,
sender_address: res.sender_address,
signature: res.signature || [],
transaction_hash: res.txn_hash,
transaction_hash: res.transaction_hash,
version: res.version,
};
}
Expand Down

0 comments on commit 548cf6e

Please sign in to comment.