知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
const Web3 = require('web3'); const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws')); var subscription = web3.eth.subscribe('pendingTransactions', function(error, result){ if (!error) console.log(result); }) .on("data", function(transaction){ console.log(transaction); }); // unsubscribes the subscription subscription.unsubscribe(function(error, success){ if(success) console.log('Successfully unsubscribed!'); });
const Web3 = require('web3'); const web3wss = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws')); const web3 = new Web3("https://mainnet.infura.io/CsS9shwaAab0z7B4LP2d"); var subscription = web3wss.eth.subscribe('pendingTransactions', function(error, result){ if (!error){ console.log("--------------------" + result +"--------------------"); var receipt = web3.eth.getTransactionReceipt(result).then(console.log); } }) .on("data", function(transaction){ console.log(transaction); }); // unsubscribes the subscription subscription.unsubscribe(function(error, success){ if(success) console.log('Successfully unsubscribed!'); });
#!/usr/bin/env node const Web3 = require('web3'); const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws')); const subscription = web3.eth.subscribe('newBlockHeaders', (error, blockHeader) => { if (error) return console.error(error); console.log('Successfully subscribed!', blockHeader); }).on('data', (blockHeader) => { console.log('data: ', blockHeader); }); // unsubscribes the subscription subscription.unsubscribe((error, success) => { if (error) return console.error(error); console.log('Successfully unsubscribed!'); });
#!/usr/bin/env node const Web3 = require('web3'); const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws')); var subscription = web3.eth.subscribe('logs', { fromBlock: 5709426, address: '0x7fFdCccC3E7e33C6163393195A947A6d45f25814' }, function(error, result){ if (!error) console.log(result); }) .on("data", function(log){ console.log(log); }) .on("changed", function(log){ }); // unsubscribes the subscription subscription.unsubscribe(function(error, success){ if(success) console.log('Successfully unsubscribed!'); });
const Web3 = require('web3'); const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws')); var subscription = web3.eth.subscribe('syncing', function(error, sync){ if (!error) console.log(sync); }) .on("data", function(sync){ // show some syncing stats }) .on("changed", function(isSyncing){ if(isSyncing) { // stop app operation } else { // regain app operation } }); // unsubscribes the subscription subscription.unsubscribe(function(error, success){ if(success) console.log('Successfully unsubscribed!'); });