Skip to content

Commit

Permalink
Merge branch 'release/0.3.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
Zikriya committed Jun 1, 2023
2 parents 64b72fe + 4bed70b commit 215ceac
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 276 deletions.
63 changes: 36 additions & 27 deletions staking_contract/staking_contract/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ pub const EARLY_WITHDRAW_REWARD: &str = "early_withdraw_reward";
pub const STAKED_TOTAL: &str = "staked_total";
pub const REWARD_BALANCE: &str = "reward_balance";
pub const STAKED_BALANCE: &str = "staked_balance";
const AMOUNT: &str = "amount";
const REWARD: &str = "reward";
const TOKEN_ADDRESS: &str = "token_address";
const STAKER_ADDRESS: &str = "staker_address";
const REWARD_AMOUNT: &str = "reward_amount";
const STAKED_AMOUNT: &str = "staked_amount";
const REQUESTED_AMOUNT: &str = "requested_amount";
const WITHDRAWABLE_AMOUNT: &str = "withdrawable_amount";

const EVENT_TYPE: &str = "event_type";

const STAKE: &str = "stake";
const PAID_OUT: &str = "paid_out";
const ADD_REWARD: &str = "add_reward";
const REFUNDED: &str = "refunded";

// Structure for managing staked tokens
pub struct StakedTokens {
Expand Down Expand Up @@ -72,7 +87,9 @@ impl StakedTokens {
let staked_amount = self
.get_amount_staked_by_address(owner)
.ok_or(Error::NotAStaker)?;
let new_amount = staked_amount - *amount;
let new_amount = staked_amount
.checked_sub(*amount)
.ok_or(Error::CheckedSub)?;
self.addresses_staked_dict
.set(&key_to_str(owner), new_amount);
Ok(())
Expand Down Expand Up @@ -136,7 +153,7 @@ pub fn withdraw_ends() -> u64 {

/// Sets the withdrawal end time
pub fn set_withdraw_ends(withdraw_ends: u64) {
set_key(WITHDRAW_STARTS, withdraw_ends);
set_key(WITHDRAW_ENDS, withdraw_ends);
}

/// Retrieves the total staking amount
Expand All @@ -149,16 +166,6 @@ pub fn set_staking_total(staking_total: U256) {
set_key(STAKING_TOTAL, staking_total);
}

/// Retrieves the stake balance
pub fn stake_balance() -> u64 {
get_key(STAKED_BALANCE).unwrap_or_default()
}

/// Sets the stake balance
pub fn set_stake_balance(stake_balance: u64) {
set_key(STAKED_BALANCE, stake_balance);
}

/// Retrieves the total reward amount
pub fn total_reward() -> U256 {
get_key(TOTAL_REWARD).unwrap_or_default()
Expand Down Expand Up @@ -236,14 +243,14 @@ pub fn emit(event: &StakingContractEvent) {
} => {
let mut param = BTreeMap::new();
param.insert(CONTRACT_PACKAGE_HASH, package.to_string());
param.insert("event_type", "stake".to_string());
param.insert("token_address", token_address.to_string());
param.insert(EVENT_TYPE, STAKE.to_string());
param.insert(TOKEN_ADDRESS, token_address.to_string());
param.insert(
"staker_address",
STAKER_ADDRESS,
TryInto::<String>::try_into(*staker_address).unwrap(),
);
param.insert("requested_amount", requested_amount.to_string());
param.insert("staked_amount", staked_amount.to_string());
param.insert(REQUESTED_AMOUNT, requested_amount.to_string());
param.insert(STAKED_AMOUNT, staked_amount.to_string());
events.push(param);
}
StakingContractEvent::PaidOut {
Expand All @@ -254,13 +261,14 @@ pub fn emit(event: &StakingContractEvent) {
} => {
let mut param = BTreeMap::new();
param.insert(CONTRACT_PACKAGE_HASH, package.to_string());
param.insert("token_address", token_address.to_string());
param.insert(EVENT_TYPE, PAID_OUT.to_string());
param.insert(TOKEN_ADDRESS, token_address.to_string());
param.insert(
"staker_address",
STAKER_ADDRESS,
TryInto::<String>::try_into(*staker_address).unwrap(),
);
param.insert("amount", amount.to_string());
param.insert("reward", reward.to_string());
param.insert(AMOUNT, amount.to_string());
param.insert(REWARD, reward.to_string());
events.push(param);
}
StakingContractEvent::AddReward {
Expand All @@ -269,9 +277,9 @@ pub fn emit(event: &StakingContractEvent) {
} => {
let mut param = BTreeMap::new();
param.insert(CONTRACT_PACKAGE_HASH, package.to_string());
param.insert("event_type", "add_reward".to_string());
param.insert("reward_amount", reward_amount.to_string());
param.insert("withdrawable_amount", withdrawable_amount.to_string());
param.insert(EVENT_TYPE, ADD_REWARD.to_string());
param.insert(REWARD_AMOUNT, reward_amount.to_string());
param.insert(WITHDRAWABLE_AMOUNT, withdrawable_amount.to_string());
events.push(param);
}
StakingContractEvent::Refunded {
Expand All @@ -281,12 +289,13 @@ pub fn emit(event: &StakingContractEvent) {
} => {
let mut param = BTreeMap::new();
param.insert(CONTRACT_PACKAGE_HASH, package.to_string());
param.insert("token_address", token_address.to_string());
param.insert(EVENT_TYPE, REFUNDED.to_string());
param.insert(TOKEN_ADDRESS, token_address.to_string());
param.insert(
"staker_address",
STAKER_ADDRESS,
TryInto::<String>::try_into(*staker_address).unwrap(),
);
param.insert("amount", amount.to_string());
param.insert(AMOUNT, amount.to_string());
events.push(param);
}
};
Expand Down
38 changes: 23 additions & 15 deletions staking_contract/staking_contract/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,42 @@ pub enum Error {
WrongArguments = 2,
/// Not required stake.
NotRequiredStake = 3,
/// Bad timing.
BadTiming = 4,
/// After bad timing.
AfterBadTiming = 4,
/// Before bad timing.
BeforeBadTiming = 5,
/// Invalid context.
InvalidContext = 5,
InvalidContext = 6,
/// Negative reward.
NegativeReward = 6,
NegativeReward = 7,
/// Negative withdrawable reward.
NegativeWithdrawableReward = 7,
NegativeWithdrawableReward = 8,
/// Negative amount.
NegativeAmount = 8,
NegativeAmount = 9,
/// Missing contract package hash.
MissingContractPackageHash = 9,
MissingContractPackageHash = 10,
/// Invalid contract package hash.
InvalidContractPackageHash = 10,
InvalidContractPackageHash = 11,
/// Invalid contract hash.
InvalidContractHash = 11,
InvalidContractHash = 12,
/// Withdraw check error early.
WithdrawCheckErrorEarly = 12,
WithdrawCheckErrorEarly = 13,
/// Withdraw check error.
WithdrawCheckError = 13,
WithdrawCheckError = 14,
/// Neither account hash nor neither contract package hash.
NeitherAccountHashNorNeitherContractPackageHash = 14,
NeitherAccountHashNorNeitherContractPackageHash = 15,
/// Not a staker.
NotAStaker = 15,
NotAStaker = 16,
/// Immediate caller address fail.
ImmediateCallerAddressFail = 16,
ImmediateCallerAddressFail = 17,
/// Not staking contract package hash.
NotStakingContractPackageHash = 17,
NotStakingContractPackageHash = 18,
StakingEndsBeforeStakingStarts = 19,
WithdrawStartsStakingEnds = 20,
WithdrawEndsWithdrawStarts = 21,
StakingStartsNow = 22,
/// Subtraction underflow
CheckedSub = 23,
}

impl From<Error> for ApiError {
Expand Down
Loading

0 comments on commit 215ceac

Please sign in to comment.