diff --git a/.github/workflows/docs-bot.yml b/.github/workflows/docs-bot.yml new file mode 100644 index 000000000..64f5467b6 --- /dev/null +++ b/.github/workflows/docs-bot.yml @@ -0,0 +1,22 @@ +name: Node.js CI +on: + push: + branches: + - main +jobs: + 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 "janek@argent.xyz" + git config user.name "Docs Bot" + git add --force -- docs/README.md + git commit -m "docs: add generated docs" + git push diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index b8ca47a9f..8c79fab94 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -6,6 +6,7 @@ on: pull_request: branches: - main + jobs: build-and-test: runs-on: ubuntu-latest @@ -22,19 +23,3 @@ jobs: - 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 "janek@argent.xyz" - git config user.name "Docs Bot" - git add --force -- docs/README.md - git commit -m "docs: add generated docs" - git push diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 000000000..12182e90a --- /dev/null +++ b/src/index.d.ts @@ -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; +} diff --git a/src/index.ts b/src/index.ts index 19dbc567e..3da4c440e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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`; @@ -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 { +export function getContractAddresses(): Promise { return new Promise((resolve, reject) => { axios - .get(`${FEEDER_GATEWAY_URL}/get_contract_addresses`) - .then((resp: any) => { + .get(`${FEEDER_GATEWAY_URL}/get_contract_addresses`) + .then((resp) => { resolve(resp.data); }) .catch(reject); }); } +// TODO: add proper type /** * Calls a function on the StarkNet contract. * @@ -49,10 +58,10 @@ export function callContract(invokeTx: object, blockId: number): Promise * @param blockId * @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions } */ -export function getBlock(blockId: number): Promise { +export function getBlock(blockId: number): Promise { return new Promise((resolve, reject) => { axios - .get(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId}`) + .get(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId}`) .then((resp: any) => { resolve(resp.data); }) @@ -67,19 +76,22 @@ export function getBlock(blockId: number): Promise { * * @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 { +export function getCode(contractAddress: string, blockId: number): Promise { return new Promise((resolve, reject) => { axios - .get(`${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${blockId}`) - .then((resp: any) => { + .get( + `${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. * @@ -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 { +export function getTransactionStatus(txId: number): Promise { return new Promise((resolve, reject) => { axios - .get(`${FEEDER_GATEWAY_URL}/get_transaction_status?transactionId=${txId}`) - .then((resp: any) => { + .get( + `${FEEDER_GATEWAY_URL}/get_transaction_status?transactionId=${txId}` + ) + .then((resp) => { resolve(resp.data); }) .catch(reject); @@ -134,17 +148,18 @@ export function getTransactionStatus(txId: number): Promise { * @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 { +export function getTransaction(txId: number): Promise { return new Promise((resolve, reject) => { axios - .get(`${FEEDER_GATEWAY_URL}/get_transaction?transactionId=${txId}`) - .then((resp: any) => { + .get(`${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 *