Skip to content

Commit

Permalink
feat: succinctx offchain requests (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani authored Nov 8, 2023
1 parent 6da987e commit 5fb0b3b
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["plonky2x/core", "plonky2x/derive", "rustx"]
members = ["plonky2x/core", "plonky2x/derive", "rustx", "client"]
resolver = "2"

[profile.release]
Expand Down
12 changes: 12 additions & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "succinct-client"
version = "0.1.0"
edition = "2021"

[dependencies]
alloy-primitives = { version = "0.4.2", features = ["serde"] }
anyhow = "1.0.75"
log = "0.4.20"
reqwest = "0.11.22"
serde = "1.0.192"
serde_json = "1.0.108"
1 change: 1 addition & 0 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod request;
85 changes: 85 additions & 0 deletions client/src/request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use alloy_primitives::{Address, Bytes, B256};
use anyhow::{Error, Result};
use log::{error, info};
use reqwest::Client;
use serde::{Deserialize, Serialize};

#[allow(non_snake_case)]
#[derive(Serialize, Deserialize)]
/// Data to be sent to the Succinct X API with an offchain request.
struct OffchainInput {
/// The chain id of the network to be used.
chainId: u32,
/// The address of the contract to call.
to: Address,
/// The calldata to be used in the contract call.
data: Bytes,
/// The Succinct X function id to be called.
functionId: B256,
/// The input to be used in the Succinct X function call.
input: Bytes,
}

#[derive(Serialize, Deserialize)]
/// Data received from the Succinct X API from an offchain request.
struct OffchainRequestResponse {
request_id: String,
}

/// Client to interact with the Succinct X API.
pub struct SuccinctClient {
client: Client,
/// The base url for the Succinct X API. (ex. https://alpha.succinct.xyz/api)
base_url: String,
}

impl SuccinctClient {
pub fn new(base_url: String) -> Self {
Self {
client: Client::new(),
base_url,
}
}

/// Submit an offchain request to the Succinct X API.
pub async fn submit_platform_request(
&self,
chain_id: u32,
to: Address,
calldata: Bytes,
function_id: B256,
input: Bytes,
) -> Result<String> {
let data = OffchainInput {
chainId: chain_id,
to,
data: calldata,
functionId: function_id,
input,
};

// Serialize the data to JSON.
let serialized_data = serde_json::to_string(&data).unwrap();

// Make off-chain request.
let request_url = format!("{}{}", self.base_url, "/request/new");
let res = self
.client
.post(request_url)
.header("Content-Type", "application/json")
.body(serialized_data)
.send()
.await
.unwrap();

// Check if the request was successful.
if res.status().is_success() {
info!("Request successful!");
let response: OffchainRequestResponse = res.json().await.unwrap();
Ok(response.request_id)
} else {
error!("Request failed!");
Err(Error::msg("Failed to submit request to Succinct X API."))
}
}
}

0 comments on commit 5fb0b3b

Please sign in to comment.