11 releases (5 stable)

new 4.1.1 Apr 17, 2024
4.1.0 Apr 10, 2024
4.0.2 Mar 14, 2024
4.0.0-rc.3 Feb 29, 2024
3.1.0 Jul 26, 2023

#1544 in Magic Beans

Download history 2/week @ 2024-02-03 3/week @ 2024-02-10 29/week @ 2024-02-17 158/week @ 2024-02-24 152/week @ 2024-03-02 571/week @ 2024-03-09 151/week @ 2024-03-16 66/week @ 2024-03-23 82/week @ 2024-03-30 171/week @ 2024-04-06

539 downloads per month
Used in 2 crates

Apache-2.0

600KB
14K SLoC

Contract Extrinsics

Defines the extrinsics for smart contracts targeting substrate.

Currently part of cargo-contract, the build tool for smart contracts written in ink!.

Common arguments

--suri

The Secret URI used for signing the extrinsic. For development chains, the well known endowed accounts can be used e.g. //Alice. For other accounts, the actual secret key must be provided e.g. an 0x prefixed 64 bit hex string, or the seed phrase. See usage of subkey for examples, and docs for the expected values in the parsing code.

⚠️ WARNING ⚠️

It is strongly recommended NOT to use secret keys from actual value bearing chains on the command line, since they are visible on screen and are often saved to the command line shell's history. For now this tool should only be used for development and testnets. It is a priority to implement a safer method of signing here before using this tool with value bearing chains.

--password

Optional. The password for the --suri, see https://docs.substrate.io/reference/command-line-tools/subkey/#password-protected-keys.

--manifest-path

Optional. The path to the Cargo.toml of the contract crate. Use this to run commands on a contract from outside of its project directory.

--url

Optional. The websockets url of an RPC node on the target chain. Defaults to a locally running node at "ws://localhost:9944".

-x/--execute

Optional. All extrinsic commands run without altering the chain state by default. This flag specifies that the extrinsic needs to be executed on chain.

--storage-deposit-limit

Optional. The maximum amount of balance that can be charged from the caller to pay for the storage consumed.

Commands

upload

Upload the Wasm code of the contract to the target chain. Invokes the upload_code dispatchable.

e.g. cargo contract upload --suri //Alice

Assumes that cargo contract build has already been run to produce the contract artifacts.

instantiate

Create an instance of a contract on chain. If the code has already been uploaded via upload, specify the resulting --code-hash which will result in a call to instantiate. If no --code-hash is specified it will attempt to both upload the code and instantiate via the instantiate_with_code dispatchable.

e.g.

cargo contract instantiate \
       --constructor new \
       --args false \
       --suri //Alice \
       --code-hash 0xbc1b42256696c8a4187ec3ed79fc602789fc11287c4c30926f5e31ed8169574e
  • --constructor the name of the contract constructor method to invoke.
  • --args accepts a space separated list of values, encoded in order as the arguments of the constructor to invoke.
  • --code-hash the hash of the uploaded code, returned from a call to contract upload or a previous contract instantiate

call

Invoke a message on an instance of a contract via the call dispatchable.

e.g.

cargo contract call \
       --contract 5FKy7RwXBCCACCEPjM5WugkhUd787FjdgieTkdj7TPngJzxN \
       --message transfer \
       --args 5FKy7RwXBCCACCEPjM5WugkhUd787FjdgieTkdj7TPngJzxN 1000 \
       --suri //Alice
  • --contract the account id of the contract to invoke, returned after a successful contract instantiate.
  • --message the name of the contract message to invoke.
  • --args accepts a space separated list of values, encoded in order as the arguments of the message to invoke.

remove

Remove the Wasm code of the contract to the target chain. Invokes the remove_code dispatchable.

e.g. cargo contract remove --suri //Alice

Assumes that cargo contract build and cargo contract upload have already been run to produce the contract artifacts. This command will only succeed if there are no contract instances of this code. Contracts which have already been instantiated from this code must either terminate themselves or have their code changed via a set_code call to pallet_contracts.

cargo contract remove \
       --suri //Alice \
       --code-hash 0xbc1b42256696c8a4187ec3ed79fc602789fc11287c4c30926f5e31ed8169574e
  • --code-hash the hash of the uploaded code, returned from a call to contract upload. If not specified the code hash will be taken from the contract artifacts.

Specifying the contract artifact

The above examples assume the working directory is the contract source code where the Cargo.toml file is located. This is used to determine the location of the contract artifacts. Alternatively, there is an optional positional argument to each of the extrinsic commands which allows specifying the contract artifact file directly. E.g.

  • cargo upload ../path/to/mycontract.wasm
  • cargo instantiate ../path/to/mycontract.contract
  • cargo call ..path/to/mycontract.json

Dependencies

~44–62MB
~1M SLoC