Skip to content

Commit

Permalink
updated PR for vortex distribution, making changes according to previ…
Browse files Browse the repository at this point in the history
…ous review comments, and rebased on main
  • Loading branch information
lsj5031 committed Nov 4, 2023
1 parent 2d5a55d commit 2f1f25e
Show file tree
Hide file tree
Showing 16 changed files with 2,272 additions and 408 deletions.
925 changes: 526 additions & 399 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion client/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use sc_service::ChainType;
use seed_runtime::{
constants::{
ONE_ROOT, ONE_XRP, ROOT_ASSET_ID, ROOT_DECIMALS, ROOT_MINIMUM_BALANCE, ROOT_NAME,
ROOT_SYMBOL, XRP_ASSET_ID, XRP_DECIMALS, XRP_MINIMUM_BALANCE, XRP_NAME, XRP_SYMBOL,
ROOT_SYMBOL, VTX_ASSET_ID, VTX_DECIMALS, VTX_MINIMUM_BALANCE, VTX_NAME, VTX_SYMBOL,
XRP_ASSET_ID, XRP_DECIMALS, XRP_MINIMUM_BALANCE, XRP_NAME, XRP_SYMBOL,
},
keys::*,
AccountId, AssetsConfig, BabeConfig, Balance, BalancesConfig, EthBridgeConfig, GenesisConfig,
Expand Down Expand Up @@ -153,10 +154,12 @@ fn testnet_genesis(
ROOT_DECIMALS,
),
(XRP_ASSET_ID, XRP_NAME.as_bytes().to_vec(), XRP_SYMBOL.as_bytes().to_vec(), XRP_DECIMALS),
(VTX_ASSET_ID, VTX_NAME.as_bytes().to_vec(), VTX_SYMBOL.as_bytes().to_vec(), VTX_DECIMALS),
];
let assets = vec![
(ROOT_ASSET_ID, root_key, true, ROOT_MINIMUM_BALANCE),
(XRP_ASSET_ID, root_key, true, XRP_MINIMUM_BALANCE),
(VTX_ASSET_ID, root_key, true, VTX_MINIMUM_BALANCE),
];
let mut endowed_assets = Vec::with_capacity(accounts_to_fund.len());
let mut endowed_balances = Vec::with_capacity(accounts_to_fund.len());
Expand Down
25 changes: 22 additions & 3 deletions e2e/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { JsonRpcProvider } from "@ethersproject/providers";
import { ApiPromise } from "@polkadot/api";
import { ApiPromise, Keyring } from "@polkadot/api";
import { SubmittableExtrinsic } from "@polkadot/api/types";
import { KeyringPair } from "@polkadot/keyring/types";
import { AnyJson } from "@polkadot/types/types";
import { BigNumber } from "ethers";
import { writeFileSync } from "fs";
import fs, { writeFileSync } from "fs";
import { CliPrettify } from "markdown-table-prettify";
import { join } from "path";
import path, { join } from "path";
import web3 from "web3";

export * from "./node";
Expand Down Expand Up @@ -644,3 +644,22 @@ export const executeForPreviousEvent = async (
currentInHistory++;
}
};

export const loadTestUsers = (userAmount?: number): KeyringPair[] => {
const content = fs.readFileSync(path.resolve(__dirname, "./generated_users.txt"), "utf-8");
const mnemonics = content
.replace(/\n{2,}/g, "\n")
.toString()
.split("\n");
const keyring = new Keyring({ type: "ethereum" });
const keypairs: KeyringPair[] = [];

for (let i = 0; i < mnemonics.length; i++) {
const mnemonic = mnemonics[i];
if (mnemonic !== "" && (userAmount === undefined || i < userAmount)) {
keypairs.push(keyring.addFromMnemonic(mnemonic, {}));
}
}
console.log(`loaded ${keypairs.length} users`);
return keypairs;
};
2 changes: 1 addition & 1 deletion e2e/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const config: HardhatUserConfig = {
apiKey: process.env.ETHERSCAN_API_KEY,
},
mocha: {
timeout: 120_000, // global tests timeout
timeout: 12_000_000, // global tests timeout
},
};

Expand Down
2 changes: 1 addition & 1 deletion evm-precompiles/futurepass/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface FuturePass {
function delegateType(address delegate) external view returns (uint8);
function registerDelegateWithSignature(address delegate, uint8 proxyType, uint32 deadline, bytes memory signature) external;
function registerDelegate(address delegate, uint8 proxyType) external;
function unregisterDelegate(address delegate) external;
function proxyCall(uint8 callType, address callTo, uint256 value, bytes memory callData) external payable;
}
Expand Down
55 changes: 55 additions & 0 deletions pallet/vortex-distribution/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[package]
name = "pallet-vortex"
version = "2.1.0"
authors = ["Centrality Developers <[email protected]>"]
edition = "2021"
license = "GPL-3.0"
repository = "https://github.com/futureversecom/seed"
description = "Vortex distribution pallet"

[dependencies]
serde = { version = "1.0.136", optional = true }
scale-info = { version = "2.1", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.30" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.30" }
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.30" }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", default-features = false }
sp-staking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.30" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.30" }
pallet-staking = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.30" }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "polkadot-v0.9.30" }

seed-primitives = { path = "../../primitives", default-features = false }
seed-pallet-common = { path = "../common", default-features = false }

[dev-dependencies]
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-assets = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-assets-ext = { path = "../assets-ext" }
frame-election-provider-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-session = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-bags-list = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }
pallet-staking-reward-curve = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.30" }

[features]
default = ["std"]
std = [
"seed-primitives/std",
"seed-pallet-common/std",
"serde",
"codec/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"sp-staking/std",
"pallet-staking/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
]
try-runtime = ["frame-support/try-runtime"]
runtime-benchmarks = ["frame-benchmarking"]
192 changes: 192 additions & 0 deletions pallet/vortex-distribution/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#![cfg(feature = "runtime-benchmarks")]

use super::*;

use crate::Pallet as Vortex;
use frame_benchmarking::{account as bench_account, benchmarks, impl_benchmark_test_suite};
use frame_support::{assert_ok, BoundedVec};
use frame_system::{Pallet as System, RawOrigin};
use sp_runtime::traits::One;

use crate::Pallet as VortexDistribution;

/// This is a helper function to get an account.
pub fn account<T: Config>(name: &'static str) -> T::AccountId {
bench_account(name, 0, 0)
}

pub fn origin<T: Config>(acc: &T::AccountId) -> RawOrigin<T::AccountId> {
RawOrigin::Signed(acc.clone())
}

fn mint_asset<T: Config>(fee_vault: &T::AccountId, root_vault: &T::AccountId) -> AssetId {
let asset_id = T::MultiCurrency::create(&fee_vault, None).unwrap();
// let mint_amount = Balance::from(10_000_000u32);
// let mint_amount = <T as pallet_staking::Config>::CurrencyBalance::default();
let mint_amount = <T as pallet_staking::Config>::CurrencyBalance::one();
assert_ok!(T::MultiCurrency::mint_into(asset_id, &fee_vault, mint_amount.into()));
assert_ok!(T::MultiCurrency::mint_into(T::NativeAssetId::get(), &root_vault, mint_amount));
assert_ok!(T::MultiCurrency::mint_into(T::VtxAssetId::get(), &root_vault, mint_amount));
asset_id
}

benchmarks! {
create_vtx_dist {
let vortex_dist_id = T::VtxDistIdentifier::default();
let amount = 100u32.into();
}: _(RawOrigin::Root, vortex_dist_id, amount)
verify {
assert_eq!(VtxDistStatuses::<T>::get(vortex_dist_id), VtxDistStatus::Enabled);
}

disable_vtx_dist {
let vortex_dist_id = T::VtxDistIdentifier::default();
let amount = 100u32.into();
assert_ok!(VortexDistribution::<T>::create_vtx_dist(RawOrigin::Root.into(), vortex_dist_id, amount));
}: _(RawOrigin::Root, vortex_dist_id)
verify {
assert_eq!(VtxDistStatuses::<T>::get(vortex_dist_id), VtxDistStatus::NotEnabled);
}

start_vtx_dist {
let vortex_dist_id = T::VtxDistIdentifier::default();
let amount = 100u32.into();
assert_ok!(VortexDistribution::<T>::create_vtx_dist(RawOrigin::Root.into(), vortex_dist_id, amount));
}: _(RawOrigin::Root, vortex_dist_id)
verify {
assert_eq!(VtxDistStatuses::<T>::get(vortex_dist_id), VtxDistStatus::Paying);
}

set_vtx_dist_eras {
let vortex_dist_id = T::VtxDistIdentifier::default();
let amount = 100u32.into();
let start_era = 1u32;
let end_era = 64u32;
assert_ok!(VortexDistribution::<T>::create_vtx_dist(RawOrigin::Root.into(), vortex_dist_id, amount));
}: _(RawOrigin::Root, vortex_dist_id, start_era, end_era)
verify {
assert_eq!(VtxDistEras::<T>::get(vortex_dist_id), (start_era, end_era));
}

set_asset_prices {
let vortex_dist_id = T::VtxDistIdentifier::default();
let amount = 100u32.into();
let asset_id = AssetId::default();
// let balance = <T as pallet_staking::Config>::CurrencyBalance::default();
let balance = <T as pallet_staking::Config>::CurrencyBalance::one();
let asset_prices = BoundedVec::try_from(vec![(asset_id, balance.into())]).unwrap();
assert_ok!(VortexDistribution::<T>::create_vtx_dist(RawOrigin::Root.into(), vortex_dist_id, amount));
}: _(RawOrigin::Root, asset_prices, vortex_dist_id)
verify {
assert_eq!(AssetPrices::<T>::get(vortex_dist_id, asset_id), balance);
}

register_rewards {
let vortex_dist_id = T::VtxDistIdentifier::default();
let amount = 100u32.into();
// let origin = origin::<T>(&account::<T>("test"));
let rewards = BoundedVec::try_from(vec![(account::<T>("test"), 100u32.into())]).unwrap();
assert_ok!(VortexDistribution::<T>::create_vtx_dist(RawOrigin::Root.into(), vortex_dist_id, amount));
}: _(RawOrigin::Root, vortex_dist_id, rewards)
verify {
let reward = VtxDistOrderbook::<T>::get(vortex_dist_id, account::<T>("test"));
assert_eq!(reward, (100u32.into(), false));
}

trigger_vtx_distribution {
let vortex_dist_id = T::VtxDistIdentifier::default();
let root_price = <T as pallet_staking::Config>::CurrencyBalance::one();
let vortex_price = <T as pallet_staking::Config>::CurrencyBalance::one();
let root_vault = account::<T>("root_vault");
let fee_vault = account::<T>("fee_vault");
let amount = 100u32.into();
let asset_id = mint_asset::<T>(&fee_vault, &root_vault);
assert_ok!(VortexDistribution::<T>::create_vtx_dist(RawOrigin::Root.into(), vortex_dist_id, amount));
let balance = <T as pallet_staking::Config>::CurrencyBalance::one();
let asset_prices = BoundedVec::try_from(vec![(asset_id, balance.into())]).unwrap();
assert_ok!(VortexDistribution::<T>::set_asset_prices(RawOrigin::Root.into(), asset_prices, vortex_dist_id));
}: _(RawOrigin::Root, root_price, vortex_price, root_vault.clone(), fee_vault.clone(), vortex_dist_id)
verify {
assert_eq!(T::MultiCurrency::balance(asset_id, &root_vault), 0u32.into());
assert_eq!(T::MultiCurrency::balance(T::NativeAssetId::get(), &root_vault), 0u32.into());

assert_eq!(T::MultiCurrency::balance(asset_id, &fee_vault), 0u32.into());
assert_eq!(T::MultiCurrency::balance(T::NativeAssetId::get(), &fee_vault), 0u32.into());

let mint_amount = <T as pallet_staking::Config>::CurrencyBalance::one();
let accum_vault = Vortex::<T>::get_vault_account_accum().unwrap();
assert_eq!(T::MultiCurrency::balance(asset_id, &accum_vault), mint_amount);
assert_eq!(T::MultiCurrency::balance(T::NativeAssetId::get(), &accum_vault), mint_amount);
}

// calc_staker_reward_points {
// let vortex_dist_id = T::VtxDistIdentifier::default();
// let root_price = <T as pallet_staking::Config>::CurrencyBalance::one();
// let vortex_price = <T as pallet_staking::Config>::CurrencyBalance::one();
// let root_vault = account::<T>("root_vault");
// let fee_vault = account::<T>("fee_vault");
// let validator_stash = account::<T>("validator");
// let amount = 100u32.into();
// let asset_id = mint_asset::<T>(&fee_vault, &root_vault);
// assert_ok!(VortexDistribution::<T>::create_vtx_dist(RawOrigin::Root.into(), vortex_dist_id, amount));
// let balance = <T as pallet_staking::Config>::CurrencyBalance::one();
// let asset_prices = vec![(asset_id, balance.into())];
// assert_ok!(VortexDistribution::<T>::set_asset_prices(RawOrigin::Root.into(), asset_prices, vortex_dist_id));
// }: _(RawOrigin::Root, validator_stash, vortex_dist_id)

redeem_tokens_from_vault {
let vortex_dist_id = T::VtxDistIdentifier::default();
let root_price = <T as pallet_staking::Config>::CurrencyBalance::one();
let vortex_price = <T as pallet_staking::Config>::CurrencyBalance::one();
let root_vault = account::<T>("root_vault");
let fee_vault = account::<T>("fee_vault");
let amount = 2u32.into();
let asset_id = mint_asset::<T>(&fee_vault, &root_vault);
assert_ok!(VortexDistribution::<T>::create_vtx_dist(RawOrigin::Root.into(), vortex_dist_id, amount));
let balance = <T as pallet_staking::Config>::CurrencyBalance::one();
let asset_prices = BoundedVec::try_from(vec![(asset_id, balance.into())]).unwrap();
let end_block: u32 = 500;
assert_ok!(VortexDistribution::<T>::set_asset_prices(RawOrigin::Root.into(), asset_prices, vortex_dist_id));
let rewards = BoundedVec::try_from(vec![(account::<T>("test"), 2u32.into())]).unwrap();
assert_ok!(VortexDistribution::<T>::register_rewards(RawOrigin::Root.into(), vortex_dist_id, rewards));
assert_ok!(VortexDistribution::<T>::trigger_vtx_distribution(RawOrigin::Root.into(), root_price, vortex_price, root_vault, fee_vault, vortex_dist_id));
assert_ok!(VortexDistribution::<T>::start_vtx_dist(RawOrigin::Root.into(), vortex_dist_id));
System::<T>::set_block_number(end_block.into());
assert_ok!(VortexDistribution::<T>::pay_unsigned(RawOrigin::None.into(), vortex_dist_id, end_block.into()));
}: _(RawOrigin::Signed(account::<T>("test")), vortex_dist_id, balance)
verify {
assert_eq!(T::MultiCurrency::balance(T::VtxAssetId::get(), &account::<T>("test")), balance);

let ratio = Perbill::from_rational(balance, amount * 2u32.into());
let mint_amount = <T as pallet_staking::Config>::CurrencyBalance::one();
let expect_balance = ratio * mint_amount;
assert_eq!(T::MultiCurrency::balance(asset_id, &account::<T>("test")), expect_balance);
assert_eq!(T::MultiCurrency::balance(T::NativeAssetId::get(), &account::<T>("test")), expect_balance);
}

pay_unsigned {
let vortex_dist_id = T::VtxDistIdentifier::default();
let root_price = <T as pallet_staking::Config>::CurrencyBalance::one();
let vortex_price = <T as pallet_staking::Config>::CurrencyBalance::one();
let root_vault = account::<T>("root_vault");
let fee_vault = account::<T>("fee_vault");
let amount = 2u32.into();
let asset_id = mint_asset::<T>(&fee_vault, &root_vault);
assert_ok!(VortexDistribution::<T>::create_vtx_dist(RawOrigin::Root.into(), vortex_dist_id, amount));
let balance = <T as pallet_staking::Config>::CurrencyBalance::one();
let asset_prices = BoundedVec::try_from(vec![(asset_id, balance.into())]).unwrap();
let end_block: u32 = 500;
assert_ok!(VortexDistribution::<T>::set_asset_prices(RawOrigin::Root.into(), asset_prices, vortex_dist_id));
let rewards = BoundedVec::try_from(vec![(account::<T>("test"), 2u32.into())]).unwrap();
assert_ok!(VortexDistribution::<T>::register_rewards(RawOrigin::Root.into(), vortex_dist_id, rewards));
assert_ok!(VortexDistribution::<T>::trigger_vtx_distribution(RawOrigin::Root.into(), root_price, vortex_price, root_vault, fee_vault, vortex_dist_id));
assert_ok!(VortexDistribution::<T>::start_vtx_dist(RawOrigin::Root.into(), vortex_dist_id));
System::<T>::set_block_number(end_block.into());
// assert_ok!(VortexDistribution::<T>::pay_unsigned(RawOrigin::None.into(), vortex_dist_id, end_block.into()));
}: _(RawOrigin::None, vortex_dist_id, end_block.into())
verify {
assert_eq!(T::MultiCurrency::balance(T::VtxAssetId::get(), &account::<T>("test")), 2u32.into());
}
}

impl_benchmark_test_suite!(VortexDistribution, crate::mock::new_test_ext(), crate::mock::Test,);
Loading

0 comments on commit 2f1f25e

Please sign in to comment.