首页 文章

需要从任何区块链上的块中检索事务

提问于
浏览
0

我被分配了这个任务,我必须从任何区块链网络上的块中检索事务,并使用GO编程语言创建日志文件 . 我搜索了以太坊区块链并试图使用geth客户端做同样的事情,但它让我下载了超过100gb的整个区块链 . 所以我的问题是,有没有办法访问任何区块链上的块并读取它的事务并使用相同的方法来创建日志文件 . 我只需要抬起头来 . 帮助赞赏 . 谢谢

1 回答

  • 1

    请使用松露Ganache ethereum客户端 . 从http://truffleframework.com/ganache/下载

    我创建了NodeJS代码来读取最新块的事务 . 步骤1:如果未安装在您的机器中,请安装nodeJS和NPM . 第2步:创建新文件夹“demo”并创建新的package.json文件 . 将下面的代码放在package.json文件中

    {
      "name": "transactionRead",
      "version": "1.0.0",
      "description": "Blockchain Transaction Read",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "dependencies": {
        "web3": "^0.19.0"
      },
      "author": "",
      "license": "ISC"
    }
    
    • 创建index.js文件并放在代码下面 .

    var Web3 = require('web3'); var fs = require('fs'); //创建一个日志文件来存储事务fs.writeFile('log.txt','Hello Transaction!',function(err){if(err)throw err; console.log('Created!');}); //使用HTTP提供程序创建web3实例 . //在雾中注意web3已经可用,因此首先检查它是否's available before instantiating if (typeof web3 !== ' undefined'){web3 = new Web3(web3.currentProvider); } else {//从Web3.providers web3 = new Web3(new Web3.providers.HttpProvider(“http://localhost:7545”))设置你想要的提供者; }

    //监视区块链事务,如果找到更改,则获取事务数据var filter = web3.eth.filter('latest',function(error,blockHash){if(!error){var block = web3.eth.getBlock(blockHash ,true); if(block.transactions.length> 0){console.log(“found”block.transactions.length“块中的事务”blockHash); fs.appendFile('log.txt',JSON.stringify(block) .transactions),function(err){if(err)throw err; console.log('Updated!');}); console.log(JSON.stringify(block.transactions));} else {console.log( “块中没有交易:”blockHash);}}});

    步骤4:通过命令行运行 $ node index.js 命令

    如果需要任何帮助,请告诉我 . 谢谢,

相关问题