ETH智能合约Solidity需要数字货币实时价格怎么处理

智能合约运行在一个封闭的空间 ,如果合约需要外部环境的数据,创建者需要实时同步数据到合约,目前Gas 的价格奇高,合约运营者需要付出大量的成本。目前低成本解决方案Chainlink 是不错的选择。

Chainlink预言机(节点)能够格式化信息并验证数据,让智能合约安全可靠地连接到各种链下资源,包括数据提供商、web API、企业系统、云平台、物联网设备以及支付系统等

Chainlink还建立了保证金惩罚制度,激励节点诚实守信。在中心化的预言机模式中,用户可以约束私营企业的行为。而为了要约束节点的行为,那就需要建立一定的保险机制。节点必须预存一定数额的LINK代币作为保证金,才能有机会接收处理某一个数据请求。如果该节点的数据被发现是异常数据,那么其保证金将被没收并退还给数据请求方作为补偿。Chainlink采用博弈论的原理激励节点提供准确的数据,否则节点就将受到罚款。

/** This example code is designed to quickly deploy an example contract using Remix.
 *  If you have never used Remix, try our example walkthrough: https://docs.chain.link/docs/example-walkthrough
 *  You will need testnet ETH and LINK.
 *     - Ropsten ETH faucet: https://faucet.ropsten.be/
 *     - Ropsten LINK faucet: https://ropsten.chain.link/
 */

pragma solidity ^0.6.7;

import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/interfaces/AggregatorInterface.sol";

contract PriceConsumer {

    AggregatorInterface internal priceFeed;

    /**
     * Network: Ropsten
     * Aggregator: ETH/USD
     * Address: 0x8468b2bDCE073A157E560AA4D9CcF6dB1DB98507
     */
    constructor() public {
        priceFeed = AggregatorInterface(0x8468b2bDCE073A157E560AA4D9CcF6dB1DB98507);
    }
  
    /**
     * Returns the latest price
     */
    function getLatestPrice() public view returns (int256) {
        return priceFeed.latestAnswer();
    }

    /**
     * Returns the timestamp of the latest price update
     */
    function getLatestPriceTimestamp() public view returns (uint256) {
        return priceFeed.latestTimestamp();
    }
}


The latestAnswer value for all USD reference data contracts is multiplied by 100000000 before being written on-chain and by 1000000000000000000 for all ETH pairs.

具体的API 接口文档可以查看 https://docs.chain.link/docs/price-feeds-api-reference

以太分析是覆盖数字货币相关日常工作的网络社区,这里有权威的分析师提供ETH 、EOS、BSV等主链智能合约游戏分析资讯,也提供各种智能合约游戏仿盘开发,游戏制度分析、游戏安全等交流平台。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值