This pallet implements a basic protocol for decentralized betting.
Every account can create a match where rest of the users can bet. To create a match it is needed to place a deposit that will be returned after distribute the prizes.
The number of bets a user can create now are limited to 1.
Everyone can bet in the match, for a basic result: victory team 1, draw or victory team 2.
When the match is over a user with SUDO priviliges can set the result of the match.
A diagram:
:warning: It is not a production-ready pallet, but a sample built for learning purposes. It is discouraged to use this code 'as-is' in a production runtime.
RuntimeEvent
– The overarching event type.Currency
– The currency type.WeightInfo
– Information on runtime weights.PalletId
– Pallet ID. Used for account derivation.MaxTeamNameLength
– Maximum length for team names.MatchDeposit
- Deposite needed to create a matchMaxBetsPerMatch
– Maximum number of bets per match.pallet-betting
to a node:information_source: The pallet is compatible with Substrate version polkadot-v0.9.35.
:information_source: This section is based on
Substrate node template.
Integrating pallet-betting
with another node might look slightly different.
Cargo.toml
Add pallet-betting
, and the RPC runtime API, to dependencies.
[dependencies.pallet-betting]
version = "0.0.3"
default-features = false
git = "https://github.com/AlexD10S/substrate-betting.git"
branch = "main"
[dependencies.pallet-betting-rpc-runtime-api]
version = "0.0.1"
default-features = false
git = "https://github.com/AlexD10S/substrate-betting.git"
branch = "main"
Update the runtime's std
feature:
std = [
# --snip--
"pallet-betting/std",
"pallet-betting-rpc-runtime-api/std",
# --snip--
]
Cargo.toml
Add pallet-betting-rpc
to dependencies.
[dependencies.pallet-betting-rpc]
version = "0.0.1"
default-features = false
git = "https://github.com/AlexD10S/substrate-betting.git"
branch = "main"
lib.rs
Configure the betting pallet.
parameter_types! {
pub const MatchDeposit: u64 = 10;
pub const BettingPalletId: PalletId = PalletId(*b"py/betts");
}
impl pallet_betting::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type PalletId = BettingPalletId;
type Currency = Balances;
type MaxTeamNameLength = ConstU32<64>;
type MaxBetsPerMatch = ConstU32<10>;
type MatchDeposit = MatchDeposit;
type WeightInfo = pallet_betting::weights::SubstrateWeight<Runtime>;
}
Add configured pallets to the construct_runtime
macro call.
construct_runtime!(
pub enum Runtime where
// --snip--
{
// --snip---
Betting: pallet_betting,
// --snip---
}
);
Add the RPC implementation.
pub type TeamName = BoundedVec<u8, ConstU32<64>>;
pub type Bet = pallet_betting::Bet<AccountId, pallet_betting::MatchResult, Balance>;
pub type Match = pallet_betting::Match<BlockNumber, TeamName, BoundedVec<Bet, ConstU32<10>>, Balance>;
impl_runtime_apis! {
impl pallet_betting_rpc_runtime_api::BettingApi<Block, AccountId, Match> for Runtime {
fn get_match(match_id: AccountId) -> pallet_betting_rpc_runtime_api::RpcResult<Match>
{
Betting::get_match(match_id)
}
}
rpc.rs
Instantiate the RPC extension and merge it into the RPC module.
pub fn create_full<C, P>(
deps: FullDeps<C, P>,
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
where
// --snip--
C::Api: pallet_betting_rpc::BettingRuntimeApi<Block, Balance>,
{
use pallet_betting_rpc::{Betting, BettingApiServer};
// --snip--
module.merge(Betting::new(client).into_rpc())?;
Ok(module)
}