Skip to content

Commit

Permalink
Impl Spot rpc (paritytech#178)
Browse files Browse the repository at this point in the history
* Add dex spot rpc

* Impl Spot RPC

* Rename lowest_offer to lowest_ask

* Pretty print BtcAddress

* .

* Pretty print bitcoin hot/cold keys

* .

* Nits

* Use mutate

* Integrate Spot RPC

* Add LatestPrice info of trading pair

* Remove =>

* Rename lowestOffer to lowestAsk in chainx_types.json

* Request by page for xspot_getOrdersByAccount

* Add money emoji

* .
  • Loading branch information
liuchengxu authored Aug 8, 2020
1 parent 066f436 commit 6fed8a7
Show file tree
Hide file tree
Showing 23 changed files with 704 additions and 115 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ members = [
"xpallets/contracts/rpc",
"xpallets/contracts/rpc/runtime-api",
"xpallets/dex/spot",
"xpallets/dex/spot/rpc",
"xpallets/dex/spot/rpc/runtime-api",
"xpallets/mining/asset",
"xpallets/mining/staking",
"xpallets/mining/staking/rpc",
Expand Down
2 changes: 2 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ chainx-primitives = { path = "../primitives" }
# ChainX pallets
xpallet-assets-rpc = { path = "../xpallets/assets/rpc" }
xpallet-assets-rpc-runtime-api = { path = "../xpallets/assets/rpc/runtime-api" }
xpallet-dex-spot-rpc = { path = "../xpallets/dex/spot/rpc" }
xpallet-dex-spot-rpc-runtime-api = { path = "../xpallets/dex/spot/rpc/runtime-api" }
xpallet-mining-staking-rpc = { path = "../xpallets/mining/staking/rpc" }
xpallet-mining-staking-rpc-runtime-api = { path = "../xpallets/mining/staking/rpc/runtime-api" }
xpallet-contracts-rpc = { path = "../xpallets/contracts/rpc" }
Expand Down
4 changes: 4 additions & 0 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ where
xpallet_assets_rpc_runtime_api::AssetsApi<Block, AccountId, Balance>,
<Client<BE, E, Block, RA> as ProvideRuntimeApi<Block>>::Api:
xpallet_mining_staking_rpc_runtime_api::XStakingApi<Block, AccountId, Balance, BlockNumber>,
<Client<BE, E, Block, RA> as ProvideRuntimeApi<Block>>::Api:
xpallet_dex_spot_rpc_runtime_api::XSpotApi<Block, AccountId, Balance, BlockNumber, Balance>,
<Client<BE, E, Block, RA> as ProvideRuntimeApi<Block>>::Api:
xpallet_gateway_records_rpc_runtime_api::XGatewayRecordsApi<
Block,
Expand All @@ -94,6 +96,7 @@ where
use substrate_frame_rpc_system::{FullSystem, SystemApi};
use xpallet_assets_rpc::{Assets, AssetsApi};
use xpallet_contracts_rpc::{Contracts, ContractsApi};
use xpallet_dex_spot_rpc::{XSpot, XSpotApi};
use xpallet_gateway_common_rpc::{XGatewayCommon, XGatewayCommonApi};
use xpallet_gateway_records_rpc::{XGatewayRecords, XGatewayRecordsApi};
use xpallet_mining_staking_rpc::{XStaking, XStakingApi};
Expand All @@ -116,6 +119,7 @@ where
io.extend_with(AssetsApi::to_delegate(Assets::new(client.clone())));
io.extend_with(ContractsApi::to_delegate(Contracts::new(client.clone())));
io.extend_with(XStakingApi::to_delegate(XStaking::new(client.clone())));
io.extend_with(XSpotApi::to_delegate(XSpot::new(client.clone())));
io.extend_with(XGatewayRecordsApi::to_delegate(XGatewayRecords::new(
client.clone(),
)));
Expand Down
2 changes: 2 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ xpallet-contracts = { path = "../xpallets/contracts", default-features = false }
xpallet-contracts-primitives = { path = "../xpallets/contracts/common", default-features = false }
xpallet-contracts-rpc-runtime-api = { path = "../xpallets/contracts/rpc/runtime-api", default-features = false }
xpallet-dex-spot = { path = "../xpallets/dex/spot", default-features = false }
xpallet-dex-spot-rpc-runtime-api = { path = "../xpallets/dex/spot/rpc/runtime-api", default-features = false }
xpallet-mining-staking = { path = "../xpallets/mining/staking", default-features = false }
xpallet-mining-staking-rpc-runtime-api = { path = "../xpallets/mining/staking/rpc/runtime-api", default-features = false }
xpallet-mining-asset = { path = "../xpallets/mining/asset", default-features = false }
Expand Down Expand Up @@ -151,6 +152,7 @@ std = [
"xpallet-contracts-primitives/std",
"xpallet-contracts-rpc-runtime-api/std",
"xpallet-dex-spot/std",
"xpallet-dex-spot-rpc-runtime-api/std",
"xpallet-mining-staking/std",
"xpallet-mining-staking-rpc-runtime-api/std",
"xpallet-mining-asset/std",
Expand Down
19 changes: 17 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;

use xpallet_contracts_rpc_runtime_api::ContractExecResult;
use xpallet_dex_spot::{Depth, FullPairInfo, RpcOrder, TradingPairId};
use xpallet_mining_staking::{RpcNominatorLedger, ValidatorInfo};
use xpallet_support::RpcBalance;
use xpallet_support::{RpcBalance, RpcPrice};

#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
Expand Down Expand Up @@ -350,7 +351,7 @@ type NegativeImbalance = <Balances as Currency<AccountId>>::NegativeImbalance;
pub struct DealWithFees;
impl OnUnbalanced<NegativeImbalance> for DealWithFees {
fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = NegativeImbalance>) {
if let Some(fees) = fees_then_tips.next() {
if let Some(_fees) = fees_then_tips.next() {
// for fees, 80% to treasury, 20% to author
// let mut split = fees.ration(80, 20);
// if let Some(tips) = fees_then_tips.next() {
Expand Down Expand Up @@ -1021,6 +1022,20 @@ impl_runtime_apis! {
}
}

impl xpallet_dex_spot_rpc_runtime_api::XSpotApi<Block, AccountId, Balance, BlockNumber, Balance> for Runtime {
fn trading_pairs() -> Vec<FullPairInfo<RpcPrice<Balance>, BlockNumber>> {
XSpot::trading_pairs()
}

fn orders(who: AccountId, page_index: u32, page_size: u32) -> Vec<RpcOrder<TradingPairId, AccountId, RpcBalance<Balance>, RpcPrice<Balance>, BlockNumber>> {
XSpot::orders(who, page_index, page_size)
}

fn depth(pair_id: TradingPairId, depth_size: u32) -> Option<Depth<RpcPrice<Balance>, RpcBalance<Balance>>> {
XSpot::depth(pair_id, depth_size)
}
}

impl xpallet_gateway_records_rpc_runtime_api::XGatewayRecordsApi<Block, AccountId, Balance, BlockNumber> for Runtime {
fn withdrawal_list() -> BTreeMap<u32, Withdrawal<AccountId, Balance, BlockNumber>> {
XGatewayRecords::withdrawal_list()
Expand Down
2 changes: 1 addition & 1 deletion scripts/chainx-js/chainx_types_manual.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"Handicap": {
"highest_bid": "Price",
"lowest_offer": "Price"
"lowest_ask": "Price"
},
"NetworkType": {
"_enum": [
Expand Down
4 changes: 2 additions & 2 deletions scripts/chainx-js/res/chainx_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
},
"Handicap": {
"highestBid": "Price",
"lowestOffer": "Price"
"lowestAsk": "Price"
},
"OrderProperty": {
"id": "OrderId",
Expand Down Expand Up @@ -354,4 +354,4 @@
},
"String": "Text",
"MiningPower": "u128"
}
}
22 changes: 22 additions & 0 deletions xpallets/dex/spot/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "xpallet-dex-spot-rpc"
version = "0.1.0"
authors = ["ChainX community <https://www.chainx.org>"]
edition = "2018"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.1" }
serde = { version = "1.0.10", features = ["derive"] }
sp-api = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
sp-std = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
sp-blockchain = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4" }
jsonrpc-core = "14.2.0"
jsonrpc-derive = "14.2.1"
jsonrpc-core-client = "14.2.0"
xpallet-dex-spot = { path = "../" }
xpallet-dex-spot-rpc-runtime-api = { path = "./runtime-api" }
xpallet-support = { path = "../../../support" }
27 changes: 27 additions & 0 deletions xpallets/dex/spot/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "xpallet-dex-spot-rpc-runtime-api"
version = "0.1.0"
authors = ["ChainX community <https://www.chainx.org>"]
edition = "2018"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc4", default-features = false }

xpallet-dex-spot = { path = "../..", default-features = false }
xpallet-support = { path = "../../../../support", default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"sp-api/std",
"sp-std/std",

"xpallet-dex-spot/std",
"xpallet-support/std",
]
27 changes: 27 additions & 0 deletions xpallets/dex/spot/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Runtime API definition required by ChainX RPC extensions.

#![cfg_attr(not(feature = "std"), no_std)]

use codec::Codec;
use sp_std::prelude::*;
use xpallet_dex_spot::{Depth, FullPairInfo, RpcOrder, TradingPairId};
use xpallet_support::{RpcBalance, RpcPrice};

sp_api::decl_runtime_apis! {
/// The API to query DEX Spot info.
pub trait XSpotApi<AccountId, Balance, BlockNumber, Price> where
AccountId: Codec,
Balance: Codec,
BlockNumber: Codec,
Price: Codec,
{
/// Get the overall info of all trading pairs.
fn trading_pairs() -> Vec<FullPairInfo<RpcPrice<Price>, BlockNumber>>;

/// Get the orders of an account.
fn orders(who: AccountId, page_index: u32, page_size: u32) -> Vec<RpcOrder<TradingPairId, AccountId, RpcBalance<Balance>, RpcPrice<Price>, BlockNumber>>;

/// Get the depth of a trading pair.
fn depth(pair_id: TradingPairId, depth_size: u32) -> Option<Depth<RpcPrice<Price>, RpcBalance<Balance>>>;
}
}
Loading

0 comments on commit 6fed8a7

Please sign in to comment.