Step 3: Implement Dapp functionality
This commit is contained in:
parent
099d0a4ce7
commit
89751aed8f
|
@ -41,16 +41,19 @@ class CreateUser extends Component {
|
|||
try {
|
||||
|
||||
// set up our contract method with the input values from the form
|
||||
const createAccount = DTwitter.methods.createAccount(username, description);
|
||||
|
||||
// get a gas estimate before sending the transaction
|
||||
const gasEstimate = await createAccount.estimateGas({ from: web3.eth.defaultAccount, gas: 10000000000 });
|
||||
|
||||
// send the transaction to create an account with our gas estimate
|
||||
// (plus a little bit more in case the contract state has changed).
|
||||
const result = await createAccount.send({ from: web3.eth.defaultAccount, gas: gasEstimate + 1000 });
|
||||
|
||||
// check result status. if status is false or '0x0', show user the tx details to debug error
|
||||
// if (result.status && !Boolean(result.status.toString().replace('0x', ''))) { // possible result values: '0x0', '0x1', or false, true
|
||||
// return this.setState({ isLoading: false, error: 'Error executing transaction, transaction details: ' + JSON.stringify(result) });
|
||||
// }
|
||||
if (result.status && !Boolean(result.status.toString().replace('0x', ''))) { // possible result values: '0x0', '0x1', or false, true
|
||||
return this.setState({ isLoading: false, error: 'Error executing transaction, transaction details: ' + JSON.stringify(result) });
|
||||
}
|
||||
|
||||
// Completed of async action, set loading state back
|
||||
this.setState({ isLoading: false });
|
||||
|
@ -95,26 +98,27 @@ class CreateUser extends Component {
|
|||
if (!this.state.isLoading) {
|
||||
|
||||
// call the userExists method in our contract asynchronously
|
||||
// .then((exists) => {
|
||||
DTwitter.methods.userExists(web3.utils.keccak256(value)).call()
|
||||
.then((exists) => {
|
||||
|
||||
// // stop loading state
|
||||
// state.isLoading = false;
|
||||
// stop loading state
|
||||
state.isLoading = false;
|
||||
|
||||
// // show error to user if user doesn't exist
|
||||
// state.error = exists ? 'Username not available' : '';
|
||||
// show error to user if user doesn't exist
|
||||
state.error = exists ? 'Username not available' : '';
|
||||
|
||||
// this.setState(state);
|
||||
this.setState(state);
|
||||
|
||||
// }).catch((err) => {
|
||||
}).catch((err) => {
|
||||
|
||||
// // stop loading state
|
||||
// state.isLoading = false;
|
||||
// stop loading state
|
||||
state.isLoading = false;
|
||||
|
||||
// // show error message to user
|
||||
// state.error = err.message;
|
||||
// show error message to user
|
||||
state.error = err.message;
|
||||
|
||||
// this.setState(state);
|
||||
// });
|
||||
this.setState(state);
|
||||
});
|
||||
|
||||
// set loading state while checking the contract
|
||||
state.isLoading = true;
|
||||
|
|
|
@ -50,9 +50,11 @@ class DoTweet extends Component{
|
|||
|
||||
try{
|
||||
// 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 });
|
||||
|
||||
// remove loading state
|
||||
this.setState({ isLoading: false });
|
||||
|
|
|
@ -43,7 +43,7 @@ class UpdateUser extends Component {
|
|||
if (this.state.picture !== '') {
|
||||
try {
|
||||
// upload the file to ipfs and get the resulting hash
|
||||
hash = '';
|
||||
hash = await EmbarkJS.Storage.uploadFile([this.inputPicture]);
|
||||
}
|
||||
catch (err) {
|
||||
// stop loading state and show user the error
|
||||
|
@ -54,17 +54,20 @@ class UpdateUser extends Component {
|
|||
const { account, user } = this.props;
|
||||
const { description } = this.state;
|
||||
// get a handle for the editAccount method
|
||||
const editAccount = DTwitter.methods.editAccount(web3.utils.keccak256(user.username), description, hash);
|
||||
|
||||
// get a gas estimate for the transaction with the input username
|
||||
// and description
|
||||
const gasEstimate = await editAccount.estimateGas({ from: web3.eth.defaultAccount, gas: 10000000000 });
|
||||
|
||||
try {
|
||||
// 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 });
|
||||
|
||||
// if (result.status && !Boolean(result.status.toString().replace('0x', ''))) {
|
||||
// return this.setState({ isLoading: false, formState: 'error', formUpdated: false, error: 'Error executing transaction, transaction details: ' + JSON.stringify(result) });
|
||||
// }
|
||||
if (result.status && !Boolean(result.status.toString().replace('0x', ''))) {
|
||||
return this.setState({ isLoading: false, formState: 'error', formUpdated: false, error: 'Error executing transaction, transaction details: ' + JSON.stringify(result) });
|
||||
}
|
||||
|
||||
// stop loading state, and render the form as successful
|
||||
this.setState({ isLoading: false, formState: 'success', formUpdated: false });
|
||||
|
|
|
@ -37,9 +37,10 @@ class UserTweets extends Component {
|
|||
*/
|
||||
_getUserDetails = async(username) => {
|
||||
// get user details and update state
|
||||
let user = { creationDate: '' } // remove me
|
||||
let user = await DTwitter.methods.users(web3.utils.keccak256(username)).call();
|
||||
|
||||
// update picture url for ipfs
|
||||
user.picture = user.picture.length > 0 ? EmbarkJS.Storage.getUrl(user.picture) : imgAvatar;
|
||||
|
||||
// format the user.creationDate for display
|
||||
user.creationDate = this._formatDate(user.creationDate);
|
||||
|
@ -56,7 +57,14 @@ class UserTweets extends Component {
|
|||
* @returns {null}
|
||||
*/
|
||||
_subscribeToNewTweetEvent(username){
|
||||
this.event = new EventEmitter() // replace me with the NewTweet subscription
|
||||
this.event = DTwitter.events.NewTweet({
|
||||
filter: {_from: web3.utils.keccak256(username)},
|
||||
fromBlock: 1
|
||||
}, (err, event) => {
|
||||
if (err){
|
||||
this.props.onError(err, 'UserTweets._subscribeToNewTweetEvent');
|
||||
}
|
||||
})
|
||||
.on('data', (event) => {
|
||||
let tweets = this.state.tweets;
|
||||
|
||||
|
|
Loading…
Reference in New Issue