referrerpolicy=no-referrer-when-downgrade

pallet_asset_conversion/
liquidity.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Trait for providing methods to mutate liquidity pools.
19
20use frame_support::{traits::tokens::Balance, transactional};
21use sp_runtime::DispatchError;
22
23use crate::{Config, Pallet};
24
25/// A struct to represent an asset and its desired and minimum amounts for adding liquidity.
26pub struct AddLiquidityAsset<AssetKind, Balance> {
27	/// The kind of asset.
28	pub asset: AssetKind,
29	/// The desired amount of the asset to add.
30	pub amount_desired: Balance,
31	/// The minimum amount of the asset to add.
32	pub amount_min: Balance,
33}
34
35/// Trait for providing methods to mutate liquidity pools. This includes creating pools,
36/// adding liquidity, and removing liquidity.
37pub trait MutateLiquidity<AccountId> {
38	/// The balance type for assets.
39	type Balance: Balance;
40	/// The type used to identify assets.
41	type AssetKind;
42	/// The type used to identify a liquidity pool.
43	type PoolId;
44
45	/// Creates a new liquidity pool for the given assets.
46	///
47	/// Mints LP tokens to the `creator` account.
48	///
49	/// Returns the ID of the newly created pool.
50	fn create_pool(
51		creator: &AccountId,
52		asset1: Self::AssetKind,
53		asset2: Self::AssetKind,
54	) -> Result<Self::PoolId, DispatchError>;
55
56	/// Adds liquidity to an existing pool.
57	///
58	/// Mints LP tokens to the `mint_to` account.
59	///
60	/// Returns the amount of LP tokens minted.
61	fn add_liquidity(
62		who: &AccountId,
63		asset1: AddLiquidityAsset<Self::AssetKind, Self::Balance>,
64		asset2: AddLiquidityAsset<Self::AssetKind, Self::Balance>,
65		mint_to: &AccountId,
66	) -> Result<Self::Balance, DispatchError>;
67
68	/// Removes liquidity from a pool.
69	///
70	/// Burns LP tokens from the `who` account and transfers the withdrawn assets to the
71	/// `withdraw_to` account.
72	///
73	/// Returns the amounts of assets withdrawn.
74	fn remove_liquidity(
75		who: &AccountId,
76		asset1: Self::AssetKind,
77		asset2: Self::AssetKind,
78		lp_token_burn: Self::Balance,
79		amount1_min_receive: Self::Balance,
80		amount2_min_receive: Self::Balance,
81		withdraw_to: &AccountId,
82	) -> Result<(Self::Balance, Self::Balance), DispatchError>;
83}
84
85impl<T: Config> MutateLiquidity<T::AccountId> for Pallet<T> {
86	type Balance = T::Balance;
87	type AssetKind = T::AssetKind;
88	type PoolId = T::PoolId;
89
90	#[transactional]
91	fn create_pool(
92		creator: &T::AccountId,
93		asset1: T::AssetKind,
94		asset2: T::AssetKind,
95	) -> Result<T::PoolId, DispatchError> {
96		Self::do_create_pool(creator, asset1, asset2)
97	}
98
99	#[transactional]
100	fn add_liquidity(
101		who: &T::AccountId,
102		asset1: AddLiquidityAsset<Self::AssetKind, Self::Balance>,
103		asset2: AddLiquidityAsset<Self::AssetKind, Self::Balance>,
104		mint_to: &T::AccountId,
105	) -> Result<T::Balance, DispatchError> {
106		Self::do_add_liquidity(
107			who,
108			asset1.asset,
109			asset2.asset,
110			asset1.amount_desired,
111			asset2.amount_desired,
112			asset1.amount_min,
113			asset2.amount_min,
114			mint_to,
115		)
116	}
117
118	#[transactional]
119	fn remove_liquidity(
120		who: &T::AccountId,
121		asset1: T::AssetKind,
122		asset2: T::AssetKind,
123		lp_token_burn: T::Balance,
124		amount1_min_receive: T::Balance,
125		amount2_min_receive: T::Balance,
126		withdraw_to: &T::AccountId,
127	) -> Result<(T::Balance, T::Balance), DispatchError> {
128		Self::do_remove_liquidity(
129			who,
130			asset1,
131			asset2,
132			lp_token_burn,
133			amount1_min_receive,
134			amount2_min_receive,
135			withdraw_to,
136		)
137	}
138}