Skip to content

Commit

Permalink
Update to ink 5.0.0-rc2 (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed authored Mar 6, 2024
1 parent ddc19c4 commit 46b1edb
Show file tree
Hide file tree
Showing 82 changed files with 2,544 additions and 131 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,20 @@ jobs:
.vscode/**
**/.gitignore
**/*.md
multi-contract-caller/**
upgradeable-contracts/**
workspace-contracts/**
ui/**
**/frontend/**
json: true

- name: Look for changes in multi and upgradable contracts
id: CHANGED_MULTI_AND_UPGRADEABLE_CONTRACTS
- name: Look for changes in upgradable contracts
id: CHANGED_UPGRADEABLE_CONTRACTS
uses: tj-actions/changed-files@v41
with:
dir_names: "true"
dir_names_exclude_current_dir: "true"
dir_names_max_depth: 2
files: |
multi-contract-caller/**
upgradeable-contracts/**
files_ignore: |
**/.gitignore
Expand Down Expand Up @@ -103,7 +101,7 @@ jobs:
run: |
ALL_CHANGED_CONTRACTS=$(jq -s 'add' \
<(echo "${{ steps.CHANGED_CONTRACTS.outputs.all_changed_files }}") \
<(echo "${{ steps.CHANGED_MULTI_AND_UPGRADEABLE_CONTRACTS.outputs.all_changed_files }}"))
<(echo "${{ steps.CHANGED_UPGRADEABLE_CONTRACTS.outputs.all_changed_files }}"))
if [ ${{steps.CHANGED_WORKSPACE_CONTRACTS.outputs.all_changed_files }} = "[]" ]
then
Expand Down Expand Up @@ -220,9 +218,15 @@ jobs:
run: cargo contract build --verbose --manifest-path=${{ matrix.contract }}/Cargo.toml;

- name: Test ${{ matrix.contract }} on ${{ matrix.platform }}-${{ matrix.toolchain }}
if: runner.os != 'Windows' && matrix.contract != 'workspace-contracts'
if: runner.os != 'Windows' && matrix.contract != 'workspace-contracts' && matrix.contract != 'static-buffer'
run: cargo test --verbose --manifest-path=${{ matrix.contract }}/Cargo.toml;

- name: Test ${{ matrix.contract }} on ${{ matrix.platform }}-${{ matrix.toolchain }}
if: runner.os != 'Windows' && matrix.contract == 'static-buffer'
run: |
cargo clean --manifest-path=${{ matrix.contract }}/Cargo.toml
INK_STATIC_BUFFER_SIZE=30 cargo test --verbose --manifest-path=${{ matrix.contract }}/Cargo.toml
- name: Build workspace ${{ matrix.contract }} on ${{ matrix.platform }}-${{ matrix.toolchain }}
if: runner.os != 'Windows' && matrix.contract == 'workspace-contracts'
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Have a look at the different examples to better understand how to use ink! to bu

### Can I add a new example here?

Please don't add them here, but create a Pull Request to the `integration-tests` folder in [the ink! repository](https://github.com/paritytech/ink) instead.
Please don't add them here, but create a Pull Request to the `integration-tests/public` folder in [the ink! repository](https://github.com/paritytech/ink) instead.
The content of that folder is synchronized with this repository on new releases.

## Preparation
Expand Down
6 changes: 3 additions & 3 deletions basic-contract-caller/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[package]
name = "basic-contract-caller"
version = "5.0.0-rc.1"
version = "5.0.0-rc.2"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
publish = false

[dependencies]
ink = { version = "5.0.0-rc.1", default-features = false }
ink = { version = "5.0.0-rc.2", default-features = false }

# Note: We **need** to specify the `ink-as-dependency` feature.
#
# If we don't we will end up with linking errors!
other-contract = { path = "other-contract", default-features = false, features = ["ink-as-dependency"] }

[dev-dependencies]
ink_e2e = { version = "5.0.0-rc.1" }
ink_e2e = { version = "5.0.0-rc.2" }

[lib]
path = "lib.rs"
Expand Down
6 changes: 3 additions & 3 deletions basic-contract-caller/other-contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[package]
name = "other-contract"
version = "5.0.0-rc.1"
version = "5.0.0-rc.2"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
publish = false

[dependencies]
ink = { version = "5.0.0-rc.1", default-features = false }
ink = { version = "5.0.0-rc.2", default-features = false }

[dev-dependencies]
ink_e2e = { version = "5.0.0-rc.1" }
ink_e2e = { version = "5.0.0-rc.2" }

[lib]
path = "lib.rs"
Expand Down
6 changes: 3 additions & 3 deletions call-runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "call-runtime"
version = "5.0.0-rc.1"
version = "5.0.0-rc.2"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
publish = false

[dependencies]
ink = { version = "5.0.0-rc.1", default-features = false }
ink = { version = "5.0.0-rc.2", default-features = false }

# Substrate
#
Expand All @@ -18,7 +18,7 @@ sp-io = { version = "23.0.0", default-features = false, features = ["disable_pan
sp-runtime = { version = "24.0.0", default-features = false }

[dev-dependencies]
ink_e2e = { version = "5.0.0-rc.1" }
ink_e2e = { version = "5.0.0-rc.2" }

[lib]
path = "lib.rs"
Expand Down
9 changes: 9 additions & 0 deletions combined-extension/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
26 changes: 26 additions & 0 deletions combined-extension/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "combined_extension"
version = "5.0.0-rc.2"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
publish = false

[dependencies]
ink = { version = "5.0.0-rc.2", default-features = false }
psp22_extension = { path = "../psp22-extension", default-features = false, features = ["ink-as-dependency"] }
rand_extension = { path = "../rand-extension", default-features = false, features = ["ink-as-dependency"] }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
"psp22_extension/std",
"rand_extension/std",
]
ink-as-dependency = [
"psp22_extension/ink-as-dependency",
"rand_extension/ink-as-dependency",
]
19 changes: 19 additions & 0 deletions combined-extension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Combined Chain Extension Example

## What is this example about?

It demonstrates how to combine several chain extensions and call them from ink!.

See [this chapter](https://use.ink/macros-attributes/chain-extension)
in our ink! documentation for more details about chain extensions.


This example uses two chain extensions, `FetchRandom`(from [rand-extension](../rand-extension))
and `Psp22Extension`(from [psp22-extension](../psp22-extension)) defined in other examples.
The example shows how to combine two chain extensions together
and use them in the contract along with each other.
Also example shows how to mock each chain extension for testing.

This example doesn't show how to define a chain extension and how
to implement in on the runtime side. For that purpose, you can
check the two examples mentioned above.
189 changes: 189 additions & 0 deletions combined-extension/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

use ink::env::{
DefaultEnvironment,
Environment,
};
use psp22_extension::Psp22Extension;
use rand_extension::{
FetchRandom,
RandomReadErr,
};

ink::combine_extensions! {
/// This extension combines the [`FetchRandom`] and [`Psp22Extension`] extensions.
/// It is possible to combine any number of extensions in this way.
///
/// This structure is an instance that is returned by the `self.env().extension()` call.
pub struct CombinedChainExtension {
/// The instance of the [`Psp22Extension`] chain extension.
///
/// It provides you access to `PSP22` functionality.
pub psp22: Psp22Extension,
/// The instance of the [`FetchRandom`] chain extension.
///
/// It provides you access to randomness functionality.
pub rand: FetchRandom,
}
}

/// An environment using default ink environment types, with PSP-22 extension included
#[derive(Debug, Clone, PartialEq, Eq)]
#[ink::scale_derive(TypeInfo)]
pub enum CustomEnvironment {}

/// We use the same types and values as for [`DefaultEnvironment`] except the
/// [`Environment::ChainExtension`] type.
impl Environment for CustomEnvironment {
const MAX_EVENT_TOPICS: usize = <DefaultEnvironment as Environment>::MAX_EVENT_TOPICS;

type AccountId = <DefaultEnvironment as Environment>::AccountId;
type Balance = <DefaultEnvironment as Environment>::Balance;
type Hash = <DefaultEnvironment as Environment>::Hash;
type Timestamp = <DefaultEnvironment as Environment>::Timestamp;
type BlockNumber = <DefaultEnvironment as Environment>::BlockNumber;

/// Setting up the combined chain extension as a primary extension.
///
/// The `self.env().extension()` call returns the instance that provides access to
/// both chain extensions.
type ChainExtension = CombinedChainExtension;
}

#[ink::contract(env = crate::CustomEnvironment)]
mod combined_extension {
use super::*;
use psp22_extension::Psp22Error;

/// Defines the storage of our contract.
///
/// The example shows how to call each extension and test it,
/// so we don't need any state to save.
#[ink(storage)]
#[derive(Default)]
pub struct CombinedExtensionContract;

impl CombinedExtensionContract {
/// Constructor that initializes empty storage.
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}

/// Returns the random value from extension.
#[ink(message)]
pub fn get_rand(&self) -> Result<[u8; 32], RandomReadErr> {
self.env().extension().rand.fetch_random([0; 32] /* seed */)
}

/// Returns the total supply from PSP22 extension.
#[ink(message)]
pub fn get_total_supply(&self) -> Result<Balance, Psp22Error> {
self.env().extension().psp22.total_supply(0)
}
}

/// Unit tests in Rust are normally defined within such a `#[cfg(test)]`
#[cfg(test)]
mod tests {
/// Imports all the definitions from the outer scope so we can use them here.
use super::*;

const RANDOM_VALUE: [u8; 32] = [3; 32];

/// Mocking the random extension to return results that we want in the tests.
struct MockedRandExtension;
impl ink::env::test::ChainExtension for MockedRandExtension {
fn ext_id(&self) -> u16 {
// It is identifier used by [`rand_extension::FetchRandom`] extension.
666
}

fn call(
&mut self,
_func_id: u16,
_input: &[u8],
output: &mut Vec<u8>,
) -> u32 {
ink::scale::Encode::encode_to(&RANDOM_VALUE, output);
0
}
}

#[ink::test]
fn rand_chain_extension_works() {
let contract = CombinedExtensionContract::new();

// given
let result = std::panic::catch_unwind(|| contract.get_rand());
// The call to random extension should fail because it is not registered.
assert!(result.is_err());

// when
ink::env::test::register_chain_extension(MockedRandExtension);

// then
assert_eq!(contract.get_rand(), Ok(RANDOM_VALUE));
}

const TOTAL_SUPPLY: u128 = 1377;

/// Mocking the PSP22 extension to return results that we want in the tests.
///
/// Because this extension has many methods, we want to implement only one of
/// them:
/// - `total_supply` with corresponding `func_id` - `0x162d`.
struct MockedPSP22Extension;
impl ink::env::test::ChainExtension for MockedPSP22Extension {
fn ext_id(&self) -> u16 {
// It is identifier used by [`psp22_extension::Psp22Extension`] extension.
13
}

fn call(&mut self, func_id: u16, _input: &[u8], output: &mut Vec<u8>) -> u32 {
match func_id {
0x162d /* `func_id` of the `total_supply` function */ => {
ink::scale::Encode::encode_to(&TOTAL_SUPPLY, output);
0
}
_ => {
1
}
}
}
}

#[ink::test]
fn psp22_chain_extension_works() {
let contract = CombinedExtensionContract::new();

// given
let result = std::panic::catch_unwind(|| contract.get_total_supply());
// The call to PSP22 extension should fail because it is not registered.
assert!(result.is_err());

// when
ink::env::test::register_chain_extension(MockedPSP22Extension);

// then
assert_eq!(contract.get_total_supply(), Ok(TOTAL_SUPPLY));
}

#[ink::test]
fn both_chain_extensions_work() {
let contract = CombinedExtensionContract::new();

// given
assert!(std::panic::catch_unwind(|| contract.get_rand()).is_err());
assert!(std::panic::catch_unwind(|| { contract.get_total_supply() }).is_err());

// when
ink::env::test::register_chain_extension(MockedRandExtension);
ink::env::test::register_chain_extension(MockedPSP22Extension);

// then
assert_eq!(contract.get_rand(), Ok(RANDOM_VALUE));
assert_eq!(contract.get_total_supply(), Ok(TOTAL_SUPPLY));
}
}
}
6 changes: 3 additions & 3 deletions conditional-compilation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "conditional-compilation"
version = "5.0.0-rc.1"
version = "5.0.0-rc.2"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"

[dependencies]
ink = { version = "5.0.0-rc.1", default-features = false }
ink = { version = "5.0.0-rc.2", default-features = false }

[dev-dependencies]
ink_e2e = { version = "5.0.0-rc.1" }
ink_e2e = { version = "5.0.0-rc.2" }

[lib]
path = "lib.rs"
Expand Down
Loading

0 comments on commit 46b1edb

Please sign in to comment.