0

I'm trying to access my contract using web3js and have given my html file (with contract details) here. I get 2 errors and tried methods like unlock account, set default account etc., and nothing seem to work. The CURL operation returns 405 error. Any suggestions please.


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>HC</title>

    <link rel="stylesheet" type="text/css" href="./js/main.css">

    <script src="./js/web3.min.js"></script>

</head>
<body>
    <div class="container">

        <h1>HC Demo</h1>

        <h2 id="instructor"></h2>

        <label for="name" class="col-lg-2 control-label">Provider Name</label>
        <input id="name" type="text">

        <label for="name" class="col-lg-2 control-label">Account Type</label>
        <input id="type" type="text">

        <button id="button">Add provider</button>


    </div>

    <script src="./js/jquery-3.2.1.slim.min.js"></script>

    <script>

    if (typeof web3 !== 'undefined') {
        web3 = new Web3(web3.currentProvider);
    } else {
        // set the provider you want from Web3.providers
        web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/"));
    }

    web3.eth.defaultAccount = "0x44261BBDAB466E1950Ef173b595322B49F4DA5C5";
    console.log("Default account = " + web3.eth.defaultAccount );
    //web3.eth.defaultAccount = "0x44261BBDAB466E1950Ef173b595322B49F4DA5C5";
    //personal.unlockAccount(web3.eth.defaultAccount);
    var hcContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"subscribers","outputs":[{"name":"id","type":"uint256"},{"name":"name","type":"string"},{"name":"accountType","type":"string"},{"name":"accBalance","type":"uint16"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"name","type":"string"},{"name":"accountType","type":"string"}],"name":"addProvider","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"name","type":"string"},{"indexed":false,"name":"accountType","type":"string"},{"indexed":false,"name":"accBalance","type":"uint16"}],"name":"eveSubscriber","type":"event"},{"constant":false,"inputs":[{"name":"id","type":"uint256"}],"name":"getAccountBalance","outputs":[{"name":"_accBal","type":"uint16"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"uint256"}],"name":"getProviderName","outputs":[{"name":"_name","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"uint256"},{"name":"_claimAmount","type":"uint16"}],"name":"submitClaim","outputs":[{"name":"status","type":"bool"},{"name":"_remainingBalance","type":"uint16"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]);
    //hcContract.web3.eth.defaultAccount="0x44261BBDAB466E1950Ef173b595322B49F4DA5C5";

    var Contract = hcContract.at('0x221e64f96560f97d8a06510da8a020484d5669eb');
    //hcContract.web3.eth.defaultAccount = "0x44261BBDAB466E1950Ef173b595322B49F4DA5C5";
    console.log(Contract);

    var event = Contract.eveSubscriber({},'latest');

    $("#button").click(function() {
        console.log($("#name").val(), $("#type").val());
        Contract.addProvider($("#name").val(), $("#type").val());
    });
    </script>
</body>
</html>


<!--


https://ropsten.etherscan.io/tx/0x5d16f717691d796a1ec901014981e2594ec8356a06cce25dca061abe65ae432a


pragma experimental ABIEncoderV2;
contract HealthCareDemo {

    //ERC20 standard 
    string public constant name = "HealthCare_v0.1";
    string public constant symbol = "HC";
    uint8 public constant decimals = 18;  // 18 is the most common number of decimal places
    address owner;

    function kill() { // returns (bool){
    //    if (msg.sender == owner)
            selfdestruct(owner);
    }
    struct SubscriberStruct {
        uint id;
        string name;
        string accountType;
        uint16 accBalance;

    }
    SubscriberStruct[] public subscribers;
//  mapping (address => SubscriberStruct[]) public subscribers;

    event eveSubscriber (
        uint id,
        string name,
        string accountType,
        uint16 accBalance
        );

    //Provider gets added and based on account type, balance is set
    function addProvider(string name, string accountType) returns (bool) {
        uint16 _accBal = 0;
        if(compareStrings(accountType,"HRA")) {
            _accBal = 500;
        }
        else if (compareStrings(accountType,"FSA")) {
            _accBal = 1000;
        }
        else {
            _accBal = 100;
        }

        SubscriberStruct memory s;
        s.name = name;
        s.accountType = accountType;
        s.id = subscribers.length;
        s.accBalance = _accBal;
        subscribers.push(s);

        //eveSubscriber(subscribers[id].id,subscribers[id].name,subscribers[id].accountType,subscribers[id].accBalance);
        eveSubscriber(s.id,s.name,s.accountType,s.accBalance);

        return true;
    }
    //https://github.com/ethereum/solidity/issues/2948 -- returning struct has issue
    function getProviderName(uint id) public returns (string _name) {
        return subscribers[id].name;
    }
    function getAccountBalance(uint id) public returns (uint16 _accBal) {
        return subscribers[id].accBalance;
    }

    function submitClaim(uint _id, uint16 _claimAmount) public returns (bool status, uint16 _remainingBalance) {
        if(subscribers[_id].accBalance >= _claimAmount) {
            subscribers[_id].accBalance -= _claimAmount;
            return (true, subscribers[_id].accBalance);
        }
        else {
            return (false, subscribers[_id].accBalance);
        }
    }

    function compareStrings (string a, string b) view private returns (bool){
       return keccak256(a) == keccak256(b);
   }

}


-->

Errors in Brave browser -

Error #1: while loading the page

Status Code:405 Method Not Allowed

Request Payload: {"jsonrpc":"2.0","id":1,"method":"eth_newFilter","params":[{"topics":["0x6988b3af4053aeb8a7e14aad74b7539182c310204e73eabe35b5aa92938f6db5"],"address":"0x221e64f96560f97d8a06510da8a020484d5669eb"}]}

Error #2: while clicking on 'Add Provider'

Status Code:405 Method Not Allowed

Request Payload: {"jsonrpc":"2.0","id":2,"method":"eth_sendTransaction","params":[{"to":"0x221e64f96560f97d8a06510da8a020484d5669eb","data":"0x705258340000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000024d4b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034653410000000000000000000000000000000000000000000000000000000000","from":"0x44261BBDAB466E1950Ef173b595322B49F4DA5C5"}]}

2 Answers 2

1

I assume the branch being followed is the one that uses Infura as the provider.

Infura can't unlock accounts or sign transactions because it doesn't know your private keys. The error message is to indicate that the methods you're calling are not supported.

7
  • So, which branch can I use other than infura that enables me to unlock my account? Mar 12, 2018 at 14:59
  • The other branch of the if statement tries to use an injected web3 provider. MetaMask is the most commonly used web3 provider, and it will let the user of your app use any account they've set up with MetaMask.
    – user19510
    Mar 12, 2018 at 15:01
  • Sorry about the lame question here. My html code calls web3 = new Web3(new Web3.providers.HttpProvider("ropsten.infura.io/")); which basically means that I get connected to ropsten network. In fact, I'm able to 'read' data from contract but when I try to write, I have issue. Mar 12, 2018 at 15:06
  • Is there something about my answer you didn't understand?
    – user19510
    Mar 12, 2018 at 15:07
  • What you said about metamask and injected web3 are fine. In fact, my code connects ropsten infura. Now the question is, if infura can't unlock accounts/ sign transactions, is there an alternate (public test) environment that does it? Like, will kovan/ rinkeby be able to unlock accounts ? Mar 12, 2018 at 15:16
0

Thanks to @smarx - now I get it.

So, the answer to whatever I wanted to do is,

  • Create an account with infura - where I will get a unique access token
  • Setup a local node for that connects to ropsten network
  • Write html page that has your unique access token url in line - Web3.providers.HttpProvider

Now, this will let me access my contract that's deployed in infura.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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