Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl BorrowPosition {
collateral_asset_deposit: 0.into(),
borrow_asset_principal: 0.into(),
// Start from current (not next) snapshot to avoid the possibility
// of borrowing "for free". e.g. if ChainTime units are epochs (12
// of borrowing "for free". e.g. if TimeChunk units are epochs (12
// hours), this prevents someone from getting 11 hours of free
// borrowing if they create the borrow 1 hour into the epoch.
borrow_asset_fees: Accumulator::new(current_snapshot_index),
Expand Down
19 changes: 0 additions & 19 deletions common/src/chain_time.rs

This file was deleted.

2 changes: 1 addition & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod accumulator;
pub mod asset;
pub mod borrow;
pub mod chain_time;
pub mod event;
pub mod fee;
pub mod interest_rate_strategy;
Expand All @@ -11,6 +10,7 @@ pub mod oracle;
pub mod snapshot;
pub mod static_yield;
pub mod supply;
pub mod time_chunk;
pub mod util;
pub mod withdrawal_queue;

Expand Down
2 changes: 2 additions & 0 deletions common/src/market/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use crate::{
fee::{Fee, TimeBasedFee},
interest_rate_strategy::InterestRateStrategy,
number::Decimal,
time_chunk::TimeChunkConfiguration,
};

use super::{AssetConversion, AssetValuation, BalanceOracleConfiguration, PricePair, YieldWeights};

#[derive(Clone, Debug)]
#[near(serializers = [json, borsh])]
pub struct MarketConfiguration {
pub time_chunk_configuration: TimeChunkConfiguration,
pub borrow_asset: FungibleAsset<BorrowAsset>,
pub collateral_asset: FungibleAsset<CollateralAsset>,
pub balance_oracle: BalanceOracleConfiguration,
Expand Down
9 changes: 4 additions & 5 deletions common/src/market/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use near_sdk::{
use crate::{
asset::BorrowAssetAmount,
borrow::{BorrowPosition, LinkedBorrowPosition, LinkedBorrowPositionMut},
chain_time::ChainTime,
event::MarketEvent,
market::MarketConfiguration,
number::Decimal,
Expand Down Expand Up @@ -89,18 +88,18 @@ impl Market {
}

fn snapshot_with_yield_distribution(&mut self, yield_distribution: BorrowAssetAmount) -> u32 {
let chain_time = ChainTime::now();
let time_chunk = self.configuration.time_chunk_configuration.now();

if let Some((last_index, old_snapshot)) =
self.snapshots.len().checked_sub(1).and_then(|last_index| {
self.snapshots
.get(last_index)
.filter(|s| s.chain_time == chain_time)
.filter(|s| s.time_chunk == time_chunk)
.map(|s| (last_index, s))
})
{
let new_snapshot = Snapshot {
chain_time,
time_chunk,
timestamp_ms: old_snapshot.timestamp_ms,
deposited: self.borrow_asset_deposited,
borrowed: self.borrow_asset_borrowed,
Expand All @@ -115,7 +114,7 @@ impl Market {
} else {
let index = self.snapshots.len();
let new_snapshot = Snapshot {
chain_time,
time_chunk,
timestamp_ms: env::block_timestamp_ms().into(),
deposited: self.borrow_asset_deposited,
borrowed: self.borrow_asset_borrowed,
Expand Down
4 changes: 2 additions & 2 deletions common/src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use near_sdk::{json_types::U64, near};

use crate::{asset::BorrowAssetAmount, chain_time::ChainTime};
use crate::{asset::BorrowAssetAmount, time_chunk::TimeChunk};

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[near(serializers = [borsh, json])]
pub struct Snapshot {
pub chain_time: ChainTime,
pub time_chunk: TimeChunk,
pub timestamp_ms: U64,
pub deposited: BorrowAssetAmount,
pub borrowed: BorrowAssetAmount,
Expand Down
28 changes: 28 additions & 0 deletions common/src/time_chunk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use near_sdk::{env, json_types::U64, near};

/// Configure a method of determining the current time chunk.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[near(serializers = [json, borsh])]
pub enum TimeChunkConfiguration {
BlockHeight { divisor: U64 },
EpochHeight { divisor: U64 },
BlockTimestampMs { divisor: U64 },
}

impl TimeChunkConfiguration {
pub fn now(&self) -> TimeChunk {
let (time, U64(mut divisor)) = match self {
Self::BlockHeight { divisor } => (env::block_height(), divisor),
Self::EpochHeight { divisor } => (env::epoch_height(), divisor),
Self::BlockTimestampMs { divisor } => (env::block_timestamp_ms(), divisor),
};
if divisor == 0 {
divisor = 1;
}
TimeChunk(U64(time / divisor))
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[near(serializers = [borsh, json])]
pub struct TimeChunk(pub U64);
4 changes: 4 additions & 0 deletions test-utils/examples/generate_testnet_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ use templar_common::{
market::{BalanceOracleConfiguration, MarketConfiguration, YieldWeights},
number::Decimal,
oracle::pyth::PriceIdentifier,
time_chunk::TimeChunkConfiguration,
};

pub fn main() {
println!(
"{{\"configuration\":{}}}",
serde_json::to_string(&MarketConfiguration {
time_chunk_configuration: TimeChunkConfiguration::BlockTimestampMs {
divisor: (1000u64 * 60 * 10).into(), // every 10 minutes
},
borrow_asset: FungibleAsset::nep141("usdt.fakes.testnet".parse().unwrap()),
collateral_asset: FungibleAsset::nep141("wrap.testnet".parse().unwrap()),
balance_oracle: BalanceOracleConfiguration {
Expand Down
5 changes: 4 additions & 1 deletion test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ impl TestController {

println!("Market snapshots:");
for (i, snapshot) in snapshots.iter().enumerate() {
println!("\t{i}: {}", snapshot.chain_time);
println!("\t{i}: {}", snapshot.time_chunk.0 .0);
println!("\t\tTimestamp:\t{}", snapshot.timestamp_ms.0);
println!("\t\tDeposited:\t{}", snapshot.deposited.as_u128());
println!("\t\tBorrowed:\t{}", snapshot.borrowed.as_u128());
Expand Down Expand Up @@ -745,6 +745,9 @@ pub fn market_configuration(
yield_weights: YieldWeights,
) -> MarketConfiguration {
MarketConfiguration {
time_chunk_configuration: templar_common::time_chunk::TimeChunkConfiguration::BlockHeight {
divisor: U64(1),
},
borrow_asset: FungibleAsset::nep141(borrow_asset_id),
collateral_asset: FungibleAsset::nep141(collateral_asset_id),
balance_oracle: BalanceOracleConfiguration {
Expand Down