- tags: Blockchain
DeFi
Links to this note
Compound 收益计算
tags: Blockchain, DeFi, ETH, AAVE 收益计算 有了前面的 AAVE 收益计算,我们采用相同的思路来查看如何计算 Compound 的收益1,首先来查看 balanceOf2 /** * @notice Query the current positive base balance of an account or zero * @dev Note: uses updated interest indices to calculate * @param account The account whose balance to query * @return The present day base balance magnitude of the account, if positive */ function balanceOf(address account) override public view returns (uint256) { (uint64 baseSupplyIndex_, ) = accruedInterestIndices(getNowInternal() - lastAccrualTime); int104 principal = userBasic[account].principal; return principal > 0 ? presentValueSupply(baseSupplyIndex_, unsigned104(principal)) : 0; } 这里用到两个变量:principal 和 baseSupplyIndex,principal 可以通过 userBasic(address) 读取3,但是 baseSupplyIndex 只能通过合约读取,无法在链下获取, 仔细研究presentValueSupply4: ...
AAVE 收益计算
tags: Blogchain, ETH, DeFi AAVE 的收益直接体现在接收的 AToken 上,比如 aEthUSDT,可以通过 balanceOf 查询代币余额,余额及本金+收益, 所以思路是看下 balanceOf 的实现即可推算出计算收益的方式,其实现如下1: /// @inheritdoc IERC20 function balanceOf( address user ) public view virtual override(IncentivizedERC20, IERC20) returns (uint256) { return super.balanceOf(user).rayMul(POOL.getReserveNormalizedIncome(_underlyingAsset)); } 上面代码调用标准的 IERC20 获取用户的余额,代表了用户的原始投入,然后乘了一个系数,这个系数应该就是代表了用户的收益,对应的实现2: /// @inheritdoc IPool function getReserveNormalizedIncome( address asset ) external view virtual override returns (uint256) { return _reserves[asset].getNormalizedIncome(); } 继续跟踪代码3: /** * @notice Returns the ongoing normalized income for the reserve. * @dev A value of 1e27 means there is no income. As time passes, the income is accrued * @dev A value of 2*1e27 means for each unit of asset one unit of income has been accrued * @param reserve The reserve object * @return The normalized income, expressed in ray */ function getNormalizedIncome( DataTypes.ReserveData storage reserve ) internal view returns (uint256) { uint40 timestamp = reserve.lastUpdateTimestamp; //solium-disable-next-line if (timestamp == block.timestamp) { //if the index was updated in the same block, no need to perform any calculation return reserve.liquidityIndex; } else { return MathUtils.calculateLinearInterest(reserve.currentLiquidityRate, timestamp).rayMul( reserve.liquidityIndex ); } } 可以看到这里涉及一个 reserve.liquidityIndex 的存储变量,通过查阅文档我们可以通过 Pool.getReserveData(address asset) 来获取4: ...
DeFi from Scratch
tags: DeFi, Blockchain, Uniswap, Uniswap V3 I need derive price from decentralized exchanges, and I have finished some research about Uniswap v3. It’s brand new for me, and after that I want to share some ideas about DeFi, and I think it would be helpful for you to understand Uniswap and some other decentralized exchanges. DeFi from Scratch We exchange stuffs very often, and we can exchange tokens in a centralized exchange. For example, Alice owns some ETH, and Bob owns some DAI. Now, Alice want to swap some ETH for DAI, and Bob want to swap some DAI for ETH. How could they do? ...
Uniswap
tags: Blockchain,Ethereum,DeFi