Skip to content

Instantly share code, notes, and snippets.

@KcPele
Created August 16, 2023 07:10
Show Gist options
  • Save KcPele/9c93b9554d95524ffabae59bc320898d to your computer and use it in GitHub Desktop.
Save KcPele/9c93b9554d95524ffabae59bc320898d to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.18+commit.87f61d96.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
error ERC1155__AddressZero();
error ERC1155__AccountAndIdNotSameLength();
error ERC1155__InsufficientBalance();
error ERC1155__ReceiverNotImplemented();
error ERC1155__MsgSenderIsNotOwner();
contract ERC1155{
event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);
event TrasnferSingle(address indexed _operator, address indexed _from, address indexed _to, uint256 _id, uint256 _amount);
event TrasnferBatch(address indexed _operator, address indexed _from, address indexed _to, uint256[] _ids, uint256[] _amounts);
mapping(uint256 => mapping(address => uint256)) internal _balances;
mapping(address => mapping(address => bool)) private _operatorApprovals;
modifier zeroAddress(address _add) {
if(_add == address(0)){
revert ERC1155__AddressZero();
}
_;
}
modifier checkOwner(address _from) {
if(_from != msg.sender || !this.isApprovedForAll(_from, msg.sender)){
revert ERC1155__MsgSenderIsNotOwner();
}
_;
}
function balanceOf(address _account, uint256 _id) public view zeroAddress(_account) returns(uint256) {
return _balances[_id][_account];
}
function balanceOfBatch(address[] memory _accounts, uint256[] memory _ids)external view returns(uint256[] memory) {
if(_accounts.length != _ids.length) {
revert ERC1155__AccountAndIdNotSameLength();
}
uint256[] memory batchBlances = new uint256[](_accounts.length);
for(uint256 i =0; i < _accounts.length;){
batchBlances[i] = balanceOf(_accounts[i], _ids[i]);
unchecked{
i++;
}
}
return batchBlances;
}
//checks if an address is an operator
function isApprovedForAll(address _account, address _operator) external view returns(bool) {
return _operatorApprovals[_account][_operator];
}
function setApprovalForAll( address _operator, bool _approved) external {
_operatorApprovals[msg.sender][_operator] = _approved;
emit ApprovalForAll(msg.sender, _operator, _approved);
}
function _trasnfer(address _from, address _to, uint256 _id, uint256 _amount) private{
uint256 _fromBalance = _balances[_id][_from];
if(_fromBalance < _amount){
revert ERC1155__InsufficientBalance();
}
_balances[_id][_from] = _fromBalance - _amount;
_balances[_id][_to] += _amount;
}
function safeTrasnferFrom(address _from, address _to, uint256 _id, uint256 _amount /*, ytes memory data */) external virtual checkOwner(_from) zeroAddress(_to) {
_trasnfer(_from, _to, _id, _amount);
emit TrasnferSingle(msg.sender, _from, _to, _id, _amount);
if(_checkOnERC1155Received()) {
revert ERC1155__ReceiverNotImplemented();
}
}
function _checkOnERC1155Received() private pure returns(bool){
return true;
}
function safeBatchTrasnferFrom(address _from, address _to, uint256[] memory _ids, uint256[] memory _amounts /*, bytes memory data */) external checkOwner(_from) zeroAddress(_to) {
if(_ids.length != _amounts.length) {
revert ERC1155__AccountAndIdNotSameLength();
}
for(uint256 i =0; i < _ids.length;){
uint256 _id = _ids[i];
uint256 _amount = _amounts[i];
_trasnfer(_from, _to, _id, _amount);
unchecked{
i++;
}
}
emit TrasnferBatch(msg.sender, _from, _to, _ids, _amounts);
if(_checkOnBatchERC1155Received()) {
revert ERC1155__ReceiverNotImplemented();
}
}
function _checkOnBatchERC1155Received() private pure returns(bool){
return true;
}
function supportInterface(bytes4 interfaceId)external pure virtual returns(bool) {
return interfaceId == 0xd9b67a26;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "./ERC1155.sol";
contract SuperMarioWorld is ERC1155{
string public name;
string public symbol;
uint256 public tokenCount;
mapping(uint256 => string) private _tokenURIs;
constructor(string memory _name, string memory _symbol) {
name = _name;
symbol = _symbol;
}
function uri(uint256 _tokenId) public view returns(string memory){
return _tokenURIs[_tokenId];
}
function mint(uint256 _amount, string memory _uri) external zeroAddress(msg.sender) {
tokenCount += 1;
_balances[tokenCount][msg.sender] += _amount;
_tokenURIs[tokenCount] = _uri;
emit TrasnferSingle(msg.sender, address(0), msg.sender, tokenCount, _amount);
}
function supportInterface(bytes4 interfaceId) external override pure returns(bool) {
return interfaceId == 0xd9b67a26 || interfaceId == 0xe089341c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment