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