Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
math / math-amortizeOpenRuns on Liberas

Loan amortization schedule

A level-payment loan schedule in exact decimal arithmetic, every amount rounded half-up (ties away from zero) to scale fractional digits. principal and annual-rate (a percentage: 6.5 is 6.5 % a year) are 1 or more ASCII digits, optionally . and 1 or more digits, no sign; principal at most 20 digits, annual-rate at most 10 digits and at most 100. months is 1 to 600, scale 0 to 6. Let P = principal rounded to scale, r = annual-rate / 1200 exactly, n = months. payment = round(P × r × (1 + r)^n / ((1 + r)^n − 1)) from the exact value, or round(P / n) when r = 0. The balance B starts at P; for each period k = 1 … n: interest = round(B × r); for k < n the payment is payment and the principal part payment − interest; for k = n the principal part is B and the payment B + interest (the last payment absorbs all rounding); then B = B − principal part, recorded as balance. Nothing is clamped: a principal part is negative when payment < interest. total-interest and total-paid sum the installments' interest and payments. Every amount has exactly scale fractional digits (no point when scale is 0), at least one integer digit, no leading zeros, - only when negative. The result is value(schedule). Errors are invalid(reason), in this order: principal not in the syntax → syntax, over 20 digits → too-long; annual-rate the same (over 10 digits → too-long); then months outside 1 to 600, scale over 6, annual-rate over 100, or P = 0 → out-of-range.

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init math-amortize --lang rust
cd math-amortize && liberas build && liberas submit

Save a token once with liberas login (make it on your page, /me, after signing in). init scaffolds the interface, the vectors and a stub in Rust (--lang js for light specs).

Interface (WIT)47 lines›
math-amortize.wit
package root:component;

world root {
  import liberas:math/types@0.1.0;

  export liberas:math/amortization@0.1.0;
}
package liberas:math@0.1.0 {
  interface types {
    record installment {
      period: u16,
      payment: string,
      interest: string,
      principal: string,
      balance: string,
    }

    record schedule {
      payment: string,
      total-interest: string,
      total-paid: string,
      installments: list<installment>,
    }

    enum math-error {
      syntax,
      too-long,
      division-by-zero,
      too-large,
      out-of-range,
      not-finite,
      empty,
      too-deep,
    }

    variant schedule-outcome {
      value(schedule),
      invalid(math-error),
    }
  }
  interface amortization {
    use types.{schedule-outcome};

    amortize: func(principal: string, annual-rate: string, months: u16, scale: u8) -> schedule-outcome;
  }
}
Test vectors20›
math-amortize.cases.json
[
  {
    "args": {
      "scale": 2,
      "months": 360,
      "principal": "200000",
      "annual-rate": "6.5"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 360,
      "principal": "100000",
      "annual-rate": "5"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 12,
      "principal": "10000",
      "annual-rate": "0"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 12,
      "principal": "1000",
      "annual-rate": "12"
    }
  },
  {
    "args": {
      "scale": 0,
      "months": 60,
      "principal": "50000000",
      "annual-rate": "4.2"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 24,
      "principal": "1000.005",
      "annual-rate": "7.25"
    }
  },
  {
    "args": {
      "scale": 0,
      "months": 600,
      "principal": "100",
      "annual-rate": "100"
    }
  },
  {
    "args": {
      "scale": 0,
      "months": 600,
      "principal": "1",
      "annual-rate": "1"
    }
  },
  {
    "args": {
      "scale": 6,
      "months": 600,
      "principal": "99999999999999999999",
      "annual-rate": "99.99999999"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 1,
      "principal": "0.01",
      "annual-rate": "3"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 12,
      "principal": "-100",
      "annual-rate": "5"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 12,
      "principal": "1e3",
      "annual-rate": "5"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 12,
      "principal": "100",
      "annual-rate": "5%"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 12,
      "principal": "111111111111111111111",
      "annual-rate": "5"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 12,
      "principal": "100",
      "annual-rate": "11111111111"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 0,
      "principal": "100",
      "annual-rate": "5"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 601,
      "principal": "100",
      "annual-rate": "5"
    }
  },
  {
    "args": {
      "scale": 7,
      "months": 12,
      "principal": "100",
      "annual-rate": "5"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 12,
      "principal": "100",
      "annual-rate": "100.01"
    }
  },
  {
    "args": {
      "scale": 2,
      "months": 12,
      "principal": "0.004",
      "annual-rate": "5"
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 20 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 70,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.