Created
August 11, 2021 11:43
-
-
Save mwaqasaslam/1267e4237515931a91f1fa162fc1840d 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.0+commit.c7dfd78e.js&optimize=false&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// contracts/GameItems.sol | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
// import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol"; | |
contract DinisiumToken is ERC1155 { | |
uint256 public constant GOLD = 11; | |
uint256 public constant SILVER = 111; | |
uint256 public constant DOLLAR = 222; | |
address immutable public Dead = 0x9AC636dCFd8E8c5B6157c8fb69DeD48B84Df8E07; | |
// Mapping from token ID to account balances | |
mapping(uint256 => mapping(address => uint256)) private balances; | |
event burned(address indexed from,address indexed _to, uint256 _id, uint256 _value); | |
event minted(address indexed from,address indexed _to, uint256 _id, uint256 _value); | |
constructor() ERC1155("https://game.example/api/item/{id}.json") { | |
mint(msg.sender, GOLD, 10**5, ""); | |
mint(msg.sender, SILVER, 10**6, ""); | |
mint(msg.sender, DOLLAR, 10**9, ""); | |
} | |
function increaseSupply (uint256 id, uint256 amount) external { | |
require (msg.sender != address(0), "Address is zero"); | |
mint (msg.sender, id, amount, ""); | |
emit minted(address(0), msg.sender, id, amount); | |
} | |
function decreaseSupply (uint256 id, uint256 amount) external { | |
require (msg.sender != address(0), "Address is zero"); | |
burn(msg.sender, id, amount); | |
emit burned(msg.sender, Dead, id, amount); | |
} | |
function mint(address account, uint256 id, uint256 amount, bytes memory data) internal virtual { | |
require(account != address(0), "ERC1155: mint to the zero address"); | |
address operator = _msgSender(); | |
beforeTokenTransfer(operator, address(0), account, asSingletonArray(id), asSingletonArray(amount), data); | |
balances[id][account] += amount; | |
emit TransferSingle(operator, address(0), account, id, amount); | |
} | |
function burn(address account, uint256 id, uint256 amount) internal virtual { | |
require(account != address(0), "ERC1155: burn from the zero address"); | |
address operator = _msgSender(); | |
beforeTokenTransfer(operator, account, Dead, asSingletonArray(id), asSingletonArray(amount), ""); | |
uint256 accountBalance = balances[id][account]; | |
require(accountBalance >= amount, "ERC1155: burn amount exceeds balance"); | |
unchecked { | |
balances[id][account] = accountBalance - amount; | |
} | |
emit TransferSingle(operator, account, Dead, id, amount); | |
} | |
function beforeTokenTransfer( | |
address operator, | |
address from, | |
address to, | |
uint256[] memory ids, | |
uint256[] memory amounts, | |
bytes memory data | |
) internal virtual | |
{ | |
} | |
function asSingletonArray(uint256 element) private pure returns (uint256[] memory) { | |
uint256[] memory array = new uint256[](1); | |
array[0] = element; | |
return array; | |
} | |
function balanceOf(address account, uint256 id) public view virtual override returns (uint256) { | |
require(account != address(0), "ERC1155: balance query for the zero address"); | |
return balances[id][account]; | |
} | |
} | |
// address to send ; 0xed9d02e382b34818e88b88a309c7fe71e65f419d |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment