当前位置:网站首页>Compound ratemodel contract analysis
Compound ratemodel contract analysis
2022-06-11 07:24:00 【thirty-three thousand three hundred and fifty-seven】
The original is published in https://github.com/33357/smartcontract-apps This is a Chinese community , Analyze the architecture and implementation of smart contract application on the market . Welcome to the open source knowledge project !
Compound RateModel Contract analysis
RateModel The contract is used to calculate Compound A contract with a specific token lending rate , By analyzing it, we can know how to calculate the loan interest rate .
Demonstrate the code warehouse :https://github.com/33357/compound-protocol
WhitePaperInterestRateModel.sol
Linear interest rate model
Contract initialization
- Public function ( Both inside and outside the contract can call )
- constructor
- Code overview
constructor(uint baseRatePerYear, uint multiplierPerYear) public { baseRatePerBlock = baseRatePerYear.div(blocksPerYear); multiplierPerBlock = multiplierPerYear.div(blocksPerYear); emit NewInterestParams(baseRatePerBlock, multiplierPerBlock); } - Parametric analysis
functionconstructorThe input parameters are 2 individual , Out of ginseng 0 individual , The corresponding explanation is as follows :constructor( uint baseRatePerYear, // Annual benchmark interest rate uint multiplierPerYear // Annual interest rate multiplier ) public { ... } - Implementation analysis
... { // Block benchmark interest rate = Annual benchmark interest rate / Number of blocks per year baseRatePerBlock = baseRatePerYear.div(blocksPerYear); // Block rate multiplier = Annual interest rate multiplier / Number of blocks per year multiplierPerBlock = multiplierPerYear.div(blocksPerYear); // Triggering event NewInterestParams emit NewInterestParams(baseRatePerBlock, multiplierPerBlock); } - summary
The contract needs to beblockCalculate interest rates as time units , Therefore, it is necessary tobaseRatePerYearandmultiplierPerYearConvert tobaseRatePerBlockandmultiplierPerBlock. thereblocksPerYearby2102400, Is on average15 secondOneblockCalculated .
- Code overview
- constructor
Lending rate of funds
- Public function ( Both inside and outside the contract can call )
- utilizationRate
- Code overview
function utilizationRate(uint cash, uint borrows, uint reserves) public pure returns (uint) { if (borrows == 0) { return 0; } return borrows.mul(1e18).div(cash.add(borrows).sub(reserves)); } - Parametric analysis
functionutilizationRateThe input parameters are 3 individual , Out of ginseng 1 individual , The corresponding explanation is as follows :constructor( uint cash, // Token balance uint borrows, // Total number of tokens lent by users uint reserves // Total number of reserve tokens ) public view returns ( uint // Lending rate of funds ){ ... } - Implementation analysis
... { // If the total number of tokens lent is 0, The lending rate is also 0 if (borrows == 0) { return 0; } // borrows * 1e18 / (cash + borrows - reserves) return borrows.mul(1e18).div(cash.add(borrows).sub(reserves)); } - summary
utilizationRateUsed to calculate tokensLending rate of funds,Lending rate of fundsThe calculation formula of isLending rate of funds = Total number of tokens lent / ( Token balance + Total number of tokens lent - Total number of reserve tokens ). Due to the use of unsigned integersLending rate of fundsIt's not accurate enough , So it needs to be multiplied by1e18, Expand the accuracy range .
- Code overview
- utilizationRate
Capital interest rate
- Public function ( Both inside and outside the contract can call )
- getBorrowRate
- Code overview
function getBorrowRate(uint cash, uint borrows, uint reserves) public view returns (uint) { uint ur = utilizationRate(cash, borrows, reserves); return ur.mul(multiplierPerBlock).div(1e18).add(baseRatePerBlock); } - Parametric analysis
functiongetBorrowRateThe input parameters are 3 individual , Out of ginseng 1 individual , The corresponding explanation is as follows :function getBorrowRate( uint cash, // Token balance uint borrows, // Total number of tokens lent by users uint reserves // Total number of reserve tokens ) public view returns ( uint // Block lending rate ) { ... } - Implementation analysis
... { // Calculate the lending rate uint ur = utilizationRate(cash, borrows, reserves); // ur * multiplierPerBlock / 1e18 + baseRatePerBlock return ur.mul(multiplierPerBlock).div(1e18).add(baseRatePerBlock); } - summary
getBorrowRateUsed to calculate tokensBlock lending rate,Block lending rateThe calculation formula of isBlock lending rate = Lending rate of funds * Block rate multiplier + Block benchmark interest rate. becauseLending rate of fundsThe accuracy range is expanded , It needs to be divided by1e18Get the actual value .
- Code overview
- getSupplyRate
- Code overview
function getSupplyRate(uint cash, uint borrows, uint reserves, uint reserveFactorMantissa) public view returns (uint) { uint oneMinusReserveFactor = uint(1e18).sub(reserveFactorMantissa); uint borrowRate = getBorrowRate(cash, borrows, reserves); uint rateToPool = borrowRate.mul(oneMinusReserveFactor).div(1e18); return utilizationRate(cash, borrows, reserves).mul(rateToPool).div(1e18); } - Parametric analysis
functiongetSupplyRateThe input parameters are 4 individual , Out of ginseng 1 individual , The corresponding explanation is as follows :getSupplyRate( uint cash, // Token balance uint borrows, // Total number of tokens lent by users uint reserves, // Total number of reserve tokens uint reserveFactorMantissa // Reserve ratio ) public view returns ( uint // Block pledge interest rate ) { ... } - Implementation analysis
... { // 1 - reserveFactorMantissa uint oneMinusReserveFactor = uint(1e18).sub(reserveFactorMantissa); // Get the loan interest rate borrowRate uint borrowRate = getBorrowRate(cash, borrows, reserves); // borrowRate * (1 - reserveFactorMantissa) uint rateToPool = borrowRate.mul(oneMinusReserveFactor).div(1e18); // utilizationRate * borrowRate * (1 - reserveFactorMantissa) return utilizationRate(cash, borrows, reserves).mul(rateToPool).div(1e18); } - summary
getSupplyRateUsed to calculate tokensBlock pledge interest rate,Block pledge interest rateThe calculation formula of isBlock pledge interest rate = Lending rate of funds * The interest rate for borrowing * (1 - Reserve ratio ) = The interest rate for borrowing * Lending rate of funds * (1 - Reserve ratio ). becauseLending rate of fundsThe accuracy range is expanded , It needs to be divided by1e18Get the actual value .
- Code overview
- getBorrowRate
BaseJumpRateModelV2.sol
Inflection point interest rate model
Contract initialization
- Internal function ( Can only be called within the contract )
- constructor
- Code overview
constructor(uint baseRatePerYear, uint multiplierPerYear, uint jumpMultiplierPerYear, uint kink_, address owner_) internal { owner = owner_; updateJumpRateModelInternal(baseRatePerYear, multiplierPerYear, jumpMultiplierPerYear, kink_); } - Parametric analysis
functionconstructorThe input parameters are 5 individual , Out of ginseng 0 individual , The corresponding explanation is as follows :constructor( uint baseRatePerYear, // Annual benchmark interest rate uint multiplierPerYear // Annual interest rate multiplier uint jumpMultiplierPerYear, // Inflection point annual interest rate multiplier uint kink_, // Inflection point capital lending rate address owner_ // owner ) internal { ... } - Implementation analysis
... { // Record owner owner = owner_; // Update inflection point interest rate model parameters updateJumpRateModelInternal(baseRatePerYear, multiplierPerYear, jumpMultiplierPerYear, kink_); } - summary
Inflection point interest rate modelThanLinear interest rate modelMoreInflection point annual interest rate multiplierandInflection point capital lending rateThese two parameters .
- Code overview
- updateJumpRateModelInternal
- Code overview
function updateJumpRateModelInternal(uint baseRatePerYear, uint multiplierPerYear, uint jumpMultiplierPerYear, uint kink_) internal { baseRatePerBlock = baseRatePerYear.div(blocksPerYear); multiplierPerBlock = (multiplierPerYear.mul(1e18)).div(blocksPerYear.mul(kink_)); jumpMultiplierPerBlock = jumpMultiplierPerYear.div(blocksPerYear); kink = kink_; emit NewInterestParams(baseRatePerBlock, multiplierPerBlock, jumpMultiplierPerBlock, kink); } - Parametric analysis
functionupdateJumpRateModelInternalThe input parameters are 4 individual , Out of ginseng 0 individual , The corresponding explanation is as follows :function updateJumpRateModelInternal( uint baseRatePerYear, // Annual benchmark interest rate uint multiplierPerYear // Annual interest rate multiplier uint jumpMultiplierPerYear, // Inflection point annual interest rate multiplier uint kink_, // Inflection point capital lending rate ) internal { ... } - Implementation analysis
... { // Block benchmark interest rate = Annual benchmark interest rate / Number of blocks per year baseRatePerBlock = baseRatePerYear.div(blocksPerYear); // Block rate multiplier = Annual benchmark interest rate / ( Number of blocks per year * Inflection point capital lending rate ) multiplierPerBlock = (multiplierPerYear.mul(1e18)).div(blocksPerYear.mul(kink_)); // Inflection point block interest rate multiplier = Inflection point annual interest rate multiplier / Number of blocks per year jumpMultiplierPerBlock = jumpMultiplierPerYear.div(blocksPerYear); // Record the inflection point capital lending rate kink = kink_; // Triggering event NewInterestParams emit NewInterestParams(baseRatePerBlock, multiplierPerBlock, jumpMultiplierPerBlock, kink); } - summary
Inflection point interest rate modelMiddle computationBlock rate multiplierwhen , Methods andLinear interest rate modelDissimilarity :Inflection point capital lending rateThe higher the ,Block rate multiplierThe lower the , I am not very clear about its purpose .( It may be that the deeper the token market is, the higher the capital lending rate at the inflection point is , The block rate multiplier can be set lower , You can discuss )
- Code overview
- constructor
Lending rate of funds
- Public function ( Both inside and outside the contract can call )
- utilizationRate
- Code overview
function utilizationRate(uint cash, uint borrows, uint reserves) public pure returns (uint) { if (borrows == 0) { return 0; } return borrows.mul(1e18).div(cash.add(borrows).sub(reserves)); } - Parametric analysis
functionutilizationRateThe input parameters are 3 individual , Out of ginseng 1 individual , The corresponding explanation is as follows :constructor( uint cash, // Token balance uint borrows, // Total number of tokens lent by users uint reserves // Total number of reserve tokens ) public view returns ( uint // Lending rate of funds ){ ... } - Implementation analysis
... { // If the total number of tokens lent is 0, The lending rate is also 0 if (borrows == 0) { return 0; } // Lending rate of funds = borrows * 1e18 / (cash + borrows - reserves) return borrows.mul(1e18).div(cash.add(borrows).sub(reserves)); } - summary
Inflection point interest rate modelMiddle computationLending rate of fundsandLinear interest rate modelidentical .
- Code overview
- utilizationRate
Capital interest rate
- Internal function ( Can only be called inside the contract )
- getBorrowRate
- Code overview
function getBorrowRateInternal(uint cash, uint borrows, uint reserves) internal view returns (uint) { uint util = utilizationRate(cash, borrows, reserves); if (util <= kink) { return util.mul(multiplierPerBlock).div(1e18).add(baseRatePerBlock); } else { uint normalRate = kink.mul(multiplierPerBlock).div(1e18).add(baseRatePerBlock); uint excessUtil = util.sub(kink); return excessUtil.mul(jumpMultiplierPerBlock).div(1e18).add(normalRate); } } - Parametric analysis
functiongetBorrowRateInternalThe input parameters are 3 individual , Out of ginseng 1 individual , The corresponding explanation is as follows :function getBorrowRate( uint cash, // Token balance uint borrows, // Total number of tokens lent by users uint reserves // Total number of reserve tokens ) internal view returns ( uint // Block lending rate ) { ... } - Implementation analysis
{ // Obtain the lending rate of funds util uint util = utilizationRate(cash, borrows, reserves); // If util < kink if (util <= kink) { // return util * multiplierPerBlock + baseRatePerBlock return util.mul(multiplierPerBlock).div(1e18).add(baseRatePerBlock); } else { // The lending rate before the inflection point = kink * multiplierPerBlock + baseRatePerBlock uint normalRate = kink.mul(multiplierPerBlock).div(1e18).add(baseRatePerBlock); // The lending rate of funds beyond the inflection point = util - kink uint excessUtil = util.sub(kink); // Block lending rate = (util - kink) * jumpMultiplierPerBlock + kink * multiplierPerBlock + baseRatePerBlock return excessUtil.mul(jumpMultiplierPerBlock).div(1e18).add(normalRate); } } - summary
Inflection point interest rate modelMiddle computationBlock lending rateThe calculation formula of is divided into two parts : stayLending rate of fundslower thanInflection point capital lending ratewhen , The formula is :Block lending rate = Lending rate of funds * Block rate multiplier + Block benchmark interest rate; stayLending rate of fundshigher thanInflection point capital lending ratewhen , The formula is :Block lending rate = ( Lending rate of funds - Inflection point capital lending rate ) * Inflection point block interest rate multiplier + Inflection point capital lending rate * Block rate multiplier + Block benchmark interest rate.
- Code overview
- getSupplyRate
- Code overview
function getSupplyRate(uint cash, uint borrows, uint reserves, uint reserveFactorMantissa) public view returns (uint) { uint oneMinusReserveFactor = uint(1e18).sub(reserveFactorMantissa); uint borrowRate = getBorrowRateInternal(cash, borrows, reserves); uint rateToPool = borrowRate.mul(oneMinusReserveFactor).div(1e18); return utilizationRate(cash, borrows, reserves).mul(rateToPool).div(1e18); } - Parametric analysis
functiongetSupplyRateThe input parameters are 4 individual , Out of ginseng 1 individual , The corresponding explanation is as follows :getSupplyRate( uint cash, // Token balance uint borrows, // Total number of tokens lent by users uint reserves, // Total number of reserve tokens uint reserveFactorMantissa // Reserve ratio ) public view returns ( uint // Block pledge interest rate ) { ... } - Implementation analysis
... { // 1 - reserveFactorMantissa uint oneMinusReserveFactor = uint(1e18).sub(reserveFactorMantissa); // Get the loan interest rate borrowRate uint borrowRate = getBorrowRateInternal(cash, borrows, reserves); // rateToPool = borrowRate * (1 - reserveFactorMantissa) uint rateToPool = borrowRate.mul(oneMinusReserveFactor).div(1e18); // Block pledge interest rate = utilizationRate * borrowRate * (1 - reserveFactorMantissa) return utilizationRate(cash, borrows, reserves).mul(rateToPool).div(1e18); } - summary
Inflection point interest rate modelMiddle computationCapital pledge interest rateandLinear interest rate modelidentical .
- Code overview
- getBorrowRate
边栏推荐
- JVM Learning record (7) - - class Loading Process and parent delegation Model
- Server parameter adjustment record
- es5和es6的学习小记
- Method to determine whether it is an array
- Leetcode-647.Palindromic Substrings
- Set center alignment
- 1734. arrangement after decoding XOR
- [STL source code analysis] summary notes (12): functors and adapters
- Leetcode-104. Maximum Depth of Binary Tree
- Analysis of key points and difficulties of ES6 promise source code
猜你喜欢

big.js--使用/实例

资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员

webserver

2022 low voltage electrician test questions and online simulation test

Education expert wangzhongze solves students' problems with one move

Biological sequence intelligent analysis platform blog (1)

資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員

Software testing weekly (issue 75): only when you look down, can you see your true self.

First day of database

一、SQLServer2008安裝(帶密碼)、創建數據庫、C#窗體項目測試
随机推荐
【编译原理】05-语法制导的语义计算——基于翻译模式的语义计算
P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
Method to determine whether it is an array
Android and IOS reverse analysis / security detection / penetration testing framework
Gobang interface of mobile console (C language)
JVM learning record (VII) -- class loading process and parental delegation model
[STL source code analysis] summary notes (12): functors and adapters
MS office level II wrong question record [7]
Regular Expression Matching
Concurrent tool class
学 SQL 必须了解的10个高级概念
資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員
Android和iOS逆向分析/安全检测/渗透测试框架
Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation
JVM Learning record (7) - - class Loading Process and parent delegation Model
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
Server parameter adjustment record
Typora set markdown syntax inline mode
Leetcode-104. Maximum Depth of Binary Tree
Group arrays by a specified size