Skip to content

Commit

Permalink
Added support for ink metadata V3 (#188)
Browse files Browse the repository at this point in the history
* Added support for ink metadata V3 #168

- Added metadata migration paths
- Updated event processing
- Updated gas prediction
- Added storage_deposit_limit kwarg
- Updated examples
- Updated unit tests

* Updated README.md and contract example
  • Loading branch information
arjanz authored Feb 10, 2022
1 parent 11ce0be commit 5f34d54
Show file tree
Hide file tree
Showing 10 changed files with 986 additions and 139 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,8 @@ print(receipt.error_message['name']) # 'LiquidityRestrictions'

```python

receipt = ExtrinsicReceipt(
substrate=substrate,
extrinsic_hash="0x56fea3010910bd8c0c97253ffe308dc13d1613b7e952e7e2028257d2b83c027a",
block_hash="0x04fb003f8bc999eeb284aa8e74f2c6f63cf5bd5c00d0d0da4cd4d253a643e4c9"
receipt = ExtrinsicReceipt.create_from_extrinsic_identifier(
substrate=substrate, extrinsic_identifier="5233297-1"
)

print(receipt.is_success) # False
Expand Down Expand Up @@ -526,15 +524,18 @@ print('Current value of "get":', result.contract_result_data)
gas_predit_result = contract.read(keypair, 'flip')

print('Result of dry-run: ', gas_predit_result.contract_result_data)
print('Gas estimate: ', gas_predit_result.gas_consumed)
print('Gas estimate: ', gas_predit_result.gas_required)

# Do the actual transfer
# Do the actual call
print('Executing contract call...')
contract_receipt = contract.exec(keypair, 'flip', args={

}, gas_limit=gas_predit_result.gas_consumed)
}, gas_limit=gas_predit_result.gas_required)

print(f'Events triggered in contract: {contract_receipt.contract_events}')
if contract_receipt.is_success:
print(f'Events triggered in contract: {contract_receipt.contract_events}')
else:
print(f'Call failed: {contract_receipt.error_message}')
```

See complete [code example](https://github.com/polkascan/py-substrate-interface/blob/master/examples/create_and_exec_contract.py) for more details
Expand Down Expand Up @@ -689,7 +690,7 @@ with SubstrateInterface(url="wss://rpc.polkadot.io") as substrate:

## Keeping type registry presets up to date

_Note: Only applicable for chains with metadata < V14_
> :information_source: Only applicable for chains with metadata < V14
When on-chain runtime upgrades occur, types used in call- or storage functions can be added or modified. Therefore it is
important to keep the type registry presets up to date, otherwise this can lead to decoding errors like
Expand Down
198 changes: 107 additions & 91 deletions examples/assets/flipper.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"metadataVersion": "0.1.0",
"source": {
"hash": "0x52f960bf3032155ed75db1490c7ac4f6c660105e7847558df5d4edb615e3eb1e",
"language": "ink! 3.0.0-rc3",
"compiler": "rustc 1.53.0-nightly"
"hash": "0xfbc9c238f6b171238c82566eda2473a9873c154b9d4e80b3ba66865bc4d9da31",
"language": "ink! 3.0.0-rc8",
"compiler": "rustc 1.59.0-nightly"
},
"contract": {
"name": "flipper",
Expand All @@ -12,99 +11,116 @@
"[your_name] <[your_email]>"
]
},
"spec": {
"constructors": [
{
"args": [
{
"name": "init_value",
"type": {
"displayName": [
"bool"
],
"type": 1
"V3": {
"spec": {
"constructors": [
{
"args": [
{
"label": "init_value",
"type": {
"displayName": [
"bool"
],
"type": 0
}
}
}
],
"docs": [
"Constructor that initializes the `bool` value to the given `init_value`."
],
"name": [
"new"
],
"selector": "0x9bae9d5e"
},
{
"args": [],
"docs": [
"Constructor that initializes the `bool` value to `false`.",
"",
"Constructors can delegate to other constructors."
],
"name": [
"default"
],
"selector": "0xed4b9d1b"
}
],
"docs": [],
"events": [],
"messages": [
{
"args": [],
"docs": [
" A message that can be called on instantiated contracts.",
" This one flips the value of the stored `bool` from `true`",
" to `false` and vice versa."
],
"mutates": true,
"name": [
"flip"
],
"payable": false,
"returnType": null,
"selector": "0x633aa551"
},
{
"args": [],
"docs": [
" Simply returns the current value of our `bool`."
],
"mutates": false,
"name": [
"get"
],
"payable": false,
"returnType": {
"displayName": [
"bool"
],
"type": 1
"docs": [
"Constructor that initializes the `bool` value to the given `init_value`."
],
"label": "new",
"payable": true,
"selector": "0x9bae9d5e"
},
"selector": "0x2f865bd9"
}
]
},
"storage": {
"struct": {
"fields": [
{
"layout": {
"cell": {
"key": "0x0000000000000000000000000000000000000000000000000000000000000000",
"ty": 1
"args": [],
"docs": [
"Constructor that initializes the `bool` value to `false`.",
"",
"Constructors can delegate to other constructors."
],
"label": "default",
"payable": false,
"selector": "0xed4b9d1b"
}
],
"docs": [],
"events": [
{
"args": [
{
"docs": [],
"indexed": true,
"label": "value",
"type": {
"displayName": [
"bool"
],
"type": 0
}
}
],
"docs": [],
"label": "Flipped"
}
],
"messages": [
{
"args": [],
"docs": [
" A message that can be called on instantiated contracts.",
" This one flips the value of the stored `bool` from `true`",
" to `false` and vice versa."
],
"label": "flip",
"mutates": true,
"payable": false,
"returnType": null,
"selector": "0x633aa551"
},
{
"args": [],
"docs": [
" Simply returns the current value of our `bool`."
],
"label": "get",
"mutates": false,
"payable": false,
"returnType": {
"displayName": [
"bool"
],
"type": 0
},
"name": "value"
"selector": "0x2f865bd9"
}
]
}
},
"types": [
{
"def": {
"primitive": "bool"
},
"storage": {
"struct": {
"fields": [
{
"layout": {
"cell": {
"key": "0x0000000000000000000000000000000000000000000000000000000000000000",
"ty": 0
}
},
"name": "value"
}
]
}
}
]
}
},
"types": [
{
"id": 0,
"type": {
"def": {
"primitive": "bool"
}
}
}
]
}
}
Binary file modified examples/assets/flipper.wasm
Binary file not shown.
12 changes: 8 additions & 4 deletions examples/create_and_exec_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)

keypair = Keypair.create_from_uri('//Alice')
contract_address = "5FSSvEfVKbJnLWyYNbjCKSKoqizbhbY9Zagsczcekqs2KzBi"
contract_address = "5FbuqfZwkNjadtaCfhAwMb5ZQ4Bi2iF5m4AnibhAR987Jra5"

# Check if contract is on chain
contract_info = substrate.query("Contracts", "ContractInfoOf", [contract_address])
Expand Down Expand Up @@ -76,15 +76,19 @@
gas_predit_result = contract.read(keypair, 'flip')

print('Result of dry-run: ', gas_predit_result.contract_result_data)
print('Gas estimate: ', gas_predit_result.gas_consumed)
print('Gas estimate: ', gas_predit_result.gas_required)

# Do the actual transfer
print('Executing contract call...')
contract_receipt = contract.exec(keypair, 'flip', args={

}, gas_limit=gas_predit_result.gas_consumed)
}, gas_limit=gas_predit_result.gas_required)

if contract_receipt.is_success:
print(f'Events triggered in contract: {contract_receipt.contract_events}')
else:
print(f'Error message: {contract_receipt.error_message}')

print(f'Events triggered in contract: {contract_receipt.contract_events}')

result = contract.read(keypair, 'get')

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ eth-keys>=0.2.1,<1
eth_utils>=1.3.0,<3
pycryptodome>=3.11.0,<4

scalecodec>=1.0.31,<2
scalecodec>=1.0.32,<2
py-sr25519-bindings>=0.1.2,<1
py-ed25519-bindings>=1.0,<2
py-bip39-bindings>=0.1.8,<1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
'eth-keys>=0.2.1,<1',
'eth_utils>=1.3.0,<3',
'pycryptodome>=3.11.0,<4',
'scalecodec>=1.0.31,<2',
'scalecodec>=1.0.32,<2',
'py-sr25519-bindings>=0.1.2,<1',
'py-ed25519-bindings>=1.0,<2',
'py-bip39-bindings>=0.1.8,<1'
Expand Down
Loading

0 comments on commit 5f34d54

Please sign in to comment.