当我想要订阅log活动并在ThunderCore上获得通知时,我会遇到一些问题。
据我所知,似乎我应该使用websocket和eth_subscribe。有没有什么例子?
或者,是否有其他解决方案可以实现我的目标?
发布于 2020-12-10 04:13:01
listen_to_thundercore_mainnet.js
/**
* Connect to Thundercore mainnet and listen for tips to a specific smart contract address.
*/
const Web3 = require('web3');
var provider = 'wss://mainnet-ws.thundercore.com';
var web3 = new Web3(provider);
let listenOnce = () => {
// Create a connection to Thundercore mainet.
const smartContractAddress = '0xb7d82e5B73e01bB4c1cFB1448f1215bf165929a2'
var subscription = web3.eth.subscribe('logs', {
address: smartContractAddress,
}, (error, result) => {
if (error) {
console.log(error)
console.log('Reconnecting to Thundercore mainnet.')
setTimeout( () => {
// Re-instantiating web3 is needed in case the connection is dropped.
web3 = new Web3(provider);
_listen()
}, 5000);
}
})
.on('connected', (subscriptionId) => {
console.log('connected')
})
.on('data', (log) => {
console.log(log);
})
.on("changed", (log) => {
console.log('changed')
})
return subscription
}
const _listen = async () => {
let tips = listenOnce();
console.log('Connected to Thundercore mainnet.')
// web3 subscription times out at 60 secs. Close and reopen at 50 secs.
setInterval( () => {
tips.unsubscribe( (error, success) => {
if(error) {
console.log('Failed to disconnect from Thundercore mainnet!');
}
if(success) {
console.log('disconnected');
}
});
tips = listenOnce();
}, (50 * 1000));
}
_listen()使用NodeJS运行
node listen_to_thundercore_mainnet.jsThundercore会在60秒后断开你的连接。因此,我们在此之前故意断开连接,然后立即重新连接。当遵循具有巨大流量的合同时,这种方法将错过一些交易。
发布于 2020-05-18 19:05:30
通过以下方式获取有关ThunderCore上新合同事件的通知:
使用到RPC节点的Websocket连接的
logs event上的https://testnet-rpc.thundercore.com
logs RPC方法自包含示例
SimpleRecord.sol
pragma solidity ^0.4.25;
contract SimpleRecord {
event Record(
address indexed _from,
uint _value
);
function write() payable public {
emit Record(msg.sender, msg.value);
}
}要生成Record事件,请运行simple-record-write以查看运行simple-record-log-subscribe的通知
const process = require('process')
const path = require('path')
const fs = require('fs')
const Web3 = require('web3')
const Accounts = require('web3-eth-accounts')
const util = require('util')
// truffle migrate --reset --network thunder-mainnet
const thunderWsUrl = 'wss://mainnet-ws.thundercore.com'
// truffle migrate --reset --network thunder-testnet
//const thunderWsUrl = 'wss://testnet-ws.thundercore.com'
const programName = () => {
return path.basename(process.argv[1])
}
const web3Url = () => {
let u = process.env['WEB3_PROVIDER_URI']
if (u === undefined) {
u = thunderWsUrl
}
return u
}
const signTx = async (fromAccount, tx) => {
const signedTx = await fromAccount.signTransaction(tx)
return signedTx.rawTransaction // hex string
}
const setup = async () => {
const privateKeys = fs.readFileSync(path.join(__dirname, '..', '.private-keys'), {encoding: 'ascii'}).split('\n').filter(x => x.length > 0)
const accounts = new Accounts()
const account = accounts.privateKeyToAccount('0x' + privateKeys[0])
const jsonBuf = fs.readFileSync(path.join(__dirname, '..', 'build', 'contracts', 'SimpleRecord.json'))
const contractData = JSON.parse(jsonBuf)
const contractAbi = contractData['abi']
const web3ProviderUrl = web3Url()
const web3 = new Web3(web3ProviderUrl)
const networkId = await web3.eth.net.getId()
let deployedNetwork, contractAddress
try {
deployedNetwork = contractData['networks'][networkId]
contractAddress = deployedNetwork['address']
} catch (err) {
msg = `error getting deployedNetwork: ${err}`
throw new Error(msg)
}
const contract = new web3.eth.Contract(contractAbi, contractAddress)
return [ web3ProviderUrl, web3, networkId, contractAddress, contract, account ]
}
const prettyPrint = (o) => {
return util.inspect(o, {showHidden: false, depth: null, colors: true})
}
const recordWrite = async () => {
const [web3ProviderUrl, web3, chainId, contractAddress, contract, fromAccount] = await setup()
console.log('web3ProviderUrl:', web3ProviderUrl)
const txnData = contract.methods.write().encodeABI()
console.log('account.address:', fromAccount.address)
const promiseResults = await Promise.all([
web3.eth.getTransactionCount(fromAccount.address),
web3.eth.getGasPrice(),
])
const nonce = promiseResults[0]
const gasPrice = promiseResults[1]
const tx = {
'gasLimit': 0,
'chainId': chainId,
'gasPrice': gasPrice,
'nonce': Web3.utils.toHex(nonce),
'from': fromAccount.address,
'to': contractAddress,
'value': 0xbeef,
'data': txnData,
}
const gasMultiple = 2.0
tx.gasLimit = (await web3.eth.estimateGas(tx)) * gasMultiple
console.log('tx:', prettyPrint(tx))
const rawTxStr = await signTx(fromAccount, tx)
const r = await web3.eth.sendSignedTransaction(rawTxStr)
console.log('sendTransaction: receipt:', prettyPrint(r))
return 0
}
const logSubscribe = () => {
return new Promise((resolve, reject) => {
setup().then(([web3ProviderUrl, web3, chainId, contractAddress, contract, account]) => {
let eventCount = 0
console.log('web3ProviderUrl:', web3ProviderUrl)
console.log('contractAddress:', contractAddress)
console.log('contract.options.jsonInterface:', prettyPrint(contract.options.jsonInterface))
const eventAbis = contract.options.jsonInterface.filter((abiObj) => abiObj.type === 'event')
web3.eth.subscribe('logs', { address: contractAddress }, (err, log) => {
console.log('eth.subscribe("logs") callback')
if (err) {
console.log('logs callback, err:', err)
reject(err)
return
}
eventCount++
console.log(`log[${eventCount}]:`, log)
const eventSig = log.topics[0]
for (let abi of eventAbis) {
if (eventSig === abi.signature) {
const decoded = web3.eth.abi.decodeLog(abi.inputs, log.data, log.topics.slice(1))
console.log('Decoded Event:', prettyPrint(abi), '\n', prettyPrint(decoded))
resolve(0)
return
}
}
})
})
})
}
(async () => {
if (programName().endsWith('-write')) {
process.exit(await recordWrite())
} else if (programName().endsWith('-log-subscribe')) {
process.exit(await logSubscribe())
} else {
console.error(`unsupported program name: "${programName()}"`)
process.exit(2)
}
})()请参阅field-support存储库的subscribe-to-logs分支中的完整项目here。
https://stackoverflow.com/questions/61812515
复制相似问题