fix: set helper methods on contracts

Set these up so we can call `deploy`, `at`, and `new` on contract
classes.
This commit is contained in:
Andre Medeiros 2019-06-18 14:01:10 -04:00 committed by Iuri Matias
parent d6d6a16169
commit 70313352a5
5 changed files with 60 additions and 43 deletions

View File

@ -4,19 +4,8 @@ const {Utils} = require('Embark/EmbarkJS');
contract("SimpleStorage Deploy", function () {
let simpleStorageInstance;
before(function() {
return new Promise(async (resolve, reject) => {
const gas = await SimpleStorage.deploy({arguments: [150]}).estimateGas();
Utils.secureSend(web3, SimpleStorage.deploy({arguments: [150]}), {gas, from: web3.eth.defaultAccount}, true, function(err, receipt) {
if(err) {
return reject(err);
}
simpleStorageInstance = SimpleStorage;
simpleStorageInstance.options.address = receipt.contractAddress;
resolve();
});
});
before(async () => {
simpleStorageInstance = await SimpleStorage.deploy([150]);
});
it("should set constructor value", async function () {

View File

@ -1,6 +1,7 @@
/* global ethereum */
import {reduce} from './async';
import { reduce } from './async';
import utils from './utils';
let Blockchain = {
Contract: Contract,
@ -244,7 +245,7 @@ function Contract(options) {
this.abi = options.abi || options.abiDefinition;
this.address = options.address || options.deployedAddress;
this.gas = options.gas;
this.code = '0x' + options.code;
this.code = utils.hexPrefix(options.code);
this.blockchainConnector = Blockchain.blockchainConnector;
@ -311,40 +312,50 @@ function Contract(options) {
}
});
// Assign helpers too
for(const method of ["deploy", "new", "at", "send", "deployed"]) {
// Make sure we don't override original methods here.
if (originalMethods.includes(method)) {
console.log(method + " is a reserved word and will not be aliased as a helper");
continue;
}
ContractClass[method] = Contract.prototype[method].bind(this);
}
this.contractClass = ContractClass;
return ContractClass;
}
Contract.prototype.deploy = function(args, _options) {
var self = this;
var contractParams;
var options = _options || {};
Contract.prototype.deploy = function(args, _options, _txOptions) {
const self = this;
const options = Object.assign({
arguments: args || [],
data: this.code
}, _options);
contractParams = args || [];
contractParams.push({
const txOptions = Object.assign({
from: this.blockchainConnector.getDefaultAccount(),
data: this.code,
gas: options.gas || 800000
gas: this.gas
}, _txOptions);
const contract = this.blockchainConnector.newContract({abi: this.abi});
this._deployPromise = new Promise((resolve, reject) => {
contract.deploy.apply(contract, [options]).send(txOptions).then(instance => {
resolve(new Contract({
abi: self.abi,
code: self.code,
address: instance.options.address
}));
// Delete the deploy promise as we don't need to track it anymore.
delete self._deployPromise;
}).catch(reject);
});
const contractObject = this.blockchainConnector.newContract({abi: this.abi});
return new Promise(function (resolve, reject) {
contractParams.push(function(err, transaction) {
if (err) {
reject(err);
} else if (transaction.address !== undefined) {
resolve(new Contract({
abi: self.abi,
code: self.code,
address: transaction.address
}));
}
});
contractObject["new"].apply(contractObject, contractParams);
});
return this._deployPromise;
};
Contract.prototype.new = Contract.prototype.deploy;
@ -369,6 +380,15 @@ Contract.prototype.send = function(value, unit, _options) {
return this.blockchainConnector.send(options);
};
Contract.prototype.deployed = function() {
// This API exists just for compatibility reasons, so it works with tools that
// don't have their Smart Contracts deployed initially a the time of importing
// them, like Embark does.
return Promise.resolve(this);
};
Blockchain.Contract = Contract;
class BlockchainConnectionError extends Error {
constructor(connectionErrors) {
super("Could not establish a connection to a node.");

View File

@ -3,6 +3,12 @@
const Web3 = global.Web3 || require('web3');
let Utils = {
hexPrefix: function(str) {
if (!(str && str.match)) return;
if (str.match(/^0x/)) return str;
return `0x${str}`;
},
fromAscii: function(str) {
var _web3 = new Web3();
return _web3.utils ? _web3.utils.fromAscii(str) : _web3.fromAscii(str);

View File

@ -48,11 +48,12 @@ __embarkWeb3.newContract = function (options) {
};
__embarkWeb3.send = function () {
console.log('ARGUEMTNS', ...arguments);
return this.web3.eth.sendTransaction(...arguments);
};
__embarkWeb3.toWei = function () {
return this.web3.toWei(...arguments);
return this.web3.utils.toWei(...arguments);
};
__embarkWeb3.getNetworkId = function () {

View File

@ -131,6 +131,7 @@ class MochaTestRunner {
compiledContracts[contract.className] = {};
}
instance.options.from = accounts[0];
instance.options.code = contract.code;
instance.options.gas = GAS_LIMIT;
Object.setPrototypeOf(compiledContracts[contract.className], instance);
}