- tags: Ethereum
ETH
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: Blockchain, 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: ...
Proof-of-stake
tags: Blockchain,Blockchain Proof,Ethereum,Solana source: https://ethereum.org/en/developers/docs/consensus-mechanisms/pos/ Proof workflow: Users stake money(ETH) to become a validator. Validators are chosen at random to create blocks and are responsible for checking and confirming blocks they don’t create. user’s stake is also used as a way to incentivise good validator behavior. For example, a user can lose a portion of their stake for things like going offline (failing to validate) or their entire stake for deliberate collusion. ...