Thanks to visit codestin.com
Credit goes to lib.rs

13 releases (4 stable)

3.5.0 May 2, 2025
3.4.0 May 2, 2025
3.2.0 May 8, 2024
3.1.0 Feb 2, 2024
0.5.2 Feb 10, 2023

#4 in #poly-mesh

Codestin Search App Codestin Search App

780 downloads per month

Apache-2.0

38KB
691 lines

An upgradable wrapper around the Polymesh Runtime API.

This allows contracts to use a stable API that can be updated to support each major Polymesh release.

Verifiable build

Install the docker image:

docker pull quay.io/subscan-explorer/wasm-compile-build:amd64-stable-1.70.0-v3.2.0

Build the contact inside the docker image:

docker run --rm -it -v .:/builds/contract -v ./target:/target/ quay.io/subscan-explorer/wasm-compile-build:amd64-stable-1.70.0-v3.2.0 cargo contract build --release

Check the contract hash from ./target/ink/polymesh_ink.json

grep hash ./target/ink/polymesh_ink.json

Compare that hash to the current API hash on Testnet/Mainnet. Query PolymeshContracts.currentApiHash.

Build the wrapped API contract.

Install cargo-contract.

cargo install cargo-contract --force

Build the contract: cargo contract build --release

Contract file needed for upload ./target/ink/polymesh_ink.contract.

Example contract that uses the 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.4.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>, amount: Balance) -> Result<()> {
            self.api
                .asset_create_and_issue(AssetName(name), AssetType::EquityCommon, true, Some(amount))?;
            Ok(())
        }
    }
}

Setup.

  1. Build and upload (don't deploy) the wrapped API contract ./target/ink/polymesh_ink.contract.
  2. Build and deploy an example contract from ./examples/.

Usable

The update_polymesh_ink calls can be used to update the code hash for the Polymesh Ink! API.

Dependencies

~31–42MB
~759K SLoC