Created
July 22, 2019 08:00
-
-
Save djuanit0x/5f3e37369f468d584f9bf60a88fc6c01 to your computer and use it in GitHub Desktop.
provable-bitcoin-price
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BitcoinPrice = artifacts.require("BitcoinPrice"); | |
const sleep = ms => { | |
return new Promise(resolve => { | |
setTimeout(resolve, ms); | |
}); | |
}; | |
const getEventLogByTopic = async eventTopic => { | |
try { | |
let logEventFromTopic; | |
do { | |
let logs = await web3.eth.getPastLogs({ | |
fromBlock: 0, | |
topic: [eventTopic] | |
}); | |
logEventFromTopic = logs.find(log => { | |
const {topics} = log; | |
const [topic] = topics; | |
return topic === eventTopic; | |
}); | |
await sleep(1000); | |
} while (logEventFromTopic === undefined); | |
return logEventFromTopic; | |
} catch (err) { | |
console.error(err); | |
} | |
}; | |
contract("BitcoinPrice", accounts => { | |
let bitcoinPrice; | |
const newProvableResultEventTopic = web3.utils.sha3("LogNewProvableResult(string)"); | |
before(async () => { | |
bitcoinPrice = await BitcoinPrice.new({from: accounts[0]}); | |
}); | |
it("should retrieve the bitcoin price in USD", async () => { | |
await bitcoinPrice.retrieveBitcoinPrice({from: accounts[0]}); | |
const newProvableResultEventLog = await getEventLogByTopic(newProvableResultEventTopic); | |
const bitcoinPriceFromCoinGecko = web3.utils.hexToUtf8(newProvableResultEventLog.data); | |
console.log("##############################################################################"); | |
console.log("### "); | |
console.log(`### Bitcoin Price: $${bitcoinPriceFromCoinGecko} `); | |
console.log("### "); | |
console.log("##############################################################################"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment