-
Notifications
You must be signed in to change notification settings - Fork 46
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
farm position functionality #760
Changes from 20 commits
4ad9758
b4c924c
667c19a
8348f52
5d2d020
5e05e32
4f6bbd3
97b52fa
6b47ab3
6502c5b
b70b0de
8997f70
d37f593
872278b
6580dba
3c4ffe1
5d7a848
b445da1
e46c4d2
ea612c1
9bdf09c
07f41a3
14baa0a
63de49e
0e7141c
f40d64a
ff3f040
f1496d5
98aa836
f371569
5b00a9c
4ff1b8a
00dc45d
0f87499
249c3ee
4b4e0b7
c044821
16576f1
3fc935a
be142a3
a77b760
3fe300e
37c9ebc
9859e47
d9fd841
65930ce
ee6644c
1e9ec92
bbff637
c609e42
8ac9c19
4303c0c
767f452
8100d57
aba2e4e
1d92432
494b301
772779a
d790945
0ee2b42
4262fa1
a6e3e00
36be963
ff4f6b1
5acf5e4
d1a9e72
75eba99
2e36524
67a7d42
4dc0203
3cf4458
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ use contexts::storage_cache::StorageCache; | |
use core::marker::PhantomData; | ||
use fixed_supply_token::FixedSupplyToken; | ||
use mergeable::Mergeable; | ||
use multiversx_sc_modules::transfer_role_proxy::PaymentsVec; | ||
use rewards::RewardsModule; | ||
|
||
pub trait AllBaseFarmImplTraits = | ||
|
@@ -187,6 +188,67 @@ pub trait FarmContract { | |
_storage_cache: &StorageCache<Self::FarmSc>, | ||
) { | ||
} | ||
|
||
fn check_and_update_user_farm_position( | ||
sc: &Self::FarmSc, | ||
user: &ManagedAddress<<Self::FarmSc as ContractBase>::Api>, | ||
farm_positions: &PaymentsVec<<Self::FarmSc as ContractBase>::Api>, | ||
) { | ||
let farm_token_mapper = sc.farm_token(); | ||
let mut total_farm_position = BigUint::zero(); | ||
let mut farm_position_increase = BigUint::zero(); | ||
for farm_position in farm_positions { | ||
farm_token_mapper.require_same_token(&farm_position.token_identifier); | ||
|
||
total_farm_position += &farm_position.amount; | ||
let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> = | ||
farm_token_mapper.get_token_attributes(farm_position.token_nonce); | ||
|
||
if &token_attributes.original_owner != user { | ||
Self::decrease_user_farm_position(sc, &farm_position); | ||
farm_position_increase += &farm_position.amount; | ||
} | ||
} | ||
|
||
let user_total_farm_position = sc.get_user_total_farm_position(user); | ||
if user_total_farm_position == BigUint::zero() { | ||
Self::increase_user_farm_position(sc, user, &total_farm_position); | ||
} else if farm_position_increase > 0 { | ||
Self::increase_user_farm_position(sc, user, &farm_position_increase); | ||
} | ||
} | ||
#[inline] | ||
fn increase_user_farm_position( | ||
sc: &Self::FarmSc, | ||
user: &ManagedAddress<<Self::FarmSc as ContractBase>::Api>, | ||
new_farm_position_amount: &BigUint<<Self::FarmSc as ContractBase>::Api>, | ||
) { | ||
sc.user_total_farm_position(user) | ||
.update(|user_farm_position_struct| { | ||
user_farm_position_struct.total_farm_position += new_farm_position_amount | ||
}); | ||
} | ||
|
||
fn decrease_user_farm_position( | ||
sc: &Self::FarmSc, | ||
farm_position: &EsdtTokenPayment<<Self::FarmSc as ContractBase>::Api>, | ||
) { | ||
let farm_token_mapper = sc.farm_token(); | ||
let token_attributes: FarmTokenAttributes<<Self::FarmSc as ContractBase>::Api> = | ||
farm_token_mapper.get_token_attributes(farm_position.token_nonce); | ||
Comment on lines
+235
to
+237
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. Getting attributes a second time, first time on line 204. Why not give them as argument? 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. We use |
||
|
||
sc.user_total_farm_position(&token_attributes.original_owner) | ||
.update(|user_farm_position_struct| { | ||
let mut user_total_farm_position = | ||
user_farm_position_struct.total_farm_position.clone(); | ||
if user_total_farm_position > farm_position.amount { | ||
user_total_farm_position -= &farm_position.amount; | ||
} else { | ||
user_total_farm_position = BigUint::zero(); | ||
} | ||
user_farm_position_struct.total_farm_position = user_total_farm_position; | ||
}); | ||
} | ||
} | ||
|
||
pub struct DefaultFarmWrapper<T> | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,10 +118,13 @@ pub trait Farm: | |
let caller = self.blockchain().get_caller(); | ||
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller); | ||
|
||
let claim_rewards_result = self.claim_rewards::<Wrapper<Self>>(orig_caller); | ||
let claim_rewards_result = self.claim_rewards::<Wrapper<Self>>(orig_caller.clone()); | ||
|
||
self.send_payment_non_zero(&caller, &claim_rewards_result.new_farm_token); | ||
self.send_payment_non_zero(&caller, &claim_rewards_result.rewards); | ||
|
||
self.update_energy_and_progress(&orig_caller); | ||
|
||
claim_rewards_result.into() | ||
} | ||
|
||
|
@@ -134,9 +137,12 @@ pub trait Farm: | |
let caller = self.blockchain().get_caller(); | ||
let orig_caller = self.get_orig_caller_from_opt(&caller, opt_orig_caller); | ||
|
||
let output_farm_token_payment = self.compound_rewards::<Wrapper<Self>>(orig_caller); | ||
let output_farm_token_payment = self.compound_rewards::<Wrapper<Self>>(orig_caller.clone()); | ||
|
||
self.send_payment_non_zero(&caller, &output_farm_token_payment); | ||
|
||
self.update_energy_and_progress(&orig_caller); | ||
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. this only reads from energy contract and writes in the current contract ? 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. Yes |
||
|
||
output_farm_token_payment | ||
} | ||
|
||
|
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.
add empty line before
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.
Fixed