Deploy contract made with OpenZeppelin from Web3.py

Question:

I’ve followed a tutorial centered in the creation of an ICO and a Crowdsale (based on the ICO that I’ve created).

It’s a very classic tutorial, I know, but now I’m integrating the 2 contracts made with the help of OpenZeppelin and Truffle framework in a Django platform with the Web3.py library, and I’m encountering a problem.

I’ve compiled the contract sources with solc, and I’ve obtained the abi and bin files.

I’ve opened the files like this in python

with open("reservations/contracts/compiled/GustavoCoin.abi") as contract_abi_file_coin:
    contract_abi_coin = json.load(contract_abi_file_coin)

with open("reservations/contracts/compiled/GustavoCoin.bin") as contract_bin_file_coin:
    contract_bytecode_coin = '0x' + contract_bin_file_coin.read()

with open("reservations/contracts/compiled/GustavoCoin.abi") as contract_abi_file:
    contract_abi = json.load(contract_abi_file)

with open("reservations/contracts/compiled/GustavoCoinCrowdsale.bin") as contract_bin_file:
    contract_bytecode = '0x' + contract_bin_file.read()

I’ve also initializated the Coin contract in Ganache blockchain emulator.

But now I don’t know how deploy the Crowdsale contract in the blockchain.

Here is the successful code for deploying the coin:

contract_coin = w3.eth.contract(abi=contract_abi_coin, bytecode=contract_bytecode_coin)

tx_param = {
    'from': w3.eth.accounts[1],
    'gasPrice': 2000,
}
tx_hash = contract_coin.deploy(transaction=tx_param)

Here is the failing code for deploying the crowdsale contract:

construct_crowdsale = contract.constructor(11, 11, 1, w3.eth.accounts[1], tx_receipt.contractAddress)
crowdsale_txn_hash = construct_crowdsale.transact()

This deploy generates a ganache error:

error vm exception while processing transaction revert

Any ideas how to correctly deploy in web3.py?


As a reference point, here is the successful code to deploy the crowdsale code with Truffle framework:

return deployer
    .then(() => {
        return deployer.deploy(GustavoCoin);
    })
    .then(() => {
        return deployer.deploy(
            GustavoCoinCrowdsale,
            openingTime,
            closingTime,
            rate,
            wallet,
            GustavoCoin.address
        );
    });
};
Asked By: Flamel

||

Answers:

When sending the deployment, remember to set important transaction fields. For example, you might set account that the transaction should be signed with. That means replacing the current line:

crowdsale_txn_hash = construct_crowdsale.transact()

with the new lines:

tx_param = {
    'from': w3.eth.accounts[1],
    'gasPrice': 2000,
}
crowdsale_txn_hash = construct_crowdsale.transact(tx_param)

Note that this is analogous to the way that the sender was set in the coin contract (using the older deploy() API):

tx_param = {
    'from': w3.eth.accounts[1],
    'gasPrice': 2000,
}
tx_hash = contract_coin.deploy(transaction=tx_param)
Answered By: carver