polkascan / py-substrate-interface

Python Substrate Interface
https://polkascan.github.io/py-substrate-interface/
Apache License 2.0
240 stars 116 forks source link

Cannot deploy ink contract to canvas node. #112

Closed bostontrader closed 3 years ago

bostontrader commented 3 years ago

I'm trying to use this library to deploy an ink contract onto my local canvas v0.1.6 node. Doing this works just fine using the canvas-ui web page.

However, when I use this library I get the error:

substrateinterface.exceptions.SubstrateRequestException: {'code': 1002, 'message': 'Verification Error: Runtime error: Execution failed: ApiError(FailedToConvertParameter { function: "validate_transaction", parameter: "tx", error: Error { cause: None, desc: "Could not decode MultiAddress, variant doesn\\'t exist" } })', 'data': 'RuntimeApi("Execution failed: ApiError(FailedToConvertParameter { function: \"validate_transaction\", parameter: \"tx\", error: Error { cause: None, desc: \"Could not decode MultiAddress, variant doesn\\\\'t exist\" } })")'}

The actual code that I'm using to do this comes from this library's "deploy a contract" example, slightly modified to use the flipper contract from my local machine...

substrate = substrateinterface.SubstrateInterface( url="ws://127.0.0.1:9944", ss58_format=42, type_registry_preset='development' )

keypair = substrateinterface.Keypair.create_from_uri('//Alice')

# Upload WASM code
code = substrateinterface.ContractCode.create_from_contract_files(
    metadata_file=os.path.join('/home/ *** ', 'flipper/target/ink/metadata.json'),
    wasm_file=os.path.join('/home/ *** ', 'flipper/target/ink/flipper.wasm'),
    substrate=substrate
)

# Deploy contract
contract = code.deploy(
    keypair=keypair, endowment=10 ** 15, gas_limit=1000000000000,
    constructor="new",
    #args={'initial_supply': 1000 * 10 ** 15},
    args={'init_value': False}, # because that's what the flipper contract wants, no initial_supply

    upload_code=True
)

My intuition presently suggests that I'm tripping over some versioning incompatibility. I'm also uneasy about the ss58_format and type_registry_preset params. The example says that this works for "Substrate 2.0.0-533bbbd" and I'm pretty certain I'm using substrate 3. If I rebuild using substrate 2 then I'll probably have to downgrade ink and rebuild my contract. Before I do that I hope that I can find some easier advice/insight/repair via this issue.

Thanks for the help.

arjanz commented 3 years ago

You mentioned you used a canvas node (https://github.com/paritytech/canvas-node)? In that case you need to use the 'canvas' type_registry_preset:

substrate = substrateinterface.SubstrateInterface(
    url="ws://127.0.0.1:9944",
    ss58_format=42,
    type_registry_preset='canvas'
)
arjanz commented 3 years ago

I have updated the example located at https://github.com/polkascan/py-substrate-interface/blob/master/examples/create_and_exec_contract.py so it matches current version, that might be helpful too.

bostontrader commented 3 years ago

Thanks a lot @arjanz! That was the magic bullet I've been looking for for a long time! :-)