11 个版本 (2 个稳定版)

3.2.0 2024 年 5 月 8 日
3.1.0 2024 年 2 月 2 日
0.5.9 2023 年 7 月 28 日
0.5.7 2023 年 3 月 29 日
0.5.2 2023 年 2 月 10 日

#1959 in 魔法豆

Download history 80/week @ 2024-05-02 35/week @ 2024-05-09 7/week @ 2024-05-16 8/week @ 2024-05-23 3/week @ 2024-05-30 2/week @ 2024-06-06

每月下载 912

Apache-2.0 和可能 GPL-3.0-or-later…

34KB
629 代码行

Polymesh 运行时 API 的可升级包装。

这允许合约使用一个稳定的 API,该 API 可以更新以支持每个主要的 Polymesh 版本。

构建包装的 API 合约。

安装 cargo-contract

cargo install cargo-contract --force

构建合约: cargo contract build --release

需要上传的合约文件 ./target/ink/polymesh_ink.contract

使用 Polymesh Ink! API 的示例合约

Cargo.toml

[package]
name = "example_contract"
version = "1.0.0"
authors = ["<author>"]
edition = "2021"
publish = false

[dependencies]
ink = { version = "4.3", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

polymesh-ink = { version = "3.0", default-features = false, features = ["as-library"] }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
    "ink/std",
    "scale/std",
    "scale-info/std",
    "polymesh-ink/std",
]
ink-as-dependency = []

lib.rs

//! Example contract for upgradable `polymesh-ink` API.

#![cfg_attr(not(feature = "std"), no_std, no_main)]

extern crate alloc;

use polymesh_ink::*;

#[ink::contract(env = PolymeshEnvironment)]
pub mod example_contract {
    use crate::*;
    use alloc::vec::Vec;

    /// Exchange contract using the Polymesh Ink! API.
    #[ink(storage)]
    pub struct ExampleContract {
        /// Upgradable Polymesh Ink API.
        api: PolymeshInk,
    }

    /// The contract error types.
    #[derive(Debug, scale::Encode, scale::Decode)]
    #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
    pub enum Error {
        /// PolymeshInk errors.
        PolymeshInk(PolymeshError),
    }

    impl From<PolymeshError> for Error {
        fn from(err: PolymeshError) -> Self {
            Self::PolymeshInk(err)
        }
    }

    /// The contract result type.
    pub type Result<T> = core::result::Result<T, Error>;

    impl ExampleContract {
        /// Instantiate this contract with an address of the `logic` contract.
        ///
        /// Sets the privileged account to the caller. Only this account may
        /// later changed the `forward_to` address.
        #[ink(constructor)]
        pub fn new() -> Result<Self> {
            Ok(Self {
                api: PolymeshInk::new()?,
            })
        }

        /// Update the `polymesh-ink` API using the tracker.
        ///
        /// Anyone can pay the gas fees to do the update using the tracker.
        #[ink(message)]
        pub fn update_polymesh_ink(&mut self) -> Result<()> {
            self.api.check_for_upgrade()?;
            Ok(())
        }

				// Simple example of using the Polymesh Ink! API.
        #[ink(message)]
        pub fn create_asset(&mut self, name: Vec<u8>, ticker: Ticker, amount: Balance) -> Result<()> {
            self.api
                .asset_create_and_issue(AssetName(name), ticker, AssetType::EquityCommon, true, Some(amount))?;
            Ok(())
        }
    }
}

设置。

  1. 构建和上传(不部署)包装的 API 合约 ./target/ink/polymesh_ink.contract
  2. 构建和部署来自 ./examples/ 的示例合约。

可用

可以使用 update_polymesh_ink 调用来更新 Polymesh Ink! API 的代码哈希。

依赖关系

~25–45MB
~693K SLoC