Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Web3 = require("web3");
- //var VM = require("./sol/VendingMachine.sol.js");
- var contract = require("truffle-contract");
- var vendingmachine_artefacts = require("./contracts/VendingMachine.json");
- var VendingMachine = contract(vendingmachine_artefacts);
- var options = require("./options.json");
- var express = require("express");
- var bodyParser = require('body-parser');
- var app = express();
- var fs = require('fs');
- var cors = require('cors');
- web3 = new Web3(
- new Web3.providers.HttpProvider("http://localhost:" + options.rpcport)
- );
- VendingMachine.setProvider(web3.currentProvider);
- var account = web3.eth.accounts[0];
- var from = {from:account}
- app.use(bodyParser.json());
- app.use(cors());
- app.post('/create', function(req, res) {
- console.log(req.body);
- console.log("Creating user " + req.body.name);
- var name = web3.fromAscii(req.body.name, 32);
- var id = req.body.person.replace(/-/g,'');
- var vm;
- VendingMachine.deployed().then(function (instance) {
- vm = instance;
- console.log("Creating id: " + id);
- return vm.createAccount(name, id, from);
- }).then(function (result) {
- console.log("Setting balance to 50 for " + id);
- return vm.setBalance(id, 50, from);
- }).then(function (result) {
- console.log("Account created");
- res.status(200).json({"status": "created"}).end();
- return;
- }).catch(function(error) {
- console.log(error);
- res.status(500).send("Something bad happened");
- });
- });
- var server = app.listen(options.serve, function(){
- var host = server.address().address;
- var port = server.address().port;
- console.log("Host live at " + host + ':' + port);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement