Skip to content

Instantly share code, notes, and snippets.


jw122/app.js Secret

Last active Jul 11, 2017
What would you like to do?
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;
// 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.");
if (accs.length == 0) {
alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
accounts = accs;
account = accounts[0];
// 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){
balance.innerHTML = value.toNumber()
}).catch(function(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;
deployedContract = instance;
arbiter = result
web3.eth.sendTransaction({from: account, to: arbiter, value: web3.toWei(parseInt(amount),"ether")}, function(err, addr){
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. :)")
// 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:");
// 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"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.