Skip to content

Commit

Permalink
wip-initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Roznovjak committed Oct 28, 2024
1 parent 9666945 commit a8b201f
Show file tree
Hide file tree
Showing 23 changed files with 1,506 additions and 32 deletions.
39 changes: 39 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 @@ -42,6 +42,7 @@ members = [
'pallets/evm-accounts',
'pallets/dynamic-evm-fee',
'pallets/xyk-liquidity-mining',
'pallets/liquidation',
'precompiles/call-permit',
'runtime-mock'
]
Expand Down Expand Up @@ -143,6 +144,7 @@ pallet-xyk-liquidity-mining = { path = "pallets/xyk-liquidity-mining", default-f
pallet-referrals = { path = "pallets/referrals", default-features = false }
pallet-evm-accounts = { path = "pallets/evm-accounts", default-features = false }
pallet-evm-accounts-rpc-runtime-api = { path = "pallets/evm-accounts/rpc/runtime-api", default-features = false }
pallet-liquidation = { path = "pallets/liquidation", default-features = false }

hydra-dx-build-script-utils = { path = "utils/build-script-utils", default-features = false }
scraper = { path = "scraper", default-features = false }
Expand Down
6 changes: 4 additions & 2 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ pallet-staking = { workspace = true }
pallet-lbp = { workspace = true }
pallet-xyk = { workspace = true }
pallet-evm-accounts = { workspace = true }
pallet-xyk-liquidity-mining = { workspace = true }
pallet-transaction-pause = { workspace = true }
pallet-liquidation = { workspace = true }

pallet-treasury = { workspace = true }
pallet-democracy = { workspace = true }
pallet-scheduler = { workspace = true }
pallet-elections-phragmen = { workspace = true }
pallet-tips = { workspace = true }
pallet-xyk-liquidity-mining = { workspace = true }
pallet-transaction-pause = { workspace = true }

# collator support
pallet-collator-selection = { workspace = true }
Expand Down Expand Up @@ -217,6 +218,7 @@ std = [
"pallet-dynamic-evm-fee/std",
"precompile-utils/std",
"pallet-transaction-pause/std",
"pallet-liquidation/std",
]

# we don't include integration tests when benchmarking feature is enabled
Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub mod utils;
mod vesting;
mod xyk;
mod xyk_liquidity_mining;
mod liquidation;

#[macro_export]
macro_rules! assert_balance {
Expand Down
52 changes: 52 additions & 0 deletions integration-tests/src/liquidation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#![cfg(test)]

use ethabi::ethereum_types::H160;
use fp_evm::ExitSucceed;
use crate::polkadot_test_net::*;

use frame_support::{
assert_noop, assert_ok,
sp_runtime::RuntimeDebug,
};
use hex_literal::hex;
use orml_traits::currency::MultiCurrency;
use orml_traits::MultiCurrencyExtended;
use sp_runtime::FixedPointNumber;
use sp_runtime::{FixedU128, Permill};
use xcm_emulator::TestExt;
use hydradx_traits::evm::EvmAddress;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use fp_evm::ExitReason::Succeed;
use fp_evm::ExitSucceed::Returned;
use hydradx_traits::evm::{CallContext, EVM};

const PATH_TO_SNAPSHOT: &str = "evm-snapshot/SNAPSHOT";

#[module_evm_utility_macro::generate_function_selector]
#[derive(RuntimeDebug, Eq, PartialEq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum Function {
GetPool = "getPool",
}

#[test]
fn liquidation() {
TestNet::reset();
hydra_live_ext(PATH_TO_SNAPSHOT).execute_with(|| {
// let mut storage = pallet_evm::AccountCodes::<hydradx_runtime::Runtime>::iter();
// for i in storage {
// println!("------ {:?}", i.0);
//
// }

let contract = EvmAddress::from_slice(hex!("82db570265c37bE24caf5bc943428a6848c3e9a6").as_slice());

let data = Into::<u32>::into(Function::GetPool).to_be_bytes().to_vec();
let context = CallContext::new_view(contract);

let (res, value) = hydradx_runtime::evm::Executor::<hydradx_runtime::Runtime>::view(context, data, 500_000);

// assert_eq!(res, Succeed(Returned));
println!("---- {:?}", value);
});
}
86 changes: 86 additions & 0 deletions pallets/liquidation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
[package]
name = 'pallet-liquidation'
version = '1.0.0'
description = 'A pallet for money market liquidations'
authors = ['GalacticCouncil']
edition = '2021'
license = 'Apache 2.0'
repository = "https://github.com/galacticcouncil/Hydradx-node"

[dependencies]
# parity
codec = { workspace = true, features = ["derive", "max-encoded-len"] }
scale-info = { workspace = true }
log = { workspace = true }

# primitives
sp-runtime = { workspace = true }
sp-std = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-arithmetic = { workspace = true }

# FRAME
frame-support = { workspace = true }
frame-system = { workspace = true }

evm = { workspace = true, features = ["with-codec"] }
module-evm-utility-macro = { workspace = true }
num_enum = { workspace = true, default-features = false }
ethabi = { workspace = true }

# HydraDX dependencies
hydradx-traits = { workspace = true }

# Optional imports for benchmarking
frame-benchmarking = { workspace = true, optional = true }

[dev-dependencies]
hydra-dx-math = { workspace = true }
pallet-omnipool = { workspace = true }
pallet-asset-registry = { workspace = true }
pallet-route-executor = { workspace = true }
pallet-balances = { workspace = true }
pallet-currencies = { workspace = true }
pallet-evm-accounts = { workspace = true }
sp-api = { workspace = true }
orml-traits = { workspace = true }
orml-tokens = { workspace = true, features = ["std"] }
hex-literal = { workspace = true }
proptest = { workspace = true }
pretty_assertions = { workspace = true }
test-utils = { workspace = true }
parking_lot = { workspace = true }

[features]
default = ['std']
std = [
'codec/std',
'frame-support/std',
'frame-system/std',
'sp-runtime/std',
'sp-core/std',
'sp-io/std',
'sp-std/std',
'sp-arithmetic/std',
'sp-api/std',
'scale-info/std',
'orml-tokens/std',
'orml-traits/std',
'hydradx-traits/std',
'hydra-dx-math/std',
'frame-benchmarking/std',
'pallet-balances/std',
'pallet-currencies/std',
'pallet-route-executor/std',
'pallet-omnipool/std',
'pallet-asset-registry/std',
'pallet-evm-accounts/std',
]

runtime-benchmarks = [
"frame-benchmarking",
"frame-system/runtime-benchmarks",
"frame-support/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
6 changes: 6 additions & 0 deletions pallets/liquidation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Pallet (Money market) Liquidation
## Description

## Notes

## Dispatachable functions
56 changes: 56 additions & 0 deletions pallets/liquidation/src/benchmarks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (C) 2020-2023 Intergalactic, Limited (GIB). 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.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::{account, benchmarks};
use frame_support::assert_ok;
use frame_support::traits::fungibles::Mutate;
use frame_system::RawOrigin;

pub const ONE: Balance = 1_000_000_000_000;
pub const HDX: u32 = 0;
pub const DAI: u32 = 2;

benchmarks! {
where_clause { where
AssetIdOf<T>: From<u32>,
<T as crate::Config>::Currency: Mutate<T::AccountId, AssetId = AssetIdOf<T>, Balance = Balance>,
T: crate::Config + pallet_otc::Config,
}
settle_otc_order {
let account: T::AccountId = account("acc", 1, 1);

<T as crate::Config>::Currency::mint_into(HDX.into(), &account, 1_000_000_000 * ONE)?;
<T as crate::Config>::Currency::mint_into(DAI.into(), &account, 1_000_000_000 * ONE)?;

assert_ok!(
pallet_otc::Pallet::<T>::place_order(RawOrigin::Signed(account).into(), HDX.into(), DAI.into(), 100_000_000 * ONE, 202_020_001 * ONE, true)
);

let route = <T as crate::Config>::Router::get_route(AssetPair {
asset_in: DAI.into(),
asset_out: HDX.into(),
});

}: _(RawOrigin::None, 0u32, 2 * ONE, route)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::mock::*;
use frame_benchmarking::impl_benchmark_test_suite;

impl_benchmark_test_suite!(Pallet, super::ExtBuilder::default().build().0, super::Test);
}
Loading

0 comments on commit a8b201f

Please sign in to comment.