Quick Question About Storage Example/Javascript API

SilentCiceroSilentCicero Toronto, CAMember Posts: 159 ✭✭✭
edited January 2015 in Solidity
In this example:
contract SimpleStorage {
uint storedData;
function set(uint x) {
storedData = x;
}
function get() constant returns (uint retVal) {
return storedData;
}
}

1. Is the storage variable storedData being stored permanently, or temporarily?

2. If I send this contract data with the JS API (such as below):

web3.eth.transact({ 'to': configAddr, data: [web3.fromAscii('set'), web3.fromAscii(10)]});

How do I access the data stored in storedData?


I've tried stateAt and storageAt to no end (such as below). Cant seem to get this going.

web3.eth.stateAt(configAddr, "0").then(function (result) {
var storage = result;
});

How would I go about using the get function in this contract? Would I use the call function in the JS API?


I'm using POC-8 unstable on Ubuntu x64.

SC.

Comments

  • chrisethchriseth Member Posts: 170 ✭✭✭
    storedData is stored permanently. The general idea is not to access it via "stateAt" (although this is possible), but rather call a Solidity function without creating a transaction from it. An example can be found here:

    https://github.com/ethereum/ethereum.js/blob/master/example/contract.html

    You also first have to tell the javascript frontend the interface of the Solidity contract you want to call (this is done by web3.contract in the example). After that, you can call solidity functions directly from javascript. If you want to make a real transaction and not just a "local call", use transact() instead of call().
  • SilentCiceroSilentCicero Toronto, CAMember Posts: 159 ✭✭✭
    edited January 2015
    Thanks @chriseth for the response.

    I posted this to the BC (with enough gas):

    contract SimpleStorage {
    uint storedData;
    function set(uint x) {
    storedData = x;
    }
    function get() constant returns (uint retVal) {
    return storedData;
    }
    }

    Retrieved the contract address. Then posted to it with this:
    web3.eth.transact({ 'to': CONTRACT_ADDRESS, data: [web3.fromAscii('set'), web3.fromAscii(10)]});

    And attempted the stateAt and storageAt functions they returned nothing except maybe a '0x' or '{}'.

    I tried the call function as well, no data was in the response.

    Any thoughts? I'll update my build tonight and try again, maybe a bug.
  • SilentCiceroSilentCicero Toronto, CAMember Posts: 159 ✭✭✭
    Solved my own problem. I should be using this:

    web3.contract(CONTRACT_ADDRESS, abiNameReg).set("My Name").transact();

    Not:

    web3.eth.transact({ 'to': CONTRACT_ADDRESS, data: [web3.fromAscii('set'), web3.fromAscii("My name")]});


    Cheers
  • HellRazorHellRazor BerlinMember Posts: 99 ✭✭
    @chriseth hi, can you explain how I make a call to a contract and send either along with it? I have a function in my contract like this:

    function depositMoney(){
    account[msg.sender].balance += msg.value;
    }

    And I want to call it like this:

    function deposit(){
    var amount = document.getElementById('depositAmount').value;
    contract.call(amount).deposit();
    }

    How can I do that? Anyone???
  • mids106mids106 Member Posts: 188 ✭✭✭
    You have to use transact (sendTransaction with the new API):

    contract.sendTransaction({value: value}).depositMoney();
  • HellRazorHellRazor BerlinMember Posts: 99 ✭✭
    @mids106 I checked that out, didn't work. I tried: contract.sendTransaction({"value": 100}).depositMoney();
    But had no luck :neutral:
    I'm pretty sure the contract object is alright, because functions that do not require ether like contract.call().blabla() work.
    Can you give me a source where you got your information from so I can check on that? Btw I'm using Alethzero not the Go client. ...Man that stuff is killing me... :s
  • SilentCiceroSilentCicero Toronto, CAMember Posts: 159 ✭✭✭
    @HellRazor I would highly recommend using the Go Ethereum CLI i.e. terminal/or git bash "go get github.com/ethereum/go-ethereum/cmd/ethereum" and then "ethereum -ws -rpc -mine". Then connect to the CLI from any local webpage by setting your EthJS httpsync address to "localhost:8545" or "localhost:40404". This is, at least for me, the most efficient way to develop in JS and Ethereum presently. Note, the DEV team is still building all these clients so its gonna be a shaky start for DApp development on Ethereum. Brace yourself for sudden api changes, clients not building, connection and potential p2p issues. Use local chains and keep a close eye on the Github build flags and issue streams. Just keep hacking away, let us know your status on the forum and will see if we can get you setup. Cheers & good luck.
Sign In or Register to comment.