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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# generated by CI
init-args.json

# Scope-of-work
sow.txt
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Templar Protocol is a chain-agnostic overcollateralized lending DeFi protocol.
./script/test.sh
```

## Generate scope-of-work for auditing

```bash
./script/sow.sh
```

## Links

- [Website](https://templarfi.org/)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::{env, near, store::Vector, BorshStorageKey, IntoStorageKey};

#[cfg(test)]
mod tests;

#[derive(Debug, Clone, Copy, BorshSerialize, BorshStorageKey, PartialEq, Eq, PartialOrd, Ord)]
enum StorageKey {
Inner,
Expand Down Expand Up @@ -156,65 +159,3 @@ impl<T: BorshSerialize + BorshDeserialize, const CHUNK_SIZE: u32> DoubleEndedIte
Some(value)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn basic() {
let mut list = ChunkedAppendOnlyList::<_, 47>::new(b"l");
assert_eq!(list.len(), 0);
assert!(list.is_empty());

for i in 0..10_000usize {
list.push(i);
assert_eq!(list.len() as usize, i + 1);
assert!(!list.is_empty());
}

let mut count = 0;
for (i, v) in list.iter().enumerate() {
assert_eq!(i, *v);
count += 1;
}

assert_eq!(count, 10_000);
}

#[test]
fn replace_last() {
let mut list = ChunkedAppendOnlyList::<_, 47>::new(b"l");
for i in 0..10_000u32 {
list.push(i);
list.replace_last(i * 2);
assert_eq!(list.len(), i + 1);
assert!(!list.is_empty());
}

for i in 0..10_000u32 {
let x = list.get(i).unwrap();
assert_eq!(*x, i * 2);
}

assert_eq!(list.len(), 10_000);
}

#[test]
fn next_back() {
let mut list = ChunkedAppendOnlyList::<_, 47>::new(b"l");
for i in 0..10_000u32 {
list.push(i);
}

let mut it = list.iter();

let mut i = 10_000;
while let Some(x) = it.next_back() {
i -= 1;
assert_eq!(*x, i);
}

assert_eq!(i, 0);
}
}
58 changes: 58 additions & 0 deletions common/src/collection/chunked_append_only_list/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use super::*;

#[test]
fn basic() {
let mut list = ChunkedAppendOnlyList::<_, 47>::new(b"l");
assert_eq!(list.len(), 0);
assert!(list.is_empty());

for i in 0..10_000usize {
list.push(i);
assert_eq!(list.len() as usize, i + 1);
assert!(!list.is_empty());
}

let mut count = 0;
for (i, v) in list.iter().enumerate() {
assert_eq!(i, *v);
count += 1;
}

assert_eq!(count, 10_000);
}

#[test]
fn replace_last() {
let mut list = ChunkedAppendOnlyList::<_, 47>::new(b"l");
for i in 0..10_000u32 {
list.push(i);
list.replace_last(i * 2);
assert_eq!(list.len(), i + 1);
assert!(!list.is_empty());
}

for i in 0..10_000u32 {
let x = list.get(i).unwrap();
assert_eq!(*x, i * 2);
}

assert_eq!(list.len(), 10_000);
}

#[test]
fn next_back() {
let mut list = ChunkedAppendOnlyList::<_, 47>::new(b"l");
for i in 0..10_000u32 {
list.push(i);
}

let mut it = list.iter();

let mut i = 10_000;
while let Some(x) = it.next_back() {
i -= 1;
assert_eq!(*x, i);
}

assert_eq!(i, 0);
}
2 changes: 2 additions & 0 deletions common/src/collection/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod chunked_append_only_list;
pub mod withdrawal_queue;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use near_sdk::{collections::LookupMap, env, near, AccountId, BorshStorageKey, In

use crate::{asset::BorrowAssetAmount, asset_op};

#[cfg(test)]
mod tests;

#[derive(Debug)]
#[near(serializers = [borsh])]
pub struct QueueNode {
Expand Down Expand Up @@ -355,61 +358,3 @@ pub mod error {
Empty(#[from] EmptyError),
}
}

#[cfg(test)]
mod tests {
use near_sdk::AccountId;

use super::WithdrawalQueue;

#[test]
fn withdrawal_remove() {
let mut wq = WithdrawalQueue::new(b"w");

let alice: AccountId = "alice".parse().unwrap();
let bob: AccountId = "bob".parse().unwrap();
let charlie: AccountId = "charlie".parse().unwrap();

wq.insert_or_update(&alice, 1.into());
wq.insert_or_update(&bob, 2.into());
wq.insert_or_update(&charlie, 3.into());
assert_eq!(wq.len(), 3);
assert_eq!(wq.remove(&bob), Some(2.into()));
assert_eq!(wq.len(), 2);
assert_eq!(wq.remove(&charlie), Some(3.into()));
assert_eq!(wq.len(), 1);
assert_eq!(wq.remove(&alice), Some(1.into()));
assert_eq!(wq.len(), 0);
}

#[test]
fn withdrawal_queueing() {
let mut wq = WithdrawalQueue::new(b"w");

let alice: AccountId = "alice".parse().unwrap();
let bob: AccountId = "bob".parse().unwrap();
let charlie: AccountId = "charlie".parse().unwrap();

assert_eq!(wq.len(), 0);
assert_eq!(wq.peek(), None);
wq.insert_or_update(&alice, 1.into());
assert_eq!(wq.len(), 1);
assert_eq!(wq.peek(), Some((alice.clone(), 1.into())));
wq.insert_or_update(&alice, 99.into());
assert_eq!(wq.len(), 1);
assert_eq!(wq.peek(), Some((alice.clone(), 99.into())));
wq.insert_or_update(&bob, 123.into());
assert_eq!(wq.len(), 2);
wq.try_lock().unwrap();
assert_eq!(wq.try_pop(), Some((alice.clone(), 99.into())));
assert_eq!(wq.len(), 1);
wq.insert_or_update(&charlie, 42.into());
assert_eq!(wq.len(), 2);
wq.try_lock().unwrap();
assert_eq!(wq.try_pop(), Some((bob.clone(), 123.into())));
assert_eq!(wq.len(), 1);
wq.try_lock().unwrap();
assert_eq!(wq.try_pop(), Some((charlie.clone(), 42.into())));
assert_eq!(wq.len(), 0);
}
}
54 changes: 54 additions & 0 deletions common/src/collection/withdrawal_queue/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use near_sdk::AccountId;

use super::WithdrawalQueue;

#[test]
fn withdrawal_remove() {
let mut wq = WithdrawalQueue::new(b"w");

let alice: AccountId = "alice".parse().unwrap();
let bob: AccountId = "bob".parse().unwrap();
let charlie: AccountId = "charlie".parse().unwrap();

wq.insert_or_update(&alice, 1.into());
wq.insert_or_update(&bob, 2.into());
wq.insert_or_update(&charlie, 3.into());
assert_eq!(wq.len(), 3);
assert_eq!(wq.remove(&bob), Some(2.into()));
assert_eq!(wq.len(), 2);
assert_eq!(wq.remove(&charlie), Some(3.into()));
assert_eq!(wq.len(), 1);
assert_eq!(wq.remove(&alice), Some(1.into()));
assert_eq!(wq.len(), 0);
}

#[test]
fn withdrawal_queueing() {
let mut wq = WithdrawalQueue::new(b"w");

let alice: AccountId = "alice".parse().unwrap();
let bob: AccountId = "bob".parse().unwrap();
let charlie: AccountId = "charlie".parse().unwrap();

assert_eq!(wq.len(), 0);
assert_eq!(wq.peek(), None);
wq.insert_or_update(&alice, 1.into());
assert_eq!(wq.len(), 1);
assert_eq!(wq.peek(), Some((alice.clone(), 1.into())));
wq.insert_or_update(&alice, 99.into());
assert_eq!(wq.len(), 1);
assert_eq!(wq.peek(), Some((alice.clone(), 99.into())));
wq.insert_or_update(&bob, 123.into());
assert_eq!(wq.len(), 2);
wq.try_lock().unwrap();
assert_eq!(wq.try_pop(), Some((alice.clone(), 99.into())));
assert_eq!(wq.len(), 1);
wq.insert_or_update(&charlie, 42.into());
assert_eq!(wq.len(), 2);
wq.try_lock().unwrap();
assert_eq!(wq.try_pop(), Some((bob.clone(), 123.into())));
assert_eq!(wq.len(), 1);
wq.try_lock().unwrap();
assert_eq!(wq.try_pop(), Some((charlie.clone(), 42.into())));
assert_eq!(wq.len(), 0);
}
54 changes: 3 additions & 51 deletions common/src/accumulator.rs → common/src/data/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use near_sdk::{json_types::U128, near, require};

use crate::asset::{AssetClass, FungibleAssetAmount};

#[cfg(test)]
mod tests;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[near(serializers = [borsh, json])]
pub struct Accumulator<T: AssetClass> {
Expand Down Expand Up @@ -108,54 +111,3 @@ impl<T: AssetClass> AccumulationRecord<T> {
self.amount
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn amortization() {
let mut a = Accumulator::<crate::asset::BorrowAsset>::new(1);

a.accumulate(AccumulationRecord {
amount: 100.into(),
fraction_as_u128_dividend: 0,
next_snapshot_index: 2,
});

assert_eq!(a.get_total(), 100.into());

a.amortize(25.into());

assert_eq!(a.get_total(), 125.into());

a.accumulate(AccumulationRecord {
amount: 100.into(),
fraction_as_u128_dividend: 0,
next_snapshot_index: 3,
});

assert_eq!(a.get_total(), 200.into());
}

#[test]
fn fraction() {
let mut a = Accumulator::<crate::asset::BorrowAsset>::new(1);

a.accumulate(AccumulationRecord {
amount: 100.into(),
fraction_as_u128_dividend: 1 << 127,
next_snapshot_index: 2,
});

assert_eq!(a.get_total(), 100.into());

a.accumulate(AccumulationRecord {
amount: 100.into(),
fraction_as_u128_dividend: 1 << 127,
next_snapshot_index: 3,
});

assert_eq!(a.get_total(), 201.into());
}
}
47 changes: 47 additions & 0 deletions common/src/data/accumulator/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use super::*;

#[test]
fn amortization() {
let mut a = Accumulator::<crate::data::asset::BorrowAsset>::new(1);

a.accumulate(AccumulationRecord {
amount: 100.into(),
fraction_as_u128_dividend: 0,
next_snapshot_index: 2,
});

assert_eq!(a.get_total(), 100.into());

a.amortize(25.into());

assert_eq!(a.get_total(), 125.into());

a.accumulate(AccumulationRecord {
amount: 100.into(),
fraction_as_u128_dividend: 0,
next_snapshot_index: 3,
});

assert_eq!(a.get_total(), 200.into());
}

#[test]
fn fraction() {
let mut a = Accumulator::<crate::data::asset::BorrowAsset>::new(1);

a.accumulate(AccumulationRecord {
amount: 100.into(),
fraction_as_u128_dividend: 1 << 127,
next_snapshot_index: 2,
});

assert_eq!(a.get_total(), 100.into());

a.accumulate(AccumulationRecord {
amount: 100.into(),
fraction_as_u128_dividend: 1 << 127,
next_snapshot_index: 3,
});

assert_eq!(a.get_total(), 201.into());
}
Loading
Loading