Skip to content

Commit

Permalink
Merge pull request #2 from AdChain/lunchbadger-fixes
Browse files Browse the repository at this point in the history
Moved URL from model settings to datasource settings
  • Loading branch information
jamesyoung authored Jan 24, 2017
2 parents cc7f4a0 + 889d116 commit 8ff3bac
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions lib/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,34 @@ var solc = require('solc');
var fs = require('fs');
var Web3DAO = require('./dao');

var web3 = new Web3();
var account;
var contracts = {};

function Web3(settings) {
this.name = 'Web3';
// if (web3.eth.accounts[0]) {
// account = web3.eth.accounts[0];
// debug('setting rpc ' + settings.name + ' @ ' + settings.url);
// web3.setProvider(new web3.providers.HttpProvider(settings.url));
// } else {
// throw "error : can not connect to rpc server - please check datasources.json"
// }
}
function Web3Connector(settings) {
this.name = settings.name;
this.settings = settings;

Web3.prototype.DataAccessObject = Web3DAO;
const url = settings.url || 'http://localhost:8545';
const web3 = new Web3(new Web3.providers.HttpProvider(url));

Web3.prototype.define = function(modelData) {
var model = modelData.model;
web3.setProvider(new web3.providers.HttpProvider(this.settings.url));

if (web3.eth.accounts[0]) {
account = web3.eth.accounts[0];
web3.eth.defaultAccount = web3.eth.accounts[0];
} else {
throw "error : can not connect to rpc server - please check datasources.json"
}

this.web3 = web3;
}

Web3Connector.prototype.DataAccessObject = Web3DAO;

Web3Connector.prototype.define = function(modelData) {
var web3 = this.web3;
var model = modelData.model;

// all models must have an ID property, and we want this model to have a String ID property
model.defineProperty('id', {type: String, id: true});

Expand Down Expand Up @@ -62,7 +64,7 @@ Web3.prototype.define = function(modelData) {
model.construct = function(data, cb) {
var params = data['params'] || null;
var contract = Contract.new(params, {
from: account,
from: web3.eth.defaultAccount,
data: bytecode.toString(),
gas: gas
}, function (e, contractInstance){
Expand All @@ -78,7 +80,7 @@ Web3.prototype.define = function(modelData) {
})
}

defineMethods(model, abi);
this.defineMethods(model, abi);

setRemoting(model.construct, {
description: 'Create a new contract instance',
Expand All @@ -104,7 +106,9 @@ function getBytecode(compiledContract, contractName) {
return compiledContract.contracts[contractName].bytecode
}

function defineMethods(model, abi) {
Web3Connector.prototype.defineMethods = function(model, abi) {
const web3 = this.web3;

abi.map(function(obj){
if (obj.constant === false && obj.type === "function") {
model.prototype[obj.name] = function(data, cb) {
Expand All @@ -113,10 +117,9 @@ function defineMethods(model, abi) {
return data[input.name];
})
var contractInstance = contracts[address];
web3.eth.defaultAccount = web3.eth.accounts[0];
var method = contractInstance[obj.name];
var args = [...params, function(err, result) {
cb && cb(null, {"account":web3.eth.defaultAccount, "txhash":result})
cb && cb(null, {"account": web3.eth.defaultAccount, "txhash":result})
}];
var result = method.apply(contractInstance, args);
}
Expand All @@ -141,4 +144,4 @@ function setRemoting(fn, options) {
fn.shared = true;
}

module.exports = Web3;
module.exports = Web3Connector;

0 comments on commit 8ff3bac

Please sign in to comment.