Skip to content

Commit 3760687

Browse files
committed
feat: start adding types
1 parent cf36305 commit 3760687

File tree

2 files changed

+99
-16
lines changed

2 files changed

+99
-16
lines changed

src/index.d.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
export interface GetContractAddressesResponse {
2+
Starknet: string;
3+
GpsStatementVerifier: string;
4+
}
5+
6+
export type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'REJECTED' | 'ACCEPTED_ONCHAIN';
7+
export type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
8+
export type EntryPointType = 'EXTERNAL';
9+
10+
export interface Transaction {
11+
type: Type;
12+
contract_address: string;
13+
entry_point_type?: EntryPointType;
14+
entry_point_selector?: string;
15+
calldata?: string[];
16+
}
17+
18+
export interface GetBlockResponse {
19+
sequence_number: number;
20+
state_root: string;
21+
block_id: number;
22+
transactions: {
23+
[txid: string]: Transaction;
24+
};
25+
timestamp: number;
26+
transaction_receipts: {
27+
[txid: string]: {
28+
block_id: number;
29+
transaction_id: number;
30+
l2_to_l1_messages: {
31+
to_address: string;
32+
payload: string[];
33+
from_address: string;
34+
}[];
35+
block_number: number;
36+
status: Status;
37+
transaction_index: number;
38+
};
39+
};
40+
previous_block_id: number;
41+
status: Status;
42+
}
43+
44+
export interface Abi {
45+
inputs: { name: string; type: string }[];
46+
name: string;
47+
outputs: { name: string; type: string }[];
48+
type: string;
49+
}
50+
51+
export interface GetCode {
52+
bytecode: string[];
53+
abi: Abi[];
54+
}
55+
56+
export interface GetTransactionStatusResponse {
57+
tx_status: Status;
58+
block_id: number;
59+
}
60+
61+
export interface GetTransactionResponse {
62+
transaction_index: number;
63+
transaction: Transaction;
64+
block_id: number;
65+
block_number: number;
66+
status: Status;
67+
transaction_id: number;
68+
}

src/index.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import axios from 'axios';
22

3+
import type {
4+
GetBlockResponse,
5+
GetCode,
6+
GetContractAddressesResponse,
7+
GetTransactionResponse,
8+
GetTransactionStatusResponse,
9+
} from './index.d';
10+
311
const API_URL: string = 'https://alpha2.starknet.io/';
412
const FEEDER_GATEWAY_URL: string = `${API_URL}/feeder_gateway`;
513
const GATEWAY_URL: string = `${API_URL}/gateway`;
@@ -8,19 +16,20 @@ const GATEWAY_URL: string = `${API_URL}/gateway`;
816
* Gets the smart contract address on the goerli testnet.
917
*
1018
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
11-
* @returns starknet smart contract address
19+
* @returns starknet smart contract addresses
1220
*/
13-
export function getContractAddresses(): Promise<object> {
21+
export function getContractAddresses(): Promise<GetContractAddressesResponse> {
1422
return new Promise((resolve, reject) => {
1523
axios
16-
.get(`${FEEDER_GATEWAY_URL}/get_contract_addresses`)
17-
.then((resp: any) => {
24+
.get<GetContractAddressesResponse>(`${FEEDER_GATEWAY_URL}/get_contract_addresses`)
25+
.then((resp) => {
1826
resolve(resp.data);
1927
})
2028
.catch(reject);
2129
});
2230
}
2331

32+
// TODO: add proper type
2433
/**
2534
* Calls a function on the StarkNet contract.
2635
*
@@ -49,10 +58,10 @@ export function callContract(invokeTx: object, blockId: number): Promise<object>
4958
* @param blockId
5059
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
5160
*/
52-
export function getBlock(blockId: number): Promise<object> {
61+
export function getBlock(blockId: number): Promise<GetBlockResponse> {
5362
return new Promise((resolve, reject) => {
5463
axios
55-
.get(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId}`)
64+
.get<GetBlockResponse>(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId}`)
5665
.then((resp: any) => {
5766
resolve(resp.data);
5867
})
@@ -67,19 +76,22 @@ export function getBlock(blockId: number): Promise<object> {
6776
*
6877
* @param contractAddress
6978
* @param blockId
70-
* @returns ABI of compiled contract in JSON
79+
* @returns Bytecode and ABI of compiled contract
7180
*/
72-
export function getCode(contractAddress: string, blockId: number): Promise<object> {
81+
export function getCode(contractAddress: string, blockId: number): Promise<GetCode> {
7382
return new Promise((resolve, reject) => {
7483
axios
75-
.get(`${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${blockId}`)
76-
.then((resp: any) => {
84+
.get<GetCode>(
85+
`${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${blockId}`
86+
)
87+
.then((resp) => {
7788
resolve(resp.data);
7889
})
7990
.catch(reject);
8091
});
8192
}
8293

94+
// TODO: add proper type
8395
/**
8496
* Gets the contract's storage variable at a specific key.
8597
*
@@ -115,11 +127,13 @@ export function getStorageAt(
115127
* @param txId
116128
* @returns the transaction status object { block_id, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
117129
*/
118-
export function getTransactionStatus(txId: number): Promise<object> {
130+
export function getTransactionStatus(txId: number): Promise<GetTransactionStatusResponse> {
119131
return new Promise((resolve, reject) => {
120132
axios
121-
.get(`${FEEDER_GATEWAY_URL}/get_transaction_status?transactionId=${txId}`)
122-
.then((resp: any) => {
133+
.get<GetTransactionStatusResponse>(
134+
`${FEEDER_GATEWAY_URL}/get_transaction_status?transactionId=${txId}`
135+
)
136+
.then((resp) => {
123137
resolve(resp.data);
124138
})
125139
.catch(reject);
@@ -134,17 +148,18 @@ export function getTransactionStatus(txId: number): Promise<object> {
134148
* @param txId
135149
* @returns the transacton object { transaction_id, status, transaction, block_id?, block_number?, transaction_index?, transaction_failure_reason? }
136150
*/
137-
export function getTransaction(txId: number): Promise<object> {
151+
export function getTransaction(txId: number): Promise<GetTransactionResponse> {
138152
return new Promise((resolve, reject) => {
139153
axios
140-
.get(`${FEEDER_GATEWAY_URL}/get_transaction?transactionId=${txId}`)
141-
.then((resp: any) => {
154+
.get<GetTransactionResponse>(`${FEEDER_GATEWAY_URL}/get_transaction?transactionId=${txId}`)
155+
.then((resp) => {
142156
resolve(resp.data);
143157
})
144158
.catch(reject);
145159
});
146160
}
147161

162+
// TODO: add proper type
148163
/**
149164
* Invoke a function on the starknet contract
150165
*

0 commit comments

Comments
 (0)