Skip to content

Commit

Permalink
ref: extract payout calculation to model (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyafromrussia authored Jan 25, 2024
1 parent 09aa633 commit f521ee9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
8 changes: 5 additions & 3 deletions integration-tests/src/mint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use integration_utils::misc::ToNear;
use near_sdk::json_types::{U128, U64};
use sweat_model::{FungibleTokenCoreIntegration, SweatApiIntegration};
use sweat_model::{FungibleTokenCoreIntegration, Payout, SweatApiIntegration};

use crate::prepare::{prepare_contract, IntegrationContext};

Expand Down Expand Up @@ -30,11 +30,13 @@ async fn test_mint() -> anyhow::Result<()> {
.call()
.await?;

let target_payout = Payout::from(TARGET_BALANCE);

let result = context.ft_contract().ft_balance_of(oracle.to_near()).call().await?;
assert_eq!(result, U128(TARGET_BALANCE * 5 / 100));
assert_eq!(result, U128(target_payout.fee));

let result = context.ft_contract().ft_balance_of(user.to_near()).call().await?;
assert_eq!(result, U128(TARGET_BALANCE * 95 / 100));
assert_eq!(result, U128(target_payout.amount_for_user));

let result = context.ft_contract().get_steps_since_tge().call().await?;
assert_eq!(result, U64(TARGET_STEPS_SINCE_TGE as u64));
Expand Down
16 changes: 16 additions & 0 deletions model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,19 @@ pub trait StorageManagement {
pub trait IntegrationTestMethods {
fn calculate_payout_with_fee_for_batch(&self, batch_size: u32, claim_amount: u32) -> (U128, U128);
}

pub struct Payout {
pub amount_for_user: u128,
pub fee: u128,
}

impl From<u128> for Payout {
fn from(value: u128) -> Self {
let fee = (value * 5).div_ceil(100);

Self {
fee,
amount_for_user: value - fee,
}
}
}
Binary file modified res/sweat.wasm
Binary file not shown.
9 changes: 4 additions & 5 deletions sweat-integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use near_sdk::{
};
use near_workspaces::{types::NearToken, Contract};
use sweat_model::{
FungibleTokenCoreIntegration, IntegrationTestMethodsIntegration, StorageManagementIntegration, SweatApiIntegration,
SweatDeferIntegration,
FungibleTokenCoreIntegration, IntegrationTestMethodsIntegration, Payout, StorageManagementIntegration,
SweatApiIntegration, SweatDeferIntegration,
};

pub const FT_CONTRACT: &str = "sweat";
Expand Down Expand Up @@ -200,10 +200,9 @@ impl IntegrationTestMethodsIntegration for SweatFt<'_> {
impl SweatFt<'_> {
pub async fn formula_detailed(&self, steps_since_tge: U64, steps: u32) -> anyhow::Result<(U128, U128, U128)> {
let token_amount = self.formula(steps_since_tge, steps).call().await?.0;
let fee = token_amount * 5 / 100;
let effective_amount = token_amount - fee;
let payout = Payout::from(token_amount);

Ok((U128(fee), U128(effective_amount), U128(token_amount)))
Ok((U128(payout.fee), U128(payout.amount_for_user), U128(token_amount)))
}
}

Expand Down
9 changes: 4 additions & 5 deletions sweat/src/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use near_sdk::{
json_types::{U128, U64},
near_bindgen,
};
use sweat_model::{IntegrationTestMethods, SweatApi};
use sweat_model::{IntegrationTestMethods, Payout, SweatApi};

use crate::{Contract, ContractExt};

Expand All @@ -20,11 +20,10 @@ impl IntegrationTestMethods for Contract {

let minted = self.formula(U64(steps_since_tge.into()), steps).0;

let fee = (minted * 5).div_ceil(100);
let for_user = minted - fee;
let payout = Payout::from(minted);

total_fee += fee;
total_for_user += for_user;
total_fee += payout.fee;
total_for_user += payout.amount_for_user;
}

(U128(total_fee), U128(total_for_user))
Expand Down
7 changes: 3 additions & 4 deletions sweat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use near_sdk::{
json_types::{U128, U64},
near_bindgen, require, AccountId, Balance, PanicOnDefault, PromiseOrValue,
};
use sweat_model::SweatApi;
use sweat_model::{Payout, SweatApi};

mod defer;
mod integration;
Expand Down Expand Up @@ -153,10 +153,9 @@ impl SweatApi for Contract {
impl Contract {
pub(crate) fn calculate_tokens_amount(&self, steps: u32) -> (u128, u128) {
let sweat_to_mint: u128 = self.formula(self.steps_since_tge, steps).0;
let trx_oracle_fee: u128 = (sweat_to_mint * 5).div_ceil(100);
let minted_to_user: u128 = sweat_to_mint - trx_oracle_fee;
let payout = Payout::from(sweat_to_mint);

(minted_to_user, trx_oracle_fee)
(payout.amount_for_user, payout.fee)
}
}

Expand Down

0 comments on commit f521ee9

Please sign in to comment.