3

I've got the following contract:

pragma solidity ^0.4.11;

contract Zusatzaufgabe {

    struct Student {
        string name;
        string vorname;
        uint matnr;
        uint punkte;
        bool[] solved;
    }

    address[] public addressen;

    mapping(address => Student) public studenten;

    modifier hurdle(uint _n) {
        require(_n == msg.value/2);
        _;
    }

    modifier onlyTillTest() {
        require(block.timestamp < 1499702400);
        _;
    }

    function Zusatzaufgabe(){

    }

    function register(string _name, string _vorname, uint _matnr) onlyTillTest(){
        studenten[msg.sender] = Student(_name,_vorname,_matnr,1,new bool[](3));
        addressen.push(msg.sender);
    }

    function onepoint() payable hurdle(10000) onlyTillTest(){
        if(studenten[msg.sender].solved[0] == false){
            studenten[msg.sender].solved[0] = true;
            studenten[msg.sender].punkte += 1;
        }
    }

    function twopoints(uint _value) onlyTillTest(){
        if(studenten[msg.sender].solved[1] == false
        && _value == studenten[addressen[0]].matnr
        ){
            studenten[msg.sender].solved[1] = true;
            studenten[msg.sender].punkte += 2;
        }
    }

    function sixpoints(bytes32 _h, uint8 _v, bytes32 _r, bytes32 _s) onlyTillTest(){
        if(
            studenten[msg.sender].solved[2] == false
            && ecrecover(_h, _v, _r, _s) == msg.sender
            && _h == sha3(this)
        ){
            studenten[msg.sender].solved[2] = true;
            studenten[msg.sender].punkte += 6;
        }
    }
}

To sucessfully use the function twopoints, I need to get the value of studenten[addressen[0]].matnr.

How can I access this by console with web3?

MyContract.studenten[addressen[0]].matnr doesn't work, even though it is public.

ReferenceError: addressen is not defined

How do I access this value?

1

This unit test works for me with the above contract using node 8.1.2 and truffle 3.4.3.

contract('Zusatzaufgabe', function(accounts) {
  it("Retrieve", async function() {
    const zusatzaufgabe = await Zusatzaufgabe.deployed();

    // Populate some entries
    await zusatzaufgabe.register("hola", "hola", 123, { from: accounts[1] });
    await zusatzaufgabe.register("toma", "joni", 321, { from: accounts[2] });

    // This returns Zusatzaufgabe.addressen[0]
    const addressen = await zusatzaufgabe.addressen.call(0);
    assert.equal(addressen, accounts[1]);

    // This returns Zusatzaufgabe.studenten[Zusatzaufgabe.addressen[0]]
    const student = await zusatzaufgabe.studenten.call(addressen);
    assert.equal(student[2].toNumber(), 123);
  });
});

The last call will return a tuple with the fields from struct Student. It will return [name, vorname, matnr, punkte]. The field solved is not returned, see https://solidity.readthedocs.io/en/develop/contracts.html#getter-functions for the documentation about getters automatically generated. Also int fieds (matnr and punkte) are returned as bignums.

0

i need to get the value of 'studenten[addressen[0]].matnr'

how can i access this by console with web3?

It could be done with Contract function like this:

function getMatnr(address addr) constant returns(uint) {
    return studenten[addr].matnr;
}

And in web3:

contract.getMatnr.call(addr, function(err, result) {

});

ReferenceError: addressen is not defined

how do i access this value?

address[] public addressen; for me, it is enough for publicity of addressen array

Check addressen array after you call register function. I think, it is still empty.

contract.addressen.call(function(err, res) {

});

Try to send tx with register function without onlyTillTest modifier.

UPDATE

If you had getMatnr, what you need to do in web3 is:

MyContract.addressen.call(function(err, addresses) {
   if (err) return console.log(err);
   var addr = addresses[0];

   MyContract.getMatnr.call(addr, function(err, matnr) {
       if (err) return console.log(err);
       console.log(matnr);
   });
});
  • I cant edit the contract. /add functions to it. as I already registered, it cant really be empty. also, a number of people should have registered already before i did. i just need the correct argument for _value to call the function twopoints, as it is checked at this line: _value == studenten[addressen[0]].matnr – qwpeo Jul 7 '17 at 15:12
  • Does MyContract.addressen.call(function(err, res) { console.log(res)}); return array of addresses from web3? – Viktor Baranov Jul 7 '17 at 15:20
  • no, it returns Error: Invalid number of arguments to Solidity function – qwpeo Jul 7 '17 at 15:25
  • I've updated my answer with web3 code you can use, if you had getMatnr function. But without it, you can't access mapping values in web3 (For example, read here for more info) – Viktor Baranov Jul 7 '17 at 15:38
  • so how am i supposed to call the function twopoints properly? because only when _value == studenten[addressen[0]].matnr is true, i will get the two points. there has to be a solution to this :/ – qwpeo Jul 7 '17 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.