Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: poc on substrate blockchain #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions substrate-blockchain/custom-token-substrate-pallet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "pallet-custom-token"
version = "4.0.0-dev"
edition = "2021"
license = "MIT-0"
publish = false

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = [
"derive",
] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-runtime = { version = "24.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

[dev-dependencies]
sp-core = { version = "21.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-io = { version = "23.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-runtime/std",
]
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
try-runtime = ["frame-support/try-runtime"]
41 changes: 41 additions & 0 deletions substrate-blockchain/custom-token-substrate-pallet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Custom Token Pallet for Substrate

The custom token pallet offered here facilitates the implementation of a custom token functionality within a Substrate runtime. It includes functionalities such as exchanging native currency for custom tokens, transferring tokens between accounts, and checking token balances.

### Features
- **Exchange Native to Custom Token**: Allows users to exchange native currency for custom tokens, ensuring sufficient balance checks.
- **Token Transfer**: Facilitates the transfer of custom tokens between accounts, managing balances accordingly.
- **Get Balance**: Enables users to retrieve their current token balance.

### Usage
To integrate this custom token pallet into your Substrate runtime, follow these steps:

1. Add the custom token pallet to your Substrate runtime Cargo.toml file:
```toml
pallet-custom-token = { version = "4.0.0-dev", default-features = false, path = "../pallets/template" }
```
2. In the Cargo.toml file, ensure that the custom token pallet is added as a dependency in the feature std by including the line:
```toml
[features]
default = ["std"]
std = [
# other dependencies
"pallet-custom-token/std"
]
```
3. Configure the `pallet_custom_token` pallet in the Substrate node runtime as follows:
```rust
impl pallet_custom_token::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
}
```
4. Deploy and run your updated Substrate runtime with the custom token pallet included to enable custom token using the following command
```bash
cargo build --release
```
5. Run the node using the following command
```bash
./target/release/node-template --dev
```
6. Use the [polkadot.org.js](https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944#/chainstate) for test the functionalities
128 changes: 128 additions & 0 deletions substrate-blockchain/custom-token-substrate-pallet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;

const TOKEN_VALUE: u32 = 2;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::traits::Currency;
use frame_system::pallet_prelude::*;

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type Currency: Currency<Self::AccountId>;
}

#[pallet::error]
pub enum Error<T> {
NoSufficientBalance,
NoneValue,
}

#[pallet::storage]
type BalanceStore<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, u32>;

// #[pallet::storage]
pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Balance {
amount: u32,
},
TokenTransferred {
from: T::AccountId,
to: T::AccountId,
amount: u32,
},
}

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(10_000)]
#[pallet::call_index(0)]
pub fn exchange_native_to_custom_token(
origin: OriginFor<T>,
amount: u32,
) -> DispatchResult {
let owner = ensure_signed(origin)?;

let total_fee: u32 = amount * TOKEN_VALUE;

if T::Currency::total_balance(&owner) < total_fee.into() {
return Err(Error::<T>::NoSufficientBalance.into());
}

T::Currency::burn(total_fee.into());
<BalanceStore<T>>::insert(&owner, amount);

Ok(())
}

#[pallet::weight(0)]
#[pallet::call_index(2)]
pub fn my_transfer(
origin: OriginFor<T>,
dest: T::AccountId,
amount: u32,
) -> DispatchResult {
let owner = ensure_signed(origin)?;

match <BalanceStore<T>>::get(&owner) {
Some(balance) => {
if balance < amount {
return Err(Error::<T>::NoSufficientBalance.into());
}
}
None => {
<BalanceStore<T>>::insert(owner.clone(), 0);
return Err(Error::<T>::NoSufficientBalance.into());
}
}

<BalanceStore<T>>::mutate(&owner, |balance| {
*balance = Some(balance.unwrap() - amount);
});

if !<BalanceStore<T>>::contains_key(dest.clone()) {
<BalanceStore<T>>::insert(dest.clone(), amount);
} else {
<BalanceStore<T>>::mutate(&dest, |balance| {
*balance = Some(balance.unwrap() + amount);
});
}

Self::deposit_event(Event::TokenTransferred {
from: owner,
to: dest,
amount,
});

Ok(())
}

#[pallet::weight(0)]
#[pallet::call_index(3)]
pub fn get_balance(origin: OriginFor<T>) -> DispatchResult {
let owner = ensure_signed(origin)?;
let balance = <BalanceStore<T>>::get(&owner);

match balance {
Some(balance) => Self::deposit_event(Event::Balance { amount: balance }),
None => {
Self::deposit_event(Event::Balance { amount: 0 });
<BalanceStore<T>>::insert(owner, 0);
}
}
Ok(())
}
}
}
8 changes: 8 additions & 0 deletions substrate-blockchain/guessing-random-number-game/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]
members = [
"node",
"pallets/template",
"runtime",
]
[profile.release]
panic = "unwind"
23 changes: 23 additions & 0 deletions substrate-blockchain/guessing-random-number-game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Substrate Custom Pallet Readme

## Overview
This custom Substrate blockchain allows users to play a number guessing game. Sudo user(admin) can set a target number and then let the players make guesses to try and match the target number. The pallet defines storage items, dispatchable functions, events, and errors to support the guessing game functionality.

## Features
- Set a target number
- Check a guess the target number
- Remove the target number
- Emit events for setting a target, making a guess, and removing the target

## Usage
Follow these steps for build and run this chain

1. Clone the repo and build the chain using the following command
```bash
cargo build --release
```
2. Run the node using the following command
```bash
./target/release/node-template --dev
```
3. Use the [polkadot.org.js](https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944#/chainstate) for test the functionalities
80 changes: 80 additions & 0 deletions substrate-blockchain/guessing-random-number-game/node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[package]
name = "node-template"
version = "4.0.0-dev"
description = "A fresh FRAME-based Substrate node, ready for hacking."
authors = ["Substrate DevHub <https://github.com/substrate-developer-hub>"]
homepage = "https://substrate.io/"
edition = "2021"
license = "MIT-0"
publish = false
repository = "https://github.com/substrate-developer-hub/substrate-node-template/"
build = "build.rs"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[[bin]]
name = "node-template"

[dependencies]
clap = { version = "4.4.2", features = ["derive"] }
futures = { version = "0.3.21", features = ["thread-pool"]}

sc-cli = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-core = { version = "21.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-executor = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-network = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-service = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-telemetry = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-transaction-pool = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-transaction-pool-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-offchain = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-statement-store = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-consensus-aura = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-consensus-aura = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-consensus = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-consensus-grandpa = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-consensus-grandpa = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-client-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-runtime = { version = "24.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-io = { version = "23.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-timestamp = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-inherents = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-keyring = { version = "24.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-system = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

# These dependencies are used for the node template's RPCs
jsonrpsee = { version = "0.16.2", features = ["server"] }
sp-api = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-rpc-api = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-blockchain = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sp-block-builder = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
sc-basic-authorship = { version = "0.10.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
substrate-frame-rpc-system = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
pallet-transaction-payment-rpc = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

# These dependencies are used for runtime benchmarking
frame-benchmarking = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }
frame-benchmarking-cli = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

# Local Dependencies
node-template-runtime = { version = "4.0.0-dev", path = "../runtime" }

# CLI-specific dependencies
try-runtime-cli = { version = "0.10.0-dev", optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

[build-dependencies]
substrate-build-script-utils = { version = "3.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v1.0.0" }

[features]
default = []
# Dependencies that are only required if runtime benchmarking should be build.
runtime-benchmarks = [
"node-template-runtime/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"frame-benchmarking-cli/runtime-benchmarks",
]
# Enable features that allow the runtime to be tried and debugged. Name might be subject to change
# in the near future.
try-runtime = ["node-template-runtime/try-runtime", "try-runtime-cli/try-runtime"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use substrate_build_script_utils::{generate_cargo_keys, rerun_if_git_head_changed};

fn main() {
generate_cargo_keys();

rerun_if_git_head_changed();
}
Loading