Skip to content

Instantly share code, notes, and snippets.

@lingqingmeng
Created December 25, 2017 20:55
Show Gist options
  • Save lingqingmeng/abf8e00e215c695a727245de3b86b07a to your computer and use it in GitHub Desktop.
Save lingqingmeng/abf8e00e215c695a727245de3b86b07a to your computer and use it in GitHub Desktop.
Profile Form Tutorial
import AuthenticationContract from '../../../../build/contracts/Authentication.json'
import SimpleStorageContract from '../../../../build/contracts/SimpleStorage.json'
import store from '../../../store'
const contract = require('truffle-contract')
export const USER_UPDATED = 'USER_UPDATED'
function userUpdated(user) {
return {
type: USER_UPDATED,
payload: user
}
}
export function updateUser(name) {
let web3 = store.getState().web3.web3Instance
// Double-check web3's status.
if (typeof web3 !== 'undefined') {
return function(dispatch) {
// Using truffle-contract we create the authentication object.
const authentication = contract(AuthenticationContract)
authentication.setProvider(web3.currentProvider)
// Declaring this for later so we can chain functions on Authentication.
var authenticationInstance
// Get current ethereum wallet.
web3.eth.getCoinbase((error, coinbase) => {
// Log errors, if any.
if (error) {
console.error(error);
}
authentication.deployed().then(function(instance) {
authenticationInstance = instance
// Attempt to login user.
authenticationInstance.update(name, {from: coinbase})
.then(function(result) {
// If no error, update user.
dispatch(userUpdated({"name": name}))
return alert('Name updated!')
})
.catch(function(result) {
// If error...
})
.then(function (renamed) {
// new start
const ss = contract(SimpleStorageContract)
ss.setProvider(web3.currentProvider)
var ssInstance;
// new end
return ss.deployed()
.then(function (instance) {
ssInstance = instance;
ssInstance.set(name.length,{from: coinbase})
.then( (res) => {
ssInstance.get()
.then((value) => {
console.log('value:',value);
console.log('value.toString():',value.toString());
return value;
})
})
.catch(function (err) {
console.log('err:',err);
})
})
})
})
})
}
} else {
console.error('Web3 is not initialized.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment