From 6c1091f8a7b020d2196fd73d735771c1909e774f Mon Sep 17 00:00:00 2001 From: code0xff Date: Tue, 10 Sep 2024 16:07:31 +0900 Subject: [PATCH] refactor: Refactor cosmos-rpc --- frame/cosmos/rpc/src/cosmos.rs | 8 ++++---- frame/cosmos/rpc/src/lib.rs | 18 ++++++++++-------- template/node/src/rpc.rs | 4 ++-- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/frame/cosmos/rpc/src/cosmos.rs b/frame/cosmos/rpc/src/cosmos.rs index 8e05efa..855f40f 100644 --- a/frame/cosmos/rpc/src/cosmos.rs +++ b/frame/cosmos/rpc/src/cosmos.rs @@ -46,8 +46,8 @@ pub struct Cosmos { } impl Cosmos { - pub fn new(pool: Arc

, client: Arc) -> Self { - Self { pool, client } + pub fn new(client: Arc, pool: Arc

) -> Self { + Self { client, pool } } } @@ -69,16 +69,16 @@ where .convert_tx(best_hash, tx_bytes.to_vec()) .map_err(internal_error)?; - let tx_hash = H256(sha2_256(&tx_bytes)); self.pool .submit_one(best_hash, TransactionSource::Local, extrinsic) - .map_ok(move |_| tx_hash) + .map_ok(move |_| H256(sha2_256(&tx_bytes))) .map_err(internal_error) .await } async fn simulate(&self, tx_bytes: Bytes) -> RpcResult { let best_hash = self.client.info().best_hash; + self.client .runtime_api() .simulate(best_hash, tx_bytes.to_vec()) diff --git a/frame/cosmos/rpc/src/lib.rs b/frame/cosmos/rpc/src/lib.rs index c197039..0ce04ac 100644 --- a/frame/cosmos/rpc/src/lib.rs +++ b/frame/cosmos/rpc/src/lib.rs @@ -16,12 +16,14 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -mod cosmos; +pub mod cosmos; -pub use cosmos::{Cosmos, CosmosApiServer}; -pub use jsonrpsee::{ - core, - types::{error, ErrorObject, ErrorObjectOwned}, +use jsonrpsee::{ + core::to_json_raw_value, + types::{ + error::{INTERNAL_ERROR_CODE, INVALID_REQUEST_CODE}, + ErrorObject, ErrorObjectOwned, + }, }; pub fn error(code: i32, message: T, data: Option<&[u8]>) -> ErrorObjectOwned { @@ -29,16 +31,16 @@ pub fn error(code: i32, message: T, data: Option<&[u8]>) -> ErrorOb code, message.to_string(), data.map(|bytes| { - core::to_json_raw_value(&format!("0x{}", hex::encode(bytes))) + to_json_raw_value(&format!("0x{}", hex::encode(bytes))) .expect("Failed to serialize data") }), ) } pub fn request_error(message: T) -> ErrorObjectOwned { - error(error::INVALID_REQUEST_CODE, message, None) + error(INVALID_REQUEST_CODE, message, None) } pub fn internal_error(message: T) -> ErrorObjectOwned { - error(error::INTERNAL_ERROR_CODE, message, None) + error(INTERNAL_ERROR_CODE, message, None) } diff --git a/template/node/src/rpc.rs b/template/node/src/rpc.rs index eb6f978..ef19b3c 100644 --- a/template/node/src/rpc.rs +++ b/template/node/src/rpc.rs @@ -39,7 +39,7 @@ where C::Api: cosmos_runtime_api::CosmosRuntimeApi, C::Api: cosmwasm_runtime_api::CosmwasmRuntimeApi>, { - use cosmos_rpc::{Cosmos, CosmosApiServer}; + use cosmos_rpc::cosmos::{Cosmos, CosmosApiServer}; use cosmwasm_rpc::{Cosmwasm, CosmwasmApiServer}; use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer}; use substrate_frame_rpc_system::{System, SystemApiServer}; @@ -50,7 +50,7 @@ where module.merge(System::new(client.clone(), pool.clone(), deny_unsafe).into_rpc())?; module.merge(TransactionPayment::new(client.clone()).into_rpc())?; - module.merge(Cosmos::new(pool, client.clone()).into_rpc())?; + module.merge(Cosmos::new(client.clone(), pool).into_rpc())?; module.merge(Cosmwasm::new(client).into_rpc())?; // Extend this RPC with a custom API by using the following syntax.