我有自定义的ERC20和ERC721协议,现在我的目标是发布ERC20令牌,但是有一个函数只接受我的ERC721契约地址为msg.sender
来创建令牌并将其发送到它的地址,我还需要ERC721契约中的ERC20令牌地址来与它进行事务处理,目前我有这样的功能
customERC20Token = new CustomERC20Token(address(this));
在我的ERC721合同的构造函数中
在我的ERC20契约中,我初始化了ERC721令牌地址,如下所示
constructor(address erc721Address) ERC20("CUSTOM ERC 20 TOKEN", "CustomToken") {
globalERC721Address = erc721Address;
}
我在修饰符中使用此globalERC721Address
将函数可访问性限制为ERC721地址。
如果我错了,请纠正我,但是我相信每一个将在代码中继承这个契约并用他们想要的地址初始化的人,都可以作为一个“所有者”,并为他们想要的任何地址造币,我该如何修复它呢?
我想在我的ERC20中添加一个函数,它允许我手动初始化ERC 721协议地址,而不是在构造函数中进行初始化,然后部署ERC 20,复制它的地址,粘贴到我的ERC721构造函数中,并部署ERC721契约。但看上去有点烦人。难道没有更好的方法吗?
发布于 2022-11-25 04:48:18
简而言之,您希望“只允许”特定的合同调用特定的函数?如果是..。然后:
现在是实现openzeppelin中的访问控制契约的时候了;使用它,您可以创建一个新的角色,并将函数调用限制在指定了该角色的地址上。
ie:
// SPDX-License-Identifier: MIT
// Creator: @casareafer
// Proof of Wololo
pragma solidity ^0.8.17;
import "./openzeppelin/ERC1155.sol";
import "./openzeppelin/AccessControl.sol";
contract Wololo is ERC1155, AccessControl {
address public yourContract;
bytes32 public constant adminControl = keccak256("Margaret_Hamilton");
bytes32 public constant withdrawControl = keccak256("Marie_Curie");
constructor(string memory _baseURI, string memory _contractURI) {
//DEFAULT_ADMIN_ROLE is the Super Role that allow you to grant roles
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
//this is my special role
_grantRole(adminControl, msg.sender);
}
//give the role to your contract address to be able to call the function
function setYourMagicContract(address foo) external onlyRole(DEFAULT_ADMIN_ROLE){
_grantRole(withdrawControl,foo);
}
//burn control only callable by admins
function burn(
address from,
uint256 id,
uint256 amount
) external onlyRole(adminControl) {
_burn(from, id, amount);
}
//withdraw function only callable by withdrawControl dudes
function withdraw() external onlyRole(withdrawControl) {
payable(yourContract).transfer(address(this).balance);
}
}
https://ethereum.stackexchange.com/questions/139993
复制相似问题