【问题标题】:Smart contract blockchain history智能合约区块链历史
【发布时间】:2017-09-23 22:23:01
【问题描述】:

智能合约是否可以查看过去在其上完成的交易,换句话说,智能合约是否可以查看自成立以来谁向其发送了以太币?

【问题讨论】:

    标签: ethereum solidity web3js


    【解决方案1】:

    如果智能合约有一个数据结构来记录这些活动的发生,那将是可能的。

    匆忙准备的例子:

    pragma solidity ^0.4.6;
    
    contract TrackPayments {
    
        struct PaymentStruct {
            address sender;
            uint amount;
        }
    
        // look up the struct with payment details using the unique key for each payment
        mapping(bytes32 => PaymentStruct) public paymentStructs;
    
        // payment keys in order received
        bytes32[] public paymentKeyList;
    
        event LogPaymentReceived(address sender, uint amount);
    
        function payMe() public payable returns(bool success) {
            if(msg.value==0) throw;
            // make a unique key ... 
            bytes32 newKey = sha3(msg.sender, paymentKeyList.length);
            paymentStructs[newKey].sender = msg.sender;
            paymentStructs[newKey].amount = msg.value;
            paymentKeyList.push(newKey);
            LogPaymentReceived(msg.sender, msg.value);
            return true;
        }
    
        function getPaymentCount() public constant returns(uint paymentCount) { return paymentKeyList.length; }
    
    }
    

    以牺牲顺序或随机访问为代价,这可能会提高存储效率。这种方式两者兼而有之。

    希望对您有所帮助。

    【讨论】:

    • 不,我真的想知道智能合约是否可以直接从区块链读取。但事实并非如此。
    • 是的,你需要手动实现。如果你有一个区块链浏览器(一个外部程序),比如live.ether.camp,你可以查看特定区块的内容。但是,如果您想跟踪合同内的内容并以编程方式检索它,则需要手动实现它。
    【解决方案2】:

    如果您尝试使用智能合约从区块链读取交易,则在solidity 语言中答案是“否”不存在这个。您可以使用 Web3.js 和 Rob 的示例来执行此操作,或者将事务的日志保存在外部数据库中,这对于检查大量块和大量事务以及异步问题的复杂性是这样的。

    在其他情况下,您可以使用外部 api 来检查交易并拥有历史记录,例如:https://etherscan.io/apis

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      相关资源
      最近更新 更多