Expand description
§pop-contracts
A crate for generating, building, deploying, and calling ink! Smart Contracts.
Used by pop-cli.
§Usage
Generate a new Smart Contract:
use pop_contracts::{create_smart_contract, Contract};
use std::path::Path;
let contract_path = Path::new("./");
create_smart_contract("my_contract", &contract_path, &Contract::Standard);Build an existing Smart Contract:
use pop_contracts::build_smart_contract;
use std::path::Path;
pub use contract_build::{Verbosity, MetadataSpec};
let contract_path = Path::new("./");
let build_release = true; // `true` for release mode, `false` for debug mode.
let result = build_smart_contract(&contract_path, build_release, Verbosity::Default, Some(MetadataSpec::Ink)); // You can build your contract with Solidity metadata using `Some(Metadata::Ink)`Test an existing Smart Contract:
use pop_common::test_project;
use pop_contracts::test_e2e_smart_contract;
use std::path::Path;
let contract_path = Path::new("./");
let contracts_node_path = Path::new("./path-to-contracts-node-binary");
//unit testing
test_project(&contract_path, None);
//e2e testing
test_e2e_smart_contract(&contract_path, Some(contracts_node_path), None);Deploy and instantiate an existing Smart Contract:
use pop_contracts::{ dry_run_gas_estimate_instantiate, instantiate_smart_contract, set_up_deployment, UpOpts};
use std::path::PathBuf;
use tokio_test;
use url::Url;
tokio_test::block_on(async {
let contract_path = PathBuf::from("./");
// prepare extrinsic for deployment
let up_opts = UpOpts {
path: contract_path,
constructor: "new".to_string(),
args: ["false".to_string()].to_vec(),
value: "1000".to_string(),
gas_limit: None,
proof_size: None,
url: Url::parse("ws://localhost:9944").unwrap(),
suri: "//Alice".to_string(),
salt: None,
};
let instantiate_exec = set_up_deployment(up_opts).await.unwrap();
// If you don't know the `gas_limit` and `proof_size`, you can perform a dry run to estimate the gas amount before instatianting the Smart Contract.
let weight = dry_run_gas_estimate_instantiate(&instantiate_exec).await.unwrap();
let contract_address = instantiate_smart_contract(instantiate_exec, weight).await.unwrap();
});Upload a Smart Contract only:
use pop_contracts::{ dry_run_upload, set_up_upload, upload_smart_contract, UpOpts};
use std::path::PathBuf;
use tokio_test;
use url::Url;
tokio_test::block_on(async {
// prepare extrinsic for deployment
let contract_path = PathBuf::from("./");
let up_opts = UpOpts {
path: contract_path,
constructor: "new".to_string(),
args: ["false".to_string()].to_vec(),
value: "1000".to_string(),
gas_limit: None,
proof_size: None,
url: Url::parse("ws://localhost:9944").unwrap(),
suri: "//Alice".to_string(),
salt: None,
};
let upload_exec = set_up_upload(up_opts).await.unwrap();
// to perform only a dry-run
let hash_code = dry_run_upload(&upload_exec).await.unwrap();
// to upload the smart contract
let code_hash = upload_smart_contract(&upload_exec).await.unwrap();
});Call a deployed (and instantiated) Smart Contract:
use pop_contracts::{call_smart_contract, dry_run_call, dry_run_gas_estimate_call, set_up_call,CallOpts};
use std::path::PathBuf;
use tokio_test;
use url::Url;
tokio_test::block_on(async {
// prepare extrinsic for call
let contract_path = PathBuf::from("./");
let get_call_opts = CallOpts {
path: contract_path.clone(),
contract: "5CLPm1CeUvJhZ8GCDZCR7nWZ2m3XXe4X5MtAQK69zEjut36A".to_string(),
message: "get".to_string(),
args: [].to_vec(),
value: "1000".to_string(),
gas_limit: None,
proof_size: None,
url: Url::parse("ws://localhost:9944").unwrap(),
suri: "//Alice".to_string(),
execute: false
};
let get_call_exec = set_up_call(get_call_opts).await.unwrap();
// For operations that only require reading from the blockchain state, it does not require to submit an extrinsic.
let call_dry_run_result = dry_run_call(&get_call_exec).await.unwrap();
// For operations that change a storage value, thus altering the blockchain state, requires to submit an extrinsic.
let flip_call_opts = CallOpts {
path: contract_path,
contract: "5CLPm1CeUvJhZ8GCDZCR7nWZ2m3XXe4X5MtAQK69zEjut36A".to_string(),
message: "flip".to_string(),
args: [].to_vec(),
value: "1000".to_string(),
gas_limit: None,
proof_size: None,
url: Url::parse("ws://localhost:9944").unwrap(),
suri: "//Alice".to_string(),
execute: true
};
let flip_call_exec = set_up_call(flip_call_opts).await.unwrap();
let url = Url::parse("ws://localhost:9944").unwrap();
// If you don't know the `gas_limit` and `proof_size`, you can perform a dry run to estimate the gas amount before calling the Smart Contract.
let (_, weight_limit) = dry_run_gas_estimate_call(&flip_call_exec).await.unwrap();
// Use this weight to execute the call.
let call_result = call_smart_contract(flip_call_exec, weight_limit, &url).await.unwrap();
});§Acknowledgements
pop-contracts would not be possible without the awesome crate:
cargo-contract.
Structs§
- Account
Mapper - A helper struct for performing account mapping operations.
- Bytes
- Hex-serialized shim for
Vec<u8>. - Call
Exec - Call
Opts - Attributes for the
callcommand. - Contract
Function - Describes a contract function.
- Contract
Info - Type to represent information about a deployed smart contract.
- Contract
Storage - Describes a contract storage item.
- Extrinsic
Opts - Arguments required for creating and sending an extrinsic to a Substrate node.
- Param
- Describes a parameter.
- UpOpts
- Attributes for the
upcommand - Upload
Code - A raw call to
pallet-contracts’supload_code. - Weight
Enums§
- Contract
- A smart contract template.
- Contract
Callable - Represents a callable entity within a smart contract, either a function or storage item.
- Default
Environment - The fundamental types of the default configuration.
- Error
- Represents the various errors that can occur in the crate.
- Function
Type - Specifies the type of contract function, either a constructor or a message.
- Metadata
Spec - Specification to use for contract metadata.
- Verbosity
- Denotes if output should be printed to stdout.
Traits§
- Environment
- The environmental types usable by contracts defined with ink!.
Functions§
- build_
smart_ contract - Build the smart contract located at the specified
pathinbuild_releasemode. - call_
smart_ contract - Call a smart contract on the blockchain.
- call_
smart_ contract_ from_ signed_ payload - Executes a smart contract call using a signed payload.
- create_
smart_ contract - Create a new smart contract.
- dry_
run_ call - Simulate a smart contract call without modifying the state of the blockchain.
- dry_
run_ gas_ estimate_ call - Estimate the gas required for a contract call without modifying the state of the blockchain.
- dry_
run_ gas_ estimate_ instantiate - Estimate the gas required for instantiating a contract without modifying the state of the blockchain.
- dry_
run_ upload - Performs a dry-run for uploading a contract without modifying the state of the blockchain.
- eth_
rpc_ generator - Retrieves the latest release of the Ethereum RPC binary, resolves its version, and constructs
a
Binary::Sourcewith the specified cache path. - extract_
function - Extracts the information of a smart contract function (message or constructor) parsing the contract artifact.
- fetch_
contract_ storage - Fetches and decodes a storage value from a deployed smart contract.
- get_
call_ payload - Generates the payload for executing a smart contract call.
- get_
contract_ code - Reads the contract code from contract file.
- get_
contract_ storage_ info - Extracts a list of smart contract storage items parsing the contract artifact.
- get_
instantiate_ payload - Gets the encoded payload call data for a contract instantiation.
- get_
message - Extracts the information of a smart contract message parsing the contract artifact.
- get_
messages - Extracts a list of smart contract messages parsing the contract artifact.
- get_
upload_ payload - Gets the encoded payload call data for contract upload (not instantiate).
- ink_
node_ generator - Retrieves the latest release of the contracts node binary, resolves its version, and constructs
a
Binary::Sourcewith the specified cache path. - instantiate_
contract_ signed - Submit a pre-signed payload for instantiating a contract.
- instantiate_
smart_ contract - Instantiate a contract.
- is_
chain_ alive - Checks if the specified node is alive and responsive.
- is_
supported - Determines whether the manifest at the supplied path is a supported smart contract project.
- is_
valid_ contract_ name - Determines whether the provided name is valid for a smart contract.
- mock_
build_ process - Mocks the build process by generating contract artifacts in a specified temporary directory.
- new_
environment - Generates a smart contract test environment.
- parse_
hex_ bytes - Parse hex encoded bytes.
- run_
eth_ rpc_ node - Runs the latest version of the
eth_rpcin the background. - run_
ink_ node - Runs the latest version of the
ink-nodein the background. - set_
up_ call - Prepare the preprocessed data for a contract
call. - set_
up_ deployment - Prepare
InstantiateExecdata to upload and instantiate a contract. - set_
up_ upload - Prepare
UploadExecdata to upload a contract. - submit_
signed_ payload - Submit a pre-signed payload.
- test_
e2e_ smart_ contract - Run e2e tests of a smart contract.
- upload_
contract_ signed - Submit a pre-signed payload for uploading a contract.
- upload_
smart_ contract - Upload a contract.