Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feature/events
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyafromrussia committed Nov 22, 2023
2 parents 735341a + d48208b commit cab58d5
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 27 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 14 additions & 15 deletions contract/src/claim/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use model::{
api::ClaimApi,
event::{emit, ClaimData, EventKind},
ClaimAvailabilityView, TokensAmount, UnixTimestamp,
ClaimAvailabilityView, ClaimResultView, TokensAmount, UnixTimestamp,
};
use near_sdk::{env, json_types::U128, near_bindgen, require, store::Vector, AccountId, PromiseOrValue};

Expand Down Expand Up @@ -50,7 +50,7 @@ impl ClaimApi for Contract {
}
}

fn claim(&mut self) -> PromiseOrValue<U128> {
fn claim(&mut self) -> PromiseOrValue<ClaimResultView> {
let account_id = env::predecessor_account_id();

require!(
Expand Down Expand Up @@ -89,7 +89,7 @@ impl ClaimApi for Contract {
if total_accrual > 0 {
self.transfer_external(now, account_id, total_accrual, details)
} else {
PromiseOrValue::Value(U128(0))
PromiseOrValue::Value(ClaimResultView::new(0))
}
}
}
Expand All @@ -102,7 +102,7 @@ impl Contract {
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
is_success: bool,
) -> U128 {
) -> ClaimResultView {
let account = self.accounts.get_mut(&account_id).expect("Account not found");

if is_success {
Expand All @@ -118,7 +118,7 @@ impl Contract {
};
emit(EventKind::Claim(event_data));

return U128(total_accrual);
return ClaimResultView::new(total_accrual);
}

for (timestamp, amount) in details {
Expand All @@ -133,16 +133,15 @@ impl Contract {
account.accruals.push((timestamp, daily_accruals.0.len() - 1));
}

U128(0)
ClaimResultView::new(0)
}
}

#[cfg(not(test))]
mod prod {
use model::{TokensAmount, UnixTimestamp};
use model::{ClaimResultView, TokensAmount, UnixTimestamp};
use near_sdk::{
env, ext_contract, is_promise_success, json_types::U128, near_bindgen, serde_json::json, AccountId, Gas,
Promise, PromiseOrValue,
env, ext_contract, is_promise_success, near_bindgen, serde_json::json, AccountId, Gas, Promise, PromiseOrValue,
};

use crate::{Contract, ContractExt};
Expand All @@ -155,7 +154,7 @@ mod prod {
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> U128;
) -> ClaimResultView;
}

#[near_bindgen]
Expand All @@ -166,7 +165,7 @@ mod prod {
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> U128 {
) -> ClaimResultView {
self.on_transfer_internal(now, account_id, total_accrual, details, is_promise_success())
}
}
Expand All @@ -178,7 +177,7 @@ mod prod {
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> PromiseOrValue<U128> {
) -> PromiseOrValue<ClaimResultView> {
let args = json!({
"receiver_id": account_id,
"amount": total_accrual.to_string(),
Expand All @@ -202,8 +201,8 @@ mod prod {

#[cfg(test)]
pub(crate) mod test {
use model::{TokensAmount, UnixTimestamp};
use near_sdk::{json_types::U128, AccountId, PromiseOrValue};
use model::{ClaimResultView, TokensAmount, UnixTimestamp};
use near_sdk::{AccountId, PromiseOrValue};

use crate::{common::tests::data::get_test_future_success, Contract};

Expand All @@ -216,7 +215,7 @@ pub(crate) mod test {
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> PromiseOrValue<U128> {
) -> PromiseOrValue<ClaimResultView> {
PromiseOrValue::Value(self.on_transfer_internal(
now,
account_id,
Expand Down
6 changes: 3 additions & 3 deletions contract/src/claim/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn test_claim_when_user_has_tokens_and_claim_period_is_passed() {
PromiseOrValue::Promise(_) => panic!("Expected value"),
PromiseOrValue::Value(value) => value,
};
assert_eq!(alice_balance, claimed_amount.0);
assert_eq!(alice_balance, claimed_amount.total.0);

let alice_new_balance = contract.get_claimable_balance_for_account(accounts.alice.clone()).0;
assert_eq!(0, alice_new_balance);
Expand All @@ -122,7 +122,7 @@ fn test_claim_when_user_has_tokens_and_burn_period_is_passed() {
PromiseOrValue::Promise(_) => panic!("Expected value"),
PromiseOrValue::Value(value) => value,
};
assert_eq!(0, claimed_amount.0);
assert_eq!(0, claimed_amount.total.0);

let alice_new_balance = contract.get_claimable_balance_for_account(accounts.alice.clone()).0;
assert_eq!(0, alice_new_balance);
Expand All @@ -144,7 +144,7 @@ fn test_claim_when_user_has_tokens_and_claim_period_is_passed_and_transfer_faile
PromiseOrValue::Promise(_) => panic!("Expected value"),
PromiseOrValue::Value(value) => value,
};
assert_eq!(0, claimed_amount.0);
assert_eq!(0, claimed_amount.total.0);

let alice_new_balance = contract.get_claimable_balance_for_account(accounts.alice.clone()).0;
assert_eq!(alice_balance, alice_new_balance);
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/interface/claim_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use model::{
AuthApiIntegration, BurnApiIntegration, ClaimApiIntegration, ConfigApiIntegration, InitApiIntegration,
RecordApiIntegration,
},
ClaimAvailabilityView, Duration,
ClaimAvailabilityView, ClaimResultView, Duration,
};
use near_sdk::{json_types::U128, AccountId};
use near_workspaces::{Account, Contract};
Expand Down Expand Up @@ -124,7 +124,7 @@ impl ClaimApiIntegration for SweatClaim<'_> {
.await
}

async fn claim(&mut self) -> Result<U128> {
async fn claim(&mut self) -> Result<ClaimResultView> {
self.call_user("claim", ()).await
}
}
Expand Down
1 change: 1 addition & 0 deletions model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ anyhow = { workspace = true }
near-sdk = { workspace = true }
async-trait = { workspace = true }
integration-trait = { workspace = true }
serde = "1.0.193"
4 changes: 2 additions & 2 deletions model/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use integration_trait::make_integration_version;
use near_sdk::{json_types::U128, AccountId, PromiseOrValue};

use crate::{ClaimAvailabilityView, Duration};
use crate::{ClaimAvailabilityView, ClaimResultView, Duration};

#[make_integration_version]
pub trait InitApi {
Expand Down Expand Up @@ -35,5 +35,5 @@ pub trait RecordApi {
pub trait ClaimApi {
fn get_claimable_balance_for_account(&self, account_id: AccountId) -> U128;
fn is_claim_available(&self, account_id: AccountId) -> ClaimAvailabilityView;
fn claim(&mut self) -> PromiseOrValue<U128>;
fn claim(&mut self) -> PromiseOrValue<ClaimResultView>;
}
16 changes: 15 additions & 1 deletion model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ pub mod account_record;
pub mod api;
pub mod event;

use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{
json_types::U128,
serde::{Deserialize, Serialize},
};

pub type UnixTimestamp = u32;
pub type AccrualIndex = u32;
Expand All @@ -16,3 +19,14 @@ pub enum ClaimAvailabilityView {
Unavailable((UnixTimestamp, Duration)),
Unregistered,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct ClaimResultView {
pub total: U128,
}

impl ClaimResultView {
pub fn new(total: u128) -> Self {
Self { total: U128(total) }
}
}
Binary file modified res/sweat.wasm
Binary file not shown.
Binary file modified res/sweat_claim.wasm
Binary file not shown.

0 comments on commit cab58d5

Please sign in to comment.