我发现一个错误:
ValueError: {'code': -32601, 'message': 'The method eth_sendTransaction does not exist/is not available'}
当我在 infura.io(ropsten 测试网络)中部署一个可靠的智能合约时。但是当我尝试 ganache 时,合约在 ganache testRPC 上成功部署。这是我的 Python 文件下面的代码。
from web3 import Web3
from solcx import compile_files, link_code
import json
w3 = Web3(Web3.HTTPProvider("https://ropsten.infura.io/v3/<PROJECT_ID>"))
def separate_main_n_link(file_path, contracts):
# separate out main file and link files
# assuming first file is main file.
main = {}
link = {}
all_keys = list(contracts.keys())
for key in all_keys:
if file_path[0] in key:
main = contracts[key]
else:
link[key] = contracts[key]
return main, link
def deploy_contract(contract_interface):
# Instantiate and deploy contract
contract = w3.eth.contract(
abi=contract_interface['abi'], bytecode=contract_interface['bin'])
acct = w3.eth.account.privateKeyToAccount("<PRIVATE_KEY>")
# Get transaction hash from deployed contract
tx_hash = contract.deploy(transaction={'from': acct.address})
# Get tx receipt to get contract address
tx_receipt = w3.eth.getTransactionReceipt(tx_hash)
return tx_receipt['contractAddress']
def deploy_n_transact(file_path, mappings=[]):
# compile all files
contracts = compile_files(file_path, import_remappings=mappings)
link_add = {}
contract_interface, links = separate_main_n_link(file_path, contracts)
# print (contract_interface)
# first deploy all link libraries
# here link is refers to the second contarct "stringUtils.sol"
for link in links:
link_add[link] = deploy_contract(links[link])
# now link dependent library code to main contract binary
# https://solidity.readthedocs.io/en/v0.4.24/using-the-compiler.html?highlight=library
if link_add:
contract_interface['bin'] = link_code(contract_interface['bin'], link_add)
# return contract receipt and abi(application binary interface)
return deploy_contract(contract_interface), contract_interface['abi']
contract_address, abi = deploy_n_transact(['./contracts/Adoption.sol'])
with open('Adoption.json', 'w') as outfile:
data = {
"abi": abi,
"contract_address": contract_address
}
json.dump(data, outfile, indent=4, sort_keys=True)
而solidity文件的版本是0.5.0。
pragma solidity ^0.5.0;
contract Adoption {
address[16] public adopters;
// Retrieving the adopters
function getAdopters() public view returns (address[16] memory) {
return adopters;
}
// Adopting a pet
function adopt(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = msg.sender;
return petId;
}
}
但我认为它不会影响在区块链测试网络上部署 Solidity 智能合约的过程。提前致谢。