Crate pink

source ·
Expand description

Phala's ink! - Writing Enhanced Smart Contracts

Pink! is a smart contract language extending Parity’s ink!. It extends the basic functionality with additional features, tailored to interact efficiently with Phala’s Phat Contract runtime.

§Getting Started

Unaltered ink! contracts are fully compatible and executable on the Phala’s Phat Contract platform. To learn how to start writing contracts with ink!, follow the Parity’s ink! documentation.

To get started with Pink!, add the following dependency to your Cargo.toml:

[dependencies]
ink = { version = "4", default-features = false }
pink = { version = "0.4", default-features = false }

[features]
std = [
    "ink/std",
    "pink/std",
]

Then, you can use the http_get! macro to make a GET request to a remote server:

#[ink::message]
fn http_get_example(&self) {
    let response = pink::http_get!("https://httpbin.org/get");
    assert_eq!(response.status_code, 200);
}

§Phat Contract-Specific Features

The Pink! crate is designed to empower you, enabling you to leverage the unique features of the Phat Contract, such as making HTTP requests as demonstrated in our examples. This crate supplies the crucial types and functions needed to seamlessly interact with the Phat Contract runtime.

There are three kind of APIs to communication with the runtime:

  • Emitting Events: These APIs are primarily used in situations where the operation could lead to side effects that need to be deterministically recorded and may be rolled back during the execution of the contract call. For additional information on Emitting Events APIs, please refer to the PinkEvent documentation.

  • Chain Extension: These APIs are predominantly used for read-only operations or operations that aren’t expected to create deterministic side effects. For an in-depth understanding of Chain Extension APIs, please refer to the PinkExt documentation.

  • System contract: There is a special contract called the System contract in each cluster. The system contract is instantiated when the cluster is created. Either ink contracts or external accounts can call the system contract to perform certain operations. For more information on the System contract, please refer to the System documentation.

For practical implementation examples, explore our Phat Contract examples repository.

§Using JavaScript with Phat Contract

Phat Contract supports JavaScript through the phat-quickjs contract.

There are two ways to use JavaScript in your contract:

  • You can deploy your phat-quickjs contract instance through a standard deployment process.

  • However, for a more convenient approach, most public clusters should already have a public driver quickjs contract deployed. You can obtain the contract code_hash with System::get_driver("JsDelegate").

  • For the simplest integration, consider using the phat_js crate. It provides an eval function that lets you evaluate JavaScript code snippets directly. For example:

    #[ink::message]
    fn eval_js_example(&self) {
        let result = phat_js::eval("'Hello,' + 'World'", &[]);
        assert_eq!(result, phat_js::Output::String("Hello,World".to_string()));
    }

Re-exports§

Modules§

Macros§

  • Same as info! but at Debug level.
  • Same as info! but at Error level.
  • Make a simple HTTP GET request
  • Make a simple HTTP POST request
  • Make a simple HTTP PUT request
  • Macro info! logs messages at the Info level in pink contract.
  • The log! macro allows you to log messages with specific logging levels in pink contract.
  • Same as info! but at Trace level.
  • Same as info! but at Warn level.

Structs§

Enums§

Traits§

Functions§

  • Deploy a SideVM instance to a given contract. (system only)
  • Returns the PinkEnvironment.
  • Force stop the side VM instance if it is running
  • Pushes a message to the associated SideVM instance.
  • Query to a sidevm in current worker.
  • Set the weight of contract used to schedule queries and sidevm virtual runtime. (system only)
  • Sets a hook receiver for a given hook point.
  • Set the SideVM program that used by js_eval code. (system only)
  • Set the log handler contract of current cluster. (system only)
  • Starts a SideVM instance with the provided code hash.
  • Stop a SideVM instance running at given contract address. (system only)
  • Upgrade the pink runtime to given version. (system only)
  • Generate a slice of verifiable random bytes.

Type Aliases§

Attribute Macros§

  • A drop-in replacement for ink::contract with pink-specific feature extensions.
  • This procedural macro marks an ink! trait as a ‘driver contract’ for the Pink system.