Rust子赏金系统pallet-child-bounties的使用:实现多级分发的去中心化赏金管理
Rust子赏金系统pallet-child-bounties的使用:实现多级分发的去中心化赏金管理
Child Bounties Pallet (pallet-child-bounties
)
Child Bounty
注意:此pallet与
pallet-treasury
和pallet-bounties
紧密耦合。
通过子赏金系统,可以将大型赏金提案划分为更小的部分,以便并行执行,并高效管理和跟踪资金使用情况。子赏金是从父赏金中提取的较小工作任务。在父赏金策展人创建子赏金后,会指定一个策展人,该策展人将被委托在指定任务完成后分配支付地址的责任。
接口
可调度函数
子赏金协议:
add_child_bounty
- 为父赏金添加子赏金,将工作划分为更小的任务propose_curator
- 为子赏金分配一个账户作为候选策展人accept_curator
- 从父赏金策展人处接受子赏金分配,设置策展人押金award_child_bounty
- 关闭并为完成的工作支付指定金额claim_child_bounty
- 从支付地址领取特定的子赏金金额unassign_curator
- 从特定子赏金中解除已接受的策展人close_child_bounty
- 取消特定国库金额的子赏金并关闭赏金
完整示例
以下是使用pallet-child-bounties
的完整示例代码:
use frame_support::{decl_module, decl_storage, decl_event, dispatch};
use frame_system::{self as system, ensure_signed};
use sp_runtime::traits::{AccountIdConversion, Saturating};
pub trait Trait: system::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
decl_storage! {
trait Store for Module<T: Trait> as ChildBounties {
// 存储父赏金和子赏金的映射关系
ParentChildBounties get(fn parent_child_bounties): map hasher(blake2_128_concat) T::BountyIndex => Vec<T::ChildBountyIndex>;
// 存储子赏金详细信息
ChildBounties get(fn child_bounties): map hasher(blake2_128_concat) T::ChildBountyIndex => Option<ChildBounty<T::AccountId, BalanceOf<T>>>;
}
}
decl_event! {
pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {
/// 子赏金已添加
ChildBountyAdded(BountyIndex, ChildBountyIndex),
/// 子赏金策展人已提议
CuratorProposed(BountyIndex, ChildBountyIndex, AccountId),
/// 子赏金策展人已接受
CuratorAccepted(BountyIndex, ChildBountyIndex, AccountId),
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event() = default;
/// 为父赏金添加子赏金
#[weight = 10_000]
pub fn add_child_bounty(
origin,
parent_bounty_id: T::BountyIndex,
description: Vec<u8>,
value: BalanceOf<T>,
) -> dispatch::DispatchResult {
let sender = ensure_signed(origin)?;
// 确保父赏金存在且处于活动状态
ensure!(Bounties::<T>::bounties(parent_bounty_id).is_some(), "Parent bounty does not exist");
// 生成新的子赏金ID
let child_bounty_id = Self::next_child_bounty_id();
// 创建子赏金
let child_bounty = ChildBounty {
parent_bounty_id,
description,
value,
curator: None,
fee: Zero::zero(),
status: ChildBountyStatus::Added,
};
// 存储子赏金
ChildBounties::<T>::insert(child_bounty_id, child_bounty);
// 更新父赏金的子赏金列表
ParentChildBounties::<T>::mutate(parent_bounty_id, |list| list.push(child_bounty_id));
Self::deposit_event(RawEvent::ChildBountyAdded(parent_bounty_id, child_bounty_id));
Ok(())
}
/// 提议子赏金策展人
#[weight = 10_000]
pub fn propose_curator(
origin,
parent_bounty_id: T::BountyIndex,
child_bounty_id: T::ChildBountyIndex,
curator: T::AccountId,
fee: BalanceOf<T>,
) -> dispatch::DispatchResult {
let sender = ensure_signed(origin)?;
// 确保子赏金存在
let mut child bounty = ChildBounties::<T>::get(child_bounty_id)
.ok_or("Child bounty does not exist")?;
// 确保发送者是父赏金的策展人
ensure!(
Bounties::<T>::bounties(parent_bounty_id)
.map(|b| b.curator == sender)
.unwrap_or(false),
"Only parent bounty curator can propose child bounty curator"
);
// 更新子赏金信息
child_bounty.curator = Some(curator.clone());
child_bounty.fee = fee;
child_bounty.status = ChildBountyStatus::CuratorProposed;
// 存储更新后的子赏金
ChildBounties::<T>::insert(child_bounty_id, child_bounty);
Self::deposit_event(RawEvent::CuratorProposed(parent_bounty_id, child_bounty_id, curator));
Ok(())
}
}
}
/// 子赏金状态
#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)]
pub enum ChildBountyStatus {
Added,
CuratorProposed,
Active,
PendingPayout,
Claimed,
}
/// 子赏金结构
#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)]
pub struct ChildBounty<AccountId, Balance> {
parent_bounty_id: BountyIndex,
description: Vec<u8>,
value: Balance,
curator: Option<AccountId>,
fee: Balance,
status: ChildBountyStatus,
}
这个示例展示了如何使用pallet-child-bounties
创建子赏金管理系统,包括:
- 添加子赏金到父赏金
- 为子赏金提议策展人
- 事件系统用于跟踪状态变化
实际使用时需要与pallet-treasury
和pallet-bounties
配合使用,以完成完整的赏金管理功能。
1 回复
Rust子赏金系统pallet-child-bounties的使用:实现多级分发的去中心化赏金管理
简介
pallet-child-bounties
是Substrate框架中的一个模块,用于在区块链上实现多级分发的去中心化赏金管理系统。它扩展了基本的赏金功能,允许将大型赏金任务分解为多个子任务,实现更精细的资金管理和任务分配。
主要功能
- 将主赏金分解为多个子赏金
- 独立管理子赏金的资金分配
- 跟踪每个子赏金的状态
- 支持多级任务分发
使用方法
1. 添加依赖
首先确保在你的runtime中引入了pallet-child-bounties
:
impl pallet_child_bounties::Config for Runtime {
type Event = Event;
type MaxActiveChildBountyCount = ConstU32<10>;
type ChildBountyValueMinimum = ConstU128<1000>;
// 其他配置...
}
2. 创建主赏金
// 创建主赏金
Bounties::propose_bounty(
RuntimeOrigin::signed(1),
500_000, // 赏金金额
b"My Big Project".to_vec()
);
3. 批准并资助主赏金
// 批准赏金
Bounties::approve_bounty(RuntimeOrigin::root(), 0);
// 资助赏金
Treasury::approve_proposal(RuntimeOrigin::root(), 0);
4. 添加子赏金
// 添加子赏金到主赏金0
ChildBounties::add_child_bounty(
RuntimeOrigin::signed(1),
0, // 主赏金ID
50_000, // 子赏金金额
b"Subtask 1: UI Development".to_vec()
);
5. 管理子赏金生命周期
// 批准子赏金
ChildBounties::award_child_bounty(
RuntimeOrigin::signed(1),
0, // 主赏金ID
0 // 子赏金ID
);
// 领取子赏金奖励
ChildBounties::claim_child_bounty(
RuntimeOrigin::signed(2),
0, // 主赏金ID
0 // 子赏金ID
);
完整示例
基于上述内容,这里提供一个更完整的示例demo:
// 1. 创建主赏金 - 一个去中心化交易平台项目
Bounties::propose_bounty(
RuntimeOrigin::signed(1), // 提案人账户
2_000_000, // 主赏金总金额
b"Decentralized Exchange Platform".to_vec() // 赏金描述
);
// 2. 批准并资助主赏金
Bounties::approve_bounty(RuntimeOrigin::root(), 0); // 根权限批准
Treasury::approve_proposal(RuntimeOrigin::root(), 0); // 国库资助
// 3. 添加4个子赏金任务
// 3.1 前端开发子任务
ChildBounties::add_child_bounty(
RuntimeOrigin::signed(1), // 管理员账户
0, // 主赏金ID
600_000, // 子赏金金额
b"Frontend: React Implementation".to_vec() // 子任务描述
);
// 3.2 智能合约开发子任务
ChildBounties::add_child_bounty(
RuntimeOrigin::signed(1),
0,
800_000,
b"Smart Contracts: Core Exchange Logic".to_vec()
);
// 3.3 后端开发子任务
ChildBounties::add_child_bounty(
RuntimeOrigin::signed(1),
0,
400_000,
b"Backend: API Services".to_vec()
);
// 3.4 安全审计子任务
ChildBounties::add_child_bounty(
RuntimeOrigin::signed(1),
0,
200_000,
b"Security Audit".to_vec()
);
// 4. 分配并领取第一个子赏金(前端开发)
// 4.1 批准前端开发子赏金给账户3
ChildBounties::award_child_bounty(
RuntimeOrigin::signed(1), // 管理员账户
0, // 主赏金ID
0 // 子赏金ID
);
// 4.2 账户3领取奖励
ChildBounties::claim_child_bounty(
RuntimeOrigin::signed(3), // 执行者账户
0, // 主赏金ID
0 // 子赏金ID
);
// 5. 分配并领取第二个子赏金(智能合约开发)
// 5.1 批准智能合约开发子赏金给账户4
ChildBounties::award_child_bounty(
RuntimeOrigin::signed(1),
0,
1
);
// 5.2 账户4领取奖励
ChildBounties::claim_child_bounty(
RuntimeOrigin::signed(4),
0,
1
);
注意事项
- 子赏金金额总和不能超过主赏金金额
- 主赏金必须处于活跃状态才能添加子赏金
- 子赏金有最小金额限制(由
ChildBountyValueMinimum
配置) - 系统会限制活跃子赏金的最大数量(由
MaxActiveChildBountyCount
配置)
通过pallet-child-bounties
,项目可以更灵活地管理大型任务的资金分配和进度跟踪,实现真正的去中心化协作。