Skip to content

Commit

Permalink
feat: upgrade to polkadot-v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
José Molina committed Aug 18, 2023
1 parent 3d0dfc1 commit c5a6c1b
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 75 deletions.
22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ repository = "https://github.com/paritytech/substrate-dex"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
parity-scale-codec = { version = "3.6.4", default-features = false, features = ["derive"] }

frame-benchmarking = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42", optional = true }
frame-support = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
frame-system = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
frame-benchmarking = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0", optional = true }
frame-support = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-system = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

pallet-assets = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
pallet-assets = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
sp-std = { default-features = false, version = "5.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-runtime = { default-features = false, version = "24.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-std = { default-features = false, version = "8.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

[dev-dependencies]
pallet-balances = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-core = { default-features = false, version = "7.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-io = { default-features = false, version = "7.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-runtime = { default-features = false, version = "7.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
pallet-balances = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-core = { default-features = false, version = "21.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-io = { default-features = false, version = "23.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

[features]
default = ["std"]
std = [
"codec/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-std/std",
"frame-benchmarking/std",
Expand Down
12 changes: 6 additions & 6 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ repository = "https://github.com/paritytech/substrate-dex"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
jsonrpsee = { version = "0.16.2", features = ["server", "macros"] }
parity-scale-codec = { version = "3.6.4", default-features = false, features = ["derive"] }

pallet-dex-rpc-runtime-api = { version = "0.0.1", path = "./runtime-api" }

sp-api = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-blockchain = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-runtime = { default-features = false, version = "7.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-api = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-blockchain = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-runtime = { default-features = false, version = "24.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

[dev-dependencies]
pallet-dex = { version = "0.0.1", path = ".." }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
substrate-test-runtime-client = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
substrate-test-runtime-client = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
tokio = { version = "1.21.2", features = ["macros", "time", "parking_lot"] }
8 changes: 4 additions & 4 deletions rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ repository = "https://github.com/paritytech/substrate-dex"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
parity-scale-codec = { version = "3.6.4", default-features = false, features = ["derive"] }

pallet-dex = { version = "0.0.1", default-features = false, path = "../.." }
sp-api = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-runtime = { default-features = false, version = "7.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-api = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-runtime = { default-features = false, version = "24.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

[features]
default = ["std"]
std = [
"codec/std",
"parity-scale-codec/std",
"pallet-dex/std",
"sp-api/std",
"sp-runtime/std",
Expand Down
2 changes: 1 addition & 1 deletion rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::Codec;
pub use pallet_dex::rpc::{RpcError, RpcResult};
use parity_scale_codec::Codec;
use sp_runtime::traits::MaybeDisplay;

sp_api::decl_runtime_apis! {
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use codec::Codec;
use jsonrpsee::{
core::{async_trait, Error as RpcError, RpcResult},
proc_macros::rpc,
types::error::{CallError, ErrorObject},
};
use parity_scale_codec::Codec;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::MaybeDisplay;
Expand Down
8 changes: 5 additions & 3 deletions src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::{AccountIdOf, Call, Config, Pallet, TradeAmount};
use frame_benchmarking::{benchmarks, whitelisted_caller};
use frame_support::pallet_prelude::DispatchResult;
use frame_support::traits::{
fungibles::{Create, Mutate},
Currency,
};
use frame_system::mocking::MockBlock;
use frame_system::RawOrigin;

use crate::{AccountIdOf, Call, Config, Pallet, TradeAmount};

const INIT_BALANCE: u128 = 1_000_000_000_000_000;
const INIT_LIQUIDITY: u128 = 1_000_000_000_000;
const ASSET_A: u32 = 1;
Expand All @@ -16,7 +18,7 @@ const LIQ_TOKEN_B: u32 = 12;

fn prepare_exchange<T>(asset_id: u32, liquidity_token_id: u32) -> DispatchResult
where
T: frame_system::Config<BlockNumber = u32>,
T: frame_system::Config<Block = MockBlock<T>>,
T: Config<AssetId = u32, AssetBalance = u128>,
T::Currency: Currency<AccountIdOf<T>, Balance = u128>,
T::Assets: Create<AccountIdOf<T>> + Mutate<AccountIdOf<T>>,
Expand All @@ -38,7 +40,7 @@ where
benchmarks! {
where_clause {
where
T: frame_system::Config<BlockNumber = u32>,
T: frame_system::Config<Block = MockBlock<T>>,
T: Config<AssetId = u32, AssetBalance = u128>,
T::Currency: Currency<AccountIdOf<T>, Balance = u128>,
T::Assets: Create<AccountIdOf<T>> + Mutate<AccountIdOf<T>>,
Expand Down
48 changes: 24 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ use sp_std::prelude::*;
pub use pallet::*;
pub use weights::WeightInfo;

use sp_runtime::{
traits::{
AccountIdConversion, CheckedAdd, CheckedMul, CheckedSub, Convert, One, Saturating, Zero,
},
FixedPointNumber, FixedPointOperand, FixedU128,
};

type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
type BalanceOf<T> = <<T as Config>::Currency as Currency<AccountIdOf<T>>>::Balance;
type AssetIdOf<T> = <T as Config>::AssetId;
Expand All @@ -35,16 +42,10 @@ type AssetBalanceOf<T> = <T as Config>::AssetBalance;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use codec::EncodeLike;
use parity_scale_codec::EncodeLike;

use frame_support::{
pallet_prelude::*,
sp_runtime::{
traits::{
AccountIdConversion, CheckedAdd, CheckedMul, CheckedSub, Convert, One, Saturating,
Zero,
},
FixedPointNumber, FixedPointOperand, FixedU128,
},
traits::{
fungibles::{Create, Destroy, Inspect, Mutate},
tokens::{Balance, Fortitude, Precision, Preservation, WithdrawConsequence},
Expand Down Expand Up @@ -156,15 +157,14 @@ pub mod pallet {
pub exchanges: Vec<GenesisExchangeInfo<T>>,
}

#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> GenesisConfig<T> {
GenesisConfig { exchanges: vec![] }
}
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
let pallet_account = T::pallet_account();
for (provider, asset_id, liquidity_token_id, currency_amount, token_amount) in
Expand Down Expand Up @@ -221,7 +221,7 @@ pub mod pallet {
T::AssetRegistry::mint_into(
liquidity_token_id.clone(),
provider,
liquidity_minted
liquidity_minted,
)
.is_ok(),
"Unexpected error while minting liquidity tokens for Provider"
Expand All @@ -238,7 +238,7 @@ pub mod pallet {
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
pub enum Event<T: Config> {
/// A new exchange was created [asset_id, liquidity_token_id]
ExchangeCreated(AssetIdOf<T>, AssetIdOf<T>),
Expand Down Expand Up @@ -390,7 +390,7 @@ pub mod pallet {
/// * `CurrencyAmountTooLow` – Specified `currency_amount` is lower than `MinDeposit`.
/// * `TokenAmountIsZero` – Specified `token_amount` equals 0.
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::create_exchange())]
#[pallet::weight(< T as Config >::WeightInfo::create_exchange())]
#[transactional]
pub fn create_exchange(
origin: OriginFor<T>,
Expand Down Expand Up @@ -465,14 +465,14 @@ pub mod pallet {
/// * `MinLiquidityTooHigh` – The amount of liquidity tokes which would be minted by depositing the specified
/// `currency_amount` is lower than the specified `min_liquidity`.
#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::add_liquidity())]
#[pallet::weight(< T as Config >::WeightInfo::add_liquidity())]
pub fn add_liquidity(
origin: OriginFor<T>,
asset_id: AssetIdOf<T>,
currency_amount: BalanceOf<T>,
min_liquidity: AssetBalanceOf<T>,
max_tokens: AssetBalanceOf<T>,
deadline: T::BlockNumber,
deadline: BlockNumberFor<T>,
) -> DispatchResult {
// -------------------------- Validation part --------------------------
let caller = ensure_signed(origin)?;
Expand Down Expand Up @@ -534,14 +534,14 @@ pub mod pallet {
/// * `MinTokensTooHigh` – The amount of tokens which could be received in exchange for the specified
/// `liquidity_amount` is lower than the specified `min_tokens`.
#[pallet::call_index(2)]
#[pallet::weight(<T as Config>::WeightInfo::remove_liquidity())]
#[pallet::weight(< T as Config >::WeightInfo::remove_liquidity())]
pub fn remove_liquidity(
origin: OriginFor<T>,
asset_id: AssetIdOf<T>,
liquidity_amount: AssetBalanceOf<T>,
min_currency: BalanceOf<T>,
min_tokens: AssetBalanceOf<T>,
deadline: T::BlockNumber,
deadline: BlockNumberFor<T>,
) -> DispatchResult {
// -------------------------- Validation part --------------------------
let caller = ensure_signed(origin)?;
Expand Down Expand Up @@ -601,12 +601,12 @@ pub mod pallet {
/// * `BalanceTooLow` – The available currency balance of the caller account is not enough to perform the trade.
/// * `Overflow` – An overflow occurred during price computation.
#[pallet::call_index(3)]
#[pallet::weight(<T as Config>::WeightInfo::currency_to_asset())]
#[pallet::weight(< T as Config >::WeightInfo::currency_to_asset())]
pub fn currency_to_asset(
origin: OriginFor<T>,
asset_id: AssetIdOf<T>,
amount: TradeAmount<BalanceOf<T>, AssetBalanceOf<T>>,
deadline: T::BlockNumber,
deadline: BlockNumberFor<T>,
recipient: Option<AccountIdOf<T>>,
) -> DispatchResult {
// -------------------------- Validation part --------------------------
Expand Down Expand Up @@ -657,12 +657,12 @@ pub mod pallet {
/// * `NotEnoughTokens` – The available asset balance of the caller account is not enough to perform the trade.
/// * `Overflow` – An overflow occurred during price computation.
#[pallet::call_index(4)]
#[pallet::weight(<T as Config>::WeightInfo::asset_to_currency())]
#[pallet::weight(< T as Config >::WeightInfo::asset_to_currency())]
pub fn asset_to_currency(
origin: OriginFor<T>,
asset_id: AssetIdOf<T>,
amount: TradeAmount<AssetBalanceOf<T>, BalanceOf<T>>,
deadline: T::BlockNumber,
deadline: BlockNumberFor<T>,
recipient: Option<AccountIdOf<T>>,
) -> DispatchResult {
// -------------------------- Validation part --------------------------
Expand Down Expand Up @@ -712,13 +712,13 @@ pub mod pallet {
/// * `NotEnoughTokens` – The available sold asset balance of the caller account is not enough to perform the trade.
/// * `Overflow` – An overflow occurred during price computation.
#[pallet::call_index(5)]
#[pallet::weight(<T as Config>::WeightInfo::asset_to_asset())]
#[pallet::weight(< T as Config >::WeightInfo::asset_to_asset())]
pub fn asset_to_asset(
origin: OriginFor<T>,
sold_asset_id: AssetIdOf<T>,
bought_asset_id: AssetIdOf<T>,
amount: TradeAmount<AssetBalanceOf<T>, AssetBalanceOf<T>>,
deadline: T::BlockNumber,
deadline: BlockNumberFor<T>,
recipient: Option<AccountIdOf<T>>,
) -> DispatchResult {
// -------------------------- Validation part --------------------------
Expand Down Expand Up @@ -756,7 +756,7 @@ pub mod pallet {
<Exchanges<T>>::get(asset_id.clone()).ok_or(Error::<T>::ExchangeNotFound)
}

fn check_deadline(deadline: &T::BlockNumber) -> Result<(), Error<T>> {
fn check_deadline(deadline: &BlockNumberFor<T>) -> Result<(), Error<T>> {
ensure!(deadline >= &<frame_system::Pallet<T>>::block_number(), Error::DeadlinePassed);
Ok(())
}
Expand Down
Loading

0 comments on commit c5a6c1b

Please sign in to comment.