Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Start adding types #1

Merged
merged 16 commits into from
Oct 19, 2021
40 changes: 40 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Node.js CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build-and-test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 14.x
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build --if-present
- run: npm test
generate-docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 14.x
- run: npm ci
- run: npm run docs
- name: Do git actions
run: |
git config user.email "[email protected]"
git config user.name "Docs Bot"
git add --force -- docs/README.md
git commit -m "docs: add generated docs"
git push
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ dist
npm-debug.log
.env
.DS_Store

docs/README.md
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<!-- primary badges -->
<p align="center">
<img src="https://img.shields.io/github/workflow/status/seanjameshan/starknet.js/Node.js%20CI/main">
<img src='https://img.shields.io/github/package-json/v/seanjameshan/starknet.js?label=npm' />
<img src='https://img.shields.io/bundlephobia/minzip/starknet?color=success&label=size' />
<!-- <img src='https://img.shields.io/npm/dt/starknet?color=blueviolet' /> -->
Expand Down
2 changes: 1 addition & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as starknet from '../index';
import * as starknet from '../';

describe('starknet endpoints', () => {
describe('feeder gateway endpoints', () => {
Expand Down
16 changes: 8 additions & 8 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ a confirmation of invoking a function on the starknet contract

#### Defined in

[index.ts:156](https://github.com/seanjameshan/starknet.js/blob/505f231/src/index.ts#L156)
[index.ts:156](https://github.com/argentlabs/starknet.js/blob/6a0a989/src/index.ts#L156)
janek26 marked this conversation as resolved.
Show resolved Hide resolved

___

Expand All @@ -66,7 +66,7 @@ the result of the function on the smart contract.

#### Defined in

[index.ts:33](https://github.com/seanjameshan/starknet.js/blob/505f231/src/index.ts#L33)
[index.ts:33](https://github.com/argentlabs/starknet.js/blob/6a0a989/src/index.ts#L33)

___

Expand All @@ -92,7 +92,7 @@ the block object { block_id, previous_block_id, state_root, status, timestamp, t

#### Defined in

[index.ts:52](https://github.com/seanjameshan/starknet.js/blob/505f231/src/index.ts#L52)
[index.ts:52](https://github.com/argentlabs/starknet.js/blob/6a0a989/src/index.ts#L52)

___

Expand All @@ -119,7 +119,7 @@ ABI of compiled contract in JSON

#### Defined in

[index.ts:72](https://github.com/seanjameshan/starknet.js/blob/505f231/src/index.ts#L72)
[index.ts:72](https://github.com/argentlabs/starknet.js/blob/6a0a989/src/index.ts#L72)

___

Expand All @@ -139,7 +139,7 @@ starknet smart contract address

#### Defined in

[index.ts:13](https://github.com/seanjameshan/starknet.js/blob/505f231/src/index.ts#L13)
[index.ts:13](https://github.com/argentlabs/starknet.js/blob/6a0a989/src/index.ts#L13)

___

Expand Down Expand Up @@ -167,7 +167,7 @@ the value of the storage variable

#### Defined in

[index.ts:93](https://github.com/seanjameshan/starknet.js/blob/505f231/src/index.ts#L93)
[index.ts:93](https://github.com/argentlabs/starknet.js/blob/6a0a989/src/index.ts#L93)

___

Expand All @@ -193,7 +193,7 @@ the transacton object { transaction_id, status, transaction, block_id?, block_nu

#### Defined in

[index.ts:137](https://github.com/seanjameshan/starknet.js/blob/505f231/src/index.ts#L137)
[index.ts:137](https://github.com/argentlabs/starknet.js/blob/6a0a989/src/index.ts#L137)

___

Expand All @@ -219,4 +219,4 @@ the transaction status object { block_id, tx_status: NOT_RECEIVED | RECEIVED | P

#### Defined in

[index.ts:118](https://github.com/seanjameshan/starknet.js/blob/505f231/src/index.ts#L118)
[index.ts:118](https://github.com/argentlabs/starknet.js/blob/6a0a989/src/index.ts#L118)
68 changes: 68 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
export interface GetContractAddressesResponse {
Starknet: string;
GpsStatementVerifier: string;
}

export type Status = 'NOT_RECEIVED' | 'RECEIVED' | 'PENDING' | 'REJECTED' | 'ACCEPTED_ONCHAIN';
export type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
export type EntryPointType = 'EXTERNAL';

export interface Transaction {
type: Type;
contract_address: string;
entry_point_type?: EntryPointType;
entry_point_selector?: string;
calldata?: string[];
}

export interface GetBlockResponse {
sequence_number: number;
state_root: string;
block_id: number;
transactions: {
[txid: string]: Transaction;
};
timestamp: number;
transaction_receipts: {
[txid: string]: {
block_id: number;
transaction_id: number;
l2_to_l1_messages: {
to_address: string;
payload: string[];
from_address: string;
}[];
block_number: number;
status: Status;
transaction_index: number;
};
};
previous_block_id: number;
status: Status;
}

export interface Abi {
inputs: { name: string; type: string }[];
name: string;
outputs: { name: string; type: string }[];
type: string;
}

export interface GetCode {
bytecode: string[];
abi: Abi[];
}

export interface GetTransactionStatusResponse {
tx_status: Status;
block_id: number;
}

export interface GetTransactionResponse {
transaction_index: number;
transaction: Transaction;
block_id: number;
block_number: number;
status: Status;
transaction_id: number;
}
47 changes: 31 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import axios from 'axios';

import type {
GetBlockResponse,
GetCode,
GetContractAddressesResponse,
GetTransactionResponse,
GetTransactionStatusResponse,
} from './index.d';

const API_URL: string = 'https://alpha2.starknet.io/';
const FEEDER_GATEWAY_URL: string = `${API_URL}/feeder_gateway`;
const GATEWAY_URL: string = `${API_URL}/gateway`;
Expand All @@ -8,19 +16,20 @@ const GATEWAY_URL: string = `${API_URL}/gateway`;
* Gets the smart contract address on the goerli testnet.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
* @returns starknet smart contract address
* @returns starknet smart contract addresses
*/
export function getContractAddresses(): Promise<object> {
export function getContractAddresses(): Promise<GetContractAddressesResponse> {
return new Promise((resolve, reject) => {
axios
.get(`${FEEDER_GATEWAY_URL}/get_contract_addresses`)
.then((resp: any) => {
.get<GetContractAddressesResponse>(`${FEEDER_GATEWAY_URL}/get_contract_addresses`)
.then((resp) => {
resolve(resp.data);
})
.catch(reject);
});
}

// TODO: add proper type
/**
* Calls a function on the StarkNet contract.
*
Expand Down Expand Up @@ -49,10 +58,10 @@ export function callContract(invokeTx: object, blockId: number): Promise<object>
* @param blockId
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
*/
export function getBlock(blockId: number): Promise<object> {
export function getBlock(blockId: number): Promise<GetBlockResponse> {
return new Promise((resolve, reject) => {
axios
.get(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId}`)
.get<GetBlockResponse>(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId}`)
.then((resp: any) => {
resolve(resp.data);
})
Expand All @@ -67,19 +76,22 @@ export function getBlock(blockId: number): Promise<object> {
*
* @param contractAddress
* @param blockId
* @returns ABI of compiled contract in JSON
* @returns Bytecode and ABI of compiled contract
*/
export function getCode(contractAddress: string, blockId: number): Promise<object> {
export function getCode(contractAddress: string, blockId: number): Promise<GetCode> {
return new Promise((resolve, reject) => {
axios
.get(`${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${blockId}`)
.then((resp: any) => {
.get<GetCode>(
`${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${blockId}`
)
.then((resp) => {
resolve(resp.data);
})
.catch(reject);
});
}

// TODO: add proper type
/**
* Gets the contract's storage variable at a specific key.
*
Expand Down Expand Up @@ -115,11 +127,13 @@ export function getStorageAt(
* @param txId
* @returns the transaction status object { block_id, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
*/
export function getTransactionStatus(txId: number): Promise<object> {
export function getTransactionStatus(txId: number): Promise<GetTransactionStatusResponse> {
return new Promise((resolve, reject) => {
axios
.get(`${FEEDER_GATEWAY_URL}/get_transaction_status?transactionId=${txId}`)
.then((resp: any) => {
.get<GetTransactionStatusResponse>(
`${FEEDER_GATEWAY_URL}/get_transaction_status?transactionId=${txId}`
)
.then((resp) => {
resolve(resp.data);
})
.catch(reject);
Expand All @@ -134,17 +148,18 @@ export function getTransactionStatus(txId: number): Promise<object> {
* @param txId
* @returns the transacton object { transaction_id, status, transaction, block_id?, block_number?, transaction_index?, transaction_failure_reason? }
*/
export function getTransaction(txId: number): Promise<object> {
export function getTransaction(txId: number): Promise<GetTransactionResponse> {
return new Promise((resolve, reject) => {
axios
.get(`${FEEDER_GATEWAY_URL}/get_transaction?transactionId=${txId}`)
.then((resp: any) => {
.get<GetTransactionResponse>(`${FEEDER_GATEWAY_URL}/get_transaction?transactionId=${txId}`)
.then((resp) => {
resolve(resp.data);
})
.catch(reject);
});
}

// TODO: add proper type
/**
* Invoke a function on the starknet contract
*
Expand Down