// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IBRC20Factory {
function parameters() external view returns (string memory name, string memory symbol, uint8 decimals);
}
contract BRC20 {
string public name;
string public symbol;
uint8 public immutable decimals;
address public immutable factory;
uint256 public totalSupply;
mapping (address => uint256) public balanceOf;
mapping (address => mapping(address => uint256)) public allowance;
mapping (address => uint256) public nonces;
bytes32 public DOMAIN_SEPARATOR;
bytes32 public constant PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
bytes32 public constant DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
event Approval(address indexed owner, address indexed spender, uint256 value);
event Transfer(address indexed from, address indexed to, uint256 value);
constructor() {
(name, symbol, decimals) = IBRC20Factory(msg.sender).parameters();
factory = msg.sender;
uint256 chainId;
assembly {
chainId := chainid()
}
DOMAIN_SEPARATOR = keccak256(abi.encode(DOMAIN_TYPEHASH, keccak256(bytes(name)), keccak256(bytes('1')), chainId, address(this)));
}
function mint(address to, uint256 amount) external {
require(msg.sender == factory, "unauthorized");
_mint(to, amount);
}
function burn(uint256 amount) external {
require(msg.sender == factory, "unauthorized");
_burn(msg.sender, amount);
}
function approve(address spender, uint256 amount) external returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transfer(address to, uint256 amount) external returns (bool) {
balanceOf[msg.sender] -= amount;
unchecked {
balanceOf[to] += amount;
}
emit Transfer(msg.sender, to, amount);
return true;
}
function transferFrom(address from, address to, uint256 amount) external returns (bool) {
uint256 allowed = allowance[from][msg.sender];
if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;
balanceOf[from] -= amount;
unchecked {
balanceOf[to] += amount;
}
emit Transfer(from, to, amount);
return true;
}
function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external {
require(deadline >= block.timestamp, 'EXPIRED');
unchecked {
bytes32 digest = keccak256(
abi.encodePacked(
'\x19\x01',
DOMAIN_SEPARATOR,
keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline))
)
);
address recoveredAddress = ecrecover(digest, v, r, s);
require(recoveredAddress != address(0) && recoveredAddress == owner, 'INVALID_SIGNATURE');
allowance[recoveredAddress][spender] = value;
}
emit Approval(owner, spender, value);
}
function _mint(address to, uint256 amount) internal {
totalSupply += amount;
unchecked {
balanceOf[to] += amount;
}
emit Transfer(address(0), to, amount);
}
function _burn(address from, uint256 amount) internal {
balanceOf[from] -= amount;
unchecked {
totalSupply -= amount;
}
emit Transfer(from, address(0), amount);
}
}
contract BRC20Factory {
bytes32 private constant DOMAIN_NAME = keccak256("MultiBit");
bytes32 public constant DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
bytes32 public constant MINT_TYPEHASH = keccak256(abi.encodePacked("Mint(address token,address to,uint256 amount,string txid)"));
bytes32 public DOMAIN_SEPARATOR;
struct Parameters {
string name;
string symbol;
uint8 decimals;
}
bool private entered;
Parameters public parameters;
address public owner;
uint256 public fee;
address[] public signers;
mapping (address => bool) public authorized;
mapping (address => uint256) public indexes;
mapping (bytes32 => bool) public used;
event OwnerChanged(address indexed oldOwner, address indexed newOwner);
event FeeChanged(uint256 indexed oldFee, uint256 indexed newFee);
event BRC20Created(address indexed sender, address indexed dog20);
event Minted(address indexed token, address indexed to, uint256 indexed amount, string txid);
event Burned(address indexed token, address indexed from, uint256 indexed amount, uint256 fee, string receiver);
event SignerAdded(address indexed sender, address indexed account);
event SignerRemoved(address indexed sender, address indexed account);
modifier nonReentrant() {
require(!entered, "REENTRANT");
entered = true;
_;
entered = false;
}
modifier onlyOwner() {
require(msg.sender == owner, "UNAUTHORIZED");
_;
}
constructor(address[] memory _signers) {
for (uint256 i = 0; i < _signers.length; i++) {
address _addr = _signers[i];
signers.push(_addr);
authorized[_addr] = true;
indexes[_addr] = i;
}
owner = msg.sender;
emit OwnerChanged(address(0), msg.sender);
fee = 0.01 ether;
uint256 chainId;
assembly {
chainId := chainid()
}
DOMAIN_SEPARATOR = keccak256(abi.encode(DOMAIN_TYPEHASH, DOMAIN_NAME, keccak256(bytes('1')), chainId, address(this)));
}
function createBRC20(string memory name, string memory symbol, uint8 decimals) external onlyOwner returns (address brc20) {
parameters = Parameters({name: name, symbol: symbol, decimals: decimals});
brc20 = address(new BRC20{salt: keccak256(abi.encode(name, symbol, decimals))}());
delete parameters;
emit BRC20Created(msg.sender, brc20);
}
function mint(address token, address to, uint256 amount, string memory txid, uint8[] memory v, bytes32[] memory r, bytes32[] memory s) external nonReentrant {
require(v.length == signers.length, "invalid signatures");
bytes32 digest = buildMintSeparator(token, to, amount, txid);
require(!used[digest], "reuse");
used[digest] = true;
address[] memory signatures = new address[](v.length);
for (uint256 i = 0; i < v.length; i++) {
address signer = ecrecover(digest, v[i], r[i], s[i]);
require(authorized[signer], "invalid signer");
for (uint256 j = 0; j < i; j++) {
require(signatures[j] != signer, "duplicated");
}
signatures[i] = signer;
}
BRC20(token).mint(to, amount);
emit Minted(token, to, amount, txid);
}
function burn(address token, uint256 amount, string memory receiver) external payable nonReentrant {
require(msg.value >= fee, "invalid ether");
BRC20(token).transferFrom(msg.sender, address(this), amount);
BRC20(token).burn(amount);
emit Burned(token, msg.sender, amount, fee, receiver);
}
function withdraw(address to) external onlyOwner {
uint256 balance = address(this).balance;
payable(to).transfer(balance);
}
function setOwner(address _owner) external onlyOwner {
emit OwnerChanged(owner, _owner);
owner = _owner;
}
function setFee(uint256 _fee) external onlyOwner {
emit FeeChanged(fee, _fee);
fee = _fee;
}
function addSigner(address account) external onlyOwner {
require(!authorized[account], "already exists");
indexes[account] = signers.length;
authorized[account] = true;
signers.push(account);
emit SignerAdded(msg.sender, account);
}
function removeSigner(address account) external onlyOwner {
require(authorized[account], "non-existent");
require(indexes[account] < signers.length, "index out of range");
uint256 index = indexes[account];
uint256 lastIndex = signers.length - 1;
if (index != lastIndex) {
address lastAddr = signers[lastIndex];
signers[index] = lastAddr;
indexes[lastAddr] = index;
}
delete authorized[account];
delete indexes[account];
signers.pop();
emit SignerRemoved(msg.sender, account);
}
function buildMintSeparator(address token, address to, uint256 amount, string memory txid) view public returns (bytes32) {
return keccak256(abi.encodePacked(
'\x19\x01',
DOMAIN_SEPARATOR,
keccak256(abi.encode(MINT_TYPEHASH, token, to, amount, keccak256(bytes(txid))))
));
}
}
{
"compilationTarget": {
"BRC20Factory.sol": "BRC20Factory"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address[]","name":"_signers","type":"address[]"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"dog20","type":"address"}],"name":"BRC20Created","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fee","type":"uint256"},{"indexed":false,"internalType":"string","name":"receiver","type":"string"}],"name":"Burned","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"oldFee","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"newFee","type":"uint256"}],"name":"FeeChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"string","name":"txid","type":"string"}],"name":"Minted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"oldOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnerChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"account","type":"address"}],"name":"SignerAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"account","type":"address"}],"name":"SignerRemoved","type":"event"},{"inputs":[],"name":"DOMAIN_SEPARATOR","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"DOMAIN_TYPEHASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"MINT_TYPEHASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"addSigner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"authorized","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"string","name":"txid","type":"string"}],"name":"buildMintSeparator","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"string","name":"receiver","type":"string"}],"name":"burn","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"internalType":"uint8","name":"decimals","type":"uint8"}],"name":"createBRC20","outputs":[{"internalType":"address","name":"brc20","type":"address"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"fee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"indexes","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"string","name":"txid","type":"string"},{"internalType":"uint8[]","name":"v","type":"uint8[]"},{"internalType":"bytes32[]","name":"r","type":"bytes32[]"},{"internalType":"bytes32[]","name":"s","type":"bytes32[]"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"parameters","outputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"internalType":"uint8","name":"decimals","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"removeSigner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_fee","type":"uint256"}],"name":"setFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_owner","type":"address"}],"name":"setOwner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"signers","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"used","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]