mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Add Dex pallet (#407)
* Move pallet-asset-conversion * update licensing * initial integration * Integrate Currency & Assets types * integrate liquidity tokens * fmt * integrate dex pallet tests * fmt * compilation error fixes * integrate dex benchmarks * fmt * cargo clippy * replace all occurrences of "asset" with "coin" * add the actual add liq/swap logic to in-instructions * add client side & tests * fix deny * Lint and changes - Renames InInstruction::AddLiquidity to InInstruction::SwapAndAddLiquidity - Makes create_pool an internal function - Makes dex-pallet exclusively create pools against a native coin - Removes various fees - Adds new crates to GH workflow * Fix rebase artifacts * Correct other rebase artifact * Correct CI specification for liquidity-tokens * Correct primitives' test to the standardized pallet account scheme --------- Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
This commit is contained in:
55
substrate/client/tests/common/dex.rs
Normal file
55
substrate/client/tests/common/dex.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use serai_runtime::primitives::{Coin, Amount};
|
||||
|
||||
use serai_client::{Serai, SeraiDex, PairSigner};
|
||||
use sp_core::{sr25519::Pair, Pair as PairTrait};
|
||||
|
||||
use subxt::config::extrinsic_params::BaseExtrinsicParamsBuilder;
|
||||
|
||||
use crate::common::tx::publish_tx;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn add_liquidity(
|
||||
serai: &Serai,
|
||||
coin: Coin,
|
||||
coin_amount: Amount,
|
||||
sri_amount: Amount,
|
||||
nonce: u32,
|
||||
pair: Pair,
|
||||
) -> [u8; 32] {
|
||||
let address = pair.public();
|
||||
|
||||
let tx = serai
|
||||
.sign(
|
||||
&PairSigner::new(pair),
|
||||
&SeraiDex::add_liquidity(coin, coin_amount, sri_amount, Amount(1), Amount(1), address.into()),
|
||||
nonce,
|
||||
BaseExtrinsicParamsBuilder::new(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
publish_tx(serai, &tx).await
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn swap(
|
||||
serai: &Serai,
|
||||
from_coin: Coin,
|
||||
to_coin: Coin,
|
||||
amount_in: Amount,
|
||||
amount_out_min: Amount,
|
||||
nonce: u32,
|
||||
pair: Pair,
|
||||
) -> [u8; 32] {
|
||||
let address = pair.public();
|
||||
|
||||
let tx = serai
|
||||
.sign(
|
||||
&PairSigner::new(pair),
|
||||
&SeraiDex::swap(from_coin, to_coin, amount_in, amount_out_min, address.into()),
|
||||
nonce,
|
||||
BaseExtrinsicParamsBuilder::new(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
publish_tx(serai, &tx).await
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
use rand_core::{RngCore, OsRng};
|
||||
use blake2::{
|
||||
digest::{consts::U32, Digest},
|
||||
Blake2b,
|
||||
@@ -8,10 +9,10 @@ use scale::Encode;
|
||||
use sp_core::Pair;
|
||||
|
||||
use serai_client::{
|
||||
primitives::insecure_pair_from_name,
|
||||
primitives::{insecure_pair_from_name, BlockHash, NetworkId, Balance, SeraiAddress},
|
||||
validator_sets::primitives::{Session, ValidatorSet},
|
||||
in_instructions::{
|
||||
primitives::{Batch, SignedBatch, batch_message},
|
||||
primitives::{Batch, SignedBatch, batch_message, InInstruction, InInstructionWithBalance},
|
||||
InInstructionsEvent,
|
||||
},
|
||||
SeraiInInstructions, Serai,
|
||||
@@ -60,3 +61,27 @@ pub async fn provide_batch(serai: &Serai, batch: Batch) -> [u8; 32] {
|
||||
|
||||
block
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn mint_coin(
|
||||
serai: &Serai,
|
||||
balance: Balance,
|
||||
network: NetworkId,
|
||||
batch_id: u32,
|
||||
address: SeraiAddress,
|
||||
) -> [u8; 32] {
|
||||
let mut block_hash = BlockHash([0; 32]);
|
||||
OsRng.fill_bytes(&mut block_hash.0);
|
||||
|
||||
let batch = Batch {
|
||||
network,
|
||||
id: batch_id,
|
||||
block: block_hash,
|
||||
instructions: vec![InInstructionWithBalance {
|
||||
instruction: InInstruction::Transfer(address),
|
||||
balance,
|
||||
}],
|
||||
};
|
||||
|
||||
provide_batch(serai, batch).await
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
pub mod tx;
|
||||
pub mod validator_sets;
|
||||
pub mod in_instructions;
|
||||
pub mod dex;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! serai_test {
|
||||
|
||||
Reference in New Issue
Block a user