Skip to content

Instantly share code, notes, and snippets.

@tymat
Last active August 29, 2015 14:27
Show Gist options
  • Save tymat/c526360c218b35f7841e to your computer and use it in GitHub Desktop.
Save tymat/c526360c218b35f7841e to your computer and use it in GitHub Desktop.
contract Master {
event CallSucceeded(address callee, bytes32 name, bool success);
int public retVal;
event IntRegistered(address from, int value);
function register(int value) public {
IntRegistered(msg.sender, value);
retVal = value;
}
event Delegated(address to, int a, int b, int result);
function delegated(address to, int a, int b) public {
bytes32 argsHash = sha3(a, b);
bool s = to.call(bytes4(sha3("add(address,int256,int256)")), address(this), a, b);
CallSucceeded(to, "add", s);
Delegated(to, a, b, retVal);
}
}
contract Sub {
event Added(int a, int b, int sum);
event IntReturned(address to, int value);
event CallSucceeded(address callee, bytes32 name, bool success);
function add(address from, int a, int b) public {
int sum = a + b;
IntReturned(msg.sender, sum);
bool s = from.call(bytes4(sha3("register(int256)")), sum);
CallSucceeded(from, "register", s);
Added(a, b, sum);
}
}
var web3 = require('web3')
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8080"));
var sub_source = fs.readFileSync("/tmp/Sub.sol").toString();
var master_source = fs.readFileSync("/tmp/Master.sol").toString();
var sub_compiled = web3.eth.compile.solidity(sub_source);
var master_compiled = web3.eth.compile.solidity(master_source);
var Master = web3.eth.contract(master_compiled.Master.info.abiDefinition);
var Sub = web3.eth.contract(sub_compiled.Sub.info.abiDefinition);
var sub_upload_tx = web3.eth.sendTransaction({from: web3.eth.coinbase, gas: 600000, data: sub_compiled.Sub.code});
var master_upload_tx = web3.eth.sendTransaction({from: web3.eth.coinbase, gas: 600000, data: master_compiled.Master.code});
// Wait until transactions are completed:
web3.eth.getTransaction(sub_upload_tx);
web3.eth.getTransaction(master_upload_tx);
// When transactions have been processed by miners you can continue:
var master_address = web3.eth.getCode(web3.eth.getTransactionReceipt(master_upload_tx).contractAddress);
var sub_address = web3.eth.getCode(web3.eth.getTransactionReceipt(sub_upload_tx).contractAddress);
// Check you got the code deployed:
web3.eth.getCode(web3.eth.getTransactionReceipt(sub_upload_tx).contractAddress);
web3.eth.getCode(web3.eth.getTransactionReceipt(master_upload_tx).contractAddress);
// When code is deployed you can setup your instances:
var master_address = web3.eth.getTransactionReceipt(master_upload_tx).contractAddress;
var sub_address = web3.eth.getTransactionReceipt(sub_upload_tx).contractAddress;
var master_instance = Master.at(master_address);
var sub_instance = Sub.at(sub_address);
// Send a test transaction:
master_instance.delegated.sendTransaction(sub_instance.address, 9, 6, {from: web3.eth.coinbase, gas: 500000});
// It should set the storage value to 9 + 6 or 0x0F
/*
PC 00000109: STOP GAS: 461232 COST: 0
STACK = 1
0000: 000000000000000000000000000000000000000000000000000000006215656a
MEM = 224
0000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0016: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0032: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0048: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0064: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 60 ...............`
0096: 00 00 00 00 00 00 00 00 00 00 00 00 59 96 65 c0 ............Y?eÀ
0112: 94 8c 14 5a 33 3d 74 88 2a a1 f1 a9 54 5f 9a 0f ???Z3=t?*¡ñ©T_??
0128: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0144: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 09 ...............?
STORAGE = 1
0000000000000000000000000000000000000000000000000000000000000000: 000000000000000000000000000000000000000000000000000000000000000f
*/
var sub_instance_added_event = sub_instance.Added(null, {fromBlock: 0, toBlock: 'latest'});
var sub_instance_added_event_result = sub_instance_added_event.get();
sub_instance_added_event[0].args.sum;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment