Skip to content

Commit

Permalink
Merge pull request #14 from r0gue-io/chungquantin/feat-runtime_error
Browse files Browse the repository at this point in the history
feat: runtime error
  • Loading branch information
Daanvdplas authored Oct 18, 2024
2 parents d94da0e + 59ae0eb commit a66205f
Show file tree
Hide file tree
Showing 8 changed files with 623 additions and 140 deletions.
8 changes: 6 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
[workspace]
resolver = "2"
members = [
"crates/ink-sandbox",
"crates/drink/drink",
"crates/drink/drink-cli",
"crates/drink/drink/test-macro",
"crates/pop-drink",
]
exclude = [
"crates/drink/examples",
"crates/ink-sandbox",
"crates/drink/drink",
"crates/drink/drink-cli",
"crates/drink/drink/test-macro",
"crates/pop-drink",
]
exclude = ["crates/drink/examples"]

[workspace.package]
edition = "2021"
Expand All @@ -32,7 +30,7 @@ proc-macro2 = { version = "1" }
quote = { version = "1" }
ratatui = { version = "0.21.0" }
scale = { package = "parity-scale-codec", version = "3.6.9", features = [
"derive",
"derive",
] }
scale-info = { version = "2.10.0" }
serde_json = { version = "1.0" }
Expand All @@ -56,6 +54,6 @@ sp-runtime-interface = { version = "28.0.0", features = ["std"] }
# Local
drink = { path = "crates/drink/drink" }
ink_sandbox = { path = "crates/ink-sandbox" }
pop-api = { path = "../pop-node/pop-api" }
pop-drink = { path = "crates/pop-drink" }
pop-runtime-devnet = { path = "../pop-node/runtime/devnet" }
pop-api = { path = "../pop-node/pop-api" }
99 changes: 58 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,85 @@
<h1 align="center"> Pop DRink! (forked from DRink!) </h1>
<p align="center"> <b>D</b>echained <b>R</b>eady-to-play <b>ink!</b> playground </p>
<div align="center">
<h1>Pop DRink!</h1>

> [!IMPORTANT]
> This repository is customized and maintained for Pop API contract end-to-end testing.
>
> Quasi tests must be run with `cargo test --release`.
<a href="r0gue.io"><img width="100px" style="border-radius:10px;" src="https://github.com/user-attachments/assets/96830651-c3db-412a-9cb4-6fcd8ea6231b" alt="R0GUE Logo" /></a>

# Pop Sandbox
[![Twitter URL](https://img.shields.io/twitter/follow/Pop?style=social)](https://x.com/onpopio/)
[![Twitter URL](https://img.shields.io/twitter/follow/R0GUE?style=social)](https://twitter.com/gor0gue)
[![Telegram](https://img.shields.io/badge/Telegram-gray?logo=telegram)](https://t.me/onpopio)

Implementation of the [`pop_drink::Sandbox`](https://github.com/r0gue-io/pop-drink) struct for the Pop Network runtimes (located in `pop-node/runtime`) required for the quasi testing with `drink`.
Forked version of [inkdevhub/drink](https://github.com/inkdevhub/drink) for E2E testing of smart contract using the [Pop API](https://github.com/r0gue-io/pop-node/tree/main/pop-api) on Pop Network.

In the context of quasi-testing with pop-drink, a sandbox refers to an isolated runtime environment that simulates the behavior of a full node, without requiring an actual node. It can emulate key processes (where runtime `pallets` are involved) such as block initialization, execution, and block finalization.
</div>

## Overview

About the repository folder structure:

- [pop-drink](/crates/pop-drink): Library for testing contracts deployed on Pop Network using drink.
- [drink](/crates/drink/drink): drink is a toolbox for ink! developers to test contracts in a sandbox environment.
- [ink-sandbox](/crates/ink-sandbox): Creates a sandbox environment for a given runtime, without having to run a node.
- [examples](/crates/drink/examples): A collection of example contracts tested with drink.
- [drink-cli](/crates/drink/drink-cli): Simple command line tool to help you play with your local contracts in a convenient way.

## Getting Started

### Installation
Add `pop-drink` crate to your contract `Cargo.toml`:

```toml
pop_drink = { version = "1.0.0", package = "pop-drink" }
drink = { version = "1.0.0", package = "pop-drink" }
```

### Import Sandbox for the specific runtime

- For `devnet` runtime

Implementation of the sandbox runtime environment for `devnet` runtime located in `pop-node/runtime/devnet`
Set up your pop-drink test environment and write your first test.

```rs
use pop_sandbox::DevnetSandbox;
```
use drink::{
devnet::{AccountId, Balance, Runtime},
TestExternalities,
deploy, call,
};

- For `testnet` runtime

Implementation of the sandbox runtime environment for `testnet` runtime located in `pop-node/runtime/testnet`
// Builds your contract(s).
#[drink::contract_bundle_provider]
enum BundleProvider {}

```rs
use pop_sandbox::TestnetSandbox;
```
/// Sandbox environment for Pop Devnet Runtime.
pub struct Pop {
ext: TestExternalities,
}

- For `mainnet` runtime
// Initialising genesis state.
impl Default for Pop {
fn default() -> Self {
let balances: Vec<(AccountId, Balance)> = vec![(ALICE, 100u128)];
let ext = BlockBuilder::<Runtime>::new_ext(balances);
Self { ext }
}
}

Implementation of the sandbox runtime environment for `mainnet` runtime located in `pop-node/runtime/mainnet`
// Implement core functionalities for the `Pop` sandbox.
drink::impl_sandbox!(Pop, Runtime, ALICE);

```rs
use pop_sandbox::MainnetSandbox;
// Write your first pop-drink test!
#[drink::test(sandbox = Pop)]
fn test(mut session: Session) { ... }
```

### Setup test environment for your contract
Important: run your tests with `--release`
```
cargo test --release
```

Below is an example for the contract testing with `pop_drink` and `pop_sandbox` for `devnet` environment using `DevnetSandbox`.
## Learn more

```rs
use pop_drink::session::Session;
use pop_sandbox::DevnetSandbox as Sandbox;
Dive into the ["quick start with drink!"](/crates/drink/examples/quick-start-with-drink/README.md) to learn more about drink. Also check out the other examples provided by drink.

#[drink::contract_bundle_provider]
enum BundleProvider {}
If you are interested in learning more about testing contracts using the Pop API, check out the [example contracts](https://github.com/r0gue-io/pop-node/tree/main/pop-api/examples) tested with pop-drink!

#[drink::test(sandbox = Sandbox)]
fn test(mut session: Session) {
// Your test case
}
```
## Support

Be part of our passionate community of Web3 builders. [Join our Telegram](https://t.me/onpopio)!

## Examples
Feel free to contribute to `drink` or `pop-drink` to help improve testing ink! smart contracts!

Please find more examples of `pop_drink` tests in the [`pop_drink/examples`](https://github.com/r0gue-io/pop-drink/tree/main/examples).
For any questions related to ink! you can also go to [Polkadot Stack Exchange](https://polkadot.stackexchange.com/) or ask the [ink! community](https://t.me/inkathon/1).
8 changes: 4 additions & 4 deletions crates/drink/drink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub use drink_test_macro::{contract_bundle_provider, test};
pub use errors::Error;
pub use frame_support;
pub use ink_sandbox::{
self, api as sandbox_api, create_sandbox, impl_sandbox,
pallet_balances, pallet_contracts, pallet_timestamp, sp_externalities, AccountId32,
DispatchError, Sandbox, Ss58Codec, Weight,
self, api as sandbox_api, create_sandbox, impl_sandbox, pallet_assets, pallet_balances,
pallet_contracts, pallet_timestamp, sp_externalities, AccountId32, DispatchError, Sandbox,
Ss58Codec, Weight,
};
#[cfg(feature = "session")]
pub use session::mock::{mock_message, ContractMock, MessageMock, MockedCallResult, Selector};
Expand All @@ -28,4 +28,4 @@ pub mod minimal {

// create_sandbox!(MinimalSandbox);
create_sandbox!(MinimalSandbox, (), crate::pallet_contracts_debugging::DrinkDebug);
}
}
8 changes: 7 additions & 1 deletion crates/pop-drink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ edition = "2021"
[dependencies]
drink.workspace = true
ink_sandbox.workspace = true
pop-runtime-devnet.workspace = true
pallet-contracts.workspace = true
pop-runtime-devnet.workspace = true
frame-system.workspace = true
frame-support.workspace = true
sp-io.workspace = true
scale.workspace = true
pop-api.workspace = true

[dev-dependencies]
scale-info = { workspace = true, features = ["derive"] }
pallet-assets.workspace = true
pallet-balances.workspace = true
pallet-timestamp.workspace = true
Loading

0 comments on commit a66205f

Please sign in to comment.