bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 15쪽 중 12쪽
flop (debt) auctions
A Note On Setting Rates
Section titled “A Note On Setting Rates”
Let’s see how to set a rate value in practice. Suppose it is desired to set the DSR to 0.5% annually. Assume the real rate will track the ideal rate. Then, we need a per-second rate value
r
such that (denoting the number of seconds in a year by
N
):
$$
r^N = 1.005
$$
An arbitrary precision calculator can be used to take the
N
-th root of the right-hand side (with
N
= 31536000 = 365_24_60*60), to obtain:
$$
r = 1.000000000158153903837946258002097…
$$
The
dsr
parameter in the Pot implementation is interpreted as a
ray
, i.e. a 27 decimal digit fixed-point number. Thus we multiply by 10^27 and drop anything after the decimal point:
$$
\text{dsr} = 1000000000158153903837946258
$$
The
dsr
could then be set to 0.5% annually by calling:
Pot.file("dsr", 1000000000158153903837946258)
Released into the public domain (CC0 1.0 Universal)
– trademarks remain with their owners; no warranty. See
full license
.
## /protocol/rates/jug
Jug
The primary function of the Jug smart contract is to accumulate stability fees for a particular collateral type whenever its
drip()
method is called. This effectively updates the accumulated debt for all Vaults of that collateral type as well as the total accumulated debt as tracked by the Vat (global) and the amount of Dai surplus (represented as the amount of Dai owned by the
Vow
.
Contract Details
Section titled “Contract Details”
Structs
Section titled “Structs”
Ilk
: contains two
uint256
values—
duty
, the collateral-specific risk premium, and
rho
, the timestamp of the last fee update
VatLike
: mock contract to make Vat interfaces callable from code without an explicit dependency on the Vat contract itself
Storage Layout
Section titled “Storage Layout”
wards
:
mapping(address => uint)
that indicates which addresses may call administrative functions
ilks
:
mapping (bytes32 => Ilk)
that stores an
Ilk
struct for each collateral type
vat
: a
VatLike
that points the the system’s Vat contract
vow
: the
address
of the Vow contract
base
: a
uint256
that specifies a fee applying to all collateral types
Public Methods
Section titled “Public Methods”
Administrative Methods
Section titled “Administrative Methods”
These methods require
wards[msg.sender] == 1
(i.e. only authorized users may call them).
rely
/
deny
: add or remove authorized users (via modifications to the
wards
mapping)
init(bytes32)
: start stability fee collection for a particular collateral type
file(bytes32, bytes32, uint)
: set
duty
for a particular collateral type
file(bytes32, data)
: set the
base
value
file(bytes32, address)
: set the
vow
value
Fee Collection Methods
Section titled “Fee Collection Methods”
drip(bytes32)
: collect stability fees for a given collateral type
Key Mechanisms & Concepts
Section titled “Key Mechanisms & Concepts”
drip
Section titled “drip”
drip(bytes32 ilk)
performs stability fee collection for a specific collateral type when it is called (note that it is a public function and may be called by anyone).
drip
does essentially three things:
calculates the change in the rate parameter for the collateral type specified by
ilk
based on the time elapsed since the last update and the current instantaneous rate (
base + duty
);
calls
Vat.fold
to update the collateral’s
rate
, total tracked debt, and Vow surplus;
updates
ilks[ilk].rho
to be equal to the current timestamp.
The change in the rate is calculated as:
$$
\Delta rate = (base+duty)^{now-rho} \cdot rate- rate
$$
where “now” represents the current time, “rate” is
Vat.ilks[ilk].rate
, “base” is
Jug.base
, “rho” is
Jug.ilks[ilk].rho
, and “duty” is
Jug.ilks[ilk].duty
. The function reverts if any sub-calculation results in under- or overflow. Refer to the Vat documentation for more detail on
fold
.
rpow
Section titled “rpow”
rpow(uint x, uint n, uint b)
, used for exponentiation in
drip
, is a fixed-point arithmetic function that raises
x
to the power
n
. It is implemented in Solidity assembly as a repeated squaring algorithm.
x
and the returned value are to be interpreted as fixed-point integers with scaling factor
b
. For example, if
b == 100
, this specifies two decimal digits of precision and the normal decimal value 2.1 would be represented as 210;
rpow(210, 2, 100)
returns 441 (the two-decimal digit fixed-point representation of 2.1^2 = 4.41). In the current implementation, 10^27 is passed for
b
, making
x
and the
rpow
result both of type
ray
in standard MCD fixed-point terminology.
rpow
’s formal invariants include “no overflow” as well as constraints on gas usage.
Parameters Can Only Be Set By Governance
Section titled “Parameters Can Only Be Set By Governance”
Jug stores some sensitive parameters, particularly the base rate and collateral-specific risk premiums that determine the overall stability fee rate for each collateral type. Its built-in authorization mechanisms need to allow only authorized Sky Ecosystem governance contracts/actors to set these values. See “Failure Modes” for a description of what can go wrong if parameters are set to unsafe values.
Gotchas (Potential Sources of User Error)
Section titled “Gotchas (Potential Sources of User Error)”
Ilk Initialization
Section titled “Ilk Initialization”
init(bytes32 ilk)
must called when a new collateral is added (setting
duty
via
file()
is not sufficient)—otherwise
rho
will be uninitialized and fees will accumulate based on a start date of January 1st, 1970 (start of Unix epoch).
base + Ilk.duty
imbalance in
drip()
Section titled “base + Ilk.duty imbalance in drip()”
A call to
drip(bytes32 ilk)
will add the
base
rate to the
Ilk.duty
rate. The rate is a calculated compounded rate, so
rate(base + duty) != rate(base) + rate(duty)
. This means that if base is set, the duty will need to be set factoring the existing compounding factor in base, otherwise the result will be outside of the rate tolerance. Updates to the
base
value will require all of the
ilks
to be updated as well.
Failure Modes (Bounds on Operating Conditions & External Risk Factors)
Section titled “Failure Modes (Bounds on Operating Conditions & External Risk Factors)”
Tragedy of the Commons
Section titled “Tragedy of the Commons”
If
drip()
is called very infrequently for some collateral types (due, for example, to low overall system usage or extremely stable collateral types that have essentially zero liquidation risk), then the system will fail to collect fees on Vaults opened and closed between
drip()
calls. As the system achieves scale, this becomes less of a concern, as both Keepers and SKY holders are have an incentive to regularly call drip (the former to trigger liquidation auctions, the latter to ensure that surplus accumulates to decrease SKY supply); however, a hypothetical asset with very low volatility yet high risk premium might still see infrequent drip calls at scale (there is not at present a real-world example of this—the most realistic possibility is
base
being large, elevating rates for all collateral types).
Malicious or Careless Parameter Setting
Section titled “Malicious or Careless Parameter Setting”
Various parameters of Jug may be set to values that damage the system. While this can occur by accident, the greatest concern is malicious attacks, especially by an entity that somehow becomes authorized to make calls directly to Jug’s administrative methods, bypassing governance. Setting
duty
(for at least one ilk) or
base
too low can lead to Dai oversupply; setting either one too high can trigger excess liquidations and therefore unjust loss of collateral. Setting a value for
vow
other than the true Vow’s address can cause surplus to be lost or stolen.
Released into the public domain (CC0 1.0 Universal)
– trademarks remain with their owners; no warranty. See
full license
.
## /protocol/rates/pot
Pot
The Pot is the core of the
Dai Savings Rate
. It allows users to deposit
dai
and activate the Dai Savings Rate and earning savings on their
dai
. The DSR is set by Sky Governance, and will typically be less than the base stability fee to remain sustainable. The purpose of Pot is to offer another incentive for holding Dai.
Contract Details
Section titled “Contract Details”
Math
Section titled “Math”
mul(uint, uint)
,
rmul(uint, uint)
,
add(uint, uint)
&
sub(uint, uint)
- will revert on overflow or underflow
rpow(uint x, uint n, uint base)
, used for exponentiation in
drip
, is a fixed-point arithmetic function that raises
x
to the power
n
. It is implemented in assembly as a repeated squaring algorithm.
x
(and the result) are to be interpreted as fixed-point integers with scaling factor
base
. For example, if
base == 100
, this specifies two decimal digits of precision and the normal decimal value 2.1 would be represented as 210;
rpow(210, 2, 100)
should return 441 (the two-decimal digit fixed-point representation of 2.1^2 = 4.41). In the current implementation, 10^27 is passed for
base
, making
x
and the
rpow
result both of type
ray
in standard MCD fixed-point terminology.
rpow
’s formal invariants include “no overflow” as well as constraints on gas usage.
Auth
Section titled “Auth”
wards
are allowed to call protected functions (Administration)
Storage
Section titled “Storage”
pie
- stores the address’
Pot
balance.
Pie
- stores the total balance in the
Pot
.
dsr
- the
dai savings rate
. It starts as
1
(
ONE = 10^27
), but can be updated by governance.
chi
- the rate accumulator. This is the always increasing value which decides how much
dai
- given when
drip()
is called.
vat
- an address that conforms to a
VatLike
interface. It is set during the constructor and
cannot be changed
.
vow
- an address that conforms to a
VowLike
interface. Not set in constructor. Must be set by governance.
rho
- the last time that drip is called.
The values of
dsr
and
vow
can be changed by an authorized address in the contract (i.e. Sky Governance). The values of
chi
,
pie
,
Pie
, and
rho
are updated internally in the contract and cannot be changed manually.
Key Mechanisms & Concepts
Section titled “Key Mechanisms & Concepts”
drip()
Section titled “drip()”
Calculates the most recent
chi
and pulls
dai
from the
vow
(by increasing the
Vow
’s
Sin
).
A user should always make sure that this has been called before calling the
exit()
function.
drip has to be called before a user
join
s and it is in their interest to call it again before they
exit
, but there isn’t a set rule for how often drip is called.
join(uint wad)
Section titled “join(uint wad)”
uint wad
this parameter is based on the amount of dai (since
wad
=
dai
/
chi
) that you want to
join
to the
pot
. The
wad * chi
must be present in the
vat
and owned by the
msg.sender
.
the
msg.sender
’s
pie
amount is updated to include the
wad
.
the total
Pie
amount is also updated to include the
wad
.
exit(uint wad)
Section titled “exit(uint wad)”
exit()
essentially functions as the exact opposite of
join()
.
uint wad
this parameter is based on the amount of dai that you want to
exit
the
pot
. The
wad * chi
must be present in the
vat
and owned by the
pot
and must be less than
msg.sender
’s
pie
balance.
The
msg.senders
pie
amount is updated by subtracting the
wad
.
The total
Pie
amount is also updated by subtracting the
wad
.
Administration
Section titled “Administration”
Various file function signatures for administering
Pot
:
Setting new dsr (
file(bytes32, uint256)
)
Setting new vow (
file(bytes32, address)
)
Usage
Section titled “Usage”
The primary usage will be for
addresses
to store their
dai
in the
pot
to accumulate interest over time
Gotchas / Integration Concerns
Section titled “Gotchas / Integration Concerns”
The
dsr
is set (globally) through the governance system. It can be set to any number > 0%. This includes the possibility of it being set to a number that would cause the DSR to accumulate faster than the collective Stability Fees, thereby accruing system debt and eventually causing SKY to be minted.
If
drip()
has not been called recently before an address calls
exit()
they will not get the full amount they have earned over the time of their deposit.
If a user wants to
join
or
exit
1 DAI into/from the Pot, they should send a
wad
= to
1 / chi
as the amount moved from their balance will be
1 * chi
(for an example of this, see
DSS-Proxy-Actions
Failure Modes and Impact
Section titled “Failure Modes and Impact”
Coding Error
Section titled “Coding Error”
A bug in the
Pot
could lead to locking of
dai
if the
exit()
function or the underlying
vat.suck()
or
vat.move()
functions were to have bugs.
Governance
Section titled “Governance”
The
dsr
rate initially can be set through the Chief. Governance will be able to change the DSR based on the rules that the DS-Chief employs (which would include a Pause for actions).
One serious risk is if governance chooses to set the
dsr
to an extremely high rate, this could cause the system’s fees to be far too high. Furthermore, if governance allows the
dsr
to (significantly) exceed the system fees, it would cause debt to accrue and increase the Flop auctions.
Released into the public domain (CC0 1.0 Universal)
– trademarks remain with their owners; no warranty. See
full license
.
## /deep-dives/rate-mechanism
Intro to the Rate Mechanism
Sky governance votes on stability fee changes using annual rates, ex:
5.5%
, but the rate mechanism in the Sky Protocol smart contracts doesn’t rely on annual rates to track the interest accrued in Sky Vaults. As a dapp developer you won’t be able to find this number stored in the smart contracts but instead you will run into a number like
1000000001697766583380253701
. This guide will help you understand how the stored rate value corresponds to the annual number, how multiple changes in both rates and debt levels over the lifetime of a Vault are accounted for in the Sky Protocol, and how the rate mechanism is used for tracking both stability fees and the Dai Savings Rate.
Learning Objectives
Section titled “Learning Objectives”
After going through this guide you will get a better understanding of,
Rate Mechanism
Stability fee calculation
Dai Savings Rate calculation
Pre-requisites
Section titled “Pre-requisites”
You will need a good understanding of these concepts to be able to work through this guide,
Solidity.
Guide
Section titled “Guide”
Compound Interest
Section titled “Compound Interest”
Investopedia defines compound interest as- “Compound interest (or compounding interest) is interest calculated on the initial principal, which also includes all of the accumulated interest of previous periods of a deposit or loan. Thought to have originated in 17th century Italy, compound interest can be thought of as “interest on interest,” and will make a sum grow at a faster rate than simple interest, which is calculated only on the principal amount.”
The formula for calculating compound interest is:
Compound Interest = Total amount of Principal and Interest in future (or Future Value) less Principal amount at present (or Present Value)
= $[P (1 + i)^n ] - P$
(Where P = Principal, i = nominal annual interest rate in percentage terms, and n = number of compounding periods.)
Please read this article to get a quick refresher on how compound interest works,
https://www.mathsisfun.com/money/compound-interest.html
Math
Section titled “Math”
Fixed Point Numbers
Section titled “Fixed Point Numbers”
Three different types of fixed point decimal numbers are used throughout the Sky protocol,
a
wad
type (18 decimal fixed point number) is used to track ERC20 token
balances
.
a higher precision
ray
type (27 decimal fixed point number) is used to track
interest rates
accrued in Vaults and the Dai Savings Rate contract over time.
a
rad
type(45 decimal fixed point number) is used to store all internal dai
balances
within the Vat contract. This is to ensure no precision is lost when storing the result of a
wad
and
ray
multiplication (18 + 27 = 45).
All these numbers are stored as a
uint256
type within smart contracts and it’s easy to miss their fixed-point decimal number type when performing a multiplication if care is not taken. Regular integer multiplication functions available in Solidity add orders of magnitude when applied to these fixed-point decimal numbers.
This issue is illustrated very clearly in the documentation of the
DSMath
library which implements math functions for wad and ray numbers.
1.1 * 2.2 == 2.42
//Regular integer arithmetic adds orders of magnitude:
110 * 220 == 24200
// Wad arithmetic does not add orders of magnitude:
wmul(1.1 ether, 2.2 ether) == 2.42 ether
You also have to be careful when using external safe math libraries whose functions may not deal with these number types correctly. All the Sky protocol smart contracts that use these number types have special math functions (usually the multiplication operation) to calculate the correct result.
Exponentiation
Section titled “Exponentiation”
Exponentiation operation is used to calculate the compound interest accrued over a number of time periods. The
rpow
function implements exponentiation in Solidity and is used to raise a
ray
to the n^th power and return a new
ray
with the correct precision.
Rate Value
Section titled “Rate Value”
The Sky protocol does not impose any restrictions on the duration a Vault should be open which allows a user to open or close their Vault anytime as long as it stays over-collateralized and safe. The stability fee rate set by Sky governance changes often and it is common for a single Vault to be charged different rates while its open.
To precisely account for the stability fees accrued over the lifetime of a Vault with these constraints, a time period of 1 second is used for all compound interest calculations in the Sky protocol which means that all the rates stored in the system are also for a second instead of a year.
Let’s fire up a Python console and enter the following commands to calculate the annual rate that corresponds to the per-second rate stored here in the
fee
variable. Let’s use
31536000
as the number of seconds in a year and add a decimal point for the rate input.
$
python
>>>
principal
=
1
>>>
rate
=
1.000000001697766583380253701
>>>
seconds
=
31536000
>>>
principal
*
(rate
**
seconds)
-
principal
0.05500000035258168
>>>
0.05500000035258168
*
100
5.500000035258168
>>>
You can now see that the stored rate compounds to a value of
5.5%
over a year.
Rate Accumulator
Section titled “Rate Accumulator”
Calculating the stability fee owed by the Vault owner would be a simple calculation if the rate applied to it never changes during its lifetime. Since rates are updated frequently in the system and Vaults need to be charged fees accurately, an additional rate accumulator variable is used to track the fee owed.
A function, typically named
drip
in various contracts, is executed to update the value of a rate accumulator over time. It reads the time elapsed in seconds since the last rate accumulator update, and the current rate value stored to calculate the new increased value.
Rate accumulators are able to track the total fee owed by a Vault taking into account all the previous rate changes but the history of the rate changes themselves and the stability fees owed during the previous periods by the Vault are only available through event logs and cannot be retrieved from the smart contract.
Tracking Balances
Section titled “Tracking Balances”
Maintaining a rate accumulator to accurately track a balance that increases over time requires frequent drip calls. The Sky protocol makes this process efficient by프로젝트로 돌아가기