Skip to content
Open
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
12 changes: 12 additions & 0 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["contracts/*"]
members = ["contracts/*", "crates/*"]
resolver = "2"

[workspace.package]
Expand All @@ -12,6 +12,9 @@ version = "0.0.1"
[workspace.dependencies.soroban-sdk]
version = "23.0.2"

[workspace.dependencies.loktin-common]
path = "crates/loktin-common"

[workspace.dependencies.stellar-access]
git = "https://github.com/OpenZeppelin/stellar-contracts"
tag = "v0.5.1"
Expand Down
1 change: 1 addition & 0 deletions contracts/locked_in/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ crate-type = ["cdylib"]
doctest = false

[dependencies]
loktin-common.workspace = true
soroban-sdk = "23.0.3"

[dev-dependencies]
Expand Down
5 changes: 2 additions & 3 deletions contracts/locked_in/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_std]
use loktin_common::{LEDGER_TTL_THRESHOLD, LEDGER_TTL_EXTEND};

mod error;
mod events;
Expand All @@ -14,9 +15,7 @@ use soroban_sdk::{
use error::Error;
use types::{DataKey, Lock};

const DAY_IN_LEDGERS: u32 = 17280;
const LEDGER_TTL_THRESHOLD: u32 = DAY_IN_LEDGERS * 30;
const LEDGER_TTL_EXTEND: u32 = DAY_IN_LEDGERS * 365;


const SECONDS_PER_MONTH: u64 = 2_592_000; // 30 days
const SECONDS_PER_YEAR: i128 = 31_536_000;
Expand Down
1 change: 1 addition & 0 deletions contracts/mock_pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ crate-type = ["lib", "cdylib"]
doctest = false

[dependencies]
loktin-common.workspace = true
soroban-sdk = "23.0.3"

[dev-dependencies]
Expand Down
5 changes: 2 additions & 3 deletions contracts/mock_pool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_std]
use loktin_common::{LEDGER_TTL_THRESHOLD, LEDGER_TTL_EXTEND};

//! Mock Blend pool.
//!
Expand All @@ -23,9 +24,7 @@ use soroban_sdk::{contract, contractimpl, token, Address, Env};
use error::Error;
use types::{DataKey, Position};

const DAY_IN_LEDGERS: u32 = 17280;
const LEDGER_TTL_THRESHOLD: u32 = DAY_IN_LEDGERS * 30;
const LEDGER_TTL_EXTEND: u32 = DAY_IN_LEDGERS * 365;


const BPS_DENOMINATOR: i128 = 10_000;
const SECONDS_PER_YEAR: i128 = 31_536_000;
Expand Down
1 change: 1 addition & 0 deletions contracts/plans/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ crate-type = ["cdylib"]
doctest = false

[dependencies]
loktin-common.workspace = true
soroban-sdk = "23.0.3"

[dev-dependencies]
Expand Down
5 changes: 2 additions & 3 deletions contracts/plans/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_std]
use loktin_common::{LEDGER_TTL_THRESHOLD, LEDGER_TTL_EXTEND};

mod error;
mod events;
Expand All @@ -10,9 +11,7 @@ use soroban_sdk::{contract, contractimpl, token, Address, Env, String, Vec};
use error::Error;
use types::{Bill, BillCycle, DataKey};

const DAY_IN_LEDGERS: u32 = 17280; // ~24 hours
const LEDGER_TTL_THRESHOLD: u32 = DAY_IN_LEDGERS * 30; // 30 days
const LEDGER_TTL_EXTEND: u32 = DAY_IN_LEDGERS * 365; // 1 year


#[contract]
pub struct Plans;
Expand Down
1 change: 1 addition & 0 deletions contracts/spend_save/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ crate-type = ["cdylib"]
doctest = false

[dependencies]
loktin-common.workspace = true
soroban-sdk = "23.0.3"

[dev-dependencies]
Expand Down
5 changes: 2 additions & 3 deletions contracts/spend_save/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_std]
use loktin_common::{LEDGER_TTL_THRESHOLD, LEDGER_TTL_EXTEND};

mod error;
mod events;
Expand All @@ -9,9 +10,7 @@ use soroban_sdk::{contract, contractimpl, token, Address, Env};
use error::Error;
use types::{DataKey, SpendSavePosition};

const DAY_IN_LEDGERS: u32 = 17280;
const LEDGER_TTL_THRESHOLD: u32 = DAY_IN_LEDGERS * 30;
const LEDGER_TTL_EXTEND: u32 = DAY_IN_LEDGERS * 365;


const BPS_DENOMINATOR: i128 = 10_000;
const SECONDS_PER_DAY: u64 = 86_400;
Expand Down
1 change: 1 addition & 0 deletions contracts/target_savings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ crate-type = ["cdylib"]
doctest = false

[dependencies]
loktin-common.workspace = true
soroban-sdk = "23.0.3"

[dev-dependencies]
Expand Down
5 changes: 2 additions & 3 deletions contracts/target_savings/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![no_std]
use loktin_common::{LEDGER_TTL_THRESHOLD, LEDGER_TTL_EXTEND};

mod error;
mod events;
Expand All @@ -24,9 +25,7 @@ pub trait Pool {
fn get_position(env: Env, supplier: Address) -> i128;
}

const DAY_IN_LEDGERS: u32 = 17280; // ~24h
const LEDGER_TTL_THRESHOLD: u32 = DAY_IN_LEDGERS * 30;
const LEDGER_TTL_EXTEND: u32 = DAY_IN_LEDGERS * 365;


const FORFEIT_BPS: u32 = 100; // 1% on early withdrawal
const BPS_DENOMINATOR: i128 = 10_000;
Expand Down
12 changes: 12 additions & 0 deletions crates/loktin-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "loktin-common"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
license.workspace = true

[dependencies]
soroban-sdk.workspace = true

[dev-dependencies]
53 changes: 53 additions & 0 deletions crates/loktin-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![no_std]

use soroban_sdk::{Env, IntoVal, Val};

pub const DAY_IN_LEDGERS: u32 = 17280;
pub const LEDGER_TTL_THRESHOLD: u32 = DAY_IN_LEDGERS * 30;
pub const LEDGER_TTL_EXTEND: u32 = DAY_IN_LEDGERS * 365;

pub fn extend_ttl_instance(env: &Env) {
env.storage()
.instance()
.extend_ttl(LEDGER_TTL_THRESHOLD, LEDGER_TTL_EXTEND);
}

pub fn extend_ttl_persistent<K>(env: &Env, key: &K)
where
K: IntoVal<Env, Val>,
{
env.storage()
.persistent()
.extend_ttl(key, LEDGER_TTL_THRESHOLD, LEDGER_TTL_EXTEND);
}

pub fn next_id<K>(env: &Env, key: &K) -> u64
where
K: IntoVal<Env, Val>,
{
let mut id: u64 = env.storage().instance().get(key).unwrap_or(0);
id += 1;
env.storage().instance().set(key, &id);
id
}

#[cfg(test)]
mod test {
use super::*;
use soroban_sdk::{symbol_short, Env};

#[test]
fn test_next_id() {
let env = Env::default();
let key = symbol_short!("ID");

let id1 = next_id(&env, &key);
assert_eq!(id1, 1);

let id2 = next_id(&env, &key);
assert_eq!(id2, 2);

let id3 = next_id(&env, &key);
assert_eq!(id3, 3);
}
}