Advertisement
Guest User

CreateAccount

a guest
Aug 16th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Web3 = require("web3");
  2. //var VM = require("./sol/VendingMachine.sol.js");
  3. var contract = require("truffle-contract");
  4. var vendingmachine_artefacts = require("./contracts/VendingMachine.json");
  5. var VendingMachine = contract(vendingmachine_artefacts);
  6. var options = require("./options.json");
  7. var express = require("express");
  8. var bodyParser = require('body-parser');
  9. var app = express();
  10. var fs = require('fs');
  11. var cors = require('cors');
  12.  
  13. web3 = new Web3(
  14.         new Web3.providers.HttpProvider("http://localhost:" + options.rpcport)
  15.         );
  16. VendingMachine.setProvider(web3.currentProvider);
  17.  
  18. var account = web3.eth.accounts[0];
  19. var from = {from:account}
  20.  
  21. app.use(bodyParser.json());
  22. app.use(cors());
  23.  
  24. app.post('/create', function(req, res) {
  25.     console.log(req.body);
  26.     console.log("Creating user " + req.body.name);
  27.     var name = web3.fromAscii(req.body.name, 32);
  28.     var id = req.body.person.replace(/-/g,'');
  29.     var vm;
  30.     VendingMachine.deployed().then(function (instance) {
  31.         vm = instance;
  32.         console.log("Creating id: " + id);
  33.         return vm.createAccount(name, id, from);
  34.     }).then(function (result) {
  35.         console.log("Setting balance to 50 for " + id);
  36.         return vm.setBalance(id, 50, from);
  37.     }).then(function (result) {
  38.         console.log("Account created");
  39.         res.status(200).json({"status": "created"}).end();
  40.         return;
  41.     }).catch(function(error) {
  42.         console.log(error);
  43.         res.status(500).send("Something bad happened");
  44.     });
  45. });
  46.  
  47. var server = app.listen(options.serve, function(){
  48.     var host = server.address().address;
  49.     var port = server.address().port;
  50.     console.log("Host live at " + host + ':' + port);
  51.  
  52. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement