mirror of https://github.com/embarklabs/embark.git
create contract wrapper
This commit is contained in:
parent
0af68cbc9c
commit
27c2ab4104
|
@ -20,6 +20,7 @@
|
||||||
"grunt": "^0.4.5",
|
"grunt": "^0.4.5",
|
||||||
"grunt-cli": "^0.1.13",
|
"grunt-cli": "^0.1.13",
|
||||||
"matchdep": "^0.3.0",
|
"matchdep": "^0.3.0",
|
||||||
"python": "^0.0.4"
|
"python": "^0.0.4",
|
||||||
|
"methodmissing": "^0.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,72 @@
|
||||||
var python = require('python').shell;
|
var python = require('python').shell;
|
||||||
var web3 = require('web3');
|
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.setProvider(new web3.providers.HttpProvider('http://localhost:8101'));
|
||||||
web3.eth.defaultAccount = web3.eth.accounts[0];
|
web3.eth.defaultAccount = web3.eth.accounts[0];
|
||||||
|
|
||||||
fs = require('fs');
|
TestContractWrapper = (function() {
|
||||||
source = fs.readFileSync('./app/contracts/simple_storage.sol').toString()
|
function TestContractWrapper(contract, className) {
|
||||||
compiled_contracts = web3.eth.compile.solidity(source)
|
this.contract = contract;
|
||||||
|
this.className = className;
|
||||||
contract = compiled_contracts.SimpleStorage
|
this.initializeContract();
|
||||||
|
}
|
||||||
|
|
||||||
|
TestContractWrapper.prototype.initializeContract = function() {
|
||||||
example_abi = JSON.stringify(contract.info.abiDefinition)
|
example_abi = JSON.stringify(contract.info.abiDefinition)
|
||||||
example_binary = contract.code.slice(2)
|
example_binary = contract.code.slice(2)
|
||||||
|
|
||||||
python("from ethertdd import EvmContract", function() {})
|
py_exec("example_abi = '" + example_abi + "'")
|
||||||
python("example_abi = '" + example_abi + "'", function() {})
|
py_exec("example_abi")
|
||||||
python("example_abi", function() { })
|
py_exec("example_binary = '" + example_binary + "'.decode('hex')")
|
||||||
python("example_binary = '" + example_binary + "'.decode('hex')", function() {})
|
py_exec("example_binary")
|
||||||
python("example_binary", function() { })
|
py_exec(className + "_contract = EvmContract(example_abi, example_binary)")
|
||||||
python("contract = EvmContract(example_abi, example_binary)", function() {})
|
|
||||||
python("contract.set(10)", function() {})
|
|
||||||
|
|
||||||
console.log("get")
|
this.contractVariable = className + "_contract"
|
||||||
python("contract.get()", function(err, data) { console.log("=>" + data)})
|
};
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
SimpleStorage.set(100);
|
||||||
|
|
||||||
|
a = SimpleStorage.get()
|
||||||
|
console.log(a)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue