Skip to content

Commit 548cf6e

Browse files
committed
fix: getClassAt and Test Fixes
1 parent c622913 commit 548cf6e

File tree

5 files changed

+67
-57
lines changed

5 files changed

+67
-57
lines changed

__tests__/defaultProvider.test.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,8 @@ describe('defaultProvider', () => {
146146
expect(latestBlock).toHaveProperty('parent_hash');
147147
expect(latestBlock).toHaveProperty('block_number');
148148
expect(latestBlock).toHaveProperty('status');
149-
expect(latestBlock).toHaveProperty('sequencer');
150149
expect(latestBlock).toHaveProperty('new_root');
151-
expect(latestBlock).toHaveProperty('old_root');
152-
expect(latestBlock).toHaveProperty('accepted_time');
153-
expect(latestBlock).toHaveProperty('gas_price');
150+
expect(latestBlock).toHaveProperty('timestamp');
154151
expect(latestBlock).toHaveProperty('transactions');
155152
expect(Array.isArray(latestBlock.transactions)).toBe(true);
156153
});
@@ -164,11 +161,8 @@ describe('defaultProvider', () => {
164161
expect(block).toHaveProperty('parent_hash');
165162
expect(block).toHaveProperty('block_number');
166163
expect(block).toHaveProperty('status');
167-
expect(block).toHaveProperty('sequencer');
168164
expect(block).toHaveProperty('new_root');
169-
expect(block).toHaveProperty('old_root');
170-
expect(block).toHaveProperty('accepted_time');
171-
expect(block).toHaveProperty('gas_price');
165+
expect(block).toHaveProperty('timestamp');
172166
expect(block).toHaveProperty('transactions');
173167
expect(Array.isArray(block.transactions)).toBe(true);
174168
});
@@ -179,11 +173,8 @@ describe('defaultProvider', () => {
179173
expect(block).toHaveProperty('parent_hash');
180174
expect(block).toHaveProperty('block_number');
181175
expect(block).toHaveProperty('status');
182-
expect(block).toHaveProperty('sequencer');
183176
expect(block).toHaveProperty('new_root');
184-
expect(block).toHaveProperty('old_root');
185-
expect(block).toHaveProperty('accepted_time');
186-
expect(block).toHaveProperty('gas_price');
177+
expect(block).toHaveProperty('timestamp');
187178
expect(block).toHaveProperty('transactions');
188179
expect(Array.isArray(block.transactions)).toBe(true);
189180
});
@@ -193,15 +184,15 @@ describe('defaultProvider', () => {
193184
test('pending', async () => {
194185
const storage = await provider.getStorageAt(
195186
'0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
196-
0
187+
'0'
197188
);
198189
expect(typeof storage).toBe('string');
199190
});
200191

201192
test('Block Hash 0x7104702055c2a5773a870ceada9552ec659d69c18053b14078983f07527dea8', async () => {
202193
const storage = await provider.getStorageAt(
203194
'0x01d1f307c073bb786a66e6e042ec2a9bdc385a3373bb3738d95b966d5ce56166',
204-
0,
195+
'0',
205196
'0x7225762c7ff5e7e5f0867f0a8e73594df4f44f05a65375339a76398e8ae3e64'
206197
);
207198
expect(typeof storage).toBe('string');
@@ -239,7 +230,6 @@ describe('defaultProvider', () => {
239230
expect(transaction.max_fee).toBeTruthy();
240231
expect(transaction.transaction_hash).toBeTruthy();
241232
expect(transaction).toHaveProperty('nonce');
242-
expect(transaction).toHaveProperty('sender_address');
243233
expect(transaction).toHaveProperty('version');
244234
});
245235
});
@@ -260,13 +250,13 @@ describe('defaultProvider', () => {
260250
});
261251

262252
describe('Contract methods', () => {
263-
let contractAddress: string;
253+
// let contractAddress: string;
264254
let deployResponse: DeployContractResponse;
265255
let declareResponse: DeclareContractResponse;
266256

267257
beforeAll(async () => {
268258
deployResponse = await provider.deployContract({ contract: compiledErc20 });
269-
contractAddress = deployResponse.contract_address;
259+
// contractAddress = deployResponse.contract_address;
270260
declareResponse = await provider.declareContract({ contract: compiledErc20 });
271261
await Promise.all([
272262
provider.waitForTransaction(deployResponse.transaction_hash),
@@ -290,7 +280,10 @@ describe('defaultProvider', () => {
290280

291281
describe('getClassAt', () => {
292282
test('response', async () => {
293-
const classResponse = await provider.getClassAt(contractAddress);
283+
const classResponse = await provider.getClassAt(
284+
'0x075c4CEe7e88010008c1aA8777D798073D5Db63B685A033140cE5AF144EA0283',
285+
'0x673c337dd17b50628e7b2a070e2c599a4fed54a2e7c1ff10e84115325c5b37e'
286+
);
294287

295288
expect(classResponse).toHaveProperty('program');
296289
expect(classResponse).toHaveProperty('entry_points_by_type');

src/provider/rpc.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,22 @@ export class RpcProvider implements ProviderInterface {
123123
]);
124124
}
125125

126+
// common interface
126127
public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
127-
return this.fetchEndpoint('starknet_getTransactionByHash', [txHash]).then(
128-
this.responseParser.parseGetTransactionResponse
129-
);
128+
return this.getTransactionByHash(txHash).then(this.responseParser.parseGetTransactionResponse);
129+
}
130+
131+
public async getTransactionByHash(
132+
txHash: BigNumberish
133+
): Promise<RPC.GetTransactionByHashResponse> {
134+
return this.fetchEndpoint('starknet_getTransactionByHash', [txHash]);
135+
}
136+
137+
public async getTransactionByBlockIdAndIndex(
138+
blockIdentifier: BlockIdentifier,
139+
index: number
140+
): Promise<RPC.GetTransactionByBlockIdAndIndex> {
141+
return this.fetchEndpoint('starknet_getTransactionByHash', [blockIdentifier, index]);
130142
}
131143

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

138-
public async getClassAt(
139-
contractAddress: string,
140-
_blockIdentifier: BlockIdentifier = 'pending'
141-
): Promise<any> {
142-
return this.fetchEndpoint('starknet_getClassAt', [contractAddress]);
150+
public async getClassAt(contractAddress: string, blockIdentifier: BlockIdentifier): Promise<any> {
151+
const blockIdentifierGetter = new BlockIdentifierClass(blockIdentifier);
152+
return this.fetchEndpoint('starknet_getClassAt', [
153+
blockIdentifierGetter.getIdentifier(),
154+
contractAddress,
155+
]);
143156
}
144157

145158
public async getEstimateFee(

src/types/api/openrpc.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* Starknet RPC version 0.1.0
33
* starknet_api_openrpc version 0.31.0
44
*
5-
* TypeScript Representation of OpenRpc protocol types | responses
5+
* TypeScript Representation of OpenRpc protocol types | results
6+
* errors are not implemented here only results
67
*/
78

89
/**
@@ -142,8 +143,26 @@ type PENDING_BLOCK_WITH_TXS = BLOCK_BODY_WITH_TXS & {
142143
parent_hash: BLOCK_HASH;
143144
};
144145

146+
type CONTRACT_CLASS = {
147+
program: string; // A base64 representation of the compressed program code
148+
entry_points_by_type: {
149+
CONSTRUCTOR: CONTRACT_ENTRY_POINT_LIST;
150+
EXTERNAL: CONTRACT_ENTRY_POINT_LIST;
151+
L1_HANDLER: CONTRACT_ENTRY_POINT_LIST;
152+
};
153+
};
154+
155+
type CONTRACT_ENTRY_POINT_LIST = Array<CONTRACT_ENTRY_POINT>;
156+
type CONTRACT_ENTRY_POINT = {
157+
offset: NUM_AS_HEX;
158+
selector: FELT;
159+
};
160+
145161
export namespace OPENRPC {
146-
export type getBlockWithTxHashesResponse = BLOCK_WITH_TX_HASHES | PENDING_BLOCK_WITH_TX_HASHES;
147-
export type getBlockWithTxs = BLOCK_WITH_TXS | PENDING_BLOCK_WITH_TXS;
162+
export type GetBlockWithTxHashesResponse = BLOCK_WITH_TX_HASHES | PENDING_BLOCK_WITH_TX_HASHES;
163+
export type GetBlockWithTxs = BLOCK_WITH_TXS | PENDING_BLOCK_WITH_TXS;
148164
export type GetStorageAtResponse = FELT;
165+
export type GetTransactionByHashResponse = TXN;
166+
export type GetTransactionByBlockIdAndIndex = TXN;
167+
export type GetClassResponse = CONTRACT_CLASS;
149168
}

src/types/api/rpc.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,8 @@ export namespace RPC {
3434
gas_price: number;
3535
};
3636

37-
export type getBlockWithTxHashesResponse = OPENRPC.getBlockWithTxHashesResponse;
38-
export type getBlockWithTxs = OPENRPC.getBlockWithTxs;
39-
/* {
40-
block_hash: string;
41-
parent_hash: string;
42-
block_number: number;
43-
status: Status;
44-
sequencer: string;
45-
new_root: string;
46-
old_root: string;
47-
timestamp: number;
48-
gas_price: string;
49-
transactions: string[];
50-
}; */
51-
37+
export type getBlockWithTxHashesResponse = OPENRPC.GetBlockWithTxHashesResponse;
38+
export type getBlockWithTxs = OPENRPC.GetBlockWithTxs;
5239
export type GetStorageAtResponse = OPENRPC.GetStorageAtResponse;
5340

5441
export type GetTransactionReceiptResponse = {
@@ -80,7 +67,8 @@ export namespace RPC {
8067
sender_address?: string;
8168
}
8269

83-
export type GetTransactionResponse = InvokeTransactionResponse & DeclareTransactionResponse;
70+
export type GetTransactionByHashResponse = OPENRPC.GetTransactionByHashResponse;
71+
export type GetTransactionByBlockIdAndIndex = OPENRPC.GetTransactionByBlockIdAndIndex;
8472

8573
export type GetTransactionCountResponse = number;
8674

@@ -165,17 +153,12 @@ export namespace RPC {
165153
starknet_getTransactionByHash: {
166154
QUERY: never;
167155
REQUEST: any[];
168-
RESPONSE: GetTransactionResponse;
169-
};
170-
starknet_getTransactionByBlockHashAndIndex: {
171-
QUERY: never;
172-
REQUEST: any[];
173-
RESPONSE: GetTransactionResponse;
156+
RESPONSE: GetTransactionByHashResponse;
174157
};
175-
starknet_getTransactionByBlockNumberAndIndex: {
158+
starknet_getTransactionByBlockIdAndIndex: {
176159
QUERY: never;
177160
REQUEST: any[];
178-
RESPONSE: GetTransactionResponse;
161+
RESPONSE: GetTransactionByBlockIdAndIndex;
179162
};
180163
starknet_getTransactionReceipt: {
181164
QUERY: never;

src/utils/responseParser/rpc.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ type RpcGetBlockResponse = RPC.getBlockWithTxHashesResponse & {
2020
[key: string]: any;
2121
};
2222

23+
type GetTransactionByHashResponse = RPC.GetTransactionByHashResponse & {
24+
[key: string]: any;
25+
};
26+
2327
export class RPCResponseParser extends ResponseParser {
2428
public parseGetBlockResponse(res: RpcGetBlockResponse): GetBlockResponse {
2529
return {
@@ -33,17 +37,15 @@ export class RPCResponseParser extends ResponseParser {
3337
};
3438
}
3539

36-
public parseGetTransactionResponse(res: RPC.GetTransactionResponse): GetTransactionResponse {
40+
public parseGetTransactionResponse(res: GetTransactionByHashResponse): GetTransactionResponse {
3741
return {
3842
calldata: res.calldata || [],
3943
contract_address: res.contract_address,
40-
contract_class: res.contract_class,
4144
entry_point_selector: res.entry_point_selector,
4245
max_fee: res.max_fee,
4346
nonce: res.nonce,
44-
sender_address: res.sender_address,
4547
signature: res.signature || [],
46-
transaction_hash: res.txn_hash,
48+
transaction_hash: res.transaction_hash,
4749
version: res.version,
4850
};
4951
}

0 commit comments

Comments
 (0)