0

I have a hardware device that detects events I would like to save in the Ethereum blockchain.

So my idea is that the hardware device will call an API function in my web application (C#, node, PHP, etc.), and the web application will call using web3.js a function in my Ethereum smart contract. Everything should be done automatically without human intervention.

My solidity program is this one:

pragma solidity ^0.4.11;

contract Logs {

  struct Log {
    string user;
    uint deviceId;
    uint time;
  }

  Log[] logsList;

  function writeLog(string _user, uint _deviceId) external {
    logsList.push(Log(_user, _deviceId, now));
  }
}

I tested using testrpc and Metamask and works fine.

But I'm wondering:

  1. How to call the writeLog function from the backend of my web application? I'm assuming that the contract has ether enough to pay for the gas.

  2. How can I be sure that the call is done only by my backend? I don't want a malicious user to call directly the function and consume all the ether of the contract

0

You can use web3.py library to invoke contract without user interaction. Using web3.py can estimate gas and get the gas price for a client.

I hope contract owner(you) will pay gas for writeLog() if so you need to change little bit your implementation.

pragma solidity ^0.4.11;

contract Logs {

  struct Log {
    string user;
    uint deviceId;
    uint time;
  }

  Log[] logsList;
  address contractOwner;
  function Logs(){
      contractOwner = msg.sender;
  }
  function writeLog(string _user, uint _deviceId) onlyContractOwner external {
    logsList.push(Log(_user, _deviceId, now));
  }
  modifier onlyContractOwner() {
    require(msg.sender == contractOwner);
    _;
  }
}

Adding onlyContractOwner modifier, to writeLog function, only contract owner will save log's. For more details click below links:

Web3.py Doc

Web3.js Doc

|improve this answer|||||

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.