Skip to content

Commit 7031335

Browse files
Andre Medeirosiurimatias
authored andcommitted
fix: set helper methods on contracts
Set these up so we can call `deploy`, `at`, and `new` on contract classes.
1 parent d6d6a16 commit 7031335

File tree

5 files changed

+60
-43
lines changed

5 files changed

+60
-43
lines changed

dapps/tests/app/test/simple_storage_deploy_spec.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,8 @@ const {Utils} = require('Embark/EmbarkJS');
44

55
contract("SimpleStorage Deploy", function () {
66
let simpleStorageInstance;
7-
before(function() {
8-
return new Promise(async (resolve, reject) => {
9-
const gas = await SimpleStorage.deploy({arguments: [150]}).estimateGas();
10-
11-
Utils.secureSend(web3, SimpleStorage.deploy({arguments: [150]}), {gas, from: web3.eth.defaultAccount}, true, function(err, receipt) {
12-
if(err) {
13-
return reject(err);
14-
}
15-
simpleStorageInstance = SimpleStorage;
16-
simpleStorageInstance.options.address = receipt.contractAddress;
17-
resolve();
18-
});
19-
});
7+
before(async () => {
8+
simpleStorageInstance = await SimpleStorage.deploy([150]);
209
});
2110

2211
it("should set constructor value", async function () {

packages/embarkjs/embarkjs/src/lib/blockchain.js

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* global ethereum */
22

3-
import {reduce} from './async';
3+
import { reduce } from './async';
4+
import utils from './utils';
45

56
let Blockchain = {
67
Contract: Contract,
@@ -244,7 +245,7 @@ function Contract(options) {
244245
this.abi = options.abi || options.abiDefinition;
245246
this.address = options.address || options.deployedAddress;
246247
this.gas = options.gas;
247-
this.code = '0x' + options.code;
248+
this.code = utils.hexPrefix(options.code);
248249

249250
this.blockchainConnector = Blockchain.blockchainConnector;
250251

@@ -311,40 +312,50 @@ function Contract(options) {
311312
}
312313
});
313314

315+
// Assign helpers too
316+
for(const method of ["deploy", "new", "at", "send", "deployed"]) {
317+
// Make sure we don't override original methods here.
318+
if (originalMethods.includes(method)) {
319+
console.log(method + " is a reserved word and will not be aliased as a helper");
320+
continue;
321+
}
322+
323+
ContractClass[method] = Contract.prototype[method].bind(this);
324+
}
325+
326+
this.contractClass = ContractClass;
327+
314328
return ContractClass;
315329
}
316330

317-
Contract.prototype.deploy = function(args, _options) {
318-
var self = this;
319-
var contractParams;
320-
var options = _options || {};
321-
322-
contractParams = args || [];
331+
Contract.prototype.deploy = function(args, _options, _txOptions) {
332+
const self = this;
333+
const options = Object.assign({
334+
arguments: args || [],
335+
data: this.code
336+
}, _options);
323337

324-
contractParams.push({
338+
const txOptions = Object.assign({
325339
from: this.blockchainConnector.getDefaultAccount(),
326-
data: this.code,
327-
gas: options.gas || 800000
340+
gas: this.gas
341+
}, _txOptions);
342+
343+
const contract = this.blockchainConnector.newContract({abi: this.abi});
344+
345+
this._deployPromise = new Promise((resolve, reject) => {
346+
contract.deploy.apply(contract, [options]).send(txOptions).then(instance => {
347+
resolve(new Contract({
348+
abi: self.abi,
349+
code: self.code,
350+
address: instance.options.address
351+
}));
352+
353+
// Delete the deploy promise as we don't need to track it anymore.
354+
delete self._deployPromise;
355+
}).catch(reject);
328356
});
329357

330-
331-
const contractObject = this.blockchainConnector.newContract({abi: this.abi});
332-
333-
return new Promise(function (resolve, reject) {
334-
contractParams.push(function(err, transaction) {
335-
if (err) {
336-
reject(err);
337-
} else if (transaction.address !== undefined) {
338-
resolve(new Contract({
339-
abi: self.abi,
340-
code: self.code,
341-
address: transaction.address
342-
}));
343-
}
344-
});
345-
346-
contractObject["new"].apply(contractObject, contractParams);
347-
});
358+
return this._deployPromise;
348359
};
349360

350361
Contract.prototype.new = Contract.prototype.deploy;
@@ -369,6 +380,15 @@ Contract.prototype.send = function(value, unit, _options) {
369380
return this.blockchainConnector.send(options);
370381
};
371382

383+
Contract.prototype.deployed = function() {
384+
// This API exists just for compatibility reasons, so it works with tools that
385+
// don't have their Smart Contracts deployed initially a the time of importing
386+
// them, like Embark does.
387+
return Promise.resolve(this);
388+
};
389+
390+
Blockchain.Contract = Contract;
391+
372392
class BlockchainConnectionError extends Error {
373393
constructor(connectionErrors) {
374394
super("Could not establish a connection to a node.");

packages/embarkjs/embarkjs/src/lib/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
const Web3 = global.Web3 || require('web3');
44

55
let Utils = {
6+
hexPrefix: function(str) {
7+
if (!(str && str.match)) return;
8+
if (str.match(/^0x/)) return str;
9+
10+
return `0x${str}`;
11+
},
612
fromAscii: function(str) {
713
var _web3 = new Web3();
814
return _web3.utils ? _web3.utils.fromAscii(str) : _web3.fromAscii(str);

packages/embarkjs/web3/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ __embarkWeb3.newContract = function (options) {
4848
};
4949

5050
__embarkWeb3.send = function () {
51+
console.log('ARGUEMTNS', ...arguments);
5152
return this.web3.eth.sendTransaction(...arguments);
5253
};
5354

5455
__embarkWeb3.toWei = function () {
55-
return this.web3.toWei(...arguments);
56+
return this.web3.utils.toWei(...arguments);
5657
};
5758

5859
__embarkWeb3.getNetworkId = function () {

packages/plugins/mocha-tests/src/lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class MochaTestRunner {
131131
compiledContracts[contract.className] = {};
132132
}
133133
instance.options.from = accounts[0];
134+
instance.options.code = contract.code;
134135
instance.options.gas = GAS_LIMIT;
135136
Object.setPrototypeOf(compiledContracts[contract.className], instance);
136137
}

0 commit comments

Comments
 (0)