Skip to content

Instantly share code, notes, and snippets.

@zelig
Created April 29, 2015 17:51
Show Gist options
  • Save zelig/4dbe653092307527d8c8 to your computer and use it in GitHub Desktop.
Save zelig/4dbe653092307527d8c8 to your computer and use it in GitHub Desktop.
rm -rf ~/tmp/thu*; ./geth --networkid 3301 --maxpeers 0 --datadir ~/tmp/thu/ --loglevel 6 --logtostderr --logfile /dev/null --password <(echo -n 'y') account new; ./geth --networkid 3301 --maxpeers 0 --datadir ~/tmp/thu/ --loglevel 6 --logtostderr --logfile /dev/null --vmdebug --password <(echo -n 'y') --unlock primary js demo.js 2>> ~/tmp/thu.log
admin.miner.start();
admin.debug.waitForBlocks(eth.blockNumber+5);
admin.miner.stop() ;
primary = eth.accounts[0];
console.log("primary: "+primary);
balance = web3.fromWei(eth.getBalance(primary), "ether");
console.log("balance: "+balance);
admin.contractInfo.newRegistry(primary);
source = "contract test {\n" +
" /// @notice will multiply `a` by 7.\n" +
" function multiply(uint a) returns(uint d) {\n" +
" return a * 7;\n" +
" }\n" +
"} ";
contract = eth.compile.solidity(source);
contractaddress = eth.sendTransaction({from: primary, data: '0x'+contract.code});
eth.getBlock("pending", true).transactions;
// wait for the next n blocks
// should use filters here and/or
// unfortunately this might crash the miner if blocks are found quickly
admin.miner.start()
// waits until block height is minimum the number given.
// basically a sleep function on variable block units of time.
admin.debug.waitForBlocks(eth.blockNumber+1);
admin.miner.stop()
code = eth.getCode(contractaddress);
abiDef = JSON.parse('[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]');
Multiply7 = eth.contract(abiDef);
multiply7 = new Multiply7(contractaddress);
fortytwo = multiply7.multiply.call(6, {from: primary});
console.log("multiply7.multiply.call(6) => "+fortytwo);
tx = multiply7.multiply.sendTransaction(6, {from: primary});
// wait for the next n blocks
// should use filters here and/or
// unfortunately this might crash the miner if blocks are found quickly
admin.miner.start();
// waits until block height is minimum the number given.
// basically a sleep function on variable block units of time.
admin.debug.waitForBlocks(eth.blockNumber+1);
admin.miner.stop();
filename = "/tmp/info.json";
// extracts the contract info json, serialises, hashes and saves it to the file given
// registers the sender as owner as and associates the contract code's hash
// to its contract info/metadoc json file.
// returns the content hash (using bzzhash in the future, now called on Sha3)
contenthash = admin.contractInfo.register(primary, contractaddress, contract, filename);
// the content hash can be registered for url hint to do oldwwweb server-storage
// or any decentralised protocol.
admin.contractInfo.registerUrl(primary, contenthash, "file://"+filename);
// wait for the next n blocks
// should use filters here and/or
// unfortunately this might crash the miner if blocks are found quickly
admin.miner.start();
// waits until block height is minimum the number given.
// basically a sleep function on variable block units of time.
admin.debug.waitForBlocks(eth.blockNumber+1);
admin.miner.stop();
// get the info bundle of the contract =
// lookup code -> Sha3(code) -> ShaBzz(content) -> URL-hint -> fetch url,
// authenticate with hash -> deserialised
info = admin.contractInfo.get(contractaddress);
console.log("Info: \n"+JSON.stringify(info));
// or see natspec in action:
admin.contractInfo.start();
abiDef = JSON.parse('[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]');
Multiply7 = eth.contract(abiDef);
multiply7 = new Multiply7(contractaddress);
var fortytwo = multiply7.multiply.sendTransaction(6, { from: primary });
console.log("multiply7.multiply.call(6) => "+fortytwo);
@frozeman
Copy link

frozeman commented May 5, 2015

viktor could you add syntax highlighting by making it a js file? will be way easier to read ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment