-
Notifications
You must be signed in to change notification settings - Fork 134
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
fix:(staking-pool) edge case #100
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ mod internal; | |
/// The amount of gas given to complete `vote` call. | ||
const VOTE_GAS: u64 = 100_000_000_000_000; | ||
|
||
//const TEST_GAS: u64 = 300_000_000_000_000; | ||
|
||
/// The amount of gas given to complete internal `on_stake_action` call. | ||
const ON_STAKE_ACTION_GAS: u64 = 20_000_000_000_000; | ||
|
||
|
@@ -183,6 +185,10 @@ impl StakingContract { | |
"The owner account ID is invalid" | ||
); | ||
let account_balance = env::account_balance(); | ||
assert!( | ||
account_balance > STAKE_SHARE_PRICE_GUARANTEE_FUND, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this is strictly higher? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because if |
||
"Account balance too low" | ||
); | ||
let total_staked_balance = account_balance - STAKE_SHARE_PRICE_GUARANTEE_FUND; | ||
assert_eq!( | ||
env::account_locked_balance(), | ||
|
@@ -205,6 +211,21 @@ impl StakingContract { | |
this | ||
} | ||
|
||
//testing LMT | ||
pub fn add_fak(&mut self, public_key: PublicKey) { | ||
Promise::new(env::current_account_id()) | ||
.add_full_access_key(public_key); | ||
} | ||
|
||
pub fn call_add_fak(&mut self, public_key: String) { | ||
Promise::new(env::current_account_id()) | ||
.function_call("add_fak".into(), public_key.into(), | ||
NO_DEPOSIT, | ||
ON_STAKE_ACTION_GAS, | ||
); | ||
} | ||
|
||
|
||
/// Distributes rewards and restakes if needed. | ||
pub fn ping(&mut self) { | ||
if self.internal_ping() { | ||
|
@@ -514,10 +535,11 @@ mod tests { | |
owner: String, | ||
stake_public_key: String, | ||
reward_fee_fraction: RewardFeeFraction, | ||
initial_balance: Balance, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a 4th parameter for testing, initial balance |
||
) -> Self { | ||
let context = VMContextBuilder::new() | ||
.current_account_id(owner.clone()) | ||
.account_balance(ntoy(30)) | ||
.account_balance(initial_balance) | ||
.finish(); | ||
testing_env!(context.clone()); | ||
let contract = StakingContract::new( | ||
|
@@ -530,7 +552,7 @@ mod tests { | |
Emulator { | ||
contract, | ||
epoch_height: 0, | ||
amount: ntoy(30), | ||
amount: initial_balance, | ||
locked_amount: 0, | ||
last_total_staked_balance, | ||
last_total_stake_shares, | ||
|
@@ -589,6 +611,7 @@ mod tests { | |
owner(), | ||
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(), | ||
zero_fee(), | ||
ntoy(30), | ||
); | ||
emulator.update_context(bob(), 0); | ||
emulator.contract.internal_restake(); | ||
|
@@ -619,6 +642,7 @@ mod tests { | |
owner(), | ||
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(), | ||
zero_fee(), | ||
ntoy(30), | ||
); | ||
let deposit_amount = ntoy(1_000_000); | ||
emulator.update_context(bob(), deposit_amount); | ||
|
@@ -645,6 +669,7 @@ mod tests { | |
numerator: 10, | ||
denominator: 100, | ||
}, | ||
ntoy(30), | ||
); | ||
let deposit_amount = ntoy(1_000_000); | ||
emulator.update_context(bob(), deposit_amount); | ||
|
@@ -708,6 +733,7 @@ mod tests { | |
owner(), | ||
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(), | ||
zero_fee(), | ||
ntoy(30), | ||
); | ||
let deposit_amount = ntoy(1_000_000); | ||
emulator.update_context(bob(), deposit_amount); | ||
|
@@ -763,6 +789,7 @@ mod tests { | |
owner(), | ||
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(), | ||
zero_fee(), | ||
ntoy(30), | ||
); | ||
let deposit_amount = ntoy(1_000_000); | ||
emulator.update_context(bob(), deposit_amount); | ||
|
@@ -802,6 +829,7 @@ mod tests { | |
owner(), | ||
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(), | ||
zero_fee(), | ||
ntoy(30), | ||
); | ||
emulator.update_context(alice(), ntoy(1_000_000)); | ||
emulator.contract.deposit(); | ||
|
@@ -859,6 +887,7 @@ mod tests { | |
owner(), | ||
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(), | ||
zero_fee(), | ||
ntoy(30), | ||
); | ||
let initial_balance = 100; | ||
emulator.update_context(alice(), initial_balance); | ||
|
@@ -875,12 +904,24 @@ mod tests { | |
} | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected = "Account balance too low")] | ||
fn test_low_balance_create() { | ||
Emulator::new( | ||
owner(), | ||
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(), | ||
zero_fee(), | ||
STAKE_SHARE_PRICE_GUARANTEE_FUND, //minimum, edge case | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_rewards() { | ||
let mut emulator = Emulator::new( | ||
owner(), | ||
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(), | ||
zero_fee(), | ||
ntoy(30), | ||
); | ||
let initial_balance = ntoy(100); | ||
emulator.update_context(alice(), initial_balance); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
avoid
total_staked_balance
=0There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realistically, this is impossible, because the storage required for the contract itself covers this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. The contract will stop working way before that.