Skip to content

Commit

Permalink
Merge pull request #8 from ParasHQ/dev
Browse files Browse the repository at this point in the history
Fix: reverse from_index for get_rewards
  • Loading branch information
Mohamad Irfianto authored Apr 29, 2021
2 parents f860cde + 9ce3f2e commit c0d1182
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
"description": "Paras claim rewards contract",
"scripts": {
"build": "./build.sh",
"test": "cargo test -- --nocapture --color always",
"test": "cargo test --all -- --nocapture --color always",
"deploy:contract:dev": "yarn build && NODE_ENV=testnet near dev-deploy --wasmFile ./res/paras_claim_rewards_contract.wasm",
"deploy:contract:testnet": "yarn build && NODE_ENV=testnet node deployContract.js",
"deploy:contract:mainnet": "yarn build && NODE_ENV=mainnet node deployContract.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/near-examples/rust-status-message.git"
"url": "git+https://github.com/ParasHQ/paras-claim-rewards-contract.git"
},
"keywords": [
"near-protocol",
"blockchain",
"rust",
"smart-contract"
],
"author": "Paras",
"author": "irfi <[email protected]>",
"license": "(MIT AND Apache-2.0)",
"dependencies": {
"axios": "^0.21.1",
Expand Down
33 changes: 7 additions & 26 deletions paras-claim-rewards-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{env, near_bindgen, AccountId, PanicOnDefault, assert_one_yocto, Promise, log};
use near_sdk::json_types::{ValidAccountId, U128};
use near_sdk::collections::{LookupMap};
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, RewardD};
use crate::rewards::{Rewards, Reward, WrappedReward};
mod utils;
mod rewards;
mod token_receiver;


/*
Implementation of claim rewards.
*/
Expand All @@ -25,15 +23,6 @@ pub struct Contract {
deposited_amount: u128,
}

impl Ownable for Contract {
fn get_owner(&self) -> AccountId {
self.owner.clone()
}

fn set_owner(&mut self, owner: AccountId) {
}
}

#[near_bindgen]
impl Contract{
#[init]
Expand All @@ -51,15 +40,15 @@ impl Contract{
this
}

#[private]
pub fn internal_deposit(&mut self, amount: u128) {
fn internal_deposit(&mut self, amount: u128) {
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<RewardD> {
pub fn get_rewards(&self, from_index: u64, limit: u64, account_id: ValidAccountId) -> Vec<WrappedReward> {
let user_rewards = self.records.get(account_id.as_ref()).unwrap();
(from_index..std::cmp::min(from_index + limit, user_rewards.get_rewards_len())).rev()
.map(|index| user_rewards.get_reward(index).to_reward_d())
let end_index = user_rewards.get_rewards_len().saturating_sub(from_index);
(end_index.saturating_sub(limit)..end_index).rev()
.map(|index| user_rewards.get_reward(index).to_wreward())
.collect()
}

Expand Down Expand Up @@ -94,7 +83,7 @@ impl Contract{

#[payable]
pub fn push_reward(&mut self, account_id: ValidAccountId, amount: U128, memo: String) {
self.assert_owner();
assert_eq!(self.owner, env::predecessor_account_id(), "ERR_NOT_OWNER");
assert_one_yocto();
assert!(self.deposited_amount >= amount.into(), "ERR_DEPOSITED_AMOUNT_NOT_ENOUGH");
let mut current_rewards = self.records.get(account_id.as_ref()).unwrap_or(Rewards::new(account_id.clone().into()));
Expand All @@ -114,14 +103,6 @@ impl Contract{

}

/*
pub fn init_reward(&mut self, account_id: ValidAccountId) {
self.assert_owner();
assert!(self.records.contains_key(account_id.as_ref()), "ERR_ACCOUNT_ALREADY_EXIST");
self.records.insert(account_id.as_ref(), &Rewards::new());
}
*/
}

#[cfg(all(test, not(target_arch = "wasm32")))]
Expand Down
8 changes: 4 additions & 4 deletions paras-claim-rewards-contract/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Reward {
}

#[derive(Deserialize, Serialize)]
pub struct RewardD {
pub struct WrappedReward {
amount: U128,
memo: String
}
Expand Down Expand Up @@ -70,12 +70,12 @@ impl Reward {
self.memo.clone()
}

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

impl RewardD {
impl WrappedReward {
pub fn new(
reward: &Reward
) -> Self {
Expand Down
Binary file modified res/paras_claim_rewards_contract.wasm
Binary file not shown.

0 comments on commit c0d1182

Please sign in to comment.