以太坊开发:从优化项目到构建去中心化交易所
1. 智能合约与预言机交互
在以太坊开发中,智能合约与外部数据交互是一个重要的环节,这通常借助预言机来实现。以下是一段使用 web3.js 监听智能合约事件的代码示例,用于接收从 Node.js 预言机生成的随机数:
contractAddress = ABI.networks['3'].address
contractInstance = new web3.eth.Contract(ABI.abi, contractAddress)
console.log('Listening to events...')
// 监听生成随机数事件以执行 __callback() 函数
const subscription = contractInstance.events.GenerateRandom()
subscription.on('data', newEvent => {
callback(newEvent.returnValues.sequence)
})
// 监听回调后发出的 ShowRandomNumber() 事件
const subscription2 = contractInstance.events.ShowRandomNumber()
subscription2.on('data', newEvent => {
console.log('Received random number! Sequence:', newEvent.returnValues.sequence, 'Randomly generated num
超级会员免费看
订阅专栏 解锁全文
778

被折叠的 条评论
为什么被折叠?



