Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: OTC offchain worker #803

Merged
merged 76 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
7df0e79
Merge branch 'feat/route-spot-price-provider' into otc_offchain_worker
Roznovjak Apr 11, 2024
935223d
Merge branch 'feat/route-spot-price-provider' into otc_offchain_worker
Roznovjak Apr 12, 2024
2864fff
implement offchain worker for OTC trades
Roznovjak Apr 15, 2024
1c21549
rebenchmark AMMs
Roznovjak Apr 15, 2024
69eec15
remove params from benchmarks
Roznovjak Apr 15, 2024
8946299
rebenchmark AMMs
Roznovjak Apr 15, 2024
391b3d3
rebenchmark router
Roznovjak Apr 15, 2024
2ecb60d
rebenchmark dca
Roznovjak Apr 15, 2024
c587394
fix benchmark test
Roznovjak Apr 15, 2024
2525c15
fix benchmarks
Roznovjak Apr 16, 2024
28041e4
rebenchmark otc settlements pallet
Roznovjak Apr 16, 2024
a47a864
update comments
Roznovjak Apr 16, 2024
8b66606
bump crate version
Roznovjak Apr 16, 2024
5b949d0
formatting
Roznovjak Apr 16, 2024
80f6ae3
Merge branch 'feat/route-spot-price-provider' into otc_offchain_worker
Roznovjak Apr 16, 2024
a10ef80
satisfy clippy
Roznovjak Apr 17, 2024
a1408dc
Merge branch 'master' into otc_offchain_worker
Roznovjak Apr 17, 2024
125b14c
update crate versions and remove unused param
Roznovjak Apr 17, 2024
0cb3695
satisfy clippy
Roznovjak Apr 17, 2024
03ccd18
remove unused code from benchmarks
Roznovjak Apr 17, 2024
1722038
include pov size in weight calculation
Roznovjak Apr 17, 2024
1b6af34
update comments
Roznovjak Apr 17, 2024
f34c26e
update comments
Roznovjak Apr 17, 2024
58088dc
satisfy clippy
Roznovjak Apr 17, 2024
d1559d7
Merge branch 'master' into otc_offchain_worker
Roznovjak Apr 19, 2024
979f816
resolve comments
Roznovjak Apr 25, 2024
883756a
Merge branch 'feat/route-spot-price-provider' into otc_offchain_worker
Roznovjak Apr 25, 2024
4619264
bump crate versions
Roznovjak Apr 25, 2024
29e7b95
Merge branch 'master' into otc_offchain_worker
Roznovjak May 9, 2024
b913f29
fix build
Roznovjak May 9, 2024
4d0ae34
remove origin check
Roznovjak May 13, 2024
5db265d
Merge branch 'master' into otc_offchain_worker
Roznovjak May 13, 2024
6e333fb
add MinTradingLimit
Roznovjak May 14, 2024
495c306
fixed unsafe math
dmoka May 14, 2024
a3a85dd
bump math version
dmoka May 14, 2024
adff992
fix bug as checked math returns a new instance
dmoka May 14, 2024
8f6d4eb
add readme file
Roznovjak May 14, 2024
b6431fe
move MaxIterations to the config
Roznovjak May 14, 2024
d8381d8
bump crate versions
Roznovjak May 14, 2024
fa0366d
Update pallets/otc-settlements/README.md
Roznovjak May 15, 2024
11a36c2
use otc order percentage to calculate min profit
Roznovjak May 15, 2024
e5f5659
satisfy clippy
Roznovjak May 15, 2024
3622848
bump crate version
Roznovjak May 15, 2024
ff3295c
Merge branch 'master' into otc_offchain_worker
Roznovjak May 24, 2024
fc26bdc
bump crate versions
Roznovjak May 24, 2024
b31d6bc
Merge branch 'master' into otc_offchain_worker
Roznovjak May 27, 2024
26c96c3
bump crate versions
Roznovjak May 27, 2024
9c8664c
Merge branch 'master' into otc_offchain_worker
Roznovjak May 27, 2024
46730d4
bump crate versions
Roznovjak May 27, 2024
bebcbde
update comments
Roznovjak May 28, 2024
ddaaa14
Merge branch 'master' into otc_offchain_worker
Roznovjak May 28, 2024
52516f7
resolve comments from the review
Roznovjak May 29, 2024
e64930c
bump crate version
Roznovjak May 29, 2024
b5254e8
formatting
Roznovjak May 29, 2024
73d67cc
fix benchmark build
Roznovjak May 29, 2024
8b166cc
rebenchmarking
Roznovjak May 29, 2024
4eb145a
Merge branch 'master' into otc_offchain_worker
Roznovjak May 29, 2024
2b614ba
bump runtime version
Roznovjak May 29, 2024
732430f
Merge branch 'master' into otc_offchain_worker
Roznovjak Jun 9, 2024
f89f4c4
subtract the initial balance from the profit
Roznovjak Jun 14, 2024
968ffae
update test
Roznovjak Jun 14, 2024
8026554
Merge branch 'master' into otc_offchain_worker
Roznovjak Jun 19, 2024
dedcd16
Update pallets/otc-settlements/src/lib.rs
Roznovjak Jun 19, 2024
ce1e3b4
resolve comments from the review
Roznovjak Jun 19, 2024
69e567b
rebenchmark lbp, omnipool, stableswap and xyk
Roznovjak Jun 20, 2024
04e23f4
fix lbp weights
Roznovjak Jun 20, 2024
d69120c
fix lbp weights
Roznovjak Jun 20, 2024
6dcc06d
rebenchmark router
Roznovjak Jun 20, 2024
800a68d
fix router weights
Roznovjak Jun 20, 2024
1bce5dc
fix incorrect weight function name
Roznovjak Jun 20, 2024
19ae17b
rebenchmark dca and otc-settlements
Roznovjak Jun 20, 2024
080cdf4
formatting
Roznovjak Jun 20, 2024
99c709d
bump crate versions
Roznovjak Jun 20, 2024
e879ff5
fix integration tests
Roznovjak Jun 20, 2024
4008c98
fix: use correct otc value
Roznovjak Jun 20, 2024
6b381a4
add comment
Roznovjak Jun 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 60 additions & 9 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ members = [
'pallets/dynamic-fees',
'pallets/duster',
'pallets/otc',
'pallets/otc-settlements',
'pallets/bonds',
'pallets/lbp',
'pallets/nft',
Expand Down Expand Up @@ -72,6 +73,7 @@ pallet-nft = { path = "pallets/nft", default-features = false }
pallet-omnipool = { path = "pallets/omnipool", default-features = false }
pallet-omnipool-liquidity-mining = { path = "pallets/omnipool-liquidity-mining", default-features = false }
pallet-otc = { path = "pallets/otc", default-features = false}
pallet-otc-settlements = { path = "pallets/otc-settlements", default-features = false}
pallet-relaychain-info = { path = "pallets/relaychain-info", default-features = false }
pallet-route-executor = { path = "pallets/route-executor", default-features = false }
pallet-stableswap = { path = "pallets/stableswap", default-features = false }
Expand Down Expand Up @@ -173,6 +175,8 @@ pallet-proxy = { git = "https://github.com/paritytech/polkadot-sdk", rev = "c8d2
pallet-scheduler = { git = "https://github.com/paritytech/polkadot-sdk", rev = "c8d2251cafadc108ba2f1f8a3208dc547ff38901", default-features = false }
pallet-session = { git = "https://github.com/paritytech/polkadot-sdk", rev = "c8d2251cafadc108ba2f1f8a3208dc547ff38901", default-features = false }
pallet-sudo = { git = "https://github.com/paritytech/polkadot-sdk", rev = "c8d2251cafadc108ba2f1f8a3208dc547ff38901", default-features = false }
pallet-state-trie-migration = { git = "https://github.com/paritytech/polkadot-sdk", rev = "c8d2251cafadc108ba2f1f8a3208dc547ff38901", default-features = false }
substrate-state-trie-migration-rpc = { git = "https://github.com/paritytech/polkadot-sdk", rev = "c8d2251cafadc108ba2f1f8a3208dc547ff38901" }
pallet-timestamp = { git = "https://github.com/paritytech/polkadot-sdk", rev = "c8d2251cafadc108ba2f1f8a3208dc547ff38901", default-features = false }
pallet-tips = { git = "https://github.com/paritytech/polkadot-sdk", rev = "c8d2251cafadc108ba2f1f8a3208dc547ff38901", default-features = false }
pallet-transaction-payment = { git = "https://github.com/paritytech/polkadot-sdk", rev = "c8d2251cafadc108ba2f1f8a3208dc547ff38901", default-features = false }
Expand Down Expand Up @@ -362,6 +366,8 @@ pallet-preimage = { git = "https://github.com/galacticcouncil/polkadot-sdk", rev
pallet-proxy = { git = "https://github.com/galacticcouncil/polkadot-sdk", rev = "062d92eae0f3bb9908faf2d4e241eef17368b9d3" }
pallet-scheduler = { git = "https://github.com/galacticcouncil/polkadot-sdk", rev = "062d92eae0f3bb9908faf2d4e241eef17368b9d3" }
pallet-session = { git = "https://github.com/galacticcouncil/polkadot-sdk", rev = "062d92eae0f3bb9908faf2d4e241eef17368b9d3" }
pallet-state-trie-migration = { git = "https://github.com/galacticcouncil/polkadot-sdk", rev = "062d92eae0f3bb9908faf2d4e241eef17368b9d3" }
substrate-state-trie-migration-rpc = { git = "https://github.com/galacticcouncil/polkadot-sdk", rev = "062d92eae0f3bb9908faf2d4e241eef17368b9d3" }
pallet-sudo = { git = "https://github.com/galacticcouncil/polkadot-sdk", rev = "062d92eae0f3bb9908faf2d4e241eef17368b9d3" }
pallet-timestamp = { git = "https://github.com/galacticcouncil/polkadot-sdk", rev = "062d92eae0f3bb9908faf2d4e241eef17368b9d3" }
pallet-tips = { git = "https://github.com/galacticcouncil/polkadot-sdk", rev = "062d92eae0f3bb9908faf2d4e241eef17368b9d3" }
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "runtime-integration-tests"
version = "1.20.2"
version = "1.20.3"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
128 changes: 127 additions & 1 deletion integration-tests/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
use crate::{assert_balance, polkadot_test_net::*};
use fp_evm::{Context, Transfer};
use fp_rpc::runtime_decl_for_ethereum_runtime_rpc_api::EthereumRuntimeRPCApi;
use frame_support::storage::with_transaction;
use frame_support::{assert_ok, dispatch::GetDispatchInfo, sp_runtime::codec::Encode, traits::Contains};
use frame_system::RawOrigin;
use hex_literal::hex;
use hydradx_runtime::evm::ExtendedAddressMapping;
use hydradx_runtime::XYK;
use hydradx_runtime::{
evm::precompiles::{
addr,
Expand All @@ -17,12 +19,15 @@ use hydradx_runtime::{
AssetRegistry, Balances, CallFilter, Currencies, EVMAccounts, Omnipool, RuntimeCall, RuntimeOrigin, Tokens,
TransactionPause, EVM,
};
use hydradx_traits::AssetKind;
use hydradx_traits::Create;
use orml_traits::MultiCurrency;
use pallet_evm::*;
use pretty_assertions::assert_eq;
use primitives::{AssetId, Balance};
use sp_core::{blake2_256, H160, H256, U256};
use sp_runtime::{traits::SignedExtension, FixedU128, Permill};
use sp_runtime::TransactionOutcome;
use sp_runtime::{traits::SignedExtension, DispatchError, FixedU128, Permill};
use std::borrow::Cow;
use xcm_emulator::TestExt;

Expand Down Expand Up @@ -1119,6 +1124,102 @@ fn dispatch_should_work_with_transfer() {
});
}

#[test]
fn dispatch_should_work_with_buying_insufficient_asset() {
TestNet::reset();

Hydra::execute_with(|| {
//Set up to idle state where the chain is not utilized at all
pallet_transaction_payment::pallet::NextFeeMultiplier::<hydradx_runtime::Runtime>::put(
hydradx_runtime::MinimumMultiplier::get(),
);
assert_ok!(EVMAccounts::bind_evm_address(hydradx_runtime::RuntimeOrigin::signed(
ALICE.into()
)));

//Create inssufficient asset
let shitcoin = with_transaction::<u32, DispatchError, _>(|| {
let name = b"SHITCO".to_vec();
let shitcoin = AssetRegistry::register_insufficient_asset(
None,
Some(name.try_into().unwrap()),
AssetKind::External,
Some(1_000),
None,
None,
None,
None,
)
.unwrap();

TransactionOutcome::Commit(Ok(shitcoin))
})
.unwrap();

create_xyk_pool_with_amounts(shitcoin, 1000000 * UNITS, HDX, 1000000 * UNITS);
init_omnipool_with_oracle_for_block_10();

assert_ok!(hydradx_runtime::Currencies::update_balance(
hydradx_runtime::RuntimeOrigin::root(),
currency_precompile::alice_substrate_evm_addr(),
WETH,
(100 * UNITS * 1_000_000) as i128,
));
assert_ok!(hydradx_runtime::MultiTransactionPayment::set_currency(
hydradx_runtime::RuntimeOrigin::signed(ALICE.into()),
WETH,
));

let swap_route = vec![
Trade {
pool: PoolType::Omnipool,
asset_in: WETH,
asset_out: HDX,
},
Trade {
pool: PoolType::XYK,
asset_in: HDX,
asset_out: shitcoin,
},
];
let router_swap = RuntimeCall::Router(pallet_route_executor::Call::buy {
asset_in: WETH,
asset_out: shitcoin,
amount_out: UNITS,
max_amount_in: u128::MAX,
route: swap_route,
});

//Arrange
let data = router_swap.encode();
let (gas_price, _) = hydradx_runtime::DynamicEvmFee::min_gas_price();

hydradx_finalize_block(); //We do this to simulate that we don't have any prices in multi-payment-pallet, but the prices can be still calculated based on onchain route

let init_balance = Tokens::free_balance(shitcoin, &currency_precompile::alice_substrate_evm_addr());
assert_eq!(init_balance, 0);

// Act
assert_ok!(EVM::call(
evm_signed_origin(currency_precompile::alice_evm_addr()),
currency_precompile::alice_evm_addr(),
DISPATCH_ADDR,
data,
U256::from(0),
1000000,
gas_price * 10,
None,
Some(U256::zero()),
[].into()
));

//EVM call passes even when the substrate tx fails, so we need to check if the tx is executed
expect_hydra_events(vec![pallet_evm::Event::Executed { address: DISPATCH_ADDR }.into()]);
let new_balance = Tokens::free_balance(shitcoin, &currency_precompile::alice_substrate_evm_addr());
assert_eq!(new_balance, UNITS);
});
}

#[test]
fn dispatch_transfer_should_not_work_with_insufficient_fees() {
TestNet::reset();
Expand Down Expand Up @@ -1500,6 +1601,8 @@ fn do_trade_to_populate_oracle(asset_1: AssetId, asset_2: AssetId, amount: Balan
}

use frame_support::traits::fungible::Mutate;
use hydradx_traits::router::{PoolType, Trade};

pub fn init_omnipol() {
let native_price = FixedU128::from_rational(29903049701668757, 73927734532192294158);
let dot_price = FixedU128::from_rational(103158291366950047, 4566210555614178);
Expand Down Expand Up @@ -1636,3 +1739,26 @@ impl PrecompileHandle for MockHandle {
None
}
}

fn create_xyk_pool_with_amounts(asset_a: u32, amount_a: u128, asset_b: u32, amount_b: u128) {
assert_ok!(Currencies::update_balance(
hydradx_runtime::RuntimeOrigin::root(),
DAVE.into(),
asset_a,
amount_a as i128,
));
assert_ok!(Currencies::update_balance(
hydradx_runtime::RuntimeOrigin::root(),
DAVE.into(),
asset_b,
amount_b as i128,
));

assert_ok!(XYK::create_pool(
RuntimeOrigin::signed(DAVE.into()),
asset_a,
amount_a,
asset_b,
amount_b,
));
}
Loading
Loading