Skip to content

Commit a9e6306

Browse files
committed
fix connecting to correct provider
1 parent e5aab5e commit a9e6306

File tree

8 files changed

+374
-270
lines changed

8 files changed

+374
-270
lines changed

js/build/embark.bundle.js

Lines changed: 90 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ return /******/ (function(modules) { // webpackBootstrap
8989

9090
var EmbarkJS = {};
9191

92+
EmbarkJS.isNewWeb3 = function() {
93+
var _web3 = new Web3();
94+
if (typeof(_web3.version) === "string") {
95+
return true;
96+
}
97+
return parseInt(_web3.version.api.split('.')[0], 10) >= 1;
98+
};
99+
92100
EmbarkJS.Contract = function(options) {
93101
var self = this;
94102
var i, abiElement;
@@ -98,94 +106,107 @@ EmbarkJS.Contract = function(options) {
98106
this.code = '0x' + options.code;
99107
this.web3 = options.web3 || web3;
100108

101-
var ContractClass = this.web3.eth.contract(this.abi);
109+
if (EmbarkJS.isNewWeb3()) {
110+
// TODO:
111+
// add default **from** address
112+
// add gasPrice
113+
var ContractClass = new this.web3.eth.Contract(this.abi, this.address);
114+
ContractClass.setProvider(this.web3.currentProvider);
115+
116+
return ContractClass;
117+
118+
} else {
119+
102120

103-
this.eventList = [];
121+
var ContractClass = this.web3.eth.contract(this.abi);
104122

105-
if (this.abi) {
123+
this.eventList = [];
124+
125+
if (this.abi) {
106126
for (i = 0; i < this.abi.length; i++) {
107-
abiElement = this.abi[i];
108-
if (abiElement.type === 'event') {
109-
this.eventList.push(abiElement.name);
110-
}
127+
abiElement = this.abi[i];
128+
if (abiElement.type === 'event') {
129+
this.eventList.push(abiElement.name);
130+
}
111131
}
112-
}
132+
}
113133

114-
var messageEvents = function() {
134+
var messageEvents = function() {
115135
this.cb = function() {};
116-
};
136+
};
117137

118-
messageEvents.prototype.then = function(cb) {
138+
messageEvents.prototype.then = function(cb) {
119139
this.cb = cb;
120-
};
140+
};
121141

122-
messageEvents.prototype.error = function(err) {
142+
messageEvents.prototype.error = function(err) {
123143
return err;
124-
};
144+
};
125145

126-
this._originalContractObject = ContractClass.at(this.address);
127-
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function(p) {
146+
this._originalContractObject = ContractClass.at(this.address);
147+
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function(p) {
128148
// TODO: check for forbidden properties
129149
if (self.eventList.indexOf(p) >= 0) {
130150

131-
self[p] = function() {
132-
var promise = new messageEvents();
133-
var args = Array.prototype.slice.call(arguments);
134-
args.push(function(err, result) {
151+
self[p] = function() {
152+
var promise = new messageEvents();
153+
var args = Array.prototype.slice.call(arguments);
154+
args.push(function(err, result) {
155+
if (err) {
156+
promise.error(err);
157+
} else {
158+
promise.cb(result);
159+
}
160+
});
161+
162+
self._originalContractObject[p].apply(self._originalContractObject[p], args);
163+
return promise;
164+
};
165+
return true;
166+
} else if (typeof self._originalContractObject[p] === 'function') {
167+
self[p] = function(_args) {
168+
var args = Array.prototype.slice.call(arguments);
169+
var fn = self._originalContractObject[p];
170+
var props = self.abi.find((x) => x.name == p);
171+
172+
var promise = new Promise(function(resolve, reject) {
173+
args.push(function(err, transaction) {
174+
promise.tx = transaction;
175+
if (err) {
176+
return reject(err);
177+
}
178+
179+
var getConfirmation = function() {
180+
self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) {
135181
if (err) {
136-
promise.error(err);
137-
} else {
138-
promise.cb(result);
182+
return reject(err);
139183
}
140-
});
141184

142-
self._originalContractObject[p].apply(self._originalContractObject[p], args);
143-
return promise;
144-
};
145-
return true;
146-
} else if (typeof self._originalContractObject[p] === 'function') {
147-
self[p] = function(_args) {
148-
var args = Array.prototype.slice.call(arguments);
149-
var fn = self._originalContractObject[p];
150-
var props = self.abi.find((x) => x.name == p);
151-
152-
var promise = new Promise(function(resolve, reject) {
153-
args.push(function(err, transaction) {
154-
promise.tx = transaction;
155-
if (err) {
156-
return reject(err);
157-
}
158-
159-
var getConfirmation = function() {
160-
self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) {
161-
if (err) {
162-
return reject(err);
163-
}
164-
165-
if (receipt !== null) {
166-
return resolve(receipt);
167-
}
168-
169-
setTimeout(getConfirmation, 1000);
170-
});
171-
};
172-
173-
if (typeof(transaction) !== "string" || props.constant) {
174-
resolve(transaction);
175-
} else {
176-
getConfirmation();
177-
}
178-
});
179-
180-
fn.apply(fn, args);
181-
});
182-
183-
return promise;
184-
};
185-
return true;
185+
if (receipt !== null) {
186+
return resolve(receipt);
187+
}
188+
189+
setTimeout(getConfirmation, 1000);
190+
});
191+
};
192+
193+
if (typeof(transaction) !== "string" || props.constant) {
194+
resolve(transaction);
195+
} else {
196+
getConfirmation();
197+
}
198+
});
199+
200+
fn.apply(fn, args);
201+
});
202+
203+
return promise;
204+
};
205+
return true;
186206
}
187207
return false;
188-
});
208+
});
209+
}
189210
};
190211

191212
EmbarkJS.Contract.prototype.deploy = function(args, _options) {

js/embark.js

Lines changed: 90 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
var EmbarkJS = {};
99

10+
EmbarkJS.isNewWeb3 = function() {
11+
var _web3 = new Web3();
12+
if (typeof(_web3.version) === "string") {
13+
return true;
14+
}
15+
return parseInt(_web3.version.api.split('.')[0], 10) >= 1;
16+
};
17+
1018
EmbarkJS.Contract = function(options) {
1119
var self = this;
1220
var i, abiElement;
@@ -16,94 +24,107 @@ EmbarkJS.Contract = function(options) {
1624
this.code = '0x' + options.code;
1725
this.web3 = options.web3 || web3;
1826

19-
var ContractClass = this.web3.eth.contract(this.abi);
27+
if (EmbarkJS.isNewWeb3()) {
28+
// TODO:
29+
// add default **from** address
30+
// add gasPrice
31+
var ContractClass = new this.web3.eth.Contract(this.abi, this.address);
32+
ContractClass.setProvider(this.web3.currentProvider);
33+
34+
return ContractClass;
35+
36+
} else {
37+
2038

21-
this.eventList = [];
39+
var ContractClass = this.web3.eth.contract(this.abi);
2240

23-
if (this.abi) {
41+
this.eventList = [];
42+
43+
if (this.abi) {
2444
for (i = 0; i < this.abi.length; i++) {
25-
abiElement = this.abi[i];
26-
if (abiElement.type === 'event') {
27-
this.eventList.push(abiElement.name);
28-
}
45+
abiElement = this.abi[i];
46+
if (abiElement.type === 'event') {
47+
this.eventList.push(abiElement.name);
48+
}
2949
}
30-
}
50+
}
3151

32-
var messageEvents = function() {
52+
var messageEvents = function() {
3353
this.cb = function() {};
34-
};
54+
};
3555

36-
messageEvents.prototype.then = function(cb) {
56+
messageEvents.prototype.then = function(cb) {
3757
this.cb = cb;
38-
};
58+
};
3959

40-
messageEvents.prototype.error = function(err) {
60+
messageEvents.prototype.error = function(err) {
4161
return err;
42-
};
62+
};
4363

44-
this._originalContractObject = ContractClass.at(this.address);
45-
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function(p) {
64+
this._originalContractObject = ContractClass.at(this.address);
65+
this._methods = Object.getOwnPropertyNames(this._originalContractObject).filter(function(p) {
4666
// TODO: check for forbidden properties
4767
if (self.eventList.indexOf(p) >= 0) {
4868

49-
self[p] = function() {
50-
var promise = new messageEvents();
51-
var args = Array.prototype.slice.call(arguments);
52-
args.push(function(err, result) {
69+
self[p] = function() {
70+
var promise = new messageEvents();
71+
var args = Array.prototype.slice.call(arguments);
72+
args.push(function(err, result) {
73+
if (err) {
74+
promise.error(err);
75+
} else {
76+
promise.cb(result);
77+
}
78+
});
79+
80+
self._originalContractObject[p].apply(self._originalContractObject[p], args);
81+
return promise;
82+
};
83+
return true;
84+
} else if (typeof self._originalContractObject[p] === 'function') {
85+
self[p] = function(_args) {
86+
var args = Array.prototype.slice.call(arguments);
87+
var fn = self._originalContractObject[p];
88+
var props = self.abi.find((x) => x.name == p);
89+
90+
var promise = new Promise(function(resolve, reject) {
91+
args.push(function(err, transaction) {
92+
promise.tx = transaction;
93+
if (err) {
94+
return reject(err);
95+
}
96+
97+
var getConfirmation = function() {
98+
self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) {
5399
if (err) {
54-
promise.error(err);
55-
} else {
56-
promise.cb(result);
100+
return reject(err);
57101
}
58-
});
59102

60-
self._originalContractObject[p].apply(self._originalContractObject[p], args);
61-
return promise;
62-
};
63-
return true;
64-
} else if (typeof self._originalContractObject[p] === 'function') {
65-
self[p] = function(_args) {
66-
var args = Array.prototype.slice.call(arguments);
67-
var fn = self._originalContractObject[p];
68-
var props = self.abi.find((x) => x.name == p);
69-
70-
var promise = new Promise(function(resolve, reject) {
71-
args.push(function(err, transaction) {
72-
promise.tx = transaction;
73-
if (err) {
74-
return reject(err);
75-
}
76-
77-
var getConfirmation = function() {
78-
self.web3.eth.getTransactionReceipt(transaction, function(err, receipt) {
79-
if (err) {
80-
return reject(err);
81-
}
82-
83-
if (receipt !== null) {
84-
return resolve(receipt);
85-
}
86-
87-
setTimeout(getConfirmation, 1000);
88-
});
89-
};
90-
91-
if (typeof(transaction) !== "string" || props.constant) {
92-
resolve(transaction);
93-
} else {
94-
getConfirmation();
95-
}
96-
});
97-
98-
fn.apply(fn, args);
99-
});
100-
101-
return promise;
102-
};
103-
return true;
103+
if (receipt !== null) {
104+
return resolve(receipt);
105+
}
106+
107+
setTimeout(getConfirmation, 1000);
108+
});
109+
};
110+
111+
if (typeof(transaction) !== "string" || props.constant) {
112+
resolve(transaction);
113+
} else {
114+
getConfirmation();
115+
}
116+
});
117+
118+
fn.apply(fn, args);
119+
});
120+
121+
return promise;
122+
};
123+
return true;
104124
}
105125
return false;
106-
});
126+
});
127+
}
107128
};
108129

109130
EmbarkJS.Contract.prototype.deploy = function(args, _options) {

js/web3-1.0.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)