Last active
January 27, 2016 18:11
-
-
Save palutz/6da3cadc93dfc22ef651 to your computer and use it in GitHub Desktop.
Eris web app for contract with bytes type data
This file contains 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
// requires | |
var fs = require ('fs'); | |
var prompt = require('prompt'); | |
var erisC = require('eris-contracts'); | |
var conv = require('binstring'); | |
// NOTE. On Windows/OSX do not use localhost. find the | |
// url of your chain with: | |
// docer-machine ls | |
// and find the docker machine name you are using (usually default or eris). | |
var erisdbURL = "http://localhost:1337/rpc"; | |
// get the abi and deployed data squared away | |
var contractData = require('./epm.json'); | |
var idisContractAddress = contractData["deployStorageK"]; | |
var idisAbi = JSON.parse(fs.readFileSync("./abi/" + idisContractAddress)); | |
// properly instantiate the contract objects manager using the erisdb URL | |
// and the account data (which is a temporary hack) | |
var accountData = require('./account.json'); | |
var contractsManager = erisC.newContractManagerDev(erisdbURL, accountData); | |
// properly instantiate the contract objects using the abi and address | |
var idisContract = contractsManager.newContractFactory(idisAbi).at(idisContractAddress); | |
// display the current value of idi's contract by calling | |
// the `get` function of idi's contract | |
function getValue(callback) { | |
idisContract.get(function(error, result){ | |
if (error) { throw error } | |
var hex = result.toString(); | |
var str = ''; | |
for (var i = 0; i < hex.length; i += 2) | |
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); | |
console.log("Previous string: " + str); | |
callback(); | |
}); | |
} | |
// prompt the user to change the value of idi's contract | |
function changeValue() { | |
prompt.message = "Insert the new string: "; | |
prompt.start(); | |
prompt.get(['value'], function (error, result) { | |
if (error) { throw error } | |
setValue(result.value) | |
}); | |
} | |
// using eris-contracts call the `set` function of idi's | |
// contract using the value which was recieved from the | |
// changeValue prompt | |
function setValue(value) { | |
var newval = conv(value, {out:'hex'}); | |
idisContract.set(newval, function(error, result){ | |
if (error) { throw error } | |
getValue(function(){}); | |
}); | |
} | |
// run | |
getValue(changeValue); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment