Skip to content

Commit

Permalink
explore-negative-transferrable-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Aug 23, 2024
1 parent ea31031 commit a5c19c3
Show file tree
Hide file tree
Showing 127 changed files with 43,463 additions and 9 deletions.
32 changes: 28 additions & 4 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ substrate-fixed = { git = "https://github.com/encointer/substrate-fixed", defaul

# External (without extra features and with default disabled if necessary)
parity-scale-codec = { version = "3.6.5", default-features = false }
codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false }
clap = { version = "4.3.24" }
futures = { version = "0.3.28", features = ["thread-pool"] }
scale-info = { version = "2.9.0", default-features = false, features = [
Expand Down Expand Up @@ -249,3 +250,7 @@ asset-hub-polkadot-runtime = { git = 'https://github.com/polkadot-fellows/runtim
polkadot-runtime-constants = { git = 'https://github.com/polkadot-fellows/runtimes', default-features = false, rev = '59172d5fb8feea2646490618bead3b2b4fb06220' }
system-parachains-constants = { git = 'https://github.com/polkadot-fellows/runtimes', default-features = false, rev = '59172d5fb8feea2646490618bead3b2b4fb06220' }
polkadot-runtime = { git = 'https://github.com/polkadot-fellows/runtimes', default-features = false, rev = '59172d5fb8feea2646490618bead3b2b4fb06220' }

[patch.crates-io]
pallet-balances = { path = "pallets/balances" }
frame-support = { path = "pallets/frame-support" }
3 changes: 2 additions & 1 deletion integration-tests/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ pub mod polimec {
use xcm_emulator::TestExt;

pub const PARA_ID: u32 = 3344;
pub const ED: Balance = polimec_runtime::EXISTENTIAL_DEPOSIT;
// pub const ED: Balance = polimec_runtime::EXISTENTIAL_DEPOSIT;
pub const ED: Balance = 1;

const GENESIS_BLOCKS_PER_ROUND: BlockNumber = 1800;
const GENESIS_COLLATOR_COMMISSION: Perbill = Perbill::from_percent(10);
Expand Down
123 changes: 120 additions & 3 deletions integration-tests/src/tests/vest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{polimec::ED, *};
use frame_support::assert_err;
use crate::{polimec::ED, tests::defaults::IntegrationInstantiator, *};
/// Tests for the oracle pallet integration.
/// Alice, Bob, Charlie are members of the OracleProvidersMembers.
/// Only members should be able to feed data into the oracle.
use frame_support::traits::fungible::Inspect;
use frame_support::traits::fungible::Mutate;
use frame_support::traits::{fungible::Mutate, WithdrawReasons};
use frame_support::traits::tokens::{Precision, Preservation};
use frame_system::pallet_prelude::BlockNumberFor;
use macros::generate_accounts;
use pallet_funding::assert_close_enough;
use pallet_vesting::VestingInfo;
use polimec_runtime::{Balances, BlockchainOperationTreasury, ParachainStaking, RuntimeOrigin, Vesting, PLMC};
use polimec_runtime::{Balances, BlockchainOperationTreasury, ParachainStaking, RuntimeHoldReason, RuntimeOrigin, Vesting, PLMC, RuntimeSlashReason};
use sp_runtime::Perquintill;
use xcm_emulator::helpers::get_account_id_from_seed;

Expand Down Expand Up @@ -180,3 +183,117 @@ fn dust_to_treasury() {
assert_eq!(initial_treasury_balance + ED - 1, final_treasury_balance);
})
}

#[test]
fn sandbox() {
use frame_support::traits::{
fungible::{hold::Balanced, Mutate, MutateHold},
LockableCurrency,
};
use pallet_dispenser::VestingSchedule;

let inst = IntegrationInstantiator::new(None);

generate_accounts!(ALICE, BOB);
let alice: PolimecAccountId = ALICE.into();
let bob: PolimecAccountId = BOB.into();

let get_acc_info = |account: PolimecAccountId| {
let account_info = PolimecSystem::account(account);
account_info
};

PolimecNet::execute_with(|| {
// dbg!(get_acc_info(alice.clone()));
Balances::mint_into(&alice.clone(), 100).unwrap();
// dbg!(get_acc_info(alice.clone()));

let locked_amount = 80;
// Balances::set_lock(*b"vesting ", &alice.clone(), locked_amount, WithdrawReasons::RESERVE);
// dbg!(get_acc_info(alice.clone()));

let now = PolimecSystem::block_number();
PolimecVesting::add_vesting_schedule(&alice.clone(), locked_amount, 10, now).unwrap();
dbg!(get_acc_info(alice.clone()));

let hold_reason = RuntimeHoldReason::Funding(pallet_funding::HoldReason::Evaluation(0));
let hold_amount = 95;
Balances::hold(&hold_reason, &alice, hold_amount).unwrap();
dbg!(get_acc_info(alice.clone()));

let slash_amount = 40;
<Balances as Balanced<PolimecAccountId>>::slash(&hold_reason, &alice, slash_amount);
dbg!(get_acc_info(alice.clone()));
Balances::release(&hold_reason, &alice, hold_amount-slash_amount, Precision::BestEffort).unwrap();
dbg!(get_acc_info(alice.clone()));

// Balances::mint_into(&alice.clone(), 20).unwrap();

// assert!(Balances::transfer(&alice.clone(), &alice.clone(), 30, Preservation::Expendable).is_err());
// assert!(Balances::transfer(&alice.clone(), &alice.clone(), 1, Preservation::Expendable).is_err());

// Balances::transfer_all(PolimecOrigin::signed(alice.clone()), bob.clone().into(), false).unwrap();
// dbg!(get_acc_info(alice.clone()));
// dbg!(get_acc_info(bob.clone()));

let locked_amount = Balances::locks(&alice.clone()).iter().find(|lock| &lock.id == b"vesting ").unwrap().amount;
let total_amount = Balances::total_balance(&alice.clone());
let new_locked_amount = total_amount.min(locked_amount);
let amount_reduced = locked_amount - new_locked_amount;
dbg!(new_locked_amount);
dbg!(amount_reduced);
let vesting_info = pallet_vesting::Vesting::<PolimecRuntime>::get(alice.clone()).unwrap();
assert_eq!(vesting_info.len(), 1);
let vesting_info = vesting_info[0];

let new_start: u32 = vesting_info.starting_block() + (amount_reduced / vesting_info.per_block()) as u32;

dbg!(now);
dbg!(new_start);

dbg!(get_acc_info(alice.clone()));
Vesting::remove_vesting_schedule(&alice.clone(), 0).unwrap();
dbg!(get_acc_info(alice.clone()));
Vesting::add_vesting_schedule(&alice.clone(), new_locked_amount, vesting_info.per_block(), new_start).unwrap();
dbg!(get_acc_info(alice.clone()));

PolimecSystem::set_block_number((new_start).try_into().unwrap());
PolimecVesting::vest(PolimecOrigin::signed(alice.clone())).unwrap();
dbg!(get_acc_info(alice.clone()));

PolimecSystem::set_block_number((new_start + 1).try_into().unwrap());
PolimecVesting::vest(PolimecOrigin::signed(alice.clone())).unwrap();
dbg!(get_acc_info(alice.clone()));




fn on_slash() {
// calculate freeze reduction after slash:
// R = account.frozen - account.free
//
// 0 -----------------60---80---90---100 PLMC amounts
// |-----free -------------|//slashed//|
// |-----------------------|~~~~~| ~ = freeze reduction
// |-----lock 1------------------|
// |-----lock 2--------|

// Calculate new lock amounts:
// min(old_amount, free)

// |-----free -------------|///slashed///|
// |-----lock 1------------|~red.~|
// |-----lock 2--------| untouched

// Calculate new vesting schedules:

//
// 0 ----------------------8---10 Block numbers:
// 0 -----------------60---80---90---100 PLMC amounts
// | - Schedule lock 1 ---|
// |-----lock 1-------------------|
// | - Schedule lock 2 ------- |
// |-----lock 2--------|
}
});
}
1 change: 1 addition & 0 deletions pallets/balances/.cargo-ok
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"v":1}
6 changes: 6 additions & 0 deletions pallets/balances/.cargo_vcs_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"git": {
"sha1": "89fdefcc61d119e1712e8be89318d25f4266243f"
},
"path_in_vcs": "substrate/frame/balances"
}
58 changes: 58 additions & 0 deletions pallets/balances/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[package]
name = "pallet-balances"
version = "28.0.0"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
homepage = "https://substrate.io"
repository.workspace = true
description = "FRAME pallet to manage balances"
readme = "README.md"

[lints]
workspace = true

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { workspace = true, default-features = false, features = ["derive", "max-encoded-len"] }
log.workspace = true
scale-info.workspace = true
frame-benchmarking = {workspace = true, optional = true}
frame-support.workspace = true
frame-system.workspace = true
sp-runtime.workspace = true
sp-std.workspace = true

[dev-dependencies]
pallet-transaction-payment.workspace = true
sp-core.workspace = true
sp-io.workspace = true
paste = "1.0.12"

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
"scale-info/std",
"sp-runtime/std",
"sp-std/std",
]
# Enable support for setting the existential deposit to zero.
insecure_zero_ed = []
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]
Loading

0 comments on commit a5c19c3

Please sign in to comment.