Skip to content

Commit 4c8e175

Browse files
authored
Merge pull request #229 from VenusProtocol/fix-exchange-rate-calculation
fix: fix vtoken to underlying exchange calculation
2 parents 1e4fa44 + f73a0e4 commit 4c8e175

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

guides/protocol-math.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Overview
44

5-
The contracts under the Venus Protocol employ a system called Exponential.sol. This system uses exponential mathematics to represent fractional quantities with high precision. 
5+
The contracts under the Venus Protocol employ a system called Exponential.sol. This system uses exponential mathematics to represent fractional quantities with high precision.
66

77
Most numbers in this system are represented by a mantissa, an unsigned integer scaled by a factor of 1 \* 10^18. This scaling ensures basic mathematical operations can be performed with a high degree of accuracy.
88

@@ -21,7 +21,7 @@ For further details, please refer to the respective token contract addresses.
2121
The exchange rate of vTokens is adjusted based on the decimal difference between the vToken and its underlying asset.
2222

2323
$$
24-
onevTokenInUnderlying = \frac{exchangeRateCurrent}{1 \times 10^{(18 + underlyingDecimals - vTokenDecimals)}}
24+
oneVTokenInUnderlying = \frac{exchangeRateCurrent}{1 \times 10^{(18 + underlyingDecimals - vTokenDecimals)}}
2525
$$
2626

2727
Here is an example illustrating how to determine the value of one vBUSD in BUSD using the Web3.js JavaScript library.
@@ -33,13 +33,13 @@ const vToken = new web3.eth.Contract(vTokenAbi, vBusdAddress);
3333
const underlyingDecimals = await underlying.methods.decimals().call();
3434
const exchangeRateCurrent = await vToken.methods.exchangeRateCurrent().call();
3535
const mantissa = 18 + parseInt(underlyingDecimals) - vTokenDecimals;
36-
const onevTokenInUnderlying = exchangeRateCurrent / Math.pow(10, mantissa);
36+
const oneVTokenInUnderlying = exchangeRateCurrent / Math.pow(10, mantissa);
3737
console.log('1 vBUSD can be redeemed for', oneVTokenInUnderlying, 'BUSD');
3838
```
3939

40-
As BNB lacks an underlying contract, you must set the 'underlyingDecimals' to 18 when dealing with vBNB. 
40+
As BNB lacks an underlying contract, you must set the 'underlyingDecimals' to 18 when dealing with vBNB.
4141

42-
To calculate the number of underlying tokens that can be redeemed using vTokens, you should divide the total amount of vTokens by the previously computed 'oneVTokenInUnderlying' value.
42+
To calculate the number of underlying tokens that can be redeemed using vTokens, you should multiply the total amount of vTokens by the previously computed 'oneVTokenInUnderlying' value.
4343

4444
$$
4545
underlyingTokens = vTokenAmount \times oneVTokenInUnderlying
@@ -49,24 +49,24 @@ $$
4949

5050
Interest rates for each market are updated in any block where there is a change in the ratio of borrowed assets to supplied assets. The magnitude of this change in interest rates depends on the interest rate model smart contract in place for the market, and the degree of change in the aforementioned ratio.
5151

52-
For a visualization of the current interest rate model applied to each market, refer to the interest rate data visualization notebook available on Observable. Historical interest rates can be sourced from the MarketHistoryServiceAPI.
52+
For a visualization of the current interest rate model applied to each market, refer to the market pages at the [Venus app](https://app.venus.io).
5353

54-
The accrual of interest to suppliers and borrowers in a market occurs when any BSC address interacts with the market's vToken contract. This interaction could be any of the following functions: mint, redeem, borrow, or repay. A successful execution of any of these functions triggers the accrueInterest method, leading to the addition of interest to the underlying balance of every supplier and borrower in the market. Interest accrues for the current block, as well as any previous blocks where the accrueInterest method was not triggered due to lack of interaction with the vToken contract. Interest only accumulates during blocks where one of the aforementioned methods is invoked on the vToken contract.
54+
The accrual of interest to suppliers and borrowers in a market occurs when any wallet interacts with the market's vToken contract. This interaction could be any of the following functions: mint, redeem, borrow, or repay. A successful execution of any of these functions triggers the `accrueInterest` method, leading to the addition of interest to the underlying balance of every supplier and borrower in the market. Interest accrues for the current block, as well as any previous blocks where the `accrueInterest` method was not triggered due to lack of interaction with the vToken contract. Interest only accumulates during blocks where one of the aforementioned methods is invoked on the vToken contract.
5555

56-
Let's consider an example of supply interest accrual: Alice supplies 1 BNB to the Venus Protocol. At the time of her supply, the supplyRatePerBlock is 37893605 Wei, which equates to 0.000000000037893605 BNB per block. For 3 BSC blocks, no interactions occur with the vBNB contract. On the subsequent 4th block, Bob borrows some BNB. As a result, Alice’s underlying balance is updated to 1.000000000151574420 BNB (calculated by multiplying 37893605 Wei by 4 blocks and adding the original 1 BNB). From this point onwards, the accrued interest on Alice’s underlying BNB balance will be based on the updated value of 1.000000000151574420 BNB, rather than the initial 1 BNB. It is important to note that the supplyRatePerBlock value may alter at any given time.
56+
Let's consider an example of supply interest accrual: Alice supplies 1 BNB to the Venus Protocol. At the time of her supply, the `supplyRatePerBlock` is 37893605 Wei, which equates to 0.000000000037893605 BNB per block. For 3 blocks, no interactions occur with the vBNB contract. On the subsequent 4th block, Bob borrows some BNB. As a result, Alice’s underlying balance is updated to 1.000000000151574420 BNB (calculated by multiplying 37893605 Wei by 4 blocks and adding the original 1 BNB). From this point onwards, the accrued interest on Alice’s underlying BNB balance will be based on the updated value of 1.000000000151574420 BNB, rather than the initial 1 BNB. It is important to note that the `supplyRatePerBlock` value may alter at any given time.
5757

5858
### Calculating the APY Using Rate Per Block
5959

60-
The Annual Percentage Yield (APY) for either supplying or borrowing in each market can be computed using the 'supplyRatePerBlock' (for Supply APY) or 'borrowRatePerBlock' (for Borrow APY) values. These rates can be used in the following formula:
60+
The Annual Percentage Yield (APY) for either supplying or borrowing in each market can be computed using the `supplyRatePerBlock` (for Supply APY) or `borrowRatePerBlock` (for Borrow APY) values. These rates can be used in the following formula (assuming a daily compound):
6161

6262
```javascript
6363
Rate = vToken.supplyRatePerBlock(); // Integer
6464
Rate = 37893566
6565
BNB Mantissa = 1 * 10 ^ 18 (BNB has 18 decimal places)
66-
Blocks Per Day = 20 * 60 * 24 (based on 20 blocks occurring every minute)
66+
Blocks Per Day = 20 * 60 * 24 (based on 20 blocks occurring every minute on BNB Chain)
6767
Days Per Year = 365
6868

