0
votes

Purpose

I want to deploy the contract to ropsten (test network of ethereum) and make a transaction using web3py.

Environment

I used remix for contract deployment, and here is the etherscan link for the deploy transaction.: etherscan.io/tx/0xb0a

and for making transaction, I used this script: gist
(I also attached the solidity contract code in the link.)

Problems

When I try to unlock my account using w3.personal.unlockAccount in web3py

w3.personal.unlockAccount(account_a, input("Password: "))

It just raises the below error. 405error

I also want to make a transaction using the giveToken function in the contract. But the same error was raised when I executed code.

contract_instance.giveToken(account_b, token_amount, transact={'from': account_a}) 

for pre-defined variables (such as contract_instance, account_b), you can see the whole code at the below or the gist link above.

Here is my code

token.sol

pragma solidity ^0.4.0;

contract BasicToken {
    // Owner
    address public owner;

    // For manage account and balance
    mapping(address => uint) userAccount;


    constructor() public {
        owner = msg.sender;
    }

    // Example
    event increaseToken(address sender, address receiver, uint amount);

    function getBalance(address addr) public view returns (uint) {
        return userAccount[addr];
    }

    function myBalance() public view returns (uint) {
        return userAccount[msg.sender];
    }

    function giveToken(address dest, uint amount) public returns (bool) {
        emit increaseToken(msg.sender, dest, amount);

        userAccount[dest] += amount;

        return true;
    }

}

web3py.py

import json
import time

from eth_utils import to_checksum_address
from web3 import Web3, HTTPProvider
from web3.contract import ConciseContract
from web3.middleware import geth_poa_middleware

INFURA_API_KEY = "this is key"

ACCOUNT_PASSWORD = "this is account password"

w3 = Web3(HTTPProvider('https://ropsten.infura.io/{}'.format(INFURA_API_KEY)))

w3.middleware_stack.inject(geth_poa_middleware, layer=0)

from eth_utils import to_checksum_address
contract_address = to_checksum_address('0xFb294910d8193DeB9a294B51F22D8878ad15f2E8')

# Instantiate and deploy contract
contract_instance = w3.eth.contract(abi=contract_abi, address=contract_address, ContractFactoryClass=ConciseContract)

account_a = "0xCa2d22Cb8ff54f2D1DCfDBb75DD6411a5A0ee6f1"
account_b = "0x8F8d1bc97E8939e3932BfBeb923A1B2B972D5A9A"

# Unlock account
w3.personal.unlockAccount(account_a, ACCOUNT_PASSWORD)  # Raise the below error!!!

"""
requests.exceptions.HTTPError: 405 Client Error: Method Not Allowed for url: https://ropsten.infura.io/API_KEY
"""

print("Contract: {}".format(contract_address))

print("Before give token")
print('My Balance: {}'.format(contract_instance.myBalance()))
print('{} Balance: {}'.format(account_a, contract_instance.getBalance(account_a)))
print('{} Balance: {}'.format(account_b, contract_instance.getBalance(account_b)))

token_amount = 1000

# Make transaction
contract_instance.giveToken(account_b, token_amount, transact={'from': account_a})  # Raise the below error!!!

"""
requests.exceptions.HTTPError: 405 Client Error: Method Not Allowed for url: https://ropsten.infura.io/API_KEY
"""

try:
    assert contract_instance.getBalance(account_b) == token_amount
except AssertionError as e:
    print("Test Error: {}".format(e))
    print("Amount: {}\t Expected: {}".format(contract_instance.getBalance(account_b), token_amount))

else:
    print("After give token")
    print('My Balance: {}'.format(contract_instance.myBalance()))
    print('{} Balance: {}'.format(account_a, contract_instance.getBalance(account_a)))
    print('{} Balance: {}'.format(account_b, contract_instance.getBalance(account_b)))

finally:
    print('Done')

FYI

With remix, I could make a transaction. etherscan.io/tx/0x129

1

1 Answers

1
votes

Infura doesn't support unlockAccount, because they don't know your private key. (Infura is a public Ethereum node used by many people.)

They don't support any method that requires them to know your private key.