-
Notifications
You must be signed in to change notification settings - Fork 9
Enhance Solana transaction support and add new dependencies #78
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,149 @@ | ||
| use axum::{ | ||
| extract::State, | ||
| http::StatusCode, | ||
| response::{IntoResponse, Json}, | ||
| }; | ||
| use base64::{Engine, engine::general_purpose::STANDARD as Base64Engine}; | ||
| use bincode::config::standard as bincode_standard; | ||
| use engine_core::{ | ||
| error::EngineError, | ||
| execution_options::solana::SolanaChainId, | ||
| }; | ||
| use engine_solana_core::transaction::SolanaTransaction; | ||
| use schemars::JsonSchema; | ||
| use serde::{Deserialize, Serialize}; | ||
| use solana_client::nonblocking::rpc_client::RpcClient; | ||
|
|
||
| use crate::http::{ | ||
| error::ApiEngineError, | ||
| extractors::{EngineJson, SigningCredentialsExtractor}, | ||
| server::EngineServerState, | ||
| types::SuccessResponse, | ||
| }; | ||
|
|
||
| // ===== REQUEST/RESPONSE TYPES ===== | ||
|
|
||
| /// Request to sign a Solana transaction | ||
| #[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct SignSolanaTransactionRequest { | ||
| /// Transaction input (instructions or serialized transaction) | ||
| #[serde(flatten)] | ||
| pub input: engine_solana_core::transaction::SolanaTransactionInput, | ||
|
|
||
| /// Solana execution options | ||
| pub execution_options: engine_core::execution_options::solana::SolanaExecutionOptions, | ||
| } | ||
|
|
||
| /// Data returned from successful signing | ||
| #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, utoipa::ToSchema)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct SignSolanaTransactionResponse { | ||
| /// The signature (base58-encoded) | ||
| pub signature: String, | ||
| /// The signed serialized transaction (base64-encoded) | ||
| pub signed_transaction: String, | ||
| } | ||
|
|
||
| // ===== ROUTE HANDLER ===== | ||
|
|
||
| #[utoipa::path( | ||
| post, | ||
| operation_id = "signSolanaTransaction", | ||
| path = "/solana/sign/transaction", | ||
| tag = "Solana", | ||
| request_body(content = SignSolanaTransactionRequest, description = "Sign Solana transaction request", content_type = "application/json"), | ||
| responses( | ||
| (status = 200, description = "Successfully signed Solana transaction", body = SuccessResponse<SignSolanaTransactionResponse>, content_type = "application/json"), | ||
| ), | ||
| params( | ||
| ("x-vault-access-token" = Option<String>, Header, description = "Vault access token"), | ||
| ) | ||
| )] | ||
| /// Sign Solana Transaction | ||
| /// | ||
| /// Sign a Solana transaction without broadcasting it | ||
| pub async fn sign_solana_transaction( | ||
| State(state): State<EngineServerState>, | ||
| SigningCredentialsExtractor(signing_credential): SigningCredentialsExtractor, | ||
| EngineJson(request): EngineJson<SignSolanaTransactionRequest>, | ||
| ) -> Result<impl IntoResponse, ApiEngineError> { | ||
| let chain_id = request.execution_options.chain_id; | ||
| let signer_address = request.execution_options.signer_address; | ||
|
|
||
| tracing::info!( | ||
| chain_id = %chain_id.as_str(), | ||
| signer = %signer_address, | ||
| "Processing Solana transaction signing request" | ||
| ); | ||
|
|
||
| // Get RPC URL for the chain | ||
| let rpc_url = get_rpc_url(chain_id); | ||
|
|
||
| // Create RPC client | ||
| let rpc_client = RpcClient::new(rpc_url.to_string()); | ||
|
|
||
| // Get recent blockhash | ||
| let recent_blockhash = rpc_client | ||
| .get_latest_blockhash() | ||
| .await | ||
| .map_err(|e| { | ||
| ApiEngineError(EngineError::ValidationError { | ||
| message: format!("Failed to get recent blockhash: {}", e), | ||
| }) | ||
| })?; | ||
|
|
||
| // Build the transaction | ||
| let solana_tx = SolanaTransaction { | ||
| input: request.input, | ||
| compute_unit_limit: request.execution_options.compute_unit_limit, | ||
| compute_unit_price: None, // Will be set if priority fee is configured | ||
| }; | ||
|
|
||
| // Convert to versioned transaction | ||
| let versioned_tx = solana_tx | ||
| .to_versioned_transaction(signer_address, recent_blockhash) | ||
| .map_err(|e| { | ||
| ApiEngineError(EngineError::ValidationError { | ||
| message: format!("Failed to build transaction: {}", e), | ||
| }) | ||
| })?; | ||
|
|
||
| // Sign the transaction | ||
| let signed_tx = state | ||
| .solana_signer | ||
| .sign_transaction(versioned_tx, signer_address, &signing_credential) | ||
| .await | ||
| .map_err(ApiEngineError)?; | ||
|
|
||
| // Get the signature (first signature in the transaction) | ||
| let signature = signed_tx.signatures[0]; | ||
|
|
||
| // Serialize the signed transaction to base64 | ||
| let signed_tx_bytes = bincode::serde::encode_to_vec(&signed_tx, bincode_standard()).map_err( | ||
| |e| { | ||
| ApiEngineError(EngineError::ValidationError { | ||
| message: format!("Failed to serialize signed transaction: {}", e), | ||
| }) | ||
| }, | ||
| )?; | ||
| let signed_tx_base64 = Base64Engine.encode(&signed_tx_bytes); | ||
|
|
||
| let response = SignSolanaTransactionResponse { | ||
| signature: signature.to_string(), | ||
| signed_transaction: signed_tx_base64, | ||
| }; | ||
|
|
||
| tracing::info!( | ||
| chain_id = %chain_id.as_str(), | ||
| signature = %signature, | ||
| "Solana transaction signed successfully" | ||
| ); | ||
|
|
||
| Ok((StatusCode::OK, Json(SuccessResponse::new(response)))) | ||
| } | ||
|
|
||
| /// Get RPC URL for a Solana chain | ||
| fn get_rpc_url(chain_id: SolanaChainId) -> &'static str { | ||
| chain_id.default_rpc_url() | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.