ropsten.infura.io引发405错误

时间:2018-05-09 08:34:42

标签: ethereum solidity smartcontracts

目的

我想将合同部署到ropsten(以太坊测试网络)并使用web3py进行交易。

环境

我使用remix进行合同部署,这里是部署事务的etherscan链接:etherscan.io/tx/0xb0a

为了进行交易,我使用了这个脚本:gist
(我还在链接中附上了可靠性合同代码。)

问题

当我尝试使用web3py中的w3.personal.unlockAccount

来解锁我的帐户时
w3.personal.unlockAccount(account_a, input("Password: "))

它只会引发以下错误。 405error

我还想使用合同中的giveToken函数进行交易。但是当我执行代码时出现了同样的错误。

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

对于预定义的变量(例如contract_instance,account_b),您可以在下面看到整个代码或上面的gist链接。

这是我的代码

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

使用混音,我可以进行交易。 etherscan.io/tx/0x129

1 个答案:

答案 0 :(得分:1)

Infura不支持unlockAccount,因为他们不知道您的私钥。 (Infura是许多人使用的公共以太坊节点。)

他们不支持任何方法,要求他们知道您的私钥。