Demurrage
Demurrage is a negative interest rate on assets held that represents the cost of holding those assets. To represent the demurrage on a token in the XRP Ledger, you can track it using a custom currency code that indicates the demurrage rate. This effectively creates separate versions of the token for each varying amount of demurrage. Client applications can support this by representing the demurraging currency code with an annual percentage rate alongside the currency code. For example: "XAU (-0.5%pa)".
Representing Demurraging Token Amounts
Rather than continuously update all amounts in the XRP Ledger, this approach divides amounts of interest-bearing or demurraging tokens into two types of amount: "ledger values" recorded in the XRP Ledger, and "display values" shown to people. The "ledger values" represent the value of the currency at a fixed point, namely the "Ripple Epoch" of midnight January 1, 2000. The "display values" represent the amount at a later point in time (usually the current time) after calculating continuous interest or demurrage from the Ripple Epoch until that time.
Thus, client software must apply two conversions:
- Taking display values at a given time and converting them to ledger values to be recorded.
- Taking ledger values and converting them to a display value at a given point in time.
Calculating Demurrage
The full formula for calculating demurrage is as follows:
D = A × ( e ^ (t ÷ τ) )
- D is the amount after demurrage
- A is the pre-demurrage amount as recorded in the global ledger
- e is Euler's number
- t is the number of seconds since the Ripple Epoch (0:00 on January 1, 2000 UTC)
- τ is the e-folding time in seconds. This value is calculated from the desired interest rate.
To convert between display amounts and ledger amounts, you can use the following steps:
- Calculate the value of
( e ^ (t ÷ τ) )
. We call this number the "demurrage coefficient". The demurrage coefficient is always relative to a specific time, such as the current time. - Apply it to the amount to convert:
- To convert ledger values to display values, multiply by the demurrage coefficient.
- To convert display values to ledger values, divide by the demurrage coefficient.
- If necessary, adjust the resulting value so that it can be represented to the desired accuracy. Ledger values are limited to 15 decimal digits of precision, according to the XRP Ledger's token format.
Interest-Bearing Currency Code Format
Rather than using the standard currency code format, tokens that have positive interest or negative interest (demurrage) use a 160-bit currency code in the following format:
- The first 8 bits must be
0x01
. - The next 24 bits represent 3 characters of ASCII. This is expected to be an ISO 4217 code. It supports the same characters as the standard format's ASCII characters.
- The next 24 bits MUST be all
0
s. - The next 64 bits are the interest rate of the currency, represented as "e-folding time" in an IEEE 754 double format.
- The next 24 bits are reserved and should be all
0
s.
Calculating e-folding Time
To convert between ledger amounts and display amounts, or to calculate a currency code for an interest-bearing/demurraging currency, you need the interest rate as an "e-folding time". The e-folding time is the amount of time it takes a quantity to increase by a factor of e (Euler's number). By convention, e-folding time is written as the letter τ in formulas.
To calculate an e-folding time for a given rate of annual percent interest:
- Add the interest rate to 100% to get the percentage of the initial amount present after applying annual interest. For demurrage, use a negative interest rate. For example, 0.5% demurrage would be an interest rate of -0.5%, resulting in 99.5% remaining.
- Represent the percentage as a decimal. For example, 99.5% becomes 0.995.
- Take the natural log of that number. For example, ln(0.995) = -0.005012541823544286. (This number is positive if the initial interest rate was positive, and negative if the interest rate was negative.)
- Take the number of seconds in one year (31536000) and divide by the natural log result from the previous step. For example, 31536000 ÷ -0.005012541823544286 = -6291418827.045599. This result is the e-folding time in seconds.
Client Support
To support interest-bearing and demurraging tokens, client applications must implement several features:
When displaying the amount of a demurraging token retrieved from ledger or transaction data, the client must convert from the ledger value to the display value. (With demurrage, the display values are smaller than the ledger values.)
When accepting input for a demurraging token, the client must convert amounts from a display format to the ledger format. (With demurrage, the ledger values are larger than the user input value.) The client must use the ledger value when creating payments, offers, and other types of transaction.
Clients must distinguish between tokens that do and do not have interest or demurrage, and among tokens that have different rates of interest or demurrage. Clients should be able to parse the Interest-Bearing Currency Code Format into a display such as "XAU (-0.5% pa)".
ripple-lib Support
Demurrage was supported in ripple-lib versions 0.7.37 through 0.12.9. Demurrage is not supported in most modern libraries.
The following code samples show how to use compatible versions of ripple-lib to convert between ledger values and display values. Also see the Ripple Demurrage Calculator.
To convert from a display value to a ledger value, use Amount.from_human()
:
// create an Amount object for the display amount of the demurring currency // and pass in a reference_date that represents the current date // (in this case, ledger value 10 XAU with 0.5% annual demurrage, // at 2017-11-04T00:07:50Z.) var demAmount = ripple.Amount.from_human('10 0158415500000000C1F76FF6ECB0BAC600000000', {reference_date:563069270}); // set the issuer demAmount.set_issuer("rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh"); // get the JSON format for the ledger amount console.log(demAmount.to_json()); // { "value": "10.93625123082769", // "currency": "0158415500000000C1F76FF6ECB0BAC600000000", // "issuer": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" }
To convert from a ledger value to a display value:
// create an Amount object with the ledger value, ledgerAmount = ripple.Amount.from_json({ "currency": "015841551A748AD2C1F76FF6ECB0CCCD00000000", "issuer": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", "value": "10.93625123082769"}) // apply interest up to the current time to get the display amount var displayAmount = demAmount.applyInterest(new Date()); console.log(displayAmount.to_json()); // { "value": "9.999998874657716", // "currency": "0158415500000000C1F76FF6ECB0BAC600000000", // "issuer": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" }