Skip to content

Commit 4195197

Browse files
ascjonesathei
andauthored
Interact with contracts: upload, instantiate and call commands (#79)
* WIP display all events * Clean up and generalize displaying events * Fix multiple event new lines * Fmt * Create wrapper types for Display, derive Debug for originals * Control indentation of pretty printing * Indent events display * More tuning of runtime events pretty printing * Fmt * Use Display impls for printing events * Add DisplayTransferEvent * Namespace events * Add CodeStored and Instantiated events * Refactor Deploy command and use verbosity for events display * Fix up string message return * Code hash LowerHex and println after events * Use LowerHex api for Bytes * Parse code hash with 0x prefix * Fmt * Spawn canvas node for integration test * Increment attempts * Get the test running with spawned canvas node * Instantiate deploy works with manual node * Test works locally and cleans up temp dir * Fmt * Fix duplicate import * Build before testing so cargo-contract bin available for testing * Separate line for build * Extract decode_hex util fn * WIP failing Literal tests for SS58 account ids * Upgrade to nom 0.6 * Use nom error in custom error, impl FromStr * Fmt * Update lockfile after merge * Make TryFrom take reference for verbosity conversion * Fix parsing literals * Plumbing for custom encoders * Implement AccountId32 custom encoder * Refactor: extract env types * Test for encoding ss58 literal * Fmt and add back failing struct test * Use AccountId type in test * TEMP: Use github dependencies of scale-info and ink * Err for 256 bit ints * Use new registry type enumeration * Extract type lookup and introduce Encoder * WIP: extract Transcoder and implement env type decoding * Implement env type decoding, removes generic Input. Custom env decoders are trait objects so can't have generic type parameters on its decode method. * Fix AccountId decoding, add some debug logging * Test for different AccountId aliasing * Passing tests for AccountId encoding and fmt * Update scale-info and ink deps, patch ink deps for now * Update scale-info and ink deps, patch ink deps for now * Remove extrinsics feature * Dependencies * More deps * Cargo.lock * Cargo.lock * WIP upgrading to latest subxt and scale-info * More upgrading to latest subxt * Fix more errors * Fix up extrinsic commands * Refactor transcoder decode, handle Compact * Refactor transcoder encode, initial compact support * Fmt * Fix metadata file loading * Constructor arg rename * Enable tokio * Make instantiate work * Update contracts runtime from substrate-contracts-node * Fix warnings and add todo * Update subxt * Add missing import * Use explicit substrate branch to match subxt import * Remove sp_rpc dependency * Remove sp_rpc dependency * Use metadata to decode events * Fmt * Pretty print events * WIP custom transcoders for events * Custom transcoders for events working * Fix some warnings * Slim down bat dependency * Decode contract events * Use correct field for instantiated contract account * Fix warnings * Fix multiple line warning * Update contracts RPC types * Fmt * Fix deploy contract account * Use ink! master branch * Oops * Remove patches * Bump ink deps to rc6 * Update to latest subxt * Update to latest subxt * Fix errors after subxt update * Fix instantiate_with_code.rs * Update subxt * Add deserializing context * Fmt * Expose public metadata fields * Implement full metadata decoding * Cargo.lock * Update calls to work with storage_gas_limit from paritytech/substrate#10082 * Cargo.lock * Update to latest subxt * Fmt * WIP unify instantiate command * Fix call command errors * Main warning unused result * Call and instantiate warns and errors and fmt * Update to ink rc7 * Default to local contract wasm if none specified * Change instantiate endowment to value * Use latest subxt branch * Update to latest subxt * Unify call and instantiate arg names * Instantiate and call args consistency * Add todo comment for encoding message args * Refactor, tidy up call and instantiate * Display decoded contract event separately * Register env types with and without aliases * Add pretty print flag * WIP add upload * Wiring up upload code extrinsic * Download latest contracts node metadata * Wire up upload command and WIP manifest path * in! default features and cargo update * Add manifest path argument for extrinsics * Only fetch crate metadata once * Fmt * Cargo upgrade * Update substrate dependencies * Pretty print code upload dry run values * Extract name value println macro * Print code hash from upload extrinsic * Refactor upload code command * Pretty print instantiate results * Add salt arg to instantiate command * WIP custom events display, removes big bat dep * Extract method for printing RPC contract result * Ugly printing of events * Fix contract event decoding * Fmt * Pretty print event fields * Only print event fields if verbose * More event prettification * Fmt * Resolve some todos * Remove some code comments, moved to follow ups in PR * Refactor: extract compact encoding function * Fix storage_deposit_limit and add todo re: compact primitives * Add todo re: compact primitives * Cargo.lock * Use released substrate crates and subxt master * Fix encoding compact, inline for each uint * Fix encoding compact, inline for each uint * Add extrinsics markdown outline * Dynamic compact encoding * Replace bitvec todos with errs, added follow up in PR * Remove todo about friendly encoding error, add to PR follow ups * Rename codec to scale for test contract, fix some transcoding tests * Attempt to fix linking with __ink_generate_metadata * Update to ink rc8 * Add test for out of order composite fields * Update ink metadata version * Update ink metadata types and methods * Fix metadata error message * Add std feature to fix test compilation with inline contract * Fix out of order struct fields * Improve encoding of field error message * Fmt imports * Rename TypeLookupId to TypeLookup * Remove alias matching for custom env type transcoding * Fix variant encoding for maps and out of order fields * Add failing test & impl for compact struct transcoding * Use released version of pallet-contracts primitives * WIP attempting to fix compact struct transcoding * Fix compact struct decoding * Add todo for integration test * Renamed SconParseError * Failing test for invalid integer * Use nom-supreme for combinators, error handling * Use released subxt * Refactor integer parsing * Refactor char parsing * Refactor seq parsing * Refactor tuple parsing * Refactor unit tuple parsing * Refactor map parsing * Refactor bytes parsing * Remove final nom `map` free function usage * Use some postfix recognizes * Remove final recognize combinator * Use postfix value combinator * Use opt postfix combinator * Move entry point and top level parser to the top of the file * Inline some string parsers * Inline some local helper functions * Fix failing integer tests * Fix Rust ident parsing * Fix up integration test * Make clippy happy * Fmt * Clippy clippy clippy * Ignore integration test for CI * Support parsing underscore separators for integer literals * + sign prefix parses to signed int * Allow underscores for balance CLI args * Add helper function for handling runtime errors * Add helper function for displaying runtime module errors * Elide lifetime * Add some basic module level docs for transcoding * Improve error message where metadata file not found * Adding common args section to extrinsics.md * Move --dry-run to extrinisic opts and more docs * Move --storage-deposit-limit to ExtrinsicOpts, move ExtrinsicOpts to extrinsics. * Add instantiate docs * Add call docs * Update Extrinsics docs * Update Cargo.toml Co-authored-by: Alexander Theißen <[email protected]> * Don't wait for finalization before displaying transaction success * Update docs/extrinsics.md Co-authored-by: Alexander Theißen <[email protected]> * Remove static error detail lookup, since it may be inaccurate if the target chain differs * CLIPPY * Print debug message lines * Display not Debug return value * Fmt Co-authored-by: Alexander Theißen <[email protected]>
1 parent 942c50c commit 4195197

28 files changed

+5870
-1684
lines changed

Cargo.lock

+1,661-1,326
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+34-15
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ heck = "0.4.0"
2828
zip = { version = "0.5.13", default-features = false }
2929
parity-wasm = "0.42.2"
3030
cargo_metadata = "0.14.1"
31-
codec = { package = "parity-scale-codec", version = "2.3.1", features = ["derive"] }
31+
scale = { package = "parity-scale-codec", version = "2.3.1", features = ["derive"] }
3232
which = "4.2.4"
3333
colored = "2.0.0"
3434
toml = "0.5.8"
@@ -43,14 +43,24 @@ url = { version = "2.2.2", features = ["serde"] }
4343
impl-serde = "0.3.2"
4444
regex = "1.5.4"
4545

46-
# dependencies for optional extrinsics feature
47-
async-std = { version = "1.10.0", optional = true }
48-
sp-core = { version = "2.0.1", optional = true }
49-
subxt = { version = "0.14.0", package = "substrate-subxt", optional = true }
50-
futures = { version = "0.3.21", optional = true }
51-
hex = { version = "0.4.3", optional = true }
52-
53-
funty = "2.0.0"
46+
# dependencies for extrinsics (deploying and calling a contract)
47+
async-std = { version = "1.10.0", features = ["attributes", "tokio1"] }
48+
ink_metadata = { version = "3.0.0-rc8", features = ["derive"] }
49+
ink_env = "3.0.0-rc8"
50+
scale-info = { version = "1.0.0", default-features = false, features = ["derive"] }
51+
sp-core = "5.0.0"
52+
sp-runtime = "5.0.0"
53+
pallet-contracts-primitives = "5.0.0"
54+
subxt = "0.17.0"
55+
futures = "0.3.19"
56+
hex = "0.4.3"
57+
jsonrpsee = { version = "0.6.1", features = ["ws-client"] }
58+
nom = "7.1.0"
59+
nom-supreme = "0.6.0"
60+
indexmap = "1.8.0"
61+
thiserror = "1.0.30"
62+
escape8259 = "0.5.1"
63+
itertools = "0.10.3"
5464

5565
[build-dependencies]
5666
anyhow = "1.0.53"
@@ -60,19 +70,28 @@ substrate-build-script-utils = "3.0.0"
6070
platforms = "2.0.0"
6171

6272
[dev-dependencies]
73+
assert_cmd = "2.0.4"
6374
assert_matches = "1.5.0"
6475
pretty_assertions = "1.1.0"
6576
wabt = "0.10.0"
77+
regex = "1.5.4"
78+
predicates = "2.1.1"
6679

67-
[features]
68-
default = []
80+
ink_primitives = "3.0.0-rc8"
81+
ink_storage = "3.0.0-rc8"
82+
ink_lang = "3.0.0-rc8"
6983

70-
# Enable this for (experimental) commands to deploy, instantiate and call contracts.
71-
#
72-
# Disabled by default
73-
extrinsics = ["sp-core", "subxt", "async-std", "futures", "hex"]
84+
[features]
85+
# This `std` feature is required for testing using an inline contract's metadata, because `ink!` annotates the metadata
86+
# generation code with `#[cfg(feature = "std")]`.
87+
default = ["std"]
88+
std = []
7489

7590
# Enable this to execute long running tests, which usually are only run on the CI server
7691
#
7792
# Disabled by default
7893
test-ci-only = []
94+
95+
# Enable this to execute tests which depend on a locally running contracts enabed chain
96+
# e.g.https://github.com/paritytech/canvas-node
97+
integration-tests = []

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ artifact to the `target/` directory.
114114

115115
Runs test suites defined for a smart contract off-chain.
116116

117+
##### `cargo contract upload`
118+
119+
Upload a contract to a `pallet-contracts` enabled chain. See [extrinsics](docs/extrinsics.md).
120+
121+
##### `cargo contract instantiate`
122+
123+
Create an instance of a contract on chain. See [extrinsics](docs/extrinsics.md).
124+
125+
##### `cargo contract call`
126+
127+
Invoke a message on an existing contract on chain. See [extrinsics](docs/extrinsics.md).
128+
117129
## License
118130

119131
The entire code within this repository is licensed under the [GPLv3](LICENSE).

docs/extrinsics.md

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Extrinsics
2+
`cargo-contract` provides CLI support for uploading, instantiating and calling your contracts directly from the command
3+
line.
4+
5+
## Common arguments
6+
7+
```
8+
--suri
9+
```
10+
The Secret URI used for signing the extrinsic. For development chains, the well known endowed accounts can be used e.g.
11+
`//Alice`. For other accounts, the actual secret key must be provided e.g. an `0x` prefixed 64 bit hex string, or the
12+
seed phrase. See usage of [`subkey`](https://docs.substrate.io/v3/tools/subkey/) for examples, and docs for the expected
13+
values in the [parsing code](https://docs.rs/sp-core/latest/sp_core/crypto/trait.Pair.html#method.from_string_with_seed).
14+
15+
:warning: **WARNING** :warning:
16+
17+
It is strongly recommended NOT to use secret keys from actual value bearing chains on the command line, since they are
18+
visible on screen and are often saved to the command line shell's history. For now this tool should only be used for
19+
development and testnets. It is a priority to implement a safer method of signing here before using this tool with value
20+
bearing chains.
21+
22+
```
23+
--password
24+
```
25+
*Optional*. The password for the `--suri`, see https://docs.substrate.io/v3/tools/subkey/#password-protected-keys.
26+
27+
```
28+
--manifest-path
29+
```
30+
*Optional*. The path to the `Cargo.toml` of the contract crate. Use this to run commands on a contract from outside of
31+
its project directory.
32+
33+
```
34+
--url
35+
```
36+
*Optional*. The websockets url of an RPC node on the target chain. Defaults to a locally running node at
37+
"ws://localhost:9944".
38+
39+
```
40+
---dry-run
41+
```
42+
*Optional*. All extrinsic commands can be run without altering the chain state. Useful for testing if a command will be
43+
successful, estimating gas costs or querying the result of `ink!` readonly messages.
44+
45+
```
46+
--storage-deposit-limit
47+
```
48+
*Optional*. The maximum amount of balance that can be charged from the caller to pay for the storage consumed.
49+
50+
## Commands
51+
52+
### `upload`
53+
54+
Upload the Wasm code of the contract to the target chain. Invokes the [`upload_code`](https://github.com/paritytech/substrate/blob/master/frame/contracts/src/lib.rs#L509)
55+
dispatchable.
56+
57+
e.g. `cargo contract upload --suri //Alice`
58+
59+
Assumes that `cargo contract build` has already been run to produce the contract artifacts.
60+
61+
### `instantiate`
62+
63+
Create an instance of a contract on chain. If the code has already been uploaded via `upload`, specify the resulting
64+
`--code-hash` which will result in a call to [`instantiate`](https://github.com/paritytech/substrate/blob/master/frame/contracts/src/lib.rs#L460).
65+
If no `--code-hash` is specified it will attempt to both upload the code and instantiate via the
66+
[`instantiate_with_code`](https://github.com/paritytech/substrate/blob/master/frame/contracts/src/lib.rs#L419)
67+
dispatchable.
68+
69+
e.g.
70+
```
71+
cargo contract instantiate \
72+
--constructor new \
73+
--args false \
74+
--suri //Alice \
75+
--code-hash 0xbc1b42256696c8a4187ec3ed79fc602789fc11287c4c30926f5e31ed8169574e
76+
```
77+
- `--constructor` the name of the contract constructor method to invoke.
78+
- `--args` accepts a space separated list of values, encoded in order as the arguments of the constructor to invoke.
79+
- `--code-hash` the hash of the uploaded code, returned from a call to `contract upload` or a previous
80+
`contract instantiate`
81+
82+
### `call`
83+
84+
Invoke a message on an instance of a contract via the [`call`](https://github.com/paritytech/substrate/blob/master/frame/contracts/src/lib.rs#L359)
85+
dispatchable.
86+
87+
e.g.
88+
```
89+
cargo contract call \
90+
--contract 5FKy7RwXBCCACCEPjM5WugkhUd787FjdgieTkdj7TPngJzxN \
91+
--message transfer \
92+
--args 5FKy7RwXBCCACCEPjM5WugkhUd787FjdgieTkdj7TPngJzxN 1000 \
93+
--suri //Alice
94+
```
95+
96+
- `--contract` the account id of the contract to invoke, returned after a successful `contract instantiate`.
97+
- `--message` the name of the contract message to invoke.
98+
- `--args` accepts a space separated list of values, encoded in order as the arguments of the message to invoke.
99+
100+
101+
102+
103+

src/cmd/deploy.rs

-111
This file was deleted.

0 commit comments

Comments
 (0)