1

I am trying to access data stored in a contract from web3. I have managed to do so with a custom get function, however from what I have read online, every public variable has a built in get and set function created by Solidity. I also understood that it has somethign to do with callbacks, but I am not sure how to implement it in my js code. Here is what I have done to get my data so far:

Solidity Contract

contract OrganizationObject {

    struct organizationDetails {
        string name; 
        string vat; 
        string headquarterAddress;
    }

    organizationDetails public details;

    address[] public staff;

    function OrganizationObject (string name, string vat, string headquarterAddress){
        details.name = name;
        details.vat = vat;
        details.headquarterAddress = headquarterAddress;
    }

    function getDetails() public constant returns (bytes32[], string, string, string){
        return (order, details.name, details.vat, details.headquarterAddress);
    }

    function addStaff(address x){
        staff.push(x);
    }
}

web3

app.post('/api/getOrganisationDetails', stormpath.authenticationRequired, function(req, res){
    var rawString = req.user.customData;
    var stringJson = JSON.stringify(rawString);
    var customData = JSON.parse(stringJson);
    var web3 = new Web3();

    web3.setProvider(new web3.providers.HttpProvider(customData.ethereum_provider));
    var address = req.body.address;

    // creation of contract object
    var MyContract = web3.eth.contract(abi.abiOrganisationObject).at(address);
    var details = MyContract.getDetails();
    console.log(details);
    res.end(details).status(200);
});

I think the better solution would be to use the getter function from solidity, I have tried like this:

app.post('/api/getOrganisationDetails', stormpath.authenticationRequired, function(req, res){
    var rawString = req.user.customData;
    var stringJson = JSON.stringify(rawString);
    var customData = JSON.parse(stringJson);
    var web3 = new Web3();

    web3.setProvider(new web3.providers.HttpProvider(customData.ethereum_provider));
    var address = req.body.address;

    // creation of contract object
    var MyContract = web3.eth.contract(abi.abiOrganisationObject).at(address);
    var details = MyContract.details();
    console.log(details);
    res.end(details).status(200);
});

But it seems to want an argument in the MyContract.details(), which i assume is a callback. What should I do to be able to access the data stored inside MyContract.details ?

1

You can't directly read a struct from web3, you need to break it down in a constant function that return each attribute like you did in getDetails() .

Moreover I would advice to make asynchronous call to the contract and use call() for a constant function.

  • Asynchronous call is safer in my opinion
  • call() will explicitly send a call (executed in the VM) rather than a sendTransaction (that must be mined)

So in your case, that should look like this:

var MyContract = web3.eth.contract(abi.abiOrganisationObject).at(address);
MyContract.getDetails.call().then(function(error, result){
    if (!error) {
        console.log(result); // details
        var order = web3.toAscii(result[0]);
        var name               = result[1];
        var vat                = result[2];
        var headquarterAddress = result[3];

    } else {
        console.log(error); 
    }
}));

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.