简体   繁体   English

需要从任何区块链上的区块中检索交易

[英]need to retrieve transaction from a block on any blockchain

I am assigned this task that I have to retrieve transactions from a block on any blockchain network and create a log file using GO programming language. 我被分配了这个任务,我必须从任何区块链网络上的一个区块中检索交易,并使用GO编程语言创建一个日志文件。 I have searched ethereum blockchain and tried to do the same using geth client but it makes me download the whole blockchain which is more than 100gb. 我已经搜索了以太坊区块链,并尝试使用geth客户端进行相同的操作,但这使我下载了超过100GB的整个区块链。 So my question is, is there any way to access a block on any blockchain and read it's transactions and use the same to create a log file. 所以我的问题是,是否有任何方法可以访问任何区块链上的块并读取其交易并使用该交易来创建日志文件。 I just need some head up. 我只需要抬头。 Help appreciated. 帮助表示赞赏。 thanks 谢谢

Please use truffle Ganache ethereum client. 请使用松露Ganache以太坊客户端。 Download from http://truffleframework.com/ganache/ http://truffleframework.com/ganache/下载

I have created NodeJS code to read transaction from latest block. 我创建了NodeJS代码以从最新的块读取事务。 Step 1: Install nodeJS and NPM if not installed in your machine. 步骤1:如果未安装在计算机中,请安装nodeJS和NPM。 Step 2: Create new folder "demo" and create new package.json file. 第2步:创建新文件夹“ demo”并创建新的package.json文件。 Place below code in package.json file 将以下代码放在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"
}
  1. Create index.js file and place below code. 创建index.js文件并将其放在代码下面。

    var Web3 = require('web3'); var Web3 = require('web3'); var fs = require('fs'); var fs = require('fs'); //Create a log file to store transaction fs.writeFile('log.txt', 'Hello Transaction!', function (err) { if (err) throw err; console.log('Created!'); }); //创建一个日志文件来存储事务fs.writeFile('log.txt','Hello Transaction!',function(err){if(err)throw err; console.log('Created!');}); // create an instance of web3 using the HTTP provider. //使用HTTP提供程序创建web3的实例。 // NOTE in mist web3 is already available, so check first if it's available before instantiating if (typeof web3 !== 'undefined') { web3 = new Web3(web3.currentProvider); //注意薄雾中的web3已经可用,因此请首先检查它是否可用,然后再实例化(typeof web3!=='undefined'){web3 = new Web3(web3.currentProvider); } else { // set the provider you want from Web3.providers web3 = new Web3(new Web3.providers.HttpProvider(" http://localhost:7545 ")); } else {//从Web3.providers web3 = new Web3(new Web3.providers.HttpProvider(“ http:// localhost:7545 ”)设置所需的提供程序; } }

    // Watch for blockchain transaction, if found changes fetch the transaction data 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 + " transactions in block " + 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("no transaction in block: " + blockHash); } } }); //监视区块链交易,如果发现更改则获取交易数据var filter = web3.eth.filter('latest',function(error,blockHash){if(!error){var block = web3.eth.getBlock(blockHash ,true);如果(block.transactions.length> 0){console.log(“ found” + block.transactions.length +“块中的事务” + blockHash); fs.appendFile('log.txt',JSON。 stringify(block.transactions),函数(err){if(err)throw err; console.log('Updated!');}); console.log(JSON.stringify(block.transactions));} else {console .log(“ block中没有事务:” + blockHash);}}});

    Step 4: Run $ node index.js command through command line 步骤4:通过命令行运行$ node index.js命令

    Let me know if need any help. 让我知道是否需要任何帮助。 Thanks, 谢谢,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM