Skip to content

Commit

Permalink
feat(client): flatten api in stainless
Browse files Browse the repository at this point in the history
  • Loading branch information
zcabter committed Jan 16, 2025
1 parent 6dc9b92 commit 7ba2534
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 68 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/generate-node-client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name: Upload OpenAPI spec to Stainless
on:
push:
branches: [main]
# enabled for testing
pull_request:
branches: [main]

jobs:
stainless:
Expand Down
66 changes: 64 additions & 2 deletions crates/jstz_node/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,43 @@
"version": "0.1.0-alpha.0"
},
"paths": {
"/accounts/{address}": {
"get": {
"tags": [
"Accounts"
],
"summary": "Get account",
"operationId": "get_account",
"parameters": [
{
"name": "address",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Account"
}
}
}
},
"404": {
"description": ""
},
"500": {
"description": ""
}
}
}
},
"/accounts/{address}/balance": {
"get": {
"tags": [
Expand Down Expand Up @@ -383,7 +420,7 @@
"tags": [
"Operations"
],
"summary": "Returns the hash of an Operation",
"summary": "Returns the hex encoded hash of an Operation",
"operationId": "hash_operation",
"requestBody": {
"content": {
Expand All @@ -401,7 +438,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Blake2b"
"$ref": "#/components/schemas/String"
}
}
}
Expand Down Expand Up @@ -456,6 +493,31 @@
},
"components": {
"schemas": {
"Account": {
"type": "object",
"required": [
"nonce",
"amount"
],
"properties": {
"amount": {
"$ref": "#/components/schemas/u64"
},
"function_code": {
"oneOf": [
{
"type": "null"
},
{
"$ref": "#/components/schemas/ParsedCode"
}
]
},
"nonce": {
"$ref": "#/components/schemas/Nonce"
}
}
},
"Address": {
"oneOf": [
{
Expand Down
25 changes: 25 additions & 0 deletions crates/jstz_node/src/services/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ struct KvQuery {

pub struct AccountsService;

/// Get account
#[utoipa::path(
get,
path = "/{address}",
tag = ACCOUNTS_TAG,
responses(
(status = 200, body = Account),
(status = 404),
(status = 500)
)
)]
async fn get_account(
State(AppState { rollup_client, .. }): State<AppState>,
Path(address): Path<String>,
) -> ServiceResult<Json<Account>> {
let key = format!("/jstz_account/{}", address);
let value = rollup_client.get_value(&key).await?;
let account = match value {
Some(value) => deserialize_account(value.as_slice())?,
None => Err(ServiceError::NotFound)?,
};
Ok(Json(account))
}

/// Get nonce of an account
#[utoipa::path(
get,
Expand Down Expand Up @@ -173,6 +197,7 @@ async fn get_kv_subkeys(
impl Service for AccountsService {
fn router_with_openapi() -> OpenApiRouter<AppState> {
let routes = OpenApiRouter::new()
.routes(routes!(get_account))
.routes(routes!(get_nonce))
.routes(routes!(get_code))
.routes(routes!(get_balance))
Expand Down
12 changes: 7 additions & 5 deletions crates/jstz_node/src/services/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use axum::{
Json,
};
use jstz_core::BINCODE_CONFIGURATION;
use jstz_proto::operation::{Operation, OperationHash, SignedOperation};
use jstz_proto::operation::{Operation, SignedOperation};
use jstz_proto::receipt::Receipt;
use tezos_data_encoding::enc::BinWriter;
use tezos_smart_rollup::inbox::ExternalMessageFrame;
Expand All @@ -18,6 +18,8 @@ pub struct OperationsService;

const OPERATIONS_TAG: &str = "Operations";

type HexEncodedOperationHash = String;

/// Inject an operation into Jstz
#[utoipa::path(
post,
Expand Down Expand Up @@ -79,21 +81,21 @@ async fn receipt(
Ok(Json(receipt))
}

/// Returns the hash of an Operation
/// Returns the hex encoded hash of an Operation
#[utoipa::path(
post,
path = "/hash",
tag = OPERATIONS_TAG,
responses(
(status = 200, body = OperationHash),
(status = 200, body = HexEncodedOperationHash),
(status = 400),
(status = 500)
)
)]
async fn hash_operation(
Json(operation): Json<Operation>,
) -> ServiceResult<Json<OperationHash>> {
Ok(Json(operation.hash()))
) -> ServiceResult<Json<HexEncodedOperationHash>> {
Ok(Json(format!("{}", operation.hash())))
}

impl Service for OperationsService {
Expand Down
94 changes: 35 additions & 59 deletions crates/jstz_node/stainless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ targets:
node:
readme_title: Jstz Client
package_name: "@jstz-dev/client"
production_repo: null
production_repo: jstz-dev/jstz-clien
publish:
npm: false
browser:
state: 'allow'

# `client_settings` define settings for the API client, such as extra constructor
# arguments (used for authentication), retry behavior, idempotency, etc.
Expand All @@ -39,71 +41,45 @@ pagination: []
# https://app.stainlessapi.com/docs/guides/configure#resources
resources:
accounts:
# Subresources define resources that are nested within another for more powerful
# logical groupings, e.g. `cards.payments`.
subresources:
balance:
# Configure the methods defined in this resource. Each key in the object is the
# name of the method and the value is either an endpoint (for example, `get /foo`)
# or an object with more detail.
#
# [reference]: https://app.stainlessapi.com/docs/reference/config#method
methods:
retrieve: get /accounts/{address}/balance
code:
# Configure the models--named types--defined in the resource. Each key in the
# object is the name of the model and the value is either the name of a schema in
# `#/components/schemas` or an object with more detail.
#
# [reference]: https://app.stainlessapi.com/docs/reference/config#model
models:
parsedCode: ParsedCode
methods:
retrieve: get /accounts/{address}/code
kv:
models:
kvValue: KvValue
methods:
retrieve: get /accounts/{address}/kv
subresources:
subkeys:
methods:
list:
type: http
endpoint: get /accounts/{address}/kv/subkeys
paginated: false
nonce:
models:
nonce: Nonce
methods:
retrieve: get /accounts/{address}/nonce

logs:
models:
logRecord: LogRecord
account: Account
code: ParsedCode
kvValue: KvValue
nonce: Nonce
methods:
stream: get /logs/{address}/stream
get: get /accounts/{address}
get_balance: get /accounts/{address}/balance
get_code: get /accounts/{address}/code
get_nonce: get /accounts/{address}/nonce
get_kv: get /accounts/{address}/kv
get_subkeys:
type: http
endpoint: get /accounts/{address}/kv/subkeys
paginated: false
subresources:
persistent_requests:
methods:
list:
type: http
endpoint: get /logs/{address}/persistent/requests
paginated: false
retrieve: get /logs/{address}/persistent/requests/{request_id}

logs:
models:
logRecord: LogRecord
methods:
stream: get /logs/{address}/stream
subresources:
persistent:
methods:
get: get /logs/{address}/persistent/requests/{request_id}
list:
type: http
endpoint: get /logs/{address}/persistent/requests
paginated: false
operations:
models:
operation: Operation
signed_operation: SignedOperation
receipt: Receipt
methods:
inject: post /operations
hash: post /operations/hash
subresources:
receipt:
models:
receipt: Receipt
methods:
retrieve: get /operations/{operation_hash}/receipt

crypto:
get_receipt: get /operations/{operation_hash}/receipt
$shared:
models:
publicKey: PublicKey
publicKeyHash: PublicKeyHash
Expand Down
14 changes: 12 additions & 2 deletions crates/jstz_proto/src/context/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ use super::new_account::NewAddress;
pub type Amount = u64;

#[derive(
Clone, Copy, Default, Debug, PartialEq, Eq, Serialize, Deserialize, ToSchema,
Clone,
Copy,
Default,
Debug,
PartialEq,
Eq,
Serialize,
Deserialize,
ToSchema,
Encode,
Decode,
)]
pub struct Nonce(pub u64);

Expand Down Expand Up @@ -66,7 +76,7 @@ impl TryFrom<String> for ParsedCode {
}
}

#[derive(Default, Debug, Clone, Serialize, Deserialize, Encode, Decode)]
#[derive(Default, Debug, Clone, Serialize, Deserialize, ToSchema, Encode, Decode)]
pub struct Account {
#[bincode(with_serde)]
pub nonce: Nonce,
Expand Down

0 comments on commit 7ba2534

Please sign in to comment.