69-
APY = ((((Rate / BNB Mantissa * Blocks Per Day + 1) ^ Days Per Year - 1)) - 1) * 100
69+
APY = (((Rate / BNB Mantissa) * Blocks Per Day) + 1) ^ (Days Per Year - 1) * 100
7070
```
7171

7272
Here is an example of calculating the supply and borrow APY with Web3.js JavaScript:
@@ -79,8 +79,8 @@ const daysPerYear = 365;
7979
const vToken = new web3.eth.Contract(vBnbAbi, vBnbAddress);
8080
const supplyRatePerBlock = await vToken.methods.supplyRatePerBlock().call();
8181
const borrowRatePerBlock = await vToken.methods.borrowRatePerBlock().call();
82-
const supplyApy = (((Math.pow((supplyRatePerBlock / bnbMantissa * blocksPerDay) + 1,
83-
const borrowApy = (((Math.pow((borrowRatePerBlock / bnbMantissa * blocksPerDay) + 1,
82+
const supplyApy = Math.pow(((supplyRatePerBlock / bnbMantissa) * blocksPerDay) + 1, daysPerYear - 1) * 100;
83+
const borrowApy = Math.pow(((borrowRatePerBlock / bnbMantissa) * blocksPerDay) + 1, daysPerYear - 1) * 100;
8484
console.log(`Supply APY for BNB ${supplyApy} %`);
8585
console.log(`Borrow APY for BNB ${borrowApy} %`);
8686
```

0 commit comments

Comments
 (0)