去中心化应用集成与去中心化交易所指南
1. 智能合约随机数接收与验证
在开发智能合约时,我们常常需要从外部获取随机数等数据。以下代码展示了如何使用 Node.js 从预言机接收随机数并验证合约是否正常工作:
contractAddress = ABI.networks['3'].address
contractInstance = new web3.eth.Contract(ABI.abi, contractAddress)
console.log('Listening to events...')
// Listen to the generate random event for executing the __callback() function
const subscription = contractInstance.events.GenerateRandom()
subscription.on('data', newEvent => {
callback(newEvent.returnValues.sequence)
})
// Listen to the ShowRandomNumber() event that gets emitted after the callback
const subscription2 = contractInstance.events.ShowRandomNumber()
subscription2.on('data', newEvent => {
console.log('Received random number! Sequence:', newEv
超级会员免费看
订阅专栏 解锁全文
1283

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



