How do I compile a two-class solidity in the same file in a py-solc-x using Smartcontract?

Asked 2 years ago, Updated 2 years ago, 94 views

Until now, I used Remix or Truffle to deploy SmartContract.
I decided to use py-solc-x because I wished I could deploy it on Python.
I couldn't solve the problem, so I would like to ask for your help this time.

Excerpts from python

compile_source(source, output_values=["abi", "bin"], solc_version=_solc_version)

abbreviation

    tx = contract.constructor("Hi!").build_transaction(
        dict(
            chainId = web3_connect.connect_provider ["chain_id" ],
            nonce = w3.eth.getTransactionCount (FROM_ADDRESS),
            type = 2,
            gas = 1000000,
            maxFeePerGas=web3_connect.gwei_to_wei(math.ceil(gas_station["maxFee")),
            maxPriorityFeePerGas=web3_connect.gwei_to_wei(math.ceil(gas_station["maxPriorityFee")),
        )
    )

solidity

pragma solidity^0.8.0;

contract Test {
    string public msg;

    constructor(string memory message) {
        msg = message;
    }

    function greet()public view returns(string memory){
        return msg;
    }
}

In the above case, I was able to deploy successfully.

I added a new Version class to solidity and made it inherit.

pragma solidity^0.8.0;

contract Version {
    US>string private_version;

    constructor(string memory version_){
        _version=version_;
    }

    function version() external view returns(string memory) {
        return_version;
    }
}

contract Test is Version {
    string public msg;

    constructor(string memory message, string memory version) Version(version) {
        msg = message;
    }

    function greet()public view returns(string memory){
        return msg;
    }
}

Due to the increasing number of constructor arguments, I changed python as follows.

tx=contract.constructor("Hi!", "1.0.0").build_transaction(
        dict(
            chainId = web3_connect.connect_provider ["chain_id" ],
            nonce = w3.eth.getTransactionCount (FROM_ADDRESS),
            type = 2,
            gas = 1000000,
            maxFeePerGas=web3_connect.gwei_to_wei(math.ceil(gas_station["maxFee")),
            maxPriorityFeePerGas=web3_connect.gwei_to_wei(math.ceil(gas_station["maxPriorityFee")),
        )
    )

This python caused an error.

"Incorrect argument count.Expected '1'.Got'2'"

When I got abi, I understood that the number of arguments did not match because it was only for the Version class.

With remix, you can specify which class to compile, but can you do the same with py-solc-x?
Or does anyone know if the version class is not good?

ethereum solidity

2022-09-30 13:48

1 Answers

Self-resolved.

There was a problem or lack of consideration with the code not listed.

compiled_sol=compile_source_file(SOLC_VERSION, main_contract+".sol")
    contract_id, contract_interface =compiled_sol.popitem()
    abi = contract_interface ["abi" ]

That's how I got abi.
The compile_source_file function is unique and runs py-solc-x.compile_source() internally.

If you have more than one class, len(completed_sol) indicates 2 in this case.
As a result, popitem took the first one and returned the version class abi and leaked the acquisition of the base class.

main_contract="Base"
    while True:
        contract_id, contract_interface =compiled_sol.popitem()
        if main_contract contract_id:
            break
        iflen(compiled_sol)<=0:
            result = {
                "error": "Compile Failed",
            }
            print.pprint(result)
            return result

I tried to loop and check as above.


2022-09-30 13:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.