[英]ERC20 token minting with AccessControl
我創建了一個無限供應的 ERC20 代幣,具有鑄造和銷毀功能。 對於訪問控制,我遵循了 OpenZeppelin AccessControl Documentation 。
如何將 MINTER_ROLE 和 BURNER_ROLE 設置為有資格在 ReactJs 項目中執行此操作的用戶。 可能嗎?
或者我可以檢查誰有資格鑄幣的任何其他方式?
我正在嘗試在他們的網站上創建與 SLP 令牌和 VIS 令牌相同的鑄造功能。因此鑄造資格取決於某些條件。 智能合約不是問題,因為文檔寫得很好。 只是不知道如何使用 reactJs 設置角色。
合同樣本如下。 只有 ADMIN_ROLE 可以設置或撤銷 MINTER/BURNER 角色。 現在,在您的 ReactJs 應用程序中,您需要使用web3js或ethersjs庫通過發送setupMinter
和setupBurner
交易與部署的 MyToken 合約進行交互。 這些交易必須由該帳戶使用 ADMIN_ROLE 發送,在我們的例子中是合約部署者。 您可以針對不同的邏輯更新setupMinter
和setupBurner
函數,但主要思想必須清楚。
contract MyToken is ERC20, AccessControl {
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant BURNER_ROLE = keccak256("BURNER_ROLE");
constructor() ERC20("MyToken", "TKN") {
_setupRole(ADMIN_ROLE, msg.sender);
}
function mint(address to, uint256 amount) public onlyRole(MINTER_ROLE) {
_mint(to, amount);
}
function burn(address from, uint256 amount) public onlyRole(BURNER_ROLE) {
_burn(from, amount);
}
/// @dev Grants or revokes MINTER_ROLE
function setupMinter(address minter, bool enabled) external onlyRole(ADMIN_ROLE) {
require(minter != address(0), "!minter");
if (enabled) _setupRole(MINTER_ROLE, minter);
else _revokeRole(MINTER_ROLE, minter);
}
/// @dev Grants or revokes BURNER_ROLE
function setupBurner(address burner, bool enabled) external onlyRole(ADMIN_ROLE) {
require(burner != address(0), "!burner");
if (enabled) _setupRole(BURNER_ROLE, burner);
else _revokeRole(BURNER_ROLE, burner);
}
/// @dev Returns true if MINTER
function isMinter(address minter) external view returns(bool) {
return hasRole(MINTER_ROLE, minter);
}
/// @dev Returns true if BURNER
function isBurner(address burner) external view returns(bool) {
return hasRole(BURNER_ROLE, burner);
}
}
https://docs.openzeppelin.com/contracts/4.x/access-control#granting-and-revoking
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.