Skip to content

Conversation

@joshuajbouw
Copy link
Contributor

In order to support #171 (DefiLlama integration) we need to be able to expose the metrics of collateral standalone, as well as in the snapshot as well. This would then allow external parties to be able to calculate using the correct TVL method. TVL (total deposits (collateral assets + borrow assets) - outstanding loans)

@joshuajbouw
Copy link
Contributor Author

@peer2f00l few things that I am unsure about:

  • Where is the best appropriate place to add to the tracking of collateral deposits in Contract and Snapshot?
  • Does inflight matter like it does in the context of borrowed?

@peer2f00l
Copy link
Collaborator

peer2f00l commented Aug 5, 2025

Add collateral step is here:

pub fn execute_collateralize(
&mut self,
account_id: AccountId,
amount: CollateralAssetAmount,
price_pair: &PricePair,
) {
// TODO: This creates a borrow record implicitly. If we
// require a discrete "sign-up" step, we will need to add
// checks before this function call.
//
// The sign-up step would only be NFT gating or something of
// that sort, which is just an additional pre condition check.
// -- https://github.com/Templar-Protocol/contract-mvp/pull/6#discussion_r1923871982
if self.borrow_position_ref(account_id.clone()).is_none() {
self.charge_for_storage(
&account_id,
self.storage_usage_borrow_position + self.storage_usage_snapshot * 2,
);
}
let mut borrow_position = self.get_or_create_borrow_position_guard(account_id);
if borrow_position.inner().is_liquidation_locked {
env::panic_str("Cannot add collateral while liquidation locked");
}
let proof = borrow_position.accumulate_interest();
require!(
!borrow_position.is_eligible_for_liquidation(price_pair, env::block_timestamp_ms()),
"Cannot add collateral when eligible for liquidation",
);
borrow_position.record_collateral_asset_deposit(proof, amount);
}

Withdraw collateral step is here:

fn withdraw_collateral(&mut self, amount: CollateralAssetAmount) -> Promise {
let account_id = env::predecessor_account_id();
let Some(mut borrow_position) = self.borrow_position_guard(account_id.clone()) else {
env::panic_str("No borrower record. Please deposit collateral first.");
};
if borrow_position
.inner()
.get_total_borrow_asset_liability()
.is_zero()
{
// No need to retrieve prices, since there is zero liability.
let proof = borrow_position.accumulate_interest();
borrow_position.record_collateral_asset_withdrawal(proof, amount);
drop(borrow_position);
self.configuration
.collateral_asset
.transfer(account_id.clone(), amount)
.then(
self_ext!(Self::GAS_WITHDRAW_COLLATERAL_02_FINALIZE)
.withdraw_collateral_02_finalize(account_id, amount),
)
} else {
drop(borrow_position);
// They still have liability, so we need to check prices.
self.configuration
.price_oracle_configuration
.retrieve_price_pair()
.then(
self_ext!(Self::GAS_WITHDRAW_COLLATERAL_01_CONSUME_PRICE)
.withdraw_collateral_01_consume_price(account_id, amount),
)
}
}

You should be able to perform just about all of the necessary calculations as-we-go by adding logic in those two places. (And to the snapshot() function, I suppose.)

@joshuajbouw joshuajbouw force-pushed the feat/collateral-tracking branch from 99e060d to de232a9 Compare August 5, 2025 22:45
@joshuajbouw joshuajbouw changed the title [WIP] feat: collateral tracking feat: collateral tracking Aug 5, 2025
Copy link
Collaborator

@peer2f00l peer2f00l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please write tests to ensure the correctness of the new field as it appears in snapshots.

@joshuajbouw joshuajbouw requested a review from peer2f00l August 6, 2025 18:04
@joshuajbouw
Copy link
Contributor Author

Added tests and encapsulation on snapshot, which is better to use defined functions so that future updaters don't accidentally make mistakes.

@joshuajbouw joshuajbouw force-pushed the feat/collateral-tracking branch from d21fcf9 to 5fa7be2 Compare August 6, 2025 18:07
@github-actions
Copy link

github-actions bot commented Aug 7, 2025

Name Deployment
Registry gh-192.templar-in-training.testnet
Default market default-16824903940.gh-192.templar-in-training.testnet

Gas Report

harvest_yield

Iterations Gas
0 3.5 Tgas
391 11.1 Tgas

Estimated snapshot limit: 14422

apply_interest

Iterations Gas
0 3.4 Tgas
391 13.2 Tgas

Estimated snapshot limit: 11235

Copy link
Collaborator

@peer2f00l peer2f00l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweaked some of the tests. In general, I think they should be more precise (checking actual values instead of just "is non-zero after deposit" or "is different from the previous state after X action").

@joshuajbouw
Copy link
Contributor Author

joshuajbouw commented Aug 7, 2025

Tweaked some of the tests. In general, I think they should be more precise (checking actual values instead of just "is non-zero after deposit" or "is different from the previous state after X action").

Do agree, they should've been more tightly bound. It was mostly the interest that was (understandably) inconsistent, but it could yield false positives.

@peer2f00l peer2f00l added the external-api Involves a change to the external API of the smart contract which may require updates to clients. label Aug 7, 2025
@joshuajbouw joshuajbouw added the priority-high This should take precedence over others label Aug 7, 2025
@joshuajbouw
Copy link
Contributor Author

Added priority high since this is blocking another priority-high #171.

@joshuajbouw joshuajbouw requested a review from peer2f00l August 7, 2025 13:27
@joshuajbouw joshuajbouw self-assigned this Aug 7, 2025
@joshuajbouw joshuajbouw force-pushed the feat/collateral-tracking branch from e8d7e85 to 07ea3a9 Compare August 7, 2025 14:48
@peer2f00l peer2f00l merged commit 1e7a7c7 into dev Aug 8, 2025
5 checks passed
@peer2f00l peer2f00l deleted the feat/collateral-tracking branch August 8, 2025 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-api Involves a change to the external API of the smart contract which may require updates to clients. priority-high This should take precedence over others

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants