您正在查看: surou 发布的文章

compound-finance compound-protocol 存和借利率合约分析

计算每块供应率
(github)

 /**
     * @notice Returns the current per-block supply interest rate for this cToken
     * @return The supply interest rate per block, scaled by 1e18
     */
function supplyRatePerBlock() external view returns (uint) {
    return interestRateModel.getSupplyRate(getCashPrior(), totalBorrows, totalReserves, reserveFactorMantissa);
}

返回-每块的供应利率,按1e18缩放
跟一下关键参数

getCashPrior()

github

/**
     * @notice Gets balance of this contract in terms of the underlying
     * @dev This excludes the value of the current message, if any
     * @return The quantity of underlying tokens owned by this contract
     */
function getCashPrior() internal view returns (uint) {
    EIP20Interface token = EIP20Interface(underlying);
    return token.balanceOf(address(this));
}

代码逻辑为,获取当前合约地址拥有的基础代币余额数量

totalBorrows

借出的代币总数

totalReserves

存入的代币总数(储备金)

reserveFactorMantissa

市场的当前储备系数

getSupplyRate

(github)

 /**
     * @notice Calculates the current supply rate per block
     * @param cash The amount of cash in the market
     * @param borrows The amount of borrows in the market
     * @param reserves The amount of reserves in the market
     * @param reserveFactorMantissa The current reserve factor for the market
     * @return The supply rate percentage per block as a mantissa (scaled by 1e18)
     */
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); // 市场利用率/缩小储备系数后的借出利率
}
附加

add sub mul div 来自SafeMath库,对等是加减乘除

utilizationRate 计算市场利用率

github

/**
     * @notice Calculates the utilization rate of the market: `borrows / (cash + borrows - reserves)`
     * @param cash The amount of cash in the market
     * @param borrows The amount of borrows in the market
     * @param reserves The amount of reserves in the market (currently unused)
     * @return The utilization rate as a mantissa between [0, 1e18]
     */
function utilizationRate(uint cash, uint borrows, uint reserves) public pure returns (uint) {
    // Utilization rate is 0 when there are no borrows
    if (borrows == 0) {
        return 0;
    }
    return borrows.mul(1e18).div(cash.add(borrows).sub(reserves)); // 借出/(账户剩余+借出-存入)
    // 借出,就是发币合约把代币转给其他的账户,当其他账户存币时,再转回发币合约账户
}

getBorrowRate 获取每个块的当前借入利率

 /**
     * @notice Calculates the current borrow rate per block, with the error code expected by the market
     * @param cash The amount of cash in the market
     * @param borrows The amount of borrows in the market
     * @param reserves The amount of reserves in the market
     * @return The borrow rate percentage per block as a mantissa (scaled by 1e18)
     */
function getBorrowRate(uint cash, uint borrows, uint reserves) public 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);
    }
}
kink

应用跳转乘数的利用率点,当利用率小于这个配置值时,走相应逻辑

multiplierPerBlock

利用率乘数,得出利率的斜率

multiplierPerYear

利用率的利率增长率(按1e18缩放
multiplierPerBlock = multiplierPerYear.div(blocksPerYear);

jumpMultiplierPerBlock

jumpMultiplierPerBlock = jumpMultiplierPerYear.div(blocksPerYear);

baseRatePerYear

初始值,每年的基础利率

blocksPerYear

利率模型假设的每年大约的块数

baseRatePerBlock

基本利率是利用率为0时的y截距,
baseRatePerBlock = baseRatePerYear.div(blocksPerYear);

jumpMultiplierPerBlock

达到指定的利用率点后的multiplierPerBlock

总结

稍后补充

参考

https://www.jinse.com/blockchain/485426.html

chrome清除缓存、不使用缓存而刷新快捷键

Ctrl+Shift+Del 清除Google浏览器缓存的快捷键
Ctrl+Shift+R 重新加载当前网页而不使用缓存内容
Ctrl + F5 去除缓存+刷新快捷键

Cosmos钱包管理和离线交易签名工具

github: https://github.com/hukkinj1/cosmospy

cosmospy

Version 5.0.1

Tools for Cosmos wallet management and offline transaction signing

Installing

Installing from PyPI repository (https://pypi.org/project/cosmospy):

pip install cosmospy

Usage

Generating a wallet

from cosmospy import generate_wallet
wallet = generate_wallet()

The value assigned to wallet will be a dictionary just like:

{
    "seed": "arch skill acquire abuse frown reject front second album pizza hill slogan guess random wonder benefit industry custom green ill moral daring glow elevator",
    "derivation_path": "m/44'/118'/0'/0/0",
    "private_key": b"\xbb\xec^\xf6\xdcg\xe6\xb5\x89\xed\x8cG\x05\x03\xdf0:\xc9\x8b \x85\x8a\x14\x12\xd7\xa6a\x01\xcd\xf8\x88\x93",
    "public_key": b"\x03h\x1d\xae\xa7\x9eO\x8e\xc5\xff\xa3sAw\xe6\xdd\xc9\xb8b\x06\x0eo\xc5a%z\xe3\xff\x1e\xd2\x8e5\xe7",
    "address": "cosmos1uuhna3psjqfxnw4msrfzsr0g08yuyfxeht0qfh",
}

Converter functions

Mnemonic seed to private key

from cosmospy import seed_to_privkey
seed = "teach there dream chase fatigue abandon lava super senior artefact close upgrade"
privkey = seed_to_privkey(seed, path="m/44'/118'/0'/0/0")

Private key to public key

from cosmospy import privkey_to_pubkey
privkey = bytes.fromhex("6dcd05d7ac71e09d3cf7da666709ebd59362486ff9e99db0e8bc663570515afa")
pubkey = privkey_to_pubkey(privkey)

Public key to address

from cosmospy import pubkey_to_address
pubkey = bytes.fromhex("03e8005aad74da5a053602f86e3151d4f3214937863a11299c960c28d3609c4775")
addr = pubkey_to_address(pubkey)

Private key to address

from cosmospy import privkey_to_address
privkey = bytes.fromhex("6dcd05d7ac71e09d3cf7da666709ebd59362486ff9e99db0e8bc663570515afa")
addr = privkey_to_address(privkey)

Signing transactions

from cosmospy import Transaction
tx = Transaction(
    privkey=bytes.fromhex("26d167d549a4b2b66f766b0d3f2bdbe1cd92708818c338ff453abde316a2bd59"),
    account_num=11335,
    sequence=0,
    fee=1000,
    gas=70000,
    memo="",
    chain_id="cosmoshub-3",
    sync_mode="sync",
)
tx.add_transfer(recipient="cosmos103l758ps7403sd9c0y8j6hrfw4xyl70j4mmwkf", amount=387000)
tx.add_transfer(recipient="cosmos1lzumfk6xvwf9k9rk72mqtztv867xyem393um48", amount=123)
pushable_tx = tx.get_pushable()

One or more token transfers can be added to a transaction by calling the add_transfer method.

When the transaction is fully prepared, calling get_pushable will return a signed transaction in the form of a JSON string. This can be used as request body when calling the POST /txs endpoint of the Cosmos REST API.

让区块链浏览器收录你的 ERC20 代币

如果想让其他浏览器显示新发的代币,需要主动提交代币信息,对方审核通过后,即可显示。

打开网址https://etherscan.io,在此处输入 token 的智能合约地址,如下图:

搜索后,如下图:

按下图指示,点击“verify and publish”来发布代码,

由于读者已经发布了代码,这里就找了几张图贴出来,以供参考。

步骤一:

步骤二:

步骤三:

若出现如下图,即表示收录成功。

转载:https://my.oschina.net/u/3050295/blog/1982504

Eosplayer -> eosjs的绑定层

Eosplayer是eosjs的绑定层,它基于eosjs并为应用程序层提供了更好的可用性。它既可以在node.js服务器上使用,也可以在已经安装了Scatter的浏览器或Dapp钱包中使用。

frontd releases : https://github.com/bagaking/eosplayer/releases

演示地址:http://eosplayer.kihand.com/#/