Skip to content

Commit f0d65b5

Browse files
committed
standards: add DRC20 primitive and reference
1 parent fe780d3 commit f0d65b5

15 files changed

Lines changed: 1510 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
STANDARDS_EXAMPLES := standards/examples/multisig_controller
1+
STANDARDS_EXAMPLES := standards/examples/drc20_roles_pausable standards/examples/multisig_controller
22
STANDARDS_WASM_CONTRACTS := $(STANDARDS_EXAMPLES)
33
LEGACY_SUBDIRS := tests/alice tests/bob tests/charlie genesis/transfer genesis/stake tests/host_fn
44
STANDARDS_PROPTEST_CASES ?= 8192

standards/Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

standards/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
members = [
33
"dusk-contract-standards",
4+
"examples/drc20_roles_pausable",
45
"examples/multisig_controller",
56
]
67

standards/dusk-contract-standards/src/access/access_control.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ impl AccessControl {
8383

8484
/// Bootstraps the default admin role.
8585
pub fn init_admin(&mut self, admin: Principal) {
86+
self.init_admin_with_roles(admin, core::iter::empty::<Role>());
87+
}
88+
89+
/// Bootstraps the default admin role and seeds additional initial roles.
90+
///
91+
/// This is intended for contract initialization only. It keeps initial role
92+
/// seeding inside the same one-time initialization gate as the default
93+
/// admin, while regular role changes still require a fresh authorization
94+
/// witness.
95+
pub fn init_admin_with_roles(
96+
&mut self,
97+
admin: Principal,
98+
roles: impl IntoIterator<Item = Role>,
99+
) {
86100
if admin.is_zero() {
87101
panic!("{}", error::ZERO_PRINCIPAL);
88102
}
@@ -94,6 +108,13 @@ impl AccessControl {
94108
.or_insert_with(|| RoleData::new(DEFAULT_ADMIN_ROLE))
95109
.members
96110
.insert(admin);
111+
for role in roles {
112+
self.roles
113+
.entry(role)
114+
.or_insert_with(|| RoleData::new(DEFAULT_ADMIN_ROLE))
115+
.members
116+
.insert(admin);
117+
}
97118
}
98119

99120
/// Returns true when `account` has `role`.

standards/dusk-contract-standards/src/core/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ pub const INVALID_OPERATION: &str = "DuskStandards: invalid operation";
1818
pub const EXPIRED: &str = "DuskStandards: expired";
1919
pub const REENTRANCY: &str = "DuskStandards: reentrant call";
2020
pub const OVERFLOW: &str = "DuskStandards: arithmetic overflow";
21+
pub const UNDERFLOW: &str = "DuskStandards: arithmetic underflow";

standards/dusk-contract-standards/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ pub mod auth;
3030
pub mod core;
3131
pub mod governance;
3232
pub mod security;
33+
pub mod token;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
//
5+
// Copyright (c) DUSK NETWORK. All rights reserved.
6+
7+
//! DRC20 event payloads.
8+
9+
use bytecheck::CheckBytes;
10+
use rkyv::{Archive, Deserialize, Serialize};
11+
12+
use crate::core::Principal;
13+
14+
/// Transfer event topic.
15+
pub const TRANSFER_TOPIC: &str = "drc20/transfer";
16+
/// Approval event topic.
17+
pub const APPROVAL_TOPIC: &str = "drc20/approval";
18+
19+
/// Transfer event.
20+
#[derive(
21+
Archive, Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq,
22+
)]
23+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24+
#[archive_attr(derive(CheckBytes))]
25+
pub struct Transfer {
26+
/// Sender.
27+
pub from: Principal,
28+
/// Recipient.
29+
pub to: Principal,
30+
/// Amount.
31+
pub amount: u64,
32+
}
33+
34+
/// Approval event.
35+
#[derive(
36+
Archive, Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq,
37+
)]
38+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
39+
#[archive_attr(derive(CheckBytes))]
40+
pub struct Approval {
41+
/// Owner.
42+
pub owner: Principal,
43+
/// Spender.
44+
pub spender: Principal,
45+
/// Amount.
46+
pub amount: u64,
47+
}
48+
49+
#[cfg(feature = "forge")]
50+
impl dusk_forge::ContractEvent for Transfer {
51+
const TOPICS: &'static [&'static str] = &[TRANSFER_TOPIC];
52+
}
53+
54+
#[cfg(feature = "forge")]
55+
impl dusk_forge::ContractEvent for Approval {
56+
const TOPICS: &'static [&'static str] = &[APPROVAL_TOPIC];
57+
}

0 commit comments

Comments
 (0)