From 586bf4fc496b250232e5d5161df559069529ef1e Mon Sep 17 00:00:00 2001 From: emizzle Date: Wed, 18 Jul 2018 23:36:41 +0200 Subject: [PATCH] Updated instructions and readme, fixed error with tests --- README.MD | 5 +---- contracts/DTwitter.sol | 8 +------- instructions/5 Coding: tests.md | 5 +++-- instructions/6 Coding: dApp.md | 23 ++++++++++------------- test/dtwitter_spec.js | 4 ++-- 5 files changed, 17 insertions(+), 28 deletions(-) diff --git a/README.MD b/README.MD index 5fb20c6..1a5343d 100644 --- a/README.MD +++ b/README.MD @@ -25,10 +25,7 @@ To follow along, please use the [instructions](./instructions). * Integrated debugger - we are currently working on this, and the developer can always use remix as an alternative ### Known issues with this dApp ##### Issues on localhost -1. Test for tweet failing - * *Cause:* there is a known error with `ganache` that sometimes causes last test does to fail with the error `ERROR: The returned value is not a convertible string`. While we don't konw the exact cause yet, we are currently investigating this issue. - * *Solution:* not yet known -2. Create account - hangs with metamask +1. Create account - hangs with metamask * Cause - most likely non-increasing nonce issue with metamask: https://github.com/MetaMask/metamask-extension/issues/1999) * Solution - open issue diff --git a/contracts/DTwitter.sol b/contracts/DTwitter.sol index 7c45ab7..efd1c58 100644 --- a/contracts/DTwitter.sol +++ b/contracts/DTwitter.sol @@ -81,7 +81,7 @@ contract DTwitter { users[usernameHash].description = description; // add entry to our owners mapping so we can retrieve - // user by their addres + // user by their address owners[msg.sender] = usernameHash; } @@ -132,12 +132,6 @@ contract DTwitter { // get the username hash of the sender's account bytes32 usernameHash = owners[msg.sender]; - // reject if that user doesn't exist - require(users[usernameHash].creationDate != 0); - - // ensure the retrieved user is indeed the sender - require(users[usernameHash].owner == msg.sender); - // get our user User storage user = users[usernameHash]; diff --git a/instructions/5 Coding: tests.md b/instructions/5 Coding: tests.md index 5fbec0b..bf2f3aa 100644 --- a/instructions/5 Coding: tests.md +++ b/instructions/5 Coding: tests.md @@ -1,5 +1,6 @@ ## Contract tests -Now that we have our contract written, we can use the code generation provided by EmbarkJS to write our contract unit test cases for TDD development. +Now that we have our contract written, we can use the code generation provided by EmbarkJS to write our contract unit test cases for TDD development. Embark will code generate a DTwitter javascript object from our contract and make it accessible in our dApp by way of the `EmbarkJS` object. +> Additionally, Embark also has an API for decentralised storage (IPFS and Swarm), and decentralised communication (Whisper), which are all configurable in config files. ###### Create account transaction should be successful Our first test is to ensure that our `createAccount` transaction is sent successfully. ``` @@ -40,7 +41,7 @@ We need to ensure that our contract events subscription works correctly when som ``` DTwitter.events.NewTweet({ filter: { _from: usernameHash }, - fromBlock: 1 + fromBlock: 0 }) .on('data', (event) => { assert.equal(event.returnValues.tweet, tweetContent); diff --git a/instructions/6 Coding: dApp.md b/instructions/6 Coding: dApp.md index 7f38131..e4ba00f 100644 --- a/instructions/6 Coding: dApp.md +++ b/instructions/6 Coding: dApp.md @@ -1,7 +1,4 @@ ## Coding: dApp -Embark will code generate a DTwitter javascript object from our contract and make it accessible in our dApp by way of the `EmbarkJS` object. -> Additionally, Embark also has an API for decentralised storage (IPFS and Swarm), and decentralised communication (Whisper), which are all configurable in config files. - Let’s use the `EmbarkJS` API to interact with our contact in our dApp. As we update the .js files, notice how Embark watches the files and compiles as we save. ###### `CreateUser.js` 1. We need to update the `_handleChange` function that will check if a user exists as the user types in a username. @@ -36,6 +33,16 @@ const gasEstimate = await editAccount.estimateGas({ from: web3.eth.defaultAccoun // send the transaction with our gas estimate (plus a little bit more in case the contract) state has changed since we got our estimate const result = await editAccount.send({ from: web3.eth.defaultAccount, gas: gasEstimate + 1000 }); ``` +###### `DoTweet.js` +The DoTweet components sends a tweet to the contract. We will need to update the `_handleClick` even to estimate gas and call the contract's `tweet` function. +``` +// estimate gas before sending tweet transaction +const gasEstimate = await tweet.estimateGas({ from: web3.eth.defaultAccount, gas: 10000000000 }); + +// send the tweet transaction plus a little extra gas in case the contract state +// has changed since we've done our gas estimate +await tweet.send({ from: web3.eth.defaultAccount, gas: gasEstimate + 1000 }); +``` ###### `UserTweets.js` The `UserTweets` component is used to 1.) show the the profile of the user who's tweets we'd like to view, and 2.) show the tweets of the user. 1. In `_getUserDetails`, we need to get the details of the user @@ -57,15 +64,5 @@ DTwitter.events.NewTweet({ } }) ``` -###### DoTweet.js -The DoTweet components sends a tweet to the contract. We will need to update the `_handleClick` even to estimate gas and call the contract's `tweet` function. -``` -// estimate gas before sending tweet transaction -const gasEstimate = await tweet.estimateGas({ from: web3.eth.defaultAccount, gas: 10000000000 }); - -// send the tweet transaction plus a little extra gas in case the contract state -// has changed since we've done our gas estimate -await tweet.send({ from: web3.eth.defaultAccount, gas: gasEstimate + 1000 }); -``` #### Run through the site and fix any errors Let's try out the functionality on the site and fix any errors we may have introduced. \ No newline at end of file diff --git a/test/dtwitter_spec.js b/test/dtwitter_spec.js index f687584..5718469 100644 --- a/test/dtwitter_spec.js +++ b/test/dtwitter_spec.js @@ -84,6 +84,8 @@ contract("DTwitter contract", function () { it("should be able to add a tweet as 'testhandle' and receive it via contract event", async function () { const usernameHash = web3.utils.keccak256(username); + await tweet(tweetContent).send(); + DTwitter.events.NewTweet({ filter: { _from: usernameHash }, fromBlock: 0 @@ -91,8 +93,6 @@ contract("DTwitter contract", function () { .on('data', (event) => { assert.equal(event.returnValues.tweet, tweetContent); }); - - await tweet(tweetContent).send(); }); });