embark-area-51/lib/test.js

94 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-06-13 13:02:19 +00:00
var python = require('python').shell;
var web3 = require('web3');
var fs = require('fs');
var mm = require('methodmissing');
var sync = require('sync-me');
var grunt = require('grunt');
2015-06-13 13:02:19 +00:00
py_exec = function(cmd) {
2015-06-15 09:46:18 +00:00
return sync(python, cmd)[1].trim();
2015-06-13 13:02:19 +00:00
}
py_exec("from ethertdd import EvmContract")
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101'));
web3.eth.defaultAccount = web3.eth.accounts[0];
TestContractWrapper = (function() {
2015-06-15 01:38:00 +00:00
function TestContractWrapper(contract, className, args) {
2015-06-13 13:02:19 +00:00
this.contract = contract;
this.className = className;
2015-06-15 01:38:00 +00:00
this.args = args;
2015-06-13 13:02:19 +00:00
this.initializeContract();
}
TestContractWrapper.prototype.initializeContract = function() {
example_abi = JSON.stringify(contract.info.abiDefinition)
example_binary = contract.code.slice(2)
py_exec("example_abi = '" + example_abi + "'")
py_exec("example_abi")
py_exec("example_binary = '" + example_binary + "'.decode('hex')")
py_exec("example_binary")
2015-06-15 01:38:00 +00:00
if (this.args == undefined) {
py_exec(this.className + "_contract = EvmContract(example_abi, example_binary, '" + this.className + "')")
}
else {
py_exec(this.className + "_contract = EvmContract(example_abi, example_binary, '" + this.className + "', [" + this.args.join(",") + "])")
}
2015-06-13 13:02:19 +00:00
2015-06-13 13:21:29 +00:00
this.contractVariable = this.className + "_contract"
2015-06-13 13:02:19 +00:00
};
TestContractWrapper.prototype.execCmd = function(method, args) {
arg_list = [];
for (key in args) {
value = args[key];
arg_list.push(value);
}
data = py_exec(this.className + "_contract." + method + "(" + arg_list.join(",") + ")");
return data;
};
return TestContractWrapper;
})();
2015-06-15 01:38:00 +00:00
TestContract = function(contract, className, args) {
var wrapper = new TestContractWrapper(contract, className, args);
2015-06-13 13:02:19 +00:00
var Obj = mm(wrapper, function (key, args) {
return wrapper.execCmd(key, args);
});
return Obj;
}
//TODO: get the files from the config
contractFiles = grunt.file.expand("./app/contracts/**/*.sol")
contractDB = {}
var i;
for (i = 0, len = contractFiles.length; i < len; i++) {
var contractFile = contractFiles[i];
var source = fs.readFileSync(contractFile).toString()
2015-06-13 13:02:19 +00:00
compiled_contracts = web3.eth.compile.solidity(source)
for (className in compiled_contracts) {
var contract = compiled_contracts[className];
contractDB[className] = contract;
}
}
2015-06-15 01:38:00 +00:00
request = function(className, args) {
var contract = contractDB[className];
2015-06-15 01:38:00 +00:00
return TestContract(contract, className, args)
2015-06-13 13:02:19 +00:00
}
2015-06-13 13:21:29 +00:00
Test = {
request: request
}
module.exports = Test;
2015-06-13 13:02:19 +00:00