2

It is the first time I do this so I have no idea what I am doing ! I am trying to call a contract's function and post data to it, this contract will then create another contract. Here is my contract how my contract looks like:

contract MedicalStaffObject {
    using strings for *;
    event LogCreatedTrip(address);

    struct medicalStaffDetails {
       string phoneNumber;
       string physicalAddress;
    }

    medicalStaffDetails public details;
    address[] public trips;
    address public orgAddresses; 

    function MedicalStaffObject (address orgAddress, string phoneNumber, string physicalAddress){
        details.phoneNumber = phoneNumber;
        details.physicalAddress = physicalAddress;
        orgAddresses = orgAddress;
    }

    function getDetails() public constant returns (string, string, address){
        return (details.phoneNumber, details.physicalAddress, orgAddresses);
    }

    function getTrips() public constant returns (address[]){
        return (trips);
    }

    function newTrip(address zone1, address zone2, address zone3, address zone4, bytes32 name, bytes32 startingDate, uint duration) public returns(address){
        TripObject newTrip = new TripObject(zone1, zone2, zone3, zone4, name, startingDate, duration);
        trips.push(newTrip);
        LogCreatedTrip(newTrip);
        return newTrip;
    }
}

And I am trying to call the function newTrip using this code in my NodeJS app:

 var web3 = new Web3();
    web3.setProvider(new web3.providers.HttpProvider(provider));
    var zonesTrimed = zones.slice(1, -1);
    var zonesArray = zonesTrimed.split(",");

    var contractAbi = web3.eth.contract(ethContracts.abiMedicalStaffObject);
    var myContract = contractAbi.at(address);
    // suppose you want to call a function named newTrip of myContract
    var getData = myContract.newTrip.getData(zonesArray[0], zonesArray[1], zonesArray[2], zonesArray[3], name, startDate, duration);
    //finally paas this data parameter to send Transaction
    web3.eth.sendTransaction({to:address, from:organisationWallet, data: getData});

However it just doesnt work and I keep getting this error:

Error: unknown account

First of all what does this error mean, does it mean it cann't find the 'to' or the 'from' account ? What I am supposed to put in there ?

  • 1
    If you want to call newTrip function, you can do that like this : myContract.newTrip.sendTransaction(zonesArray[0], zonesArray[1], zonesArray[2], zonesArray[3], name, startDate, duration, {from: web3.eth.coinbase, value : 120000}) Try this and let me know the result. – Aniket Apr 10 '17 at 7:28
  • This works perfect ! however now i get this error: Error: authentication needed: password or unlock I assum this has something to do with the account im using being locked but I am not sure how to deal with it – Rémi Apr 11 '17 at 12:53
  • 1
    Unlock your account using personal.unlockAccount(eth.coinbase, 'your password', duration) on your node – Aniket Apr 13 '17 at 7:58
  • Please could you write it as an answer this is what I was looking for thank you – Rémi Apr 13 '17 at 12:05
2

If you want to call newTrip function, you can do that like this : myContract.newTrip.sendTransaction(zonesArray[0], zonesArray[1], zonesArray[2], zonesArray[3], name, startDate, duration, {from: web3.eth.coinbase, value : 120000})

To unlock your account, use personal.unlockAccount(eth.coinbase, 'your password', duration) on your node

  • Thanks for this, once this is done the transactions is being mined, then i can retreive the block using web3.eth.getBlock(blockHash); but where is the data that the contract has returned in the JSON ? – Rémi Apr 13 '17 at 23:02
  • 1
    As this is not a constant function, you need to broadcast an Event in your function in contract and you can get the data by watching the event. Check this : solidity.readthedocs.io/en/develop/contracts.html#events – Aniket Apr 14 '17 at 6:38

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.