-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create /contracts/invoke method for firefly
- Loading branch information
1 parent
ce88ecf
commit 5302d02
Showing
6 changed files
with
76 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod chain; | ||
pub mod contracts; | ||
pub mod health; | ||
pub mod streams; | ||
pub mod transaction; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use axum::{extract::State, Json}; | ||
use firefly_server::apitypes::{ApiError, ApiResult, NoContent}; | ||
use reqwest::StatusCode; | ||
use schemars::JsonSchema; | ||
use serde::Deserialize; | ||
use serde_json::Value; | ||
|
||
use crate::AppState; | ||
|
||
#[derive(Deserialize, JsonSchema)] | ||
pub struct InvokeRequest { | ||
#[expect(unused)] | ||
/// The FireFly operation ID of this request. | ||
pub id: String, | ||
/// The name of the contract getting invoked | ||
pub address: String, | ||
/// A description of the method getting invoked. | ||
pub method: ABIMethod, | ||
/// Any parameters needed to invoke the method. | ||
pub params: Vec<Value>, | ||
} | ||
|
||
#[derive(Deserialize, JsonSchema)] | ||
pub struct ABIMethod { | ||
pub name: String, | ||
pub params: Vec<ABIParameter>, | ||
} | ||
|
||
#[derive(Deserialize, JsonSchema)] | ||
pub struct ABIParameter { | ||
pub name: String, | ||
} | ||
|
||
pub async fn invoke_contract( | ||
State(AppState { contracts, .. }): State<AppState>, | ||
Json(req): Json<InvokeRequest>, | ||
) -> ApiResult<NoContent> { | ||
let mut params = serde_json::Map::new(); | ||
for (schema, value) in req.method.params.iter().zip(req.params.into_iter()) { | ||
params.insert(schema.name.to_string(), value); | ||
} | ||
match contracts | ||
.invoke(&req.address, &req.method.name, params.into()) | ||
.await | ||
{ | ||
Ok(_res) => { | ||
// TODO: send res to the websocket | ||
Ok(NoContent) | ||
} | ||
Err(error) => { | ||
let err = ApiError::new(StatusCode::BAD_REQUEST, error.to_string()) | ||
.with_field("submissionRejected", true); | ||
Err(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters