Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Split InterestAccrual trait: interest & debt #1231

Open
lemunozm opened this issue Mar 6, 2023 · 1 comment · May be fixed by #1310
Open

Refactor: Split InterestAccrual trait: interest & debt #1231

lemunozm opened this issue Mar 6, 2023 · 1 comment · May be fixed by #1310
Labels
crcl-cross-chain Circle cross-chain related. I6-refactoring Code needs refactoring. P2-nice-to-have Issue is worth doing.

Comments

@lemunozm
Copy link
Contributor

lemunozm commented Mar 6, 2023

Which part of the code is the issue addressing?

  • Refactoring

Description

Analyzing interest-accrual pallet and talking with @branan. It seems like its main purpose is accumulating interest (no big discovery here 😆), but without any knowledge of what debt is or it is computed. In fact, the debt logic does not make use of the storage and is built on top of the accumulated rates. So we have two things:

  • Accumulate rates.
  • Accumulate debts (using the accumulated rates)

We could divide it into two layers:

// Only knows about rates
// `pallet-interest-accrual` only implement this trait
trait InterestAccrual {
    type RateData;

    fn get_rate(rate_per_year) -> RateData; // RateData will be (Rate, Rate) for the current impl.
    fn reference_rate(rate_per_year);
    fn unreference_rate(rate_per_year);
}

// Knows about debt.
// Nobody implemented this. 
// Adds a new layer of debt for free to any InterestAccrual.
trait DebtAccrual: InterestAccrual {
    fn current_debt(rate_per_year, norm_debt) -> Balance {
        // Default impl at the trait level
    }
    fn adjust_debt(rate_per_year, norm_debt, value) -> Balance {
        // Default impl at the trait level
    }
    fn normalize(rate_per_year, norm_debt, value) -> Balance {
        // Default impl at the trait level
    }
}

// This trait comes free
impl<T: InterestAccrual> DebtAccrual for T {}

How will this affect the code base

  • Easier to reason for a reader what's happening in the interest-accrual pallet.
  • Easier to mock (3 methods)
  • Easier to play with different implementations.

What are forseen obstacles or hurdles to overcome?

No migration is needed. Storage remains without change.

@mustermeiszer
Copy link
Collaborator

I totally aggree that this is a simple and elegant change to enhance the usability of this.

@lemunozm lemunozm added P2-nice-to-have Issue is worth doing. I6-refactoring Code needs refactoring. labels Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crcl-cross-chain Circle cross-chain related. I6-refactoring Code needs refactoring. P2-nice-to-have Issue is worth doing.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants