2 releases

0.4.1 Feb 27, 2024
0.4.0 Feb 27, 2024
0.1.0-dev.0 Feb 22, 2024
0.0.0 Jan 19, 2024

#1 in #phat-contract

Download history 31/week @ 2024-06-15 34/week @ 2024-06-22 4/week @ 2024-06-29 35/week @ 2024-07-06 44/week @ 2024-07-13 25/week @ 2024-07-20 25/week @ 2024-07-27 23/week @ 2024-08-03 20/week @ 2024-08-10 15/week @ 2024-08-17 29/week @ 2024-08-24 21/week @ 2024-08-31 17/week @ 2024-09-07 25/week @ 2024-09-14 42/week @ 2024-09-21 28/week @ 2024-09-28

113 downloads per month
Used in 7 crates

Apache-2.0

77KB
958 lines

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()));
    }
    

Dependencies

~8–19MB
~251K SLoC