Skip to content

Conversation

@Kay-Zee
Copy link
Member

@Kay-Zee Kay-Zee commented Dec 12, 2025

Description

Implementation of insurance fund


For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

}

// If no credit balance, nothing to collect
if self.totalCreditBalance == 0.0 as UFix128 {
Copy link
Member

Choose a reason for hiding this comment

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

No need to static cast, the type is already inferred

Suggested change
if self.totalCreditBalance == 0.0 as UFix128 {
if self.totalCreditBalance == 0.0 {

Comment on lines +559 to +564
let secondsPerYear: UFix64 = 365.25 * 24.0 * 60.0 * 60.0
let yearsElapsed = timeElapsed / secondsPerYear
let insuranceRate: UFix128 = FlowCreditMarketMath.toUFix128(self.insuranceRate)
// Insurance amount is a percentage of total credit balance per year
let insuranceAmount: UFix128 = self.totalCreditBalance * insuranceRate * FlowCreditMarketMath.toUFix128(yearsElapsed)
let insuranceAmountUFix64 = FlowCreditMarketMath.toUFix64RoundDown(insuranceAmount)
Copy link
Member

Choose a reason for hiding this comment

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

Type annotations are not needed, types can get inferred.
Also, FlowCreditMarketMath.toUFix128 is not needed, just call the UFix128 conversion function directly

Suggested change
let secondsPerYear: UFix64 = 365.25 * 24.0 * 60.0 * 60.0
let yearsElapsed = timeElapsed / secondsPerYear
let insuranceRate: UFix128 = FlowCreditMarketMath.toUFix128(self.insuranceRate)
// Insurance amount is a percentage of total credit balance per year
let insuranceAmount: UFix128 = self.totalCreditBalance * insuranceRate * FlowCreditMarketMath.toUFix128(yearsElapsed)
let insuranceAmountUFix64 = FlowCreditMarketMath.toUFix64RoundDown(insuranceAmount)
let secondsPerYear = 365.25 * 24.0 * 60.0 * 60.0
let yearsElapsed = timeElapsed / secondsPerYear
let insuranceRate = UFix128(self.insuranceRate)
// Insurance amount is a percentage of total credit balance per year
let insuranceAmount = self.totalCreditBalance * insuranceRate * FlowCreditMarketMath.toUFix128(yearsElapsed)
let insuranceAmountUFix64 = FlowCreditMarketMath.toUFix64RoundDown(insuranceAmount)

Comment on lines +584 to +589
// Validate swapper output type (input type is already validated when swapper is set)
assert(self.insuranceSwapper!.outType() == Type<@MOET.Vault>(), message: "Insurance swapper must output MOET")

// Get quote and perform swap
let quote = self.insuranceSwapper!.quoteOut(forProvided: amountToCollect, reverse: false)
var moetVault <- self.insuranceSwapper!.swap(quote: quote, inVault: <-insuranceVault)
Copy link
Member

Choose a reason for hiding this comment

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

Avoid force unwrapping, repeatedly:

Suggested change
// Validate swapper output type (input type is already validated when swapper is set)
assert(self.insuranceSwapper!.outType() == Type<@MOET.Vault>(), message: "Insurance swapper must output MOET")
// Get quote and perform swap
let quote = self.insuranceSwapper!.quoteOut(forProvided: amountToCollect, reverse: false)
var moetVault <- self.insuranceSwapper!.swap(quote: quote, inVault: <-insuranceVault)
let insuranceSwapper = self.insuranceSwapper ?? panic("missing insurance swapper")
// Validate swapper output type (input type is already validated when swapper is set)
assert(insuranceSwapper.outType() == Type<@MOET.Vault>(), message: "Insurance swapper must output MOET")
// Get quote and perform swap
let quote = insuranceSwapper.quoteOut(forProvided: amountToCollect, reverse: false)
var moetVault <- insuranceSwapper.swap(quote: quote, inVault: <-insuranceVault)


/// Returns the balance of the MOET insurance fund
access(all) view fun insuranceFundBalance(): UFix64 {
let fundRef = (&self.insuranceFund as &MOET.Vault)
Copy link
Member

Choose a reason for hiding this comment

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

Here and below: Remove unnecessary parentheses

Suggested change
let fundRef = (&self.insuranceFund as &MOET.Vault)
let fundRef = &self.insuranceFund as &MOET.Vault

Comment on lines +2383 to +2387
if swapper != nil {
// Validate swapper types match
assert(swapper!.inType() == tokenType, message: "Swapper input type must match token type")
assert(swapper!.outType() == Type<@MOET.Vault>(), message: "Swapper output type must be MOET")
}
Copy link
Member

Choose a reason for hiding this comment

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

Avoid force unwrapping, use if-let instead:

Suggested change
if swapper != nil {
// Validate swapper types match
assert(swapper!.inType() == tokenType, message: "Swapper input type must match token type")
assert(swapper!.outType() == Type<@MOET.Vault>(), message: "Swapper output type must be MOET")
}
if let swapper = swapper {
// Validate swapper types match
assert(swapper.inType() == tokenType, message: "Swapper input type must match token type")
assert(swapper.outType() == Type<@MOET.Vault>(), message: "Swapper output type must be MOET")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants