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

Farming contract #16

Merged
merged 41 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
8d54617
Added farming contract
PierreOssun Oct 14, 2022
95f296b
Added storage + events
PierreOssun Oct 14, 2022
3245faf
separated Traits
PierreOssun Oct 14, 2022
aa90d40
Added Ownable + add
PierreOssun Oct 14, 2022
2d05454
update pool + storage refacto
PierreOssun Oct 17, 2022
b286a1c
Provide tentative stake/unstake features
HyunggyuJang Oct 18, 2022
0c0d7a0
Implement add correctly
HyunggyuJang Oct 18, 2022
bcc6192
Fix deposit (initialization is needed)
HyunggyuJang Oct 18, 2022
8e2dd3a
Refactor rewarders structure
HyunggyuJang Oct 18, 2022
9ba71f6
Merge pull request #12 from AstarNetwork/feature/farming-interface
HyunggyuJang Oct 19, 2022
031b7fb
updated toolchain
PierreOssun Oct 19, 2022
0bc28c9
Finished update all pools
PierreOssun Oct 19, 2022
9818239
pending arsw
PierreOssun Oct 19, 2022
d775935
set function
PierreOssun Oct 19, 2022
5681e3f
Merge branch 'main' into feature/farming-contract-add
PierreOssun Oct 19, 2022
164f293
fix mut of var
PierreOssun Oct 19, 2022
b5d89ea
fmt
PierreOssun Oct 19, 2022
c9d46ec
clean code - pr comments
PierreOssun Oct 20, 2022
b399ac5
Merge branch 'main' of github.com:AstarNetwork/uniswap-dex-example in…
PierreOssun Oct 20, 2022
d71ec07
refactor file structure
PierreOssun Oct 20, 2022
f148248
Impl rewarder
PierreOssun Oct 21, 2022
a10a9d2
withdraw & harvest
PierreOssun Oct 21, 2022
f1836b5
Finished update all pools
PierreOssun Oct 19, 2022
fce8b43
pending arsw
PierreOssun Oct 19, 2022
ddcaee7
set function
PierreOssun Oct 19, 2022
8248c1f
clean code - pr comments
PierreOssun Oct 20, 2022
09e2dfe
refactor file structure
PierreOssun Oct 20, 2022
d2e407c
Impl rewarder
PierreOssun Oct 21, 2022
79bbc70
withdraw & harvest
PierreOssun Oct 21, 2022
f438d6a
Merge branch 'feature/farming-contract-add' of github.com:AstarNetwor…
PierreOssun Oct 27, 2022
81b984b
Merge branch 'main' of github.com:AstarNetwork/uniswap-dex-example in…
PierreOssun Oct 27, 2022
3f6c0fa
Pr comments
PierreOssun Oct 27, 2022
801cf5c
Pr comments
PierreOssun Oct 27, 2022
c5c7232
Pr comments
PierreOssun Oct 27, 2022
5a8223d
Pr comments
PierreOssun Oct 28, 2022
3785d66
Added deposit_arsw + emergency_withdraw + withdraw_and_harvest
PierreOssun Oct 28, 2022
dd34a0b
refactor guards
PierreOssun Oct 28, 2022
995eb29
PR comments - clean code
PierreOssun Oct 31, 2022
115d004
PR comments
PierreOssun Oct 31, 2022
ce7eab0
cast as u128
PierreOssun Nov 4, 2022
cd310b9
Fixed error enum
PierreOssun Nov 4, 2022
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
4 changes: 4 additions & 0 deletions farming/logics/traits/master_chef/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ pub enum FarmingError {
CastTou128Error5,
CastTou128Error6,
CastTou128Error7,
CastTou128Error8,
CastToi128Error,
CastToi128Error2,
CastToi128Error3,
CastToi128Error4,
CastToi128Error5,
CastToi128Error6,
CastToi128Error7,
CastToi128Error8,
RewarderNotFound,
SubUnderflow1,
SubUnderflow2,
Expand All @@ -42,6 +44,8 @@ pub enum FarmingError {
SubUnderflow8,
SubUnderflow9,
SubUnderflow10,
SubUnderflow11,
SubUnderflow12,
AddOverflow1,
AddOverflow2,
AddOverflow3,
Expand Down
110 changes: 48 additions & 62 deletions farming/logics/traits/master_chef/farming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
self._update_all_pools()?;

self.data::<Data>().total_alloc_point = self
.data::<Data>()
.total_alloc_point
.get_total_alloc_point()
.checked_add(alloc_point)
.ok_or(FarmingError::AddOverflow2)?;
self.data::<Data>().lp_tokens.push(lp_token);
Expand Down Expand Up @@ -96,8 +95,7 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
.ok_or(FarmingError::PoolNotFound)?;
self._update_all_pools()?;
self.data::<Data>().total_alloc_point = self
.data::<Data>()
.total_alloc_point
.get_total_alloc_point()
.checked_sub(pool_info.alloc_point)
.ok_or(FarmingError::SubUnderflow7)?
.checked_add(alloc_point)
Expand Down Expand Up @@ -148,13 +146,12 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
.ok_or(FarmingError::AddOverflow8)?;
}

let pending = (user_info
.amount
.checked_mul(acc_arsw_per_share)
.ok_or(FarmingError::MulOverflow9)?
/ ACC_ARSW_PRECISION)
.checked_add_signed(-user_info.reward_debt)
.ok_or(FarmingError::AddOverflow14)?;
let pending = <U256 as TryInto<u128>>::try_into(
casted_mul(user_info.amount, acc_arsw_per_share) / ACC_ARSW_PRECISION,
)
.map_err(|_| FarmingError::CastTou128Error8)?
.checked_add_signed(-user_info.reward_debt)
.ok_or(FarmingError::AddOverflow14)?;

Ok(pending)
}
Expand Down Expand Up @@ -240,7 +237,7 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
let user_amount = user
.amount
.checked_sub(amount)
.ok_or(FarmingError::AddOverflow12)?;
.ok_or(FarmingError::SubUnderflow12)?;

self.data::<Data>().user_info.insert(
&(pool_id, to),
Expand Down Expand Up @@ -273,22 +270,19 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
.get_user_info(pool_id, caller)
.ok_or(FarmingError::UserNotFound)?;

let accumulated_arsw = user
.amount
.checked_mul(pool.acc_arsw_per_share)
.ok_or(FarmingError::MulOverflow12)?
/ ACC_ARSW_PRECISION;
let accumulated_arsw = <U256 as TryInto<i128>>::try_into(
casted_mul(user.amount, pool.acc_arsw_per_share) / ACC_ARSW_PRECISION,
)
.map_err(|_| FarmingError::CastToi128Error8)?;

let pending_arsw = accumulated_arsw
.checked_add_signed(-user.reward_debt)
.ok_or(FarmingError::AddOverflow13)?;
.checked_sub(user.reward_debt)
.ok_or(FarmingError::SubUnderflow11)? as u128;

self.data::<Data>().user_info.insert(
&(pool_id, to),
&UserInfo {
reward_debt: accumulated_arsw
.try_into()
.map_err(|_| FarmingError::CastToi128Error5)?,
reward_debt: accumulated_arsw,
..user
},
);
Expand Down Expand Up @@ -333,23 +327,19 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
.get_user_info(pool_id, caller)
.ok_or(FarmingError::UserNotFound)?;

let accumulated_arsw = user
.amount
.checked_mul(pool.acc_arsw_per_share)
.ok_or(FarmingError::MulOverflow13)?
/ ACC_ARSW_PRECISION;
let accumulated_arsw =
casted_mul(user.amount, pool.acc_arsw_per_share) / ACC_ARSW_PRECISION;

let pending_arsw = accumulated_arsw
let pending_arsw = <U256 as TryInto<u128>>::try_into(accumulated_arsw)
.map_err(|_| FarmingError::CastTou128Error8)?
.checked_add_signed(-user.reward_debt)
.ok_or(FarmingError::AddOverflow15)?;

let user_reward_debt = accumulated_arsw
.checked_sub(
(casted_mul(amount, pool.acc_arsw_per_share) / ACC_ARSW_PRECISION)
.try_into()
.map_err(|_| FarmingError::CastTou128Error7)?,
)
.ok_or(FarmingError::SubUnderflow10)?;
let user_reward_debt: i128 = accumulated_arsw
.checked_sub(casted_mul(amount, pool.acc_arsw_per_share) / ACC_ARSW_PRECISION)
.ok_or(FarmingError::SubUnderflow10)?
.try_into()
.map_err(|_| FarmingError::CastTou128Error7)?;

let user_amount = user
.amount
Expand All @@ -359,15 +349,13 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
self.data::<Data>().user_info.insert(
&(pool_id, to),
&UserInfo {
reward_debt: user_reward_debt
.try_into()
.map_err(|_| FarmingError::CastToi128Error7)?,
reward_debt: user_reward_debt,
amount: user_amount,
},
);

PSP22Ref::transfer(
&mut self.data::<Data>().arsw_token,
&self.data::<Data>().arsw_token,
to,
pending_arsw,
Vec::new(),
Expand All @@ -387,8 +375,8 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
.ok_or(FarmingError::LpTokenNotFound)?;
PSP22Ref::transfer(&lp_token, to, amount, Vec::new())?;

self._emit_harvest_event(caller, pool_id, pending_arsw);
self._emit_withdraw_event(caller, pool_id, amount, to);
self._emit_harvest_event(caller, pool_id, pending_arsw);
Ok(())
}

Expand Down Expand Up @@ -472,7 +460,7 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
.ok_or(FarmingError::AddOverflow8)?;
}
pool.last_reward_block = current_block;
self.data::<Data>().pool_info.insert(pool_id, &pool);
self.data::<Data>().pool_info.insert(&pool_id, &pool);

self._emit_log_update_pool_event(
pool_id,
Expand All @@ -491,27 +479,25 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
lp_supply: Balance,
) -> Result<Balance, FarmingError> {
ensure!(lp_supply > 0, FarmingError::LpSupplyIsZero);
let last_reward_block_period = self._get_period(pool_info.last_reward_block)?;
let current_period = self._get_period(Self::env().block_number())?;

let current_period = self._get_period(current_block)?;
let mut arsw_reward: Balance = 0;
let mut last_block = pool_info.last_reward_block;
let last_reward_block_period = self._get_period(last_block)?;
let mut period = last_reward_block_period;
let total_alloc_point = self.get_total_alloc_point();
while period <= current_period {
if period > MAX_PERIOD {
break
}
let total_alloc_point: u32 = self.data::<Data>().total_alloc_point;
if current_block <= self._period_max(period)? {
arsw_reward = arsw_reward
.checked_add(
casted_mul(
(current_block
.checked_sub(last_block)
.ok_or(FarmingError::SubUnderflow4)?
as u128)
.checked_add(pool_info.alloc_point as u128)
.ok_or(FarmingError::MulOverflow4)?,
* pool_info.alloc_point)
.into(),
PierreOssun marked this conversation as resolved.
Show resolved Hide resolved
self._arsw_per_block(period)?,
)
.checked_div(total_alloc_point.into())
Expand All @@ -524,11 +510,12 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
arsw_reward = arsw_reward
.checked_add(
casted_mul(
(self._period_max(period)? as u128)
(self
._period_max(period)?
.checked_sub(last_block.into())
.ok_or(FarmingError::SubUnderflow5)?
.checked_mul(pool_info.alloc_point.into())
.ok_or(FarmingError::MulOverflow6)?,
* pool_info.alloc_point)
.into(),
PierreOssun marked this conversation as resolved.
Show resolved Hide resolved
self._arsw_per_block(period)? as u128,
)
.checked_div(total_alloc_point.into())
Expand All @@ -543,11 +530,11 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far

period += 1;
}
Ok(casted_mul(arsw_reward, ACC_ARSW_PRECISION)
.checked_div(lp_supply.into())
.ok_or(FarmingError::DivByZero3)?
.try_into()
.map_err(|_| FarmingError::CastTou128Error6)?)
Ok(
(casted_mul(arsw_reward, ACC_ARSW_PRECISION) / U256::from(lp_supply))
.try_into()
.map_err(|_| FarmingError::CastTou128Error6)?,
)
}

fn _get_period(&self, block_number: u32) -> Result<u32, FarmingError> {
Expand Down Expand Up @@ -578,15 +565,14 @@ pub trait Farming: Storage<Data> + Storage<ownable::Data> + FarmingGetters + Far
if period > MAX_PERIOD {
return Ok(0)
}
Ok(casted_mul(
Ok((casted_mul(
FIRST_PERIOD_REWERD_SUPPLY,
9u128
.checked_pow(period)
.ok_or(FarmingError::PowOverflow1)?
/ 10u128
.checked_pow(period)
.ok_or(FarmingError::PowOverflow2)?,
)
.ok_or(FarmingError::PowOverflow1)?,
) / 10u128
.checked_pow(period)
.ok_or(FarmingError::PowOverflow2)?)
.try_into()
.map_err(|_| FarmingError::CastTou128Error5)?)
}
Expand Down