Skip to content

Instantly share code, notes, and snippets.

@jw122

jw122/app.js Secret

Last active July 11, 2017 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jw122/bda506dc831e200ac5eb397b36cb2db5 to your computer and use it in GitHub Desktop.
Save jw122/bda506dc831e200ac5eb397b36cb2db5 to your computer and use it in GitHub Desktop.
Escrow payment
// Import the page's CSS. Webpack will know what to do with it.
import "../stylesheets/app.css";
// Import libraries we need.
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'
// Import our contract artifacts and turn them into usable abstractions.
import purchase_artifacts from '../../build/contracts/Purchase.json'
// MetaCoin is our usable abstraction, which we'll use through the code below.
var Purchase = contract(purchase_artifacts);
// The following code is simple to show off interacting with your contracts.
// As your needs grow you will likely need to change its form and structure.
// For application bootstrapping, check out window.addEventListener below.
var accounts;
var account;
window.App = {
start: function() {
var self = this;
var deployedContract;
var valueElement;
var contractValue;
Purchase.setProvider(web3.currentProvider);
// Get the initial account balance so it can be displayed
web3.eth.getAccounts(function(err, accs) {
if (err != null) {
alert ("There was an error fetching your accounts.");
return;
}
if (accs.length == 0) {
alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
return;
}
console.log(accs)
accounts = accs;
account = accounts[0];
self.refreshBalance();
});
},
// Refreshes the contract balance
refreshBalance: function(){
var self = this;
var deployedContract;
var balance;
Purchase.deployed().then(function(instance) {
deployedContract = instance;
balance = document.getElementById("balance");
web3.eth.getBalance(account, function(err, value){
if (!err){
console.log(value.toNumber());
balance.innerHTML = value.toNumber()
}
})
}).catch(function(e) {
console.log(e);
// self.setStatus("Error getting balance; see log.");
});
},
// This is the function being called to execute a transaction
makePayment: function(amount) {
var self = this;
var deployedContract;
var arbiter;
Purchase.deployed().then(function(instance){
deployedContract = instance;
deployedContract.getArbiter().then(function(result){
console.log(result)
arbiter = result
});
web3.eth.sendTransaction({from: account, to: arbiter, value: web3.toWei(parseInt(amount),"ether")}, function(err, addr){
if(!err){
console.log(addr);
}
})
}).then(function(){
self.refreshBalance();
}).catch(function(e){
console.log(e)
});
},
};
window.addEventListener('load', function() {
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 Ether, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
console.warn("No web3 detected. Falling back to http://localhost:8545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
App.start();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment