Created
January 11, 2017 08:06
-
-
Save jorpic/5e9a6b24f8e81d352c1f56a278ab615d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const solc = require('solc'); | |
const Web3 = require('web3'); | |
const web3 = new Web3(); | |
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')); | |
function compile(src) { | |
const ctrPath = path.join(__dirname, src); | |
const code = fs.readFileSync(ctrPath).toString(); | |
const input = {[src]: code}; | |
const compiled = solc.compile({sources: input}, 1); | |
if(compiled.errors) { | |
console.log(compiled.errors.join('\n')); | |
return null; | |
} | |
return compiled.contracts; | |
} | |
function deploy(compiled, cb) { | |
const abi = JSON.parse(compiled.interface); | |
const ctr = web3.eth.contract(abi); | |
ctr.new( | |
{ from: web3.eth.accounts[0], | |
data: compiled.bytecode, | |
gas: '4700000' | |
}, | |
(e, inst) => { | |
e && console.log(e); | |
if(!e && inst.address) { | |
inst = ctr.at(inst.address); | |
inst.allEvents((err, res) => console.log(res.event, res.args)); | |
cb || console.log(inst.address); | |
cb && cb(inst); | |
} | |
} | |
); | |
} | |
web3.eth.defaultAccount = web3.eth.accounts[0]; | |
const ctr = compile('./test.sol'); | |
deploy(ctr.CallMe, callme => | |
deploy(ctr.Caller, caller => { | |
console.log('callme', callme.address); | |
console.log('caller', caller.address); | |
caller.proxyRequest(callme.address, 'hello'); | |
}) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ node test.js | |
callme 0xf29c07da3e712ccda8fac94a9af499774bdbeeaa | |
caller 0x56157bb7ee83e749b5368f4c47ffc406fc653e5a | |
SendingTransaction { _requestor: '0xf29c07da3e712ccda8fac94a9af499774bdbeeaa', | |
action: { [String: '1'] s: 1, e: 0, c: [ 1 ] } } | |
SendingTransaction { _requestor: '0xf29c07da3e712ccda8fac94a9af499774bdbeeaa', | |
action: { [String: '3'] s: 1, e: 0, c: [ 3 ] } } | |
UserRegistered { _from: '0xf29c07da3e712ccda8fac94a9af499774bdbeeaa', | |
_attr: '' } | |
SendingTransaction { _requestor: '0xf29c07da3e712ccda8fac94a9af499774bdbeeaa', | |
action: { [String: '1'] s: 1, e: 0, c: [ 1 ] } } | |
SendingTransaction { _requestor: '0xf29c07da3e712ccda8fac94a9af499774bdbeeaa', | |
action: { [String: '3'] s: 1, e: 0, c: [ 3 ] } } | |
^C |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.7; | |
contract CallMe | |
{ | |
event UserRegistered(address indexed _from, string _attr); | |
address public minter; // keep track of the entity that created the contract | |
modifier onlyOwner { | |
if (msg.sender != minter) | |
throw; | |
_; | |
} | |
function CallMe() { | |
minter = msg.sender; | |
} | |
function() { | |
throw; | |
} | |
function registrationRequest(address _toAdd, string _code) { | |
// this event fires ok if I invoke it via web3 | |
// this event DOES NOT fire ok if function invoked by 'Caller' | |
UserRegistered(_toAdd, _code); | |
} | |
function getMinter() constant returns (address) { | |
return minter; | |
} | |
} | |
contract Caller | |
{ | |
event SendingTransaction(address indexed _requestor, uint8 indexed action); | |
address public minter; | |
bytes public email; // hash of the email address | |
modifier onlyOwner { | |
if (msg.sender != minter) | |
throw; | |
_; | |
} | |
function() { | |
throw; | |
} | |
function Caller(bytes _email) { | |
// constructor | |
minter= msg.sender; | |
email= _email; | |
} | |
function proxyRequest(address _target, string _payload) onlyOwner public { | |
SendingTransaction(_target, 1); | |
// Event fires OK when I subscribe to it via Web3 code. | |
if (!_target.call(bytes4(sha3("registrationRequest(address,string)")),_target, _payload)) | |
{ | |
SendingTransaction(_target, 2); | |
// Event fires OK when I subscribe to it via Web3 code. | |
} | |
else | |
{ | |
SendingTransaction(_target, 3); | |
// Event NEVER fires | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment