Skip to content

Commit

Permalink
fixed warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
VladasZ committed Nov 3, 2023
1 parent d11e46f commit 12ae185
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 154 deletions.
135 changes: 74 additions & 61 deletions contract/src/burn/api.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
use model::{api::BurnApi, TokensAmount, UnixTimestamp};
use near_sdk::{
env, ext_contract, is_promise_success, json_types::U128, near_bindgen, require, serde_json::json, Gas, Promise,
PromiseOrValue,
};
use near_sdk::{json_types::U128, near_bindgen, require, PromiseOrValue};

#[cfg(test)]
use crate::common::tests::data::get_test_future_success;
use crate::{common::unix_timestamp, Contract, ContractExt};

#[cfg(test)]
pub(crate) const EXT_BURN_FUTURE: &str = "ext_burn";
use crate::{common::now_seconds, Contract, ContractExt};

#[near_bindgen]
impl BurnApi for Contract {
Expand All @@ -22,7 +14,7 @@ impl BurnApi for Contract {

let mut total_to_burn = 0;
let mut keys_to_remove: Vec<UnixTimestamp> = vec![];
let now: UnixTimestamp = unix_timestamp(env::block_timestamp_ms());
let now = now_seconds();

for (datetime, (_, total)) in self.accruals.iter() {
if now - datetime >= self.burn_period {
Expand All @@ -41,57 +33,7 @@ impl BurnApi for Contract {
}
}

#[cfg(not(test))]
#[ext_contract(ext_self)]
pub trait SelfCallback {
fn on_burn(&mut self, total_to_burn: TokensAmount, keys_to_remove: Vec<UnixTimestamp>) -> U128;
}

#[cfg(not(test))]
#[near_bindgen]
impl SelfCallback for Contract {
fn on_burn(&mut self, total_to_burn: TokensAmount, keys_to_remove: Vec<UnixTimestamp>) -> U128 {
self.on_burn_internal(total_to_burn, keys_to_remove, is_promise_success())
}
}

impl Contract {
#[cfg(not(test))]
fn burn_external(
&mut self,
total_to_burn: TokensAmount,
keys_to_remove: Vec<UnixTimestamp>,
) -> PromiseOrValue<U128> {
let args = json!({
"amount": U128(total_to_burn),
})
.to_string()
.as_bytes()
.to_vec();

Promise::new(self.token_account_id.clone())
.function_call("burn".to_string(), args, 0, Gas(5 * Gas::ONE_TERA.0))
.then(
ext_self::ext(env::current_account_id())
.with_static_gas(Gas(5 * Gas::ONE_TERA.0))
.on_burn(total_to_burn, keys_to_remove),
)
.into()
}

#[cfg(test)]
fn burn_external(
&mut self,
total_to_burn: TokensAmount,
keys_to_remove: Vec<UnixTimestamp>,
) -> PromiseOrValue<U128> {
PromiseOrValue::Value(self.on_burn_internal(
total_to_burn,
keys_to_remove,
get_test_future_success(EXT_BURN_FUTURE),
))
}

fn on_burn_internal(
&mut self,
total_to_burn: TokensAmount,
Expand All @@ -111,3 +53,74 @@ impl Contract {
}
}
}

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

use crate::{Contract, ContractExt};

#[ext_contract(ext_self)]
pub trait SelfCallback {
fn on_burn(&mut self, total_to_burn: TokensAmount, keys_to_remove: Vec<UnixTimestamp>) -> U128;
}

#[near_bindgen]
impl SelfCallback for Contract {
fn on_burn(&mut self, total_to_burn: TokensAmount, keys_to_remove: Vec<UnixTimestamp>) -> U128 {
self.on_burn_internal(total_to_burn, keys_to_remove, is_promise_success())
}
}

impl Contract {
pub(crate) fn burn_external(
&mut self,
total_to_burn: TokensAmount,
keys_to_remove: Vec<UnixTimestamp>,
) -> PromiseOrValue<U128> {
let args = json!({
"amount": U128(total_to_burn),
})
.to_string()
.as_bytes()
.to_vec();

Promise::new(self.token_account_id.clone())
.function_call("burn".to_string(), args, 0, Gas(5 * Gas::ONE_TERA.0))
.then(
ext_self::ext(env::current_account_id())
.with_static_gas(Gas(5 * Gas::ONE_TERA.0))
.on_burn(total_to_burn, keys_to_remove),
)
.into()
}
}
}

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

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

pub(crate) const EXT_BURN_FUTURE: &str = "ext_burn";

impl Contract {
pub(crate) fn burn_external(
&mut self,
total_to_burn: TokensAmount,
keys_to_remove: Vec<UnixTimestamp>,
) -> PromiseOrValue<U128> {
PromiseOrValue::Value(self.on_burn_internal(
total_to_burn,
keys_to_remove,
get_test_future_success(EXT_BURN_FUTURE),
))
}
}
}
2 changes: 1 addition & 1 deletion contract/src/burn/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use model::api::{BurnApi, ClaimApi, RecordApi};
use near_sdk::{json_types::U128, PromiseOrValue};

use crate::{
burn::api::EXT_BURN_FUTURE,
burn::api::test::EXT_BURN_FUTURE,
common::tests::{data::set_test_future_success, Context},
};

Expand Down
173 changes: 93 additions & 80 deletions contract/src/claim/api.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
use model::{api::ClaimApi, AccrualIndex, ClaimAvailabilityView, TokensAmount, UnixTimestamp};
use near_sdk::{
env, ext_contract, is_promise_success, json_types::U128, near_bindgen, require, serde_json::json, store::Vector,
AccountId, Gas, Promise, PromiseOrValue,
};
use model::{api::ClaimApi, ClaimAvailabilityView, TokensAmount, UnixTimestamp};
use near_sdk::{env, json_types::U128, near_bindgen, require, store::Vector, AccountId, PromiseOrValue};

#[cfg(test)]
use crate::common::tests::data::get_test_future_success;
use crate::{common::unix_timestamp, Contract, ContractExt, StorageKey::AccrualsEntry};

#[cfg(test)]
pub(crate) const EXT_TRANSFER_FUTURE: &str = "ext_transfer";
use crate::{common::now_seconds, Contract, ContractExt, StorageKey::AccrualsEntry};

#[near_bindgen]
impl ClaimApi for Contract {
fn get_claimable_balance_for_account(&self, account_id: AccountId) -> U128 {
if let Some(account_data) = self.accounts.get(&account_id) {
let mut total_accrual: TokensAmount = 0;
let now: UnixTimestamp = unix_timestamp(env::block_timestamp_ms());
let now = now_seconds();

for (datetime, index) in &account_data.accruals {
if now - datetime > self.burn_period {
Expand All @@ -42,7 +34,7 @@ impl ClaimApi for Contract {
return ClaimAvailabilityView::Available;
};

let now_seconds = unix_timestamp(env::block_timestamp_ms());
let now_seconds = now_seconds();

if now_seconds - last_claim_at > self.claim_period {
ClaimAvailabilityView::Available
Expand All @@ -64,7 +56,7 @@ impl ClaimApi for Contract {

let account_data = self.accounts.get_mut(&account_id).expect("Account data is not found");

let now = unix_timestamp(env::block_timestamp_ms());
let now = now_seconds();
let mut total_accrual: TokensAmount = 0;
let mut details: Vec<(UnixTimestamp, TokensAmount)> = vec![];

Expand Down Expand Up @@ -94,72 +86,7 @@ impl ClaimApi for Contract {
}
}

#[cfg(not(test))]
#[ext_contract(ext_self)]
pub trait SelfCallback {
fn on_transfer(
&mut self,
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> U128;
}

#[cfg(not(test))]
#[near_bindgen]
impl SelfCallback for Contract {
fn on_transfer(
&mut self,
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> U128 {
self.on_transfer_internal(account_id, total_accrual, details, is_promise_success())
}
}

impl Contract {
#[cfg(not(test))]
fn transfer_external(
&mut self,
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> PromiseOrValue<U128> {
let args = json!({
"receiver_id": account_id,
"amount": total_accrual.to_string(),
"memo": "",
})
.to_string()
.as_bytes()
.to_vec();

Promise::new(self.token_account_id.clone())
.function_call("ft_transfer".to_string(), args, 1, Gas(5 * Gas::ONE_TERA.0))
.then(
ext_self::ext(env::current_account_id())
.with_static_gas(Gas(5 * Gas::ONE_TERA.0))
.on_transfer(account_id, total_accrual, details),
)
.into()
}

#[cfg(test)]
fn transfer_external(
&mut self,
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> PromiseOrValue<U128> {
PromiseOrValue::Value(self.on_transfer_internal(
account_id,
total_accrual,
details,
get_test_future_success(EXT_TRANSFER_FUTURE),
))
}

fn on_transfer_internal(
&mut self,
account_id: AccountId,
Expand All @@ -170,7 +97,7 @@ impl Contract {
let account = self.accounts.get_mut(&account_id).expect("Account not found");

if is_success {
account.last_claim_at = Some(unix_timestamp(env::block_timestamp_ms()));
account.last_claim_at = Some(now_seconds());

U128(total_accrual)
} else {
Expand All @@ -191,3 +118,89 @@ impl Contract {
}
}
}

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

use crate::{Contract, ContractExt};

#[ext_contract(ext_self)]
pub trait SelfCallback {
fn on_transfer(
&mut self,
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> U128;
}

#[near_bindgen]
impl SelfCallback for Contract {
fn on_transfer(
&mut self,
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> U128 {
self.on_transfer_internal(account_id, total_accrual, details, is_promise_success())
}
}

impl Contract {
pub(crate) fn transfer_external(
&mut self,
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> PromiseOrValue<U128> {
let args = json!({
"receiver_id": account_id,
"amount": total_accrual.to_string(),
"memo": "",
})
.to_string()
.as_bytes()
.to_vec();

Promise::new(self.token_account_id.clone())
.function_call("ft_transfer".to_string(), args, 1, Gas(5 * Gas::ONE_TERA.0))
.then(
ext_self::ext(env::current_account_id())
.with_static_gas(Gas(5 * Gas::ONE_TERA.0))
.on_transfer(account_id, total_accrual, details),
)
.into()
}
}
}

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

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

pub(crate) const EXT_TRANSFER_FUTURE: &str = "ext_transfer";

impl Contract {
pub(crate) fn transfer_external(
&mut self,
account_id: AccountId,
total_accrual: TokensAmount,
details: Vec<(UnixTimestamp, TokensAmount)>,
) -> PromiseOrValue<U128> {
PromiseOrValue::Value(self.on_transfer_internal(
account_id,
total_accrual,
details,
get_test_future_success(EXT_TRANSFER_FUTURE),
))
}
}
}
2 changes: 1 addition & 1 deletion contract/src/claim/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use model::{
use near_sdk::{json_types::U128, PromiseOrValue};

use crate::{
claim::api::EXT_TRANSFER_FUTURE,
claim::api::test::EXT_TRANSFER_FUTURE,
common::tests::{data::set_test_future_success, Context},
};

Expand Down
Loading

0 comments on commit 12ae185

Please sign in to comment.