Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
refactor: Refactor cosmos-rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
code0xff committed Sep 10, 2024
1 parent c4fe3cf commit 6c1091f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
8 changes: 4 additions & 4 deletions frame/cosmos/rpc/src/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub struct Cosmos<C, P> {
}

impl<C, P> Cosmos<C, P> {
pub fn new(pool: Arc<P>, client: Arc<C>) -> Self {
Self { pool, client }
pub fn new(client: Arc<C>, pool: Arc<P>) -> Self {
Self { client, pool }
}
}

Expand All @@ -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<SimulateResponse> {
let best_hash = self.client.info().best_hash;

self.client
.runtime_api()
.simulate(best_hash, tx_bytes.to_vec())
Expand Down
18 changes: 10 additions & 8 deletions frame/cosmos/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,31 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

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<T: ToString>(code: i32, message: T, data: Option<&[u8]>) -> ErrorObjectOwned {
ErrorObject::owned(
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<T: ToString>(message: T) -> ErrorObjectOwned {
error(error::INVALID_REQUEST_CODE, message, None)
error(INVALID_REQUEST_CODE, message, None)
}

pub fn internal_error<T: ToString>(message: T) -> ErrorObjectOwned {
error(error::INTERNAL_ERROR_CODE, message, None)
error(INTERNAL_ERROR_CODE, message, None)
}
4 changes: 2 additions & 2 deletions template/node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
C::Api: cosmos_runtime_api::CosmosRuntimeApi<Block>,
C::Api: cosmwasm_runtime_api::CosmwasmRuntimeApi<Block, Vec<u8>>,
{
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};
Expand All @@ -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.
Expand Down

0 comments on commit 6c1091f

Please sign in to comment.