From ada43e967d55fbc38fbc66d8c934b6d8f5fc8507 Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Tue, 3 Jan 2023 12:42:38 +0000 Subject: [PATCH 1/2] As we have state_call we don't need a custom RPC --- Cargo.lock | 27 +------ Cargo.toml | 2 - bin/node/rpc/Cargo.toml | 1 - bin/node/rpc/src/lib.rs | 3 - bin/node/runtime/Cargo.toml | 3 +- bin/node/runtime/src/lib.rs | 2 +- frame/dex/Cargo.toml | 1 + frame/dex/rpc/Cargo.toml | 23 ------ frame/dex/rpc/README.md | 3 - frame/dex/rpc/runtime-api/Cargo.toml | 28 -------- frame/dex/rpc/runtime-api/README.md | 3 - frame/dex/rpc/runtime-api/src/lib.rs | 31 -------- frame/dex/rpc/src/lib.rs | 103 --------------------------- frame/dex/src/lib.rs | 21 +++++- 14 files changed, 24 insertions(+), 227 deletions(-) delete mode 100644 frame/dex/rpc/Cargo.toml delete mode 100644 frame/dex/rpc/README.md delete mode 100644 frame/dex/rpc/runtime-api/Cargo.toml delete mode 100644 frame/dex/rpc/runtime-api/README.md delete mode 100644 frame/dex/rpc/runtime-api/src/lib.rs delete mode 100644 frame/dex/rpc/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c92aa1612aa49..5b8e17c83f49c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3081,7 +3081,6 @@ dependencies = [ "pallet-conviction-voting", "pallet-democracy", "pallet-dex", - "pallet-dex-rpc-runtime-api", "pallet-election-provider-multi-phase", "pallet-election-provider-support-benchmarking", "pallet-elections-phragmen", @@ -4359,7 +4358,6 @@ dependencies = [ "jsonrpsee", "mmr-rpc", "node-primitives", - "pallet-dex-rpc", "pallet-transaction-payment-rpc", "sc-chain-spec", "sc-client-api", @@ -5105,6 +5103,7 @@ dependencies = [ "pallet-balances", "parity-scale-codec", "scale-info", + "sp-api", "sp-core", "sp-io", "sp-keystore", @@ -5112,30 +5111,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "pallet-dex-rpc" -version = "4.0.0-dev" -dependencies = [ - "jsonrpsee", - "pallet-dex-rpc-runtime-api", - "parity-scale-codec", - "sp-api", - "sp-blockchain", - "sp-core", - "sp-rpc", - "sp-runtime", -] - -[[package]] -name = "pallet-dex-rpc-runtime-api" -version = "4.0.0-dev" -dependencies = [ - "pallet-dex", - "parity-scale-codec", - "sp-api", - "sp-runtime", -] - [[package]] name = "pallet-election-provider-multi-phase" version = "4.0.0-dev" diff --git a/Cargo.toml b/Cargo.toml index 11d1fd1671a65..facc413f06c88 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -96,8 +96,6 @@ members = [ "frame/conviction-voting", "frame/democracy", "frame/dex", - "frame/dex/rpc", - "frame/dex/rpc/runtime-api", "frame/fast-unstake", "frame/try-runtime", "frame/elections-phragmen", diff --git a/bin/node/rpc/Cargo.toml b/bin/node/rpc/Cargo.toml index c27a5996bd946..f34922a287dfe 100644 --- a/bin/node/rpc/Cargo.toml +++ b/bin/node/rpc/Cargo.toml @@ -17,7 +17,6 @@ jsonrpsee = { version = "0.16.2", features = ["server"] } node-primitives = { version = "2.0.0", path = "../primitives" } pallet-transaction-payment-rpc = { version = "4.0.0-dev", path = "../../../frame/transaction-payment/rpc/" } mmr-rpc = { version = "4.0.0-dev", path = "../../../client/merkle-mountain-range/rpc/" } -pallet-dex-rpc = { version = "4.0.0-dev", path = "../../../frame/dex/rpc/" } sc-chain-spec = { version = "4.0.0-dev", path = "../../../client/chain-spec" } sc-client-api = { version = "4.0.0-dev", path = "../../../client/api" } sc-consensus-babe = { version = "0.10.0-dev", path = "../../../client/consensus/babe" } diff --git a/bin/node/rpc/src/lib.rs b/bin/node/rpc/src/lib.rs index 829f750b3eaa5..0dc5ba4039b00 100644 --- a/bin/node/rpc/src/lib.rs +++ b/bin/node/rpc/src/lib.rs @@ -110,7 +110,6 @@ where C::Api: substrate_frame_rpc_system::AccountNonceApi, C::Api: mmr_rpc::MmrRuntimeApi::Hash, BlockNumber>, C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi, - C::Api: pallet_dex_rpc::DexRuntimeApi, C::Api: BabeApi, C::Api: BlockBuilder, P: TransactionPool + 'static, @@ -119,7 +118,6 @@ where B::State: sc_client_api::backend::StateBackend>, { use mmr_rpc::{Mmr, MmrApiServer}; - use pallet_dex_rpc::{Dex, DexApiServer}; use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer}; use sc_consensus_babe_rpc::{Babe, BabeApiServer}; use sc_finality_grandpa_rpc::{Grandpa, GrandpaApiServer}; @@ -152,7 +150,6 @@ where // These RPCs should use an asynchronous caller instead. io.merge(Mmr::new(client.clone()).into_rpc())?; io.merge(TransactionPayment::new(client.clone()).into_rpc())?; - io.merge(Dex::new(client.clone()).into_rpc())?; io.merge( Babe::new( client.clone(), diff --git a/bin/node/runtime/Cargo.toml b/bin/node/runtime/Cargo.toml index 80fe3af48d935..44f9ce934aaf3 100644 --- a/bin/node/runtime/Cargo.toml +++ b/bin/node/runtime/Cargo.toml @@ -111,7 +111,6 @@ pallet-uniques = { version = "4.0.0-dev", default-features = false, path = "../. pallet-vesting = { version = "4.0.0-dev", default-features = false, path = "../../../frame/vesting" } pallet-whitelist = { version = "4.0.0-dev", default-features = false, path = "../../../frame/whitelist" } pallet-dex = { version = "4.0.0-dev", default-features = false, path = "../../../frame/dex" } -pallet-dex-rpc-runtime-api = { version = "4.0.0-dev", default-features = false, path = "../../../frame/dex/rpc/runtime-api/" } [build-dependencies] substrate-wasm-builder = { version = "5.0.0-dev", path = "../../../utils/wasm-builder" } @@ -205,7 +204,6 @@ std = [ "sp-io/std", "pallet-child-bounties/std", "pallet-alliance/std", - "pallet-dex-rpc-runtime-api/std", "pallet-dex/std", ] runtime-benchmarks = [ @@ -224,6 +222,7 @@ runtime-benchmarks = [ "pallet-contracts/runtime-benchmarks", "pallet-conviction-voting/runtime-benchmarks", "pallet-democracy/runtime-benchmarks", + "pallet-dex/runtime-benchmarks", "pallet-election-provider-multi-phase/runtime-benchmarks", "pallet-election-provider-support-benchmarking/runtime-benchmarks", "pallet-elections-phragmen/runtime-benchmarks", diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 52c0796add48e..ef337e9b5b413 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -2133,7 +2133,7 @@ impl_runtime_apis! { } } - impl pallet_dex_rpc_runtime_api::DexApi< + impl pallet_dex::DexApi< Block, Balance, > for Runtime diff --git a/frame/dex/Cargo.toml b/frame/dex/Cargo.toml index 30cdb7366a0ba..df5bb9df2f28c 100644 --- a/frame/dex/Cargo.toml +++ b/frame/dex/Cargo.toml @@ -15,6 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } scale-info = { version = "2.0.0", default-features = false, features = ["derive"] } +sp-api = { version = "4.0.0-dev", default-features = false, path = "../../primitives/api" } sp-std = { version = "5.0.0", default-features = false, path = "../../primitives/std" } sp-runtime = { version = "7.0.0", default-features = false, path = "../../primitives/runtime" } frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" } diff --git a/frame/dex/rpc/Cargo.toml b/frame/dex/rpc/Cargo.toml deleted file mode 100644 index 70ccf2f0e0fc9..0000000000000 --- a/frame/dex/rpc/Cargo.toml +++ /dev/null @@ -1,23 +0,0 @@ -[package] -name = "pallet-dex-rpc" -version = "4.0.0-dev" -authors = ["Parity Technologies "] -edition = "2021" -license = "Apache-2.0" -homepage = "https://substrate.io" -repository = "https://github.com/paritytech/substrate/" -description = "RPC interface for the DEX pallet." -readme = "README.md" - -[package.metadata.docs.rs] -targets = ["x86_64-unknown-linux-gnu"] - -[dependencies] -codec = { package = "parity-scale-codec", version = "3.0.0" } -jsonrpsee = { version = "0.16.2", features = ["server", "macros"] } -pallet-dex-rpc-runtime-api = { version = "4.0.0-dev", path = "./runtime-api" } -sp-api = { version = "4.0.0-dev", path = "../../../primitives/api" } -sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" } -sp-core = { version = "7.0.0", path = "../../../primitives/core" } -sp-rpc = { version = "7.0.0", path = "../../../primitives/rpc" } -sp-runtime = { version = "7.0.0", path = "../../../primitives/runtime" } diff --git a/frame/dex/rpc/README.md b/frame/dex/rpc/README.md deleted file mode 100644 index 5514b605debed..0000000000000 --- a/frame/dex/rpc/README.md +++ /dev/null @@ -1,3 +0,0 @@ -RPC interface for the DEX pallet. - -License: Apache-2.0 diff --git a/frame/dex/rpc/runtime-api/Cargo.toml b/frame/dex/rpc/runtime-api/Cargo.toml deleted file mode 100644 index 41281c2992f41..0000000000000 --- a/frame/dex/rpc/runtime-api/Cargo.toml +++ /dev/null @@ -1,28 +0,0 @@ -[package] -name = "pallet-dex-rpc-runtime-api" -version = "4.0.0-dev" -authors = ["Parity Technologies "] -edition = "2021" -license = "Apache-2.0" -homepage = "https://substrate.io" -repository = "https://github.com/paritytech/substrate/" -description = "RPC runtime API for DEX FRAME pallet" -readme = "README.md" - -[package.metadata.docs.rs] -targets = ["x86_64-unknown-linux-gnu"] - -[dependencies] -codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } -pallet-dex = { version = "4.0.0-dev", default-features = false, path = "../../../dex" } -sp-api = { version = "4.0.0-dev", default-features = false, path = "../../../../primitives/api" } -sp-runtime = { version = "7.0.0", default-features = false, path = "../../../../primitives/runtime" } - -[features] -default = ["std"] -std = [ - "codec/std", - "pallet-dex/std", - "sp-api/std", - "sp-runtime/std", -] diff --git a/frame/dex/rpc/runtime-api/README.md b/frame/dex/rpc/runtime-api/README.md deleted file mode 100644 index c2f26ce842642..0000000000000 --- a/frame/dex/rpc/runtime-api/README.md +++ /dev/null @@ -1,3 +0,0 @@ -Runtime API definition for DEX pallet. - -License: Apache-2.0 diff --git a/frame/dex/rpc/runtime-api/src/lib.rs b/frame/dex/rpc/runtime-api/src/lib.rs deleted file mode 100644 index 17184b50ba310..0000000000000 --- a/frame/dex/rpc/runtime-api/src/lib.rs +++ /dev/null @@ -1,31 +0,0 @@ -// This file is part of Substrate. - -// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: Apache-2.0 - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//! Runtime API definition for transaction payment pallet. - -#![cfg_attr(not(feature = "std"), no_std)] - -use codec::Codec; -use sp_runtime::traits::MaybeDisplay; - -sp_api::decl_runtime_apis! { - pub trait DexApi where - Balance: Codec + MaybeDisplay, - { - fn quote_price(asset1: Option, asset2: Option, amount: u64) -> Option; - } -} diff --git a/frame/dex/rpc/src/lib.rs b/frame/dex/rpc/src/lib.rs deleted file mode 100644 index 1bedd44e96f00..0000000000000 --- a/frame/dex/rpc/src/lib.rs +++ /dev/null @@ -1,103 +0,0 @@ -// This file is part of Substrate. - -// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: Apache-2.0 - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//! RPC interface for the transaction payment pallet. - -use std::{marker::PhantomData, sync::Arc}; - -use codec::Codec; -use jsonrpsee::{ - core::RpcResult, - proc_macros::rpc, - types::error::{CallError, ErrorObject}, -}; -use sp_api::ProvideRuntimeApi; -use sp_blockchain::HeaderBackend; -use sp_runtime::{ - generic::BlockId, - traits::{Block as BlockT, MaybeDisplay}, -}; - -pub use pallet_dex_rpc_runtime_api::DexApi as DexRuntimeApi; - -#[rpc(client, server)] -pub trait DexApi -where - Balance: Copy, -{ - #[method(name = "dex_quotePrice")] - fn quote_price( - &self, - asset1: Option, - asset2: Option, - amount: u64, - ) -> RpcResult>; -} - -/// Dex RPC methods. -pub struct Dex { - client: Arc, - _marker: PhantomData, -} - -impl Dex { - /// Creates a new instance of the DEX RPC helper. - pub fn new(client: Arc) -> Self { - Self { client, _marker: Default::default() } - } -} - -/// Error type of this RPC api. -pub enum Error { - /// The call to runtime failed. - RuntimeError, -} - -impl From for i32 { - fn from(e: Error) -> i32 { - match e { - Error::RuntimeError => 1, - } - } -} - -impl DexApiServer for Dex -where - Block: BlockT, - Client: ProvideRuntimeApi + HeaderBackend + Send + Sync + 'static, - Client::Api: DexRuntimeApi, - Balance: Codec + MaybeDisplay + Copy, -{ - fn quote_price( - &self, - asset1: Option, - asset2: Option, - amount: u64, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = BlockId::hash(self.client.info().best_hash); - - api.quote_price(&at, asset1, asset2, amount).map_err(|e| { - CallError::Custom(ErrorObject::owned( - Error::RuntimeError.into(), - "Unable to query price info.", - Some(e.to_string()), - )) - .into() - }) - } -} diff --git a/frame/dex/src/lib.rs b/frame/dex/src/lib.rs index d75d4247471cc..ac950cf588355 100644 --- a/frame/dex/src/lib.rs +++ b/frame/dex/src/lib.rs @@ -27,7 +27,16 @@ //! - provide the liquidity and receive back an LP token //! - exchange the LP token back to assets //! - swap 2 assets if there is a pool created -//! - query for an exchange price via a new RPC endpoint +//! - query for an exchange price via a new runtime call endpoint +//! +//! Here is an example `state_call` that asks for a quote of a pool of native versus asset 1: +//! +//! ``` +//! curl -sS -H "Content-Type: application/json" -d +//! '{"id":1, "jsonrpc":"2.0", "method": "state_call", "params": ["DexApi_quote_price", "0x0101000000000000000000000011"]}' +//! http://localhost:9933/ +//! ``` + #![cfg_attr(not(feature = "std"), no_std)] use frame_support::traits::Incrementable; @@ -43,7 +52,9 @@ mod tests; #[cfg(test)] mod mock; +use codec::Codec; pub use pallet::*; +use sp_runtime::traits::MaybeDisplay; pub use types::*; pub use weights::WeightInfo; @@ -834,3 +845,11 @@ pub mod pallet { } } } + +sp_api::decl_runtime_apis! { + pub trait DexApi where + Balance: Codec + MaybeDisplay, + { + fn quote_price(asset1: Option, asset2: Option, amount: u64) -> Option; + } +} From d4dfee5259671677577c2ed73dc9dcf51b504c2b Mon Sep 17 00:00:00 2001 From: Giles Cope Date: Tue, 3 Jan 2023 12:47:22 +0000 Subject: [PATCH 2/2] fix docs --- frame/dex/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frame/dex/src/lib.rs b/frame/dex/src/lib.rs index ac950cf588355..a990413f6482b 100644 --- a/frame/dex/src/lib.rs +++ b/frame/dex/src/lib.rs @@ -31,9 +31,9 @@ //! //! Here is an example `state_call` that asks for a quote of a pool of native versus asset 1: //! -//! ``` -//! curl -sS -H "Content-Type: application/json" -d -//! '{"id":1, "jsonrpc":"2.0", "method": "state_call", "params": ["DexApi_quote_price", "0x0101000000000000000000000011"]}' +//! ```text +//! curl -sS -H "Content-Type: application/json" -d \ +//! '{"id":1, "jsonrpc":"2.0", "method": "state_call", "params": ["DexApi_quote_price", "0x0101000000000000000000000011"]}' \ //! http://localhost:9933/ //! ```