Skip to content

Commit

Permalink
refactor: get_rewards and claim
Browse files Browse the repository at this point in the history
1. reverse vector indexing returned
2. change u128 to U128 for return type
2. claim reward requires one yocto near
  • Loading branch information
irfi committed Apr 17, 2021
1 parent f5e7798 commit cf74146
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ near call --accountId owner.testnet --networkId network_id contract_account new
### Claim reward

```
claim_reward({"amount":"1"})
claim_reward '{"amount":"1"}' --amount 0.000000000000000000000001
```

### Push reward - Only Owner
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use near_contract_standards::upgrade::Ownable;
near_sdk::setup_alloc!();

use crate::utils::{ext_fungible_token, GAS_FOR_FT_TRANSFER};
use crate::rewards::{Rewards, Reward};
use crate::rewards::{Rewards, Reward, RewardD};
mod utils;
mod rewards;
mod token_receiver;
Expand All @@ -31,8 +31,6 @@ impl Ownable for Contract {
}

fn set_owner(&mut self, owner: AccountId) {
self.assert_owner();
self.owner = owner;
}
}

Expand All @@ -58,10 +56,10 @@ impl Contract{
self.deposited_amount = self.deposited_amount.checked_add(amount).expect("ERR_INTEGER_OVERFLOW");
}

pub fn get_rewards(&self, from_index: u64, limit: u64, account_id: ValidAccountId) -> Vec<Reward> {
pub fn get_rewards(&self, from_index: u64, limit: u64, account_id: ValidAccountId) -> Vec<RewardD> {
let user_rewards = self.records.get(account_id.as_ref()).unwrap();
(from_index..std::cmp::min(from_index + limit, user_rewards.get_rewards_len()))
.map(|index| user_rewards.get_reward(index))
(from_index..std::cmp::min(from_index + limit, user_rewards.get_rewards_len())).rev()
.map(|index| user_rewards.get_reward(index).to_reward_d())
.collect()
}

Expand All @@ -71,7 +69,9 @@ impl Contract{
}


#[payable]
pub fn claim_reward(&mut self, amount: U128) -> Promise {
assert_one_yocto();
let mut current_rewards = self.records.get(&env::predecessor_account_id()).unwrap();
let current_amount = current_rewards.internal_reward_amount();
let amount: u128 = amount.into();
Expand Down Expand Up @@ -211,7 +211,7 @@ mod tests {
contract.push_reward(accounts(3), TEN_PARAS_TOKEN, "first reward".to_string());
testing_env!(context
.predecessor_account_id(accounts(3))
.attached_deposit(0)
.attached_deposit(1)
.build());
contract.claim_reward(TEN_PARAS_TOKEN);
assert_eq!(contract.get_reward_amount(accounts(3)), U128(0));
Expand Down
23 changes: 21 additions & 2 deletions src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ use near_sdk::collections::{Vector};
use near_sdk::serde::{Deserialize, Serialize};


#[derive(BorshDeserialize, BorshSerialize, Deserialize, Serialize)]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Reward {
amount: u128,
memo: String,
}

#[derive(Deserialize, Serialize)]
pub struct RewardD {
amount: U128,
memo: String
}


#[derive(BorshDeserialize, BorshSerialize)]
pub struct Rewards {
Expand Down Expand Up @@ -57,12 +63,25 @@ impl Reward {
memo: memo,
}
}

pub fn get_amount(&self) -> u128 {
self.amount
}
pub fn get_memo(&self) -> String {
self.memo.clone()
}

pub fn to_reward_d(&self) -> RewardD {
RewardD::new(self)
}
}

impl RewardD {
pub fn new(
reward: &Reward
) -> Self {
Self {
amount: reward.get_amount().into(),
memo: reward.get_memo()
}
}
}

0 comments on commit cf74146

Please sign in to comment.