Hands-On Blockchain Development in 7 Days by Will Button

Hands-On Blockchain Development in 7 Days by Will Button

Author:Will Button
Language: eng
Format: epub
Tags: COM018000 - COMPUTERS / Data Processing, COM083000 - COMPUTERS / Security / Cryptography, COM062000 - COMPUTERS / Data Modeling and Design
Publisher: Packt Publishing
Published: 2019-03-15T04:27:58+00:00


Then we're going to use async and await to get the deployed version of our contract from the Ethereum network. So, let me break a async and await down for you in case you haven't seen that before. Let's say we had a line of JavaScript: it's going to use the artifact we imported representing our contract to get the actual contract instance deployed on the Ethereum network, but the way JavaScript works once we call this function means that it thinks it's done and it moves on from here: it's asynchronous. So, even though we called the deployed function and it hasn't returned a value, JavaScript moved on anyway. The gaming variable is actually undefined until this call completes, which causes a lot of headaches as you try to figure out why this variable sometimes has a value, and sometimes doesn't:

gaming = Gaming.deployed()

const fundGame = gaming.fundGame()

So, to avoid that pain we use async and await. The way it works is we declare this anonymous function here using the async keyword, then inside the function anytime we have a function or call that we need to wait on we use the await keyword. Now, there's a lot more going on under the hood than that, but that's the bare minimum you need to know to understand this. Instead of async and await, some other patterns you might see include callbacks and promises.

And now we've got one more variable declared, which is a constant called fundGame. This function lets me send some initial Ethereum to the contract, so that as we start testing our contract has some funds to pay out any winners. Without this Ethereum, any test that results in a winning scenario would fail because the contract would have insufficient funds to cover the payout. And take a look at this: it's also using the await keyword, because once we call this function it doesn't mean the execution is complete. We need to wait for that block to be mined before the operation is considered a success:

const fundGame = await gaming.fundGame({from: owner, value: web3.utils.toWei('10', 'ether')})

})



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.