diff --git a/boilerplate/package.json b/boilerplate/package.json index 280933681..5adc32081 100644 --- a/boilerplate/package.json +++ b/boilerplate/package.json @@ -20,6 +20,7 @@ "grunt": "^0.4.5", "grunt-cli": "^0.1.13", "matchdep": "^0.3.0", - "python": "^0.0.4" + "python": "^0.0.4", + "methodmissing": "^0.0.3" } } diff --git a/boilerplate/spec/test.js b/boilerplate/spec/test.js index 9193dd8dd..df424f55f 100644 --- a/boilerplate/spec/test.js +++ b/boilerplate/spec/test.js @@ -1,26 +1,72 @@ var python = require('python').shell; var web3 = require('web3'); +var fs = require('fs'); +var mm = require('methodmissing'); + +py_exec = function(cmd) { + python(cmd, function() {}); +} + +py_exec("from ethertdd import EvmContract") web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101')); web3.eth.defaultAccount = web3.eth.accounts[0]; -fs = require('fs'); -source = fs.readFileSync('./app/contracts/simple_storage.sol').toString() +TestContractWrapper = (function() { + function TestContractWrapper(contract, className) { + this.contract = contract; + this.className = className; + 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") + py_exec(className + "_contract = EvmContract(example_abi, example_binary)") + + this.contractVariable = className + "_contract" + }; + + TestContractWrapper.prototype.execCmd = function(method, args) { + arg_list = []; + for (key in args) { + value = args[key]; + arg_list.push(value); + } + + console.log(this.className + "_contract." + method + "(" + arg_list.join(",") + ")") + python(this.className + "_contract." + method + "(" + arg_list.join(",") + ")", function(err, data) { + console.log("res: " + data); + }) + }; + + return TestContractWrapper; + +})(); + +TestContract = function(contract, className) { + var wrapper = new TestContractWrapper(contract, className); + var Obj = mm(wrapper, function (key, args) { + wrapper.execCmd(key, args) + }); + return Obj; +} + +filename = './app/contracts/simple_storage.sol' +source = fs.readFileSync(filename).toString() +className = 'SimpleStorage' + compiled_contracts = web3.eth.compile.solidity(source) +contract = compiled_contracts[className] +SimpleStorage = TestContract(contract, className) -contract = compiled_contracts.SimpleStorage +SimpleStorage.set(100); -example_abi = JSON.stringify(contract.info.abiDefinition) -example_binary = contract.code.slice(2) - -python("from ethertdd import EvmContract", function() {}) -python("example_abi = '" + example_abi + "'", function() {}) -python("example_abi", function() { }) -python("example_binary = '" + example_binary + "'.decode('hex')", function() {}) -python("example_binary", function() { }) -python("contract = EvmContract(example_abi, example_binary)", function() {}) -python("contract.set(10)", function() {}) - -console.log("get") -python("contract.get()", function(err, data) { console.log("=>" + data)}) +a = SimpleStorage.get() +console.log(a)