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§

pub use chain_extension::pink_extension_instance as ext;
pub use logger::ResultExt;
pub use pink_types as types;

Modules§

chain_extension
logger
Logger for Pink contracts.
system

Macros§

debug
Same as info! but at Debug level.
dispatch_ext_call
error
Same as info! but at Error level.
http_get
Make a simple HTTP GET request
http_post
Make a simple HTTP POST request
http_put
Make a simple HTTP PUT request
http_req
info
Macro info! logs messages at the Info level in pink contract.
log
The log! macro allows you to log messages with specific logging levels in pink contract.
panic
trace
Same as info! but at Trace level.
warn
Same as info! but at Warn level.

Structs§

SidevmConfig

Enums§

CacheOp
Instructions to manipulate the cache.
HookPoint
Hook points defined in the runtime.
PinkEnvironment
Pink defined environment. This environment is used to access the phat contract extended runtime features.
PinkEvent
System Event used to communicate between the contract and the runtime.
SidevmOperation
Workers

Traits§

ConvertTo

Functions§

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

Type Aliases§

AccountId
Balance
BlockNumber
EcdhPublicKey
EcdsaPublicKey
EcdsaSignature
Hash
WorkerId

Attribute Macros§

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