├── README.md └── Contracts ├── BEP20.sol └── NFT.sol /README.md: -------------------------------------------------------------------------------- 1 | # BSC-Contracts -------------------------------------------------------------------------------- /Contracts/BEP20.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.23; 2 | 3 | // based on https://github.com/OpenZeppelin/openzeppelin-solidity/tree/v1.10.0 4 | /** 5 | * @title SafeMath 6 | * @dev Math operations with safety checks that throw on error 7 | */ 8 | library SafeMath { 9 | 10 | /** 11 | * @dev Multiplies two numbers, throws on overflow. 12 | */ 13 | function mul(uint256 a, uint256 b) internal pure returns (uint256 c) { 14 | if (a == 0) { 15 | return 0; 16 | } 17 | c = a * b; 18 | assert(c / a == b); 19 | return c; 20 | } 21 | 22 | /** 23 | * @dev Integer division of two numbers, truncating the quotient. 24 | */ 25 | function div(uint256 a, uint256 b) internal pure returns (uint256) { 26 | // assert(b > 0); // Solidity automatically throws when dividing by 0 27 | // uint256 c = a / b; 28 | // assert(a == b * c + a % b); // There is no case in which this doesn't hold 29 | return a / b; 30 | } 31 | 32 | /** 33 | * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). 34 | */ 35 | function sub(uint256 a, uint256 b) internal pure returns (uint256) { 36 | assert(b <= a); 37 | return a - b; 38 | } 39 | 40 | /** 41 | * @dev Adds two numbers, throws on overflow. 42 | */ 43 | function add(uint256 a, uint256 b) internal pure returns (uint256 c) { 44 | c = a + b; 45 | assert(c >= a); 46 | return c; 47 | } 48 | } 49 | 50 | /** 51 | * @title ERC20Basic 52 | * @dev Simpler version of ERC20 interface 53 | * @dev see https://github.com/ethereum/EIPs/issues/179 54 | */ 55 | contract ERC20Basic { 56 | function totalSupply() public view returns (uint256); 57 | function balanceOf(address who) public view returns (uint256); 58 | function transfer(address to, uint256 value) public returns (bool); 59 | event Transfer(address indexed from, address indexed to, uint256 value); 60 | } 61 | 62 | /** 63 | * @title Basic token 64 | * @dev Basic version of StandardToken, with no allowances. 65 | */ 66 | contract BasicToken is ERC20Basic { 67 | using SafeMath for uint256; 68 | 69 | mapping(address => uint256) balances; 70 | 71 | uint256 totalSupply_; 72 | 73 | /** 74 | * @dev total number of tokens in existence 75 | */ 76 | function totalSupply() public view returns (uint256) { 77 | return totalSupply_; 78 | } 79 | 80 | /** 81 | * @dev transfer token for a specified address 82 | * @param _to The address to transfer to. 83 | * @param _value The amount to be transferred. 84 | */ 85 | function transfer(address _to, uint256 _value) public returns (bool) { 86 | require(_to != address(0)); 87 | require(_value <= balances[msg.sender]); 88 | 89 | balances[msg.sender] = balances[msg.sender].sub(_value); 90 | balances[_to] = balances[_to].add(_value); 91 | emit Transfer(msg.sender, _to, _value); 92 | return true; 93 | } 94 | 95 | /** 96 | * @dev Gets the balance of the specified address. 97 | * @param _owner The address to query the the balance of. 98 | * @return An uint256 representing the amount owned by the passed address. 99 | */ 100 | function balanceOf(address _owner) public view returns (uint256) { 101 | return balances[_owner]; 102 | } 103 | } 104 | 105 | /** 106 | * @title ERC20 interface 107 | * @dev see https://github.com/ethereum/EIPs/issues/20 108 | */ 109 | contract ERC20 is ERC20Basic { 110 | function allowance(address owner, address spender) 111 | public view returns (uint256); 112 | 113 | function transferFrom(address from, address to, uint256 value) 114 | public returns (bool); 115 | 116 | function approve(address spender, uint256 value) public returns (bool); 117 | event Approval( 118 | address indexed owner, 119 | address indexed spender, 120 | uint256 value 121 | ); 122 | } 123 | 124 | /** 125 | * @title Standard ERC20 token 126 | * 127 | * @dev Implementation of the basic standard token. 128 | * @dev https://github.com/ethereum/EIPs/issues/20 129 | * @dev Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol 130 | */ 131 | contract StandardToken is ERC20, BasicToken { 132 | 133 | mapping (address => mapping (address => uint256)) internal allowed; 134 | 135 | /** 136 | * @dev Transfer tokens from one address to another 137 | * @param _from address The address which you want to send tokens from 138 | * @param _to address The address which you want to transfer to 139 | * @param _value uint256 the amount of tokens to be transferred 140 | */ 141 | function transferFrom( 142 | address _from, 143 | address _to, 144 | uint256 _value 145 | ) 146 | public 147 | returns (bool) 148 | { 149 | require(_to != address(0)); 150 | require(_value <= balances[_from]); 151 | require(_value <= allowed[_from][msg.sender]); 152 | 153 | balances[_from] = balances[_from].sub(_value); 154 | balances[_to] = balances[_to].add(_value); 155 | allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value); 156 | emit Transfer(_from, _to, _value); 157 | return true; 158 | } 159 | 160 | /** 161 | * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender. 162 | * 163 | * Beware that changing an allowance with this method brings the risk that someone may use both the old 164 | * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this 165 | * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards: 166 | * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 167 | * @param _spender The address which will spend the funds. 168 | * @param _value The amount of tokens to be spent. 169 | */ 170 | function approve(address _spender, uint256 _value) public returns (bool) { 171 | allowed[msg.sender][_spender] = _value; 172 | emit Approval(msg.sender, _spender, _value); 173 | return true; 174 | } 175 | 176 | /** 177 | * @dev Function to check the amount of tokens that an owner allowed to a spender. 178 | * @param _owner address The address which owns the funds. 179 | * @param _spender address The address which will spend the funds. 180 | * @return A uint256 specifying the amount of tokens still available for the spender. 181 | */ 182 | function allowance( 183 | address _owner, 184 | address _spender 185 | ) 186 | public 187 | view 188 | returns (uint256) 189 | { 190 | return allowed[_owner][_spender]; 191 | } 192 | 193 | /** 194 | * @dev Increase the amount of tokens that an owner allowed to a spender. 195 | * 196 | * approve should be called when allowed[_spender] == 0. To increment 197 | * allowed value is better to use this function to avoid 2 calls (and wait until 198 | * the first transaction is mined) 199 | * From MonolithDAO Token.sol 200 | * @param _spender The address which will spend the funds. 201 | * @param _addedValue The amount of tokens to increase the allowance by. 202 | */ 203 | function increaseApproval( 204 | address _spender, 205 | uint _addedValue 206 | ) 207 | public 208 | returns (bool) 209 | { 210 | allowed[msg.sender][_spender] = ( 211 | allowed[msg.sender][_spender].add(_addedValue)); 212 | emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); 213 | return true; 214 | } 215 | 216 | /** 217 | * @dev Decrease the amount of tokens that an owner allowed to a spender. 218 | * 219 | * approve should be called when allowed[_spender] == 0. To decrement 220 | * allowed value is better to use this function to avoid 2 calls (and wait until 221 | * the first transaction is mined) 222 | * From MonolithDAO Token.sol 223 | * @param _spender The address which will spend the funds. 224 | * @param _subtractedValue The amount of tokens to decrease the allowance by. 225 | */ 226 | function decreaseApproval( 227 | address _spender, 228 | uint _subtractedValue 229 | ) 230 | public 231 | returns (bool) 232 | { 233 | uint oldValue = allowed[msg.sender][_spender]; 234 | if (_subtractedValue > oldValue) { 235 | allowed[msg.sender][_spender] = 0; 236 | } else { 237 | allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue); 238 | } 239 | emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]); 240 | return true; 241 | } 242 | 243 | } 244 | 245 | 246 | /** 247 | * @title Ownable 248 | * @dev The Ownable contract has an owner address, and provides basic authorization control 249 | * functions, this simplifies the implementation of "user permissions". 250 | */ 251 | contract Ownable { 252 | address public owner; 253 | 254 | event OwnershipRenounced(address indexed previousOwner); 255 | event OwnershipTransferred( 256 | address indexed previousOwner, 257 | address indexed newOwner 258 | ); 259 | 260 | 261 | /** 262 | * @dev The Ownable constructor sets the original `owner` of the contract to the sender 263 | * account. 264 | */ 265 | constructor() public { 266 | owner = msg.sender; 267 | } 268 | 269 | /** 270 | * @dev Throws if called by any account other than the owner. 271 | */ 272 | modifier onlyOwner() { 273 | require(msg.sender == owner); 274 | _; 275 | } 276 | 277 | /** 278 | * @dev Allows the current owner to transfer control of the contract to a newOwner. 279 | * @param newOwner The address to transfer ownership to. 280 | */ 281 | function transferOwnership(address newOwner) public onlyOwner { 282 | require(newOwner != address(0)); 283 | emit OwnershipTransferred(owner, newOwner); 284 | owner = newOwner; 285 | } 286 | } 287 | 288 | /** 289 | * @title Mintable token 290 | * @dev Simple ERC20 Token example, with mintable token creation 291 | * @dev Issue: * https://github.com/OpenZeppelin/openzeppelin-solidity/issues/120 292 | * Based on code by TokenMarketNet: https://github.com/TokenMarketNet/ico/blob/master/contracts/MintableToken.sol 293 | */ 294 | contract MintableToken is StandardToken, Ownable { 295 | event Mint(address indexed to, uint256 amount); 296 | 297 | bool public mintingFinished = false; 298 | uint public mintTotal = 0; 299 | 300 | modifier canMint() { 301 | require(!mintingFinished); 302 | _; 303 | } 304 | 305 | modifier hasMintPermission() { 306 | require(msg.sender == owner); 307 | _; 308 | } 309 | 310 | /** 311 | * @dev Function to mint tokens 312 | * @param _to The address that will receive the minted tokens. 313 | * @param _amount The amount of tokens to mint. 314 | * @return A boolean that indicates if the operation was successful. 315 | */ 316 | function mint( 317 | address _to, 318 | uint256 _amount 319 | ) 320 | hasMintPermission 321 | canMint 322 | public 323 | returns (bool) 324 | { 325 | uint tmpTotal = mintTotal.add(_amount); 326 | require(tmpTotal <= totalSupply_); 327 | mintTotal = mintTotal.add(_amount); 328 | balances[_to] = balances[_to].add(_amount); 329 | emit Mint(_to, _amount); 330 | emit Transfer(address(0), _to, _amount); 331 | return true; 332 | } 333 | } 334 | 335 | 336 | /** 337 | * @title Pausable 338 | * @dev Base contract which allows children to implement an emergency stop mechanism. 339 | */ 340 | contract Pausable is Ownable { 341 | event Pause(); 342 | event Unpause(); 343 | 344 | bool public paused = true; 345 | 346 | 347 | /** 348 | * @dev Modifier to make a function callable only when the contract is not paused. 349 | */ 350 | modifier whenNotPaused() { 351 | require(!paused); 352 | _; 353 | } 354 | 355 | /** 356 | * @dev Modifier to make a function callable only when the contract is paused. 357 | */ 358 | modifier whenPaused() { 359 | require(paused); 360 | _; 361 | } 362 | 363 | /** 364 | * @dev called by the owner to pause, triggers stopped state 365 | */ 366 | function pause() onlyOwner whenNotPaused public { 367 | paused = true; 368 | emit Pause(); 369 | } 370 | 371 | /** 372 | * @dev called by the owner to unpause, returns to normal state 373 | */ 374 | function unpause() onlyOwner whenPaused public { 375 | paused = false; 376 | emit Unpause(); 377 | } 378 | } 379 | 380 | 381 | /** 382 | * @title Pausable token 383 | * @dev StandardToken modified with pausable transfers. 384 | **/ 385 | contract PausableToken is StandardToken, Pausable { 386 | 387 | function transfer( 388 | address _to, 389 | uint256 _value 390 | ) 391 | public 392 | whenNotPaused 393 | returns (bool) 394 | { 395 | return super.transfer(_to, _value); 396 | } 397 | 398 | function transferFrom( 399 | address _from, 400 | address _to, 401 | uint256 _value 402 | ) 403 | public 404 | whenNotPaused 405 | returns (bool) 406 | { 407 | return super.transferFrom(_from, _to, _value); 408 | } 409 | 410 | function approve( 411 | address _spender, 412 | uint256 _value 413 | ) 414 | public 415 | whenNotPaused 416 | returns (bool) 417 | { 418 | return super.approve(_spender, _value); 419 | } 420 | 421 | function increaseApproval( 422 | address _spender, 423 | uint _addedValue 424 | ) 425 | public 426 | whenNotPaused 427 | returns (bool success) 428 | { 429 | return super.increaseApproval(_spender, _addedValue); 430 | } 431 | 432 | function decreaseApproval( 433 | address _spender, 434 | uint _subtractedValue 435 | ) 436 | public 437 | whenNotPaused 438 | returns (bool success) 439 | { 440 | return super.decreaseApproval(_spender, _subtractedValue); 441 | } 442 | } 443 | 444 | contract BEP20Token is PausableToken, MintableToken { 445 | // public variables 446 | string public name = "ABC Token"; 447 | string public symbol = "ABC"; 448 | uint8 public decimals = 18; 449 | 450 | constructor() public { 451 | totalSupply_ = 10000 * (10 ** uint256(decimals)); 452 | } 453 | 454 | function () public payable { 455 | revert(); 456 | } 457 | } -------------------------------------------------------------------------------- /Contracts/NFT.sol: -------------------------------------------------------------------------------- 1 | /*** 2 | * MIT License 3 | * =========== 4 | * 5 | * Copyright (c) 2020 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all 15 | * copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | */ 24 | // File: @openzeppelin/contracts/GSN/Context.sol 25 | 26 | pragma solidity ^0.5.0; 27 | 28 | /* 29 | * @dev Provides information about the current execution context, including the 30 | * sender of the transaction and its data. While these are generally available 31 | * via msg.sender and msg.data, they should not be accessed in such a direct 32 | * manner, since when dealing with GSN meta-transactions the account sending and 33 | * paying for execution may not be the actual sender (as far as an application 34 | * is concerned). 35 | * 36 | * This contract is only required for intermediate, library-like contracts. 37 | */ 38 | contract Context { 39 | // Empty internal constructor, to prevent people from mistakenly deploying 40 | // an instance of this contract, which should be used via inheritance. 41 | constructor () internal { } 42 | // solhint-disable-previous-line no-empty-blocks 43 | 44 | function _msgSender() internal view returns (address payable) { 45 | return msg.sender; 46 | } 47 | 48 | function _msgData() internal view returns (bytes memory) { 49 | this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691 50 | return msg.data; 51 | } 52 | } 53 | 54 | // File: @openzeppelin/contracts/introspection/IERC165.sol 55 | 56 | pragma solidity ^0.5.0; 57 | 58 | /** 59 | * @dev Interface of the ERC165 standard, as defined in the 60 | * https://eips.ethereum.org/EIPS/eip-165[EIP]. 61 | * 62 | * Implementers can declare support of contract interfaces, which can then be 63 | * queried by others ({ERC165Checker}). 64 | * 65 | * For an implementation, see {ERC165}. 66 | */ 67 | interface IERC165 { 68 | /** 69 | * @dev Returns true if this contract implements the interface defined by 70 | * `interfaceId`. See the corresponding 71 | * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section] 72 | * to learn more about how these ids are created. 73 | * 74 | * This function call must use less than 30 000 gas. 75 | */ 76 | function supportsInterface(bytes4 interfaceId) external view returns (bool); 77 | } 78 | 79 | // File: @openzeppelin/contracts/token/ERC721/IERC721.sol 80 | 81 | pragma solidity ^0.5.0; 82 | 83 | 84 | /** 85 | * @dev Required interface of an ERC721 compliant contract. 86 | */ 87 | contract IERC721 is IERC165 { 88 | event Transfer(address indexed from, address indexed to, uint256 indexed tokenId); 89 | event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId); 90 | event ApprovalForAll(address indexed owner, address indexed operator, bool approved); 91 | 92 | /** 93 | * @dev Returns the number of NFTs in `owner`'s account. 94 | */ 95 | function balanceOf(address owner) public view returns (uint256 balance); 96 | 97 | /** 98 | * @dev Returns the owner of the NFT specified by `tokenId`. 99 | */ 100 | function ownerOf(uint256 tokenId) public view returns (address owner); 101 | 102 | /** 103 | * @dev Transfers a specific NFT (`tokenId`) from one account (`from`) to 104 | * another (`to`). 105 | * 106 | * 107 | * 108 | * Requirements: 109 | * - `from`, `to` cannot be zero. 110 | * - `tokenId` must be owned by `from`. 111 | * - If the caller is not `from`, it must be have been allowed to move this 112 | * NFT by either {approve} or {setApprovalForAll}. 113 | */ 114 | function safeTransferFrom(address from, address to, uint256 tokenId) public; 115 | /** 116 | * @dev Transfers a specific NFT (`tokenId`) from one account (`from`) to 117 | * another (`to`). 118 | * 119 | * Requirements: 120 | * - If the caller is not `from`, it must be approved to move this NFT by 121 | * either {approve} or {setApprovalForAll}. 122 | */ 123 | function transferFrom(address from, address to, uint256 tokenId) public; 124 | function approve(address to, uint256 tokenId) public; 125 | function getApproved(uint256 tokenId) public view returns (address operator); 126 | 127 | function setApprovalForAll(address operator, bool _approved) public; 128 | function isApprovedForAll(address owner, address operator) public view returns (bool); 129 | 130 | 131 | function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public; 132 | } 133 | 134 | // File: @openzeppelin/contracts/token/ERC721/IERC721Receiver.sol 135 | 136 | pragma solidity ^0.5.0; 137 | 138 | /** 139 | * @title ERC721 token receiver interface 140 | * @dev Interface for any contract that wants to support safeTransfers 141 | * from ERC721 asset contracts. 142 | */ 143 | contract IERC721Receiver { 144 | /** 145 | * @notice Handle the receipt of an NFT 146 | * @dev The ERC721 smart contract calls this function on the recipient 147 | * after a {IERC721-safeTransferFrom}. This function MUST return the function selector, 148 | * otherwise the caller will revert the transaction. The selector to be 149 | * returned can be obtained as `this.onERC721Received.selector`. This 150 | * function MAY throw to revert and reject the transfer. 151 | * Note: the ERC721 contract address is always the message sender. 152 | * @param operator The address which called `safeTransferFrom` function 153 | * @param from The address which previously owned the token 154 | * @param tokenId The NFT identifier which is being transferred 155 | * @param data Additional data with no specified format 156 | * @return bytes4 `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` 157 | */ 158 | function onERC721Received(address operator, address from, uint256 tokenId, bytes memory data) 159 | public returns (bytes4); 160 | } 161 | 162 | // File: @openzeppelin/contracts/math/SafeMath.sol 163 | 164 | pragma solidity ^0.5.0; 165 | 166 | /** 167 | * @dev Wrappers over Solidity's arithmetic operations with added overflow 168 | * checks. 169 | * 170 | * Arithmetic operations in Solidity wrap on overflow. This can easily result 171 | * in bugs, because programmers usually assume that an overflow raises an 172 | * error, which is the standard behavior in high level programming languages. 173 | * `SafeMath` restores this intuition by reverting the transaction when an 174 | * operation overflows. 175 | * 176 | * Using this library instead of the unchecked operations eliminates an entire 177 | * class of bugs, so it's recommended to use it always. 178 | */ 179 | library SafeMath { 180 | /** 181 | * @dev Returns the addition of two unsigned integers, reverting on 182 | * overflow. 183 | * 184 | * Counterpart to Solidity's `+` operator. 185 | * 186 | * Requirements: 187 | * - Addition cannot overflow. 188 | */ 189 | function add(uint256 a, uint256 b) internal pure returns (uint256) { 190 | uint256 c = a + b; 191 | require(c >= a, "SafeMath: addition overflow"); 192 | 193 | return c; 194 | } 195 | 196 | /** 197 | * @dev Returns the subtraction of two unsigned integers, reverting on 198 | * overflow (when the result is negative). 199 | * 200 | * Counterpart to Solidity's `-` operator. 201 | * 202 | * Requirements: 203 | * - Subtraction cannot overflow. 204 | */ 205 | function sub(uint256 a, uint256 b) internal pure returns (uint256) { 206 | return sub(a, b, "SafeMath: subtraction overflow"); 207 | } 208 | 209 | /** 210 | * @dev Returns the subtraction of two unsigned integers, reverting with custom message on 211 | * overflow (when the result is negative). 212 | * 213 | * Counterpart to Solidity's `-` operator. 214 | * 215 | * Requirements: 216 | * - Subtraction cannot overflow. 217 | * 218 | * _Available since v2.4.0._ 219 | */ 220 | function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { 221 | require(b <= a, errorMessage); 222 | uint256 c = a - b; 223 | 224 | return c; 225 | } 226 | 227 | /** 228 | * @dev Returns the multiplication of two unsigned integers, reverting on 229 | * overflow. 230 | * 231 | * Counterpart to Solidity's `*` operator. 232 | * 233 | * Requirements: 234 | * - Multiplication cannot overflow. 235 | */ 236 | function mul(uint256 a, uint256 b) internal pure returns (uint256) { 237 | // Gas optimization: this is cheaper than requiring 'a' not being zero, but the 238 | // benefit is lost if 'b' is also tested. 239 | // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 240 | if (a == 0) { 241 | return 0; 242 | } 243 | 244 | uint256 c = a * b; 245 | require(c / a == b, "SafeMath: multiplication overflow"); 246 | 247 | return c; 248 | } 249 | 250 | /** 251 | * @dev Returns the integer division of two unsigned integers. Reverts on 252 | * division by zero. The result is rounded towards zero. 253 | * 254 | * Counterpart to Solidity's `/` operator. Note: this function uses a 255 | * `revert` opcode (which leaves remaining gas untouched) while Solidity 256 | * uses an invalid opcode to revert (consuming all remaining gas). 257 | * 258 | * Requirements: 259 | * - The divisor cannot be zero. 260 | */ 261 | function div(uint256 a, uint256 b) internal pure returns (uint256) { 262 | return div(a, b, "SafeMath: division by zero"); 263 | } 264 | 265 | /** 266 | * @dev Returns the integer division of two unsigned integers. Reverts with custom message on 267 | * division by zero. The result is rounded towards zero. 268 | * 269 | * Counterpart to Solidity's `/` operator. Note: this function uses a 270 | * `revert` opcode (which leaves remaining gas untouched) while Solidity 271 | * uses an invalid opcode to revert (consuming all remaining gas). 272 | * 273 | * Requirements: 274 | * - The divisor cannot be zero. 275 | * 276 | * _Available since v2.4.0._ 277 | */ 278 | function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { 279 | // Solidity only automatically asserts when dividing by 0 280 | require(b > 0, errorMessage); 281 | uint256 c = a / b; 282 | // assert(a == b * c + a % b); // There is no case in which this doesn't hold 283 | 284 | return c; 285 | } 286 | 287 | /** 288 | * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), 289 | * Reverts when dividing by zero. 290 | * 291 | * Counterpart to Solidity's `%` operator. This function uses a `revert` 292 | * opcode (which leaves remaining gas untouched) while Solidity uses an 293 | * invalid opcode to revert (consuming all remaining gas). 294 | * 295 | * Requirements: 296 | * - The divisor cannot be zero. 297 | */ 298 | function mod(uint256 a, uint256 b) internal pure returns (uint256) { 299 | return mod(a, b, "SafeMath: modulo by zero"); 300 | } 301 | 302 | /** 303 | * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), 304 | * Reverts with custom message when dividing by zero. 305 | * 306 | * Counterpart to Solidity's `%` operator. This function uses a `revert` 307 | * opcode (which leaves remaining gas untouched) while Solidity uses an 308 | * invalid opcode to revert (consuming all remaining gas). 309 | * 310 | * Requirements: 311 | * - The divisor cannot be zero. 312 | * 313 | * _Available since v2.4.0._ 314 | */ 315 | function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { 316 | require(b != 0, errorMessage); 317 | return a % b; 318 | } 319 | } 320 | 321 | // File: @openzeppelin/contracts/utils/Address.sol 322 | 323 | pragma solidity ^0.5.5; 324 | 325 | /** 326 | * @dev Collection of functions related to the address type 327 | */ 328 | library Address { 329 | /** 330 | * @dev Returns true if `account` is a contract. 331 | * 332 | * [IMPORTANT] 333 | * ==== 334 | * It is unsafe to assume that an address for which this function returns 335 | * false is an externally-owned account (EOA) and not a contract. 336 | * 337 | * Among others, `isContract` will return false for the following 338 | * types of addresses: 339 | * 340 | * - an externally-owned account 341 | * - a contract in construction 342 | * - an address where a contract will be created 343 | * - an address where a contract lived, but was destroyed 344 | * ==== 345 | */ 346 | function isContract(address account) internal view returns (bool) { 347 | // According to EIP-1052, 0x0 is the value returned for not-yet created accounts 348 | // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned 349 | // for accounts without code, i.e. `keccak256('')` 350 | bytes32 codehash; 351 | bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470; 352 | // solhint-disable-next-line no-inline-assembly 353 | assembly { codehash := extcodehash(account) } 354 | return (codehash != accountHash && codehash != 0x0); 355 | } 356 | 357 | /** 358 | * @dev Converts an `address` into `address payable`. Note that this is 359 | * simply a type cast: the actual underlying value is not changed. 360 | * 361 | * _Available since v2.4.0._ 362 | */ 363 | function toPayable(address account) internal pure returns (address payable) { 364 | return address(uint160(account)); 365 | } 366 | 367 | /** 368 | * @dev Replacement for Solidity's `transfer`: sends `amount` wei to 369 | * `recipient`, forwarding all available gas and reverting on errors. 370 | * 371 | * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost 372 | * of certain opcodes, possibly making contracts go over the 2300 gas limit 373 | * imposed by `transfer`, making them unable to receive funds via 374 | * `transfer`. {sendValue} removes this limitation. 375 | * 376 | * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more]. 377 | * 378 | * IMPORTANT: because control is transferred to `recipient`, care must be 379 | * taken to not create reentrancy vulnerabilities. Consider using 380 | * {ReentrancyGuard} or the 381 | * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern]. 382 | * 383 | * _Available since v2.4.0._ 384 | */ 385 | function sendValue(address payable recipient, uint256 amount) internal { 386 | require(address(this).balance >= amount, "Address: insufficient balance"); 387 | 388 | // solhint-disable-next-line avoid-call-value 389 | (bool success, ) = recipient.call.value(amount)(""); 390 | require(success, "Address: unable to send value, recipient may have reverted"); 391 | } 392 | } 393 | 394 | // File: @openzeppelin/contracts/drafts/Counters.sol 395 | 396 | pragma solidity ^0.5.0; 397 | 398 | 399 | /** 400 | * @title Counters 401 | * @author Matt Condon (@shrugs) 402 | * @dev Provides counters that can only be incremented or decremented by one. This can be used e.g. to track the number 403 | * of elements in a mapping, issuing ERC721 ids, or counting request ids. 404 | * 405 | * Include with `using Counters for Counters.Counter;` 406 | * Since it is not possible to overflow a 256 bit integer with increments of one, `increment` can skip the {SafeMath} 407 | * overflow check, thereby saving gas. This does assume however correct usage, in that the underlying `_value` is never 408 | * directly accessed. 409 | */ 410 | library Counters { 411 | using SafeMath for uint256; 412 | 413 | struct Counter { 414 | // This variable should never be directly accessed by users of the library: interactions must be restricted to 415 | // the library's function. As of Solidity v0.5.2, this cannot be enforced, though there is a proposal to add 416 | // this feature: see https://github.com/ethereum/solidity/issues/4637 417 | uint256 _value; // default: 0 418 | } 419 | 420 | function current(Counter storage counter) internal view returns (uint256) { 421 | return counter._value; 422 | } 423 | 424 | function increment(Counter storage counter) internal { 425 | // The {SafeMath} overflow check can be skipped here, see the comment at the top 426 | counter._value += 1; 427 | } 428 | 429 | function decrement(Counter storage counter) internal { 430 | counter._value = counter._value.sub(1); 431 | } 432 | } 433 | 434 | // File: @openzeppelin/contracts/introspection/ERC165.sol 435 | 436 | pragma solidity ^0.5.0; 437 | 438 | 439 | /** 440 | * @dev Implementation of the {IERC165} interface. 441 | * 442 | * Contracts may inherit from this and call {_registerInterface} to declare 443 | * their support of an interface. 444 | */ 445 | contract ERC165 is IERC165 { 446 | /* 447 | * bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7 448 | */ 449 | bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7; 450 | 451 | /** 452 | * @dev Mapping of interface ids to whether or not it's supported. 453 | */ 454 | mapping(bytes4 => bool) private _supportedInterfaces; 455 | 456 | constructor () internal { 457 | // Derived contracts need only register support for their own interfaces, 458 | // we register support for ERC165 itself here 459 | _registerInterface(_INTERFACE_ID_ERC165); 460 | } 461 | 462 | /** 463 | * @dev See {IERC165-supportsInterface}. 464 | * 465 | * Time complexity O(1), guaranteed to always use less than 30 000 gas. 466 | */ 467 | function supportsInterface(bytes4 interfaceId) external view returns (bool) { 468 | return _supportedInterfaces[interfaceId]; 469 | } 470 | 471 | /** 472 | * @dev Registers the contract as an implementer of the interface defined by 473 | * `interfaceId`. Support of the actual ERC165 interface is automatic and 474 | * registering its interface id is not required. 475 | * 476 | * See {IERC165-supportsInterface}. 477 | * 478 | * Requirements: 479 | * 480 | * - `interfaceId` cannot be the ERC165 invalid interface (`0xffffffff`). 481 | */ 482 | function _registerInterface(bytes4 interfaceId) internal { 483 | require(interfaceId != 0xffffffff, "ERC165: invalid interface id"); 484 | _supportedInterfaces[interfaceId] = true; 485 | } 486 | } 487 | 488 | // File: @openzeppelin/contracts/token/ERC721/ERC721.sol 489 | 490 | pragma solidity ^0.5.0; 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | /** 500 | * @title ERC721 Non-Fungible Token Standard basic implementation 501 | * @dev see https://eips.ethereum.org/EIPS/eip-721 502 | */ 503 | contract ERC721 is Context, ERC165, IERC721 { 504 | using SafeMath for uint256; 505 | using Address for address; 506 | using Counters for Counters.Counter; 507 | 508 | // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` 509 | // which can be also obtained as `IERC721Receiver(0).onERC721Received.selector` 510 | bytes4 private constant _ERC721_RECEIVED = 0x150b7a02; 511 | 512 | // Mapping from token ID to owner 513 | mapping (uint256 => address) private _tokenOwner; 514 | 515 | // Mapping from token ID to approved address 516 | mapping (uint256 => address) private _tokenApprovals; 517 | 518 | // Mapping from owner to number of owned token 519 | mapping (address => Counters.Counter) private _ownedTokensCount; 520 | 521 | // Mapping from owner to operator approvals 522 | mapping (address => mapping (address => bool)) private _operatorApprovals; 523 | 524 | /* 525 | * bytes4(keccak256('balanceOf(address)')) == 0x70a08231 526 | * bytes4(keccak256('ownerOf(uint256)')) == 0x6352211e 527 | * bytes4(keccak256('approve(address,uint256)')) == 0x095ea7b3 528 | * bytes4(keccak256('getApproved(uint256)')) == 0x081812fc 529 | * bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465 530 | * bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5 531 | * bytes4(keccak256('transferFrom(address,address,uint256)')) == 0x23b872dd 532 | * bytes4(keccak256('safeTransferFrom(address,address,uint256)')) == 0x42842e0e 533 | * bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) == 0xb88d4fde 534 | * 535 | * => 0x70a08231 ^ 0x6352211e ^ 0x095ea7b3 ^ 0x081812fc ^ 536 | * 0xa22cb465 ^ 0xe985e9c ^ 0x23b872dd ^ 0x42842e0e ^ 0xb88d4fde == 0x80ac58cd 537 | */ 538 | bytes4 private constant _INTERFACE_ID_ERC721 = 0x80ac58cd; 539 | 540 | constructor () public { 541 | // register the supported interfaces to conform to ERC721 via ERC165 542 | _registerInterface(_INTERFACE_ID_ERC721); 543 | } 544 | 545 | /** 546 | * @dev Gets the balance of the specified address. 547 | * @param owner address to query the balance of 548 | * @return uint256 representing the amount owned by the passed address 549 | */ 550 | function balanceOf(address owner) public view returns (uint256) { 551 | require(owner != address(0), "ERC721: balance query for the zero address"); 552 | 553 | return _ownedTokensCount[owner].current(); 554 | } 555 | 556 | /** 557 | * @dev Gets the owner of the specified token ID. 558 | * @param tokenId uint256 ID of the token to query the owner of 559 | * @return address currently marked as the owner of the given token ID 560 | */ 561 | function ownerOf(uint256 tokenId) public view returns (address) { 562 | address owner = _tokenOwner[tokenId]; 563 | require(owner != address(0), "ERC721: owner query for nonexistent token"); 564 | 565 | return owner; 566 | } 567 | 568 | /** 569 | * @dev Approves another address to transfer the given token ID 570 | * The zero address indicates there is no approved address. 571 | * There can only be one approved address per token at a given time. 572 | * Can only be called by the token owner or an approved operator. 573 | * @param to address to be approved for the given token ID 574 | * @param tokenId uint256 ID of the token to be approved 575 | */ 576 | function approve(address to, uint256 tokenId) public { 577 | address owner = ownerOf(tokenId); 578 | require(to != owner, "ERC721: approval to current owner"); 579 | 580 | require(_msgSender() == owner || isApprovedForAll(owner, _msgSender()), 581 | "ERC721: approve caller is not owner nor approved for all" 582 | ); 583 | 584 | _tokenApprovals[tokenId] = to; 585 | emit Approval(owner, to, tokenId); 586 | } 587 | 588 | /** 589 | * @dev Gets the approved address for a token ID, or zero if no address set 590 | * Reverts if the token ID does not exist. 591 | * @param tokenId uint256 ID of the token to query the approval of 592 | * @return address currently approved for the given token ID 593 | */ 594 | function getApproved(uint256 tokenId) public view returns (address) { 595 | require(_exists(tokenId), "ERC721: approved query for nonexistent token"); 596 | 597 | return _tokenApprovals[tokenId]; 598 | } 599 | 600 | /** 601 | * @dev Sets or unsets the approval of a given operator 602 | * An operator is allowed to transfer all tokens of the sender on their behalf. 603 | * @param to operator address to set the approval 604 | * @param approved representing the status of the approval to be set 605 | */ 606 | function setApprovalForAll(address to, bool approved) public { 607 | require(to != _msgSender(), "ERC721: approve to caller"); 608 | 609 | _operatorApprovals[_msgSender()][to] = approved; 610 | emit ApprovalForAll(_msgSender(), to, approved); 611 | } 612 | 613 | /** 614 | * @dev Tells whether an operator is approved by a given owner. 615 | * @param owner owner address which you want to query the approval of 616 | * @param operator operator address which you want to query the approval of 617 | * @return bool whether the given operator is approved by the given owner 618 | */ 619 | function isApprovedForAll(address owner, address operator) public view returns (bool) { 620 | return _operatorApprovals[owner][operator]; 621 | } 622 | 623 | /** 624 | * @dev Transfers the ownership of a given token ID to another address. 625 | * Usage of this method is discouraged, use {safeTransferFrom} whenever possible. 626 | * Requires the msg.sender to be the owner, approved, or operator. 627 | * @param from current owner of the token 628 | * @param to address to receive the ownership of the given token ID 629 | * @param tokenId uint256 ID of the token to be transferred 630 | */ 631 | function transferFrom(address from, address to, uint256 tokenId) public { 632 | //solhint-disable-next-line max-line-length 633 | require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved"); 634 | 635 | _transferFrom(from, to, tokenId); 636 | } 637 | 638 | /** 639 | * @dev Safely transfers the ownership of a given token ID to another address 640 | * If the target address is a contract, it must implement {IERC721Receiver-onERC721Received}, 641 | * which is called upon a safe transfer, and return the magic value 642 | * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, 643 | * the transfer is reverted. 644 | * Requires the msg.sender to be the owner, approved, or operator 645 | * @param from current owner of the token 646 | * @param to address to receive the ownership of the given token ID 647 | * @param tokenId uint256 ID of the token to be transferred 648 | */ 649 | function safeTransferFrom(address from, address to, uint256 tokenId) public { 650 | safeTransferFrom(from, to, tokenId, ""); 651 | } 652 | 653 | /** 654 | * @dev Safely transfers the ownership of a given token ID to another address 655 | * If the target address is a contract, it must implement {IERC721Receiver-onERC721Received}, 656 | * which is called upon a safe transfer, and return the magic value 657 | * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, 658 | * the transfer is reverted. 659 | * Requires the _msgSender() to be the owner, approved, or operator 660 | * @param from current owner of the token 661 | * @param to address to receive the ownership of the given token ID 662 | * @param tokenId uint256 ID of the token to be transferred 663 | * @param _data bytes data to send along with a safe transfer check 664 | */ 665 | function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public { 666 | require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved"); 667 | _safeTransferFrom(from, to, tokenId, _data); 668 | } 669 | 670 | /** 671 | * @dev Safely transfers the ownership of a given token ID to another address 672 | * If the target address is a contract, it must implement `onERC721Received`, 673 | * which is called upon a safe transfer, and return the magic value 674 | * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, 675 | * the transfer is reverted. 676 | * Requires the msg.sender to be the owner, approved, or operator 677 | * @param from current owner of the token 678 | * @param to address to receive the ownership of the given token ID 679 | * @param tokenId uint256 ID of the token to be transferred 680 | * @param _data bytes data to send along with a safe transfer check 681 | */ 682 | function _safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) internal { 683 | _transferFrom(from, to, tokenId); 684 | require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer"); 685 | } 686 | 687 | /** 688 | * @dev Returns whether the specified token exists. 689 | * @param tokenId uint256 ID of the token to query the existence of 690 | * @return bool whether the token exists 691 | */ 692 | function _exists(uint256 tokenId) internal view returns (bool) { 693 | address owner = _tokenOwner[tokenId]; 694 | return owner != address(0); 695 | } 696 | 697 | /** 698 | * @dev Returns whether the given spender can transfer a given token ID. 699 | * @param spender address of the spender to query 700 | * @param tokenId uint256 ID of the token to be transferred 701 | * @return bool whether the msg.sender is approved for the given token ID, 702 | * is an operator of the owner, or is the owner of the token 703 | */ 704 | function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) { 705 | require(_exists(tokenId), "ERC721: operator query for nonexistent token"); 706 | address owner = ownerOf(tokenId); 707 | return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender)); 708 | } 709 | 710 | /** 711 | * @dev Internal function to safely mint a new token. 712 | * Reverts if the given token ID already exists. 713 | * If the target address is a contract, it must implement `onERC721Received`, 714 | * which is called upon a safe transfer, and return the magic value 715 | * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, 716 | * the transfer is reverted. 717 | * @param to The address that will own the minted token 718 | * @param tokenId uint256 ID of the token to be minted 719 | */ 720 | function _safeMint(address to, uint256 tokenId) internal { 721 | _safeMint(to, tokenId, ""); 722 | } 723 | 724 | /** 725 | * @dev Internal function to safely mint a new token. 726 | * Reverts if the given token ID already exists. 727 | * If the target address is a contract, it must implement `onERC721Received`, 728 | * which is called upon a safe transfer, and return the magic value 729 | * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, 730 | * the transfer is reverted. 731 | * @param to The address that will own the minted token 732 | * @param tokenId uint256 ID of the token to be minted 733 | * @param _data bytes data to send along with a safe transfer check 734 | */ 735 | function _safeMint(address to, uint256 tokenId, bytes memory _data) internal { 736 | _mint(to, tokenId); 737 | require(_checkOnERC721Received(address(0), to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer"); 738 | } 739 | 740 | /** 741 | * @dev Internal function to mint a new token. 742 | * Reverts if the given token ID already exists. 743 | * @param to The address that will own the minted token 744 | * @param tokenId uint256 ID of the token to be minted 745 | */ 746 | function _mint(address to, uint256 tokenId) internal { 747 | require(to != address(0), "ERC721: mint to the zero address"); 748 | require(!_exists(tokenId), "ERC721: token already minted"); 749 | 750 | _tokenOwner[tokenId] = to; 751 | _ownedTokensCount[to].increment(); 752 | 753 | emit Transfer(address(0), to, tokenId); 754 | } 755 | 756 | /** 757 | * @dev Internal function to burn a specific token. 758 | * Reverts if the token does not exist. 759 | * Deprecated, use {_burn} instead. 760 | * @param owner owner of the token to burn 761 | * @param tokenId uint256 ID of the token being burned 762 | */ 763 | function _burn(address owner, uint256 tokenId) internal { 764 | require(ownerOf(tokenId) == owner, "ERC721: burn of token that is not own"); 765 | 766 | _clearApproval(tokenId); 767 | 768 | _ownedTokensCount[owner].decrement(); 769 | _tokenOwner[tokenId] = address(0); 770 | 771 | emit Transfer(owner, address(0), tokenId); 772 | } 773 | 774 | /** 775 | * @dev Internal function to burn a specific token. 776 | * Reverts if the token does not exist. 777 | * @param tokenId uint256 ID of the token being burned 778 | */ 779 | function _burn(uint256 tokenId) internal { 780 | _burn(ownerOf(tokenId), tokenId); 781 | } 782 | 783 | /** 784 | * @dev Internal function to transfer ownership of a given token ID to another address. 785 | * As opposed to {transferFrom}, this imposes no restrictions on msg.sender. 786 | * @param from current owner of the token 787 | * @param to address to receive the ownership of the given token ID 788 | * @param tokenId uint256 ID of the token to be transferred 789 | */ 790 | function _transferFrom(address from, address to, uint256 tokenId) internal { 791 | require(ownerOf(tokenId) == from, "ERC721: transfer of token that is not own"); 792 | require(to != address(0), "ERC721: transfer to the zero address"); 793 | 794 | _clearApproval(tokenId); 795 | 796 | _ownedTokensCount[from].decrement(); 797 | _ownedTokensCount[to].increment(); 798 | 799 | _tokenOwner[tokenId] = to; 800 | 801 | emit Transfer(from, to, tokenId); 802 | } 803 | 804 | /** 805 | * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address. 806 | * The call is not executed if the target address is not a contract. 807 | * 808 | * This is an internal detail of the `ERC721` contract and its use is deprecated. 809 | * @param from address representing the previous owner of the given token ID 810 | * @param to target address that will receive the tokens 811 | * @param tokenId uint256 ID of the token to be transferred 812 | * @param _data bytes optional data to send along with the call 813 | * @return bool whether the call correctly returned the expected magic value 814 | */ 815 | function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data) 816 | internal returns (bool) 817 | { 818 | if (!to.isContract()) { 819 | return true; 820 | } 821 | // solhint-disable-next-line avoid-low-level-calls 822 | (bool success, bytes memory returndata) = to.call(abi.encodeWithSelector( 823 | IERC721Receiver(to).onERC721Received.selector, 824 | _msgSender(), 825 | from, 826 | tokenId, 827 | _data 828 | )); 829 | if (!success) { 830 | if (returndata.length > 0) { 831 | // solhint-disable-next-line no-inline-assembly 832 | assembly { 833 | let returndata_size := mload(returndata) 834 | revert(add(32, returndata), returndata_size) 835 | } 836 | } else { 837 | revert("ERC721: transfer to non ERC721Receiver implementer"); 838 | } 839 | } else { 840 | bytes4 retval = abi.decode(returndata, (bytes4)); 841 | return (retval == _ERC721_RECEIVED); 842 | } 843 | } 844 | 845 | /** 846 | * @dev Private function to clear current approval of a given token ID. 847 | * @param tokenId uint256 ID of the token to be transferred 848 | */ 849 | function _clearApproval(uint256 tokenId) private { 850 | if (_tokenApprovals[tokenId] != address(0)) { 851 | _tokenApprovals[tokenId] = address(0); 852 | } 853 | } 854 | } 855 | 856 | // File: @openzeppelin/contracts/token/ERC721/IERC721Enumerable.sol 857 | 858 | pragma solidity ^0.5.0; 859 | 860 | 861 | /** 862 | * @title ERC-721 Non-Fungible Token Standard, optional enumeration extension 863 | * @dev See https://eips.ethereum.org/EIPS/eip-721 864 | */ 865 | contract IERC721Enumerable is IERC721 { 866 | function totalSupply() public view returns (uint256); 867 | function tokenOfOwnerByIndex(address owner, uint256 index) public view returns (uint256 tokenId); 868 | 869 | function tokenByIndex(uint256 index) public view returns (uint256); 870 | } 871 | 872 | // File: @openzeppelin/contracts/token/ERC721/ERC721Enumerable.sol 873 | 874 | pragma solidity ^0.5.0; 875 | 876 | 877 | 878 | 879 | 880 | /** 881 | * @title ERC-721 Non-Fungible Token with optional enumeration extension logic 882 | * @dev See https://eips.ethereum.org/EIPS/eip-721 883 | */ 884 | contract ERC721Enumerable is Context, ERC165, ERC721, IERC721Enumerable { 885 | // Mapping from owner to list of owned token IDs 886 | mapping(address => uint256[]) private _ownedTokens; 887 | 888 | // Mapping from token ID to index of the owner tokens list 889 | mapping(uint256 => uint256) private _ownedTokensIndex; 890 | 891 | // Array with all token ids, used for enumeration 892 | uint256[] private _allTokens; 893 | 894 | // Mapping from token id to position in the allTokens array 895 | mapping(uint256 => uint256) private _allTokensIndex; 896 | 897 | /* 898 | * bytes4(keccak256('totalSupply()')) == 0x18160ddd 899 | * bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) == 0x2f745c59 900 | * bytes4(keccak256('tokenByIndex(uint256)')) == 0x4f6ccce7 901 | * 902 | * => 0x18160ddd ^ 0x2f745c59 ^ 0x4f6ccce7 == 0x780e9d63 903 | */ 904 | bytes4 private constant _INTERFACE_ID_ERC721_ENUMERABLE = 0x780e9d63; 905 | 906 | /** 907 | * @dev Constructor function. 908 | */ 909 | constructor () public { 910 | // register the supported interface to conform to ERC721Enumerable via ERC165 911 | _registerInterface(_INTERFACE_ID_ERC721_ENUMERABLE); 912 | } 913 | 914 | /** 915 | * @dev Gets the token ID at a given index of the tokens list of the requested owner. 916 | * @param owner address owning the tokens list to be accessed 917 | * @param index uint256 representing the index to be accessed of the requested tokens list 918 | * @return uint256 token ID at the given index of the tokens list owned by the requested address 919 | */ 920 | function tokenOfOwnerByIndex(address owner, uint256 index) public view returns (uint256) { 921 | require(index < balanceOf(owner), "ERC721Enumerable: owner index out of bounds"); 922 | return _ownedTokens[owner][index]; 923 | } 924 | 925 | /** 926 | * @dev Gets the total amount of tokens stored by the contract. 927 | * @return uint256 representing the total amount of tokens 928 | */ 929 | function totalSupply() public view returns (uint256) { 930 | return _allTokens.length; 931 | } 932 | 933 | /** 934 | * @dev Gets the token ID at a given index of all the tokens in this contract 935 | * Reverts if the index is greater or equal to the total number of tokens. 936 | * @param index uint256 representing the index to be accessed of the tokens list 937 | * @return uint256 token ID at the given index of the tokens list 938 | */ 939 | function tokenByIndex(uint256 index) public view returns (uint256) { 940 | require(index < totalSupply(), "ERC721Enumerable: global index out of bounds"); 941 | return _allTokens[index]; 942 | } 943 | 944 | /** 945 | * @dev Internal function to transfer ownership of a given token ID to another address. 946 | * As opposed to transferFrom, this imposes no restrictions on msg.sender. 947 | * @param from current owner of the token 948 | * @param to address to receive the ownership of the given token ID 949 | * @param tokenId uint256 ID of the token to be transferred 950 | */ 951 | function _transferFrom(address from, address to, uint256 tokenId) internal { 952 | super._transferFrom(from, to, tokenId); 953 | 954 | _removeTokenFromOwnerEnumeration(from, tokenId); 955 | 956 | _addTokenToOwnerEnumeration(to, tokenId); 957 | } 958 | 959 | /** 960 | * @dev Internal function to mint a new token. 961 | * Reverts if the given token ID already exists. 962 | * @param to address the beneficiary that will own the minted token 963 | * @param tokenId uint256 ID of the token to be minted 964 | */ 965 | function _mint(address to, uint256 tokenId) internal { 966 | super._mint(to, tokenId); 967 | 968 | _addTokenToOwnerEnumeration(to, tokenId); 969 | 970 | _addTokenToAllTokensEnumeration(tokenId); 971 | } 972 | 973 | /** 974 | * @dev Internal function to burn a specific token. 975 | * Reverts if the token does not exist. 976 | * Deprecated, use {ERC721-_burn} instead. 977 | * @param owner owner of the token to burn 978 | * @param tokenId uint256 ID of the token being burned 979 | */ 980 | function _burn(address owner, uint256 tokenId) internal { 981 | super._burn(owner, tokenId); 982 | 983 | _removeTokenFromOwnerEnumeration(owner, tokenId); 984 | // Since tokenId will be deleted, we can clear its slot in _ownedTokensIndex to trigger a gas refund 985 | _ownedTokensIndex[tokenId] = 0; 986 | 987 | _removeTokenFromAllTokensEnumeration(tokenId); 988 | } 989 | 990 | /** 991 | * @dev Gets the list of token IDs of the requested owner. 992 | * @param owner address owning the tokens 993 | * @return uint256[] List of token IDs owned by the requested address 994 | */ 995 | function _tokensOfOwner(address owner) internal view returns (uint256[] storage) { 996 | return _ownedTokens[owner]; 997 | } 998 | 999 | /** 1000 | * @dev Private function to add a token to this extension's ownership-tracking data structures. 1001 | * @param to address representing the new owner of the given token ID 1002 | * @param tokenId uint256 ID of the token to be added to the tokens list of the given address 1003 | */ 1004 | function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private { 1005 | _ownedTokensIndex[tokenId] = _ownedTokens[to].length; 1006 | _ownedTokens[to].push(tokenId); 1007 | } 1008 | 1009 | /** 1010 | * @dev Private function to add a token to this extension's token tracking data structures. 1011 | * @param tokenId uint256 ID of the token to be added to the tokens list 1012 | */ 1013 | function _addTokenToAllTokensEnumeration(uint256 tokenId) private { 1014 | _allTokensIndex[tokenId] = _allTokens.length; 1015 | _allTokens.push(tokenId); 1016 | } 1017 | 1018 | /** 1019 | * @dev Private function to remove a token from this extension's ownership-tracking data structures. Note that 1020 | * while the token is not assigned a new owner, the `_ownedTokensIndex` mapping is _not_ updated: this allows for 1021 | * gas optimizations e.g. when performing a transfer operation (avoiding double writes). 1022 | * This has O(1) time complexity, but alters the order of the _ownedTokens array. 1023 | * @param from address representing the previous owner of the given token ID 1024 | * @param tokenId uint256 ID of the token to be removed from the tokens list of the given address 1025 | */ 1026 | function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private { 1027 | // To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and 1028 | // then delete the last slot (swap and pop). 1029 | 1030 | uint256 lastTokenIndex = _ownedTokens[from].length.sub(1); 1031 | uint256 tokenIndex = _ownedTokensIndex[tokenId]; 1032 | 1033 | // When the token to delete is the last token, the swap operation is unnecessary 1034 | if (tokenIndex != lastTokenIndex) { 1035 | uint256 lastTokenId = _ownedTokens[from][lastTokenIndex]; 1036 | 1037 | _ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token 1038 | _ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index 1039 | } 1040 | 1041 | // This also deletes the contents at the last position of the array 1042 | _ownedTokens[from].length--; 1043 | 1044 | // Note that _ownedTokensIndex[tokenId] hasn't been cleared: it still points to the old slot (now occupied by 1045 | // lastTokenId, or just over the end of the array if the token was the last one). 1046 | } 1047 | 1048 | /** 1049 | * @dev Private function to remove a token from this extension's token tracking data structures. 1050 | * This has O(1) time complexity, but alters the order of the _allTokens array. 1051 | * @param tokenId uint256 ID of the token to be removed from the tokens list 1052 | */ 1053 | function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private { 1054 | // To prevent a gap in the tokens array, we store the last token in the index of the token to delete, and 1055 | // then delete the last slot (swap and pop). 1056 | 1057 | uint256 lastTokenIndex = _allTokens.length.sub(1); 1058 | uint256 tokenIndex = _allTokensIndex[tokenId]; 1059 | 1060 | // When the token to delete is the last token, the swap operation is unnecessary. However, since this occurs so 1061 | // rarely (when the last minted token is burnt) that we still do the swap here to avoid the gas cost of adding 1062 | // an 'if' statement (like in _removeTokenFromOwnerEnumeration) 1063 | uint256 lastTokenId = _allTokens[lastTokenIndex]; 1064 | 1065 | _allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token 1066 | _allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index 1067 | 1068 | // This also deletes the contents at the last position of the array 1069 | _allTokens.length--; 1070 | _allTokensIndex[tokenId] = 0; 1071 | } 1072 | } 1073 | 1074 | // File: @openzeppelin/contracts/token/ERC721/IERC721Metadata.sol 1075 | 1076 | pragma solidity ^0.5.0; 1077 | 1078 | 1079 | /** 1080 | * @title ERC-721 Non-Fungible Token Standard, optional metadata extension 1081 | * @dev See https://eips.ethereum.org/EIPS/eip-721 1082 | */ 1083 | contract IERC721Metadata is IERC721 { 1084 | function name() external view returns (string memory); 1085 | function symbol() external view returns (string memory); 1086 | function tokenURI(uint256 tokenId) external view returns (string memory); 1087 | } 1088 | 1089 | // File: @openzeppelin/contracts/token/ERC721/ERC721Metadata.sol 1090 | 1091 | pragma solidity ^0.5.0; 1092 | 1093 | 1094 | 1095 | 1096 | 1097 | contract ERC721Metadata is Context, ERC165, ERC721, IERC721Metadata { 1098 | // Token name 1099 | string private _name; 1100 | 1101 | // Token symbol 1102 | string private _symbol; 1103 | 1104 | // Base URI 1105 | string private _baseURI; 1106 | 1107 | // Optional mapping for token URIs 1108 | mapping(uint256 => string) private _tokenURIs; 1109 | 1110 | /* 1111 | * bytes4(keccak256('name()')) == 0x06fdde03 1112 | * bytes4(keccak256('symbol()')) == 0x95d89b41 1113 | * bytes4(keccak256('tokenURI(uint256)')) == 0xc87b56dd 1114 | * 1115 | * => 0x06fdde03 ^ 0x95d89b41 ^ 0xc87b56dd == 0x5b5e139f 1116 | */ 1117 | bytes4 private constant _INTERFACE_ID_ERC721_METADATA = 0x5b5e139f; 1118 | 1119 | /** 1120 | * @dev Constructor function 1121 | */ 1122 | constructor (string memory name, string memory symbol) public { 1123 | _name = name; 1124 | _symbol = symbol; 1125 | 1126 | // register the supported interfaces to conform to ERC721 via ERC165 1127 | _registerInterface(_INTERFACE_ID_ERC721_METADATA); 1128 | } 1129 | 1130 | /** 1131 | * @dev Gets the token name. 1132 | * @return string representing the token name 1133 | */ 1134 | function name() external view returns (string memory) { 1135 | return _name; 1136 | } 1137 | 1138 | /** 1139 | * @dev Gets the token symbol. 1140 | * @return string representing the token symbol 1141 | */ 1142 | function symbol() external view returns (string memory) { 1143 | return _symbol; 1144 | } 1145 | 1146 | /** 1147 | * @dev Returns the URI for a given token ID. May return an empty string. 1148 | * 1149 | * If the token's URI is non-empty and a base URI was set (via 1150 | * {_setBaseURI}), it will be added to the token ID's URI as a prefix. 1151 | * 1152 | * Reverts if the token ID does not exist. 1153 | */ 1154 | function tokenURI(uint256 tokenId) external view returns (string memory) { 1155 | require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token"); 1156 | 1157 | string memory _tokenURI = _tokenURIs[tokenId]; 1158 | 1159 | // Even if there is a base URI, it is only appended to non-empty token-specific URIs 1160 | if (bytes(_tokenURI).length == 0) { 1161 | return ""; 1162 | } else { 1163 | // abi.encodePacked is being used to concatenate strings 1164 | return string(abi.encodePacked(_baseURI, _tokenURI)); 1165 | } 1166 | } 1167 | 1168 | /** 1169 | * @dev Internal function to set the token URI for a given token. 1170 | * 1171 | * Reverts if the token ID does not exist. 1172 | * 1173 | * TIP: if all token IDs share a prefix (e.g. if your URIs look like 1174 | * `http://api.myproject.com/token/`), use {_setBaseURI} to store 1175 | * it and save gas. 1176 | */ 1177 | function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal { 1178 | require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token"); 1179 | _tokenURIs[tokenId] = _tokenURI; 1180 | } 1181 | 1182 | /** 1183 | * @dev Internal function to set the base URI for all token IDs. It is 1184 | * automatically added as a prefix to the value returned in {tokenURI}. 1185 | * 1186 | * _Available since v2.5.0._ 1187 | */ 1188 | function _setBaseURI(string memory baseURI) internal { 1189 | _baseURI = baseURI; 1190 | } 1191 | 1192 | /** 1193 | * @dev Returns the base URI set via {_setBaseURI}. This will be 1194 | * automatically added as a preffix in {tokenURI} to each token's URI, when 1195 | * they are non-empty. 1196 | * 1197 | * _Available since v2.5.0._ 1198 | */ 1199 | function baseURI() external view returns (string memory) { 1200 | return _baseURI; 1201 | } 1202 | 1203 | /** 1204 | * @dev Internal function to burn a specific token. 1205 | * Reverts if the token does not exist. 1206 | * Deprecated, use _burn(uint256) instead. 1207 | * @param owner owner of the token to burn 1208 | * @param tokenId uint256 ID of the token being burned by the msg.sender 1209 | */ 1210 | function _burn(address owner, uint256 tokenId) internal { 1211 | super._burn(owner, tokenId); 1212 | 1213 | // Clear metadata (if any) 1214 | if (bytes(_tokenURIs[tokenId]).length != 0) { 1215 | delete _tokenURIs[tokenId]; 1216 | } 1217 | } 1218 | } 1219 | 1220 | // File: @openzeppelin/contracts/token/ERC721/ERC721Full.sol 1221 | 1222 | pragma solidity ^0.5.0; 1223 | 1224 | 1225 | 1226 | 1227 | /** 1228 | * @title Full ERC721 Token 1229 | * @dev This implementation includes all the required and some optional functionality of the ERC721 standard 1230 | * Moreover, it includes approve all functionality using operator terminology. 1231 | * 1232 | * See https://eips.ethereum.org/EIPS/eip-721 1233 | */ 1234 | contract ERC721Full is ERC721, ERC721Enumerable, ERC721Metadata { 1235 | constructor (string memory name, string memory symbol) public ERC721Metadata(name, symbol) { 1236 | // solhint-disable-previous-line no-empty-blocks 1237 | } 1238 | } 1239 | 1240 | // File: contracts/library/Governance.sol 1241 | 1242 | pragma solidity ^0.5.0; 1243 | 1244 | contract Governance { 1245 | 1246 | address public _governance; 1247 | 1248 | constructor() public { 1249 | _governance = tx.origin; 1250 | } 1251 | 1252 | event GovernanceTransferred(address indexed previousOwner, address indexed newOwner); 1253 | 1254 | modifier onlyGovernance { 1255 | require(msg.sender == _governance, "not governance"); 1256 | _; 1257 | } 1258 | 1259 | function setGovernance(address governance) public onlyGovernance 1260 | { 1261 | require(governance != address(0), "new governance the zero address"); 1262 | emit GovernanceTransferred(_governance, governance); 1263 | _governance = governance; 1264 | } 1265 | 1266 | 1267 | } 1268 | 1269 | // File: contracts/library/MathwalletUtil.sol 1270 | 1271 | pragma solidity ^0.5.0; 1272 | 1273 | 1274 | library MathwalletUtil { 1275 | function uintToString(uint _i) internal pure returns (string memory _uintAsString) { 1276 | if (_i == 0) { 1277 | return "0"; 1278 | } 1279 | uint j = _i; 1280 | uint len; 1281 | while (j != 0) { 1282 | len++; 1283 | j /= 10; 1284 | } 1285 | bytes memory bstr = new bytes(len); 1286 | uint k = len - 1; 1287 | while (_i != 0) { 1288 | bstr[k--] = byte(uint8(48 + _i % 10)); 1289 | _i /= 10; 1290 | } 1291 | return string(bstr); 1292 | } 1293 | } 1294 | 1295 | // File: contracts/nft/MathCon2Token.sol 1296 | 1297 | pragma solidity ^0.5.5; 1298 | 1299 | 1300 | 1301 | 1302 | contract NFTToken is ERC721Full, Governance { 1303 | // for minters 1304 | mapping(address => bool) public _minters; 1305 | 1306 | 1307 | 1308 | constructor() public ERC721Full("NAME", "SYMBOL") { 1309 | _setBaseURI("https://BASEURI"); 1310 | } 1311 | 1312 | 1313 | function setURIPrefix(string memory baseURI) internal { 1314 | _setBaseURI(baseURI); 1315 | } 1316 | 1317 | 1318 | /** 1319 | * @dev Function to mint tokens. 1320 | * @param to The address that will receive the minted token. 1321 | * @param tokenId The token id to mint. 1322 | * @return A boolean that indicates if the operation was successful. 1323 | */ 1324 | function mint(address to, uint256 tokenId) external returns (bool) { 1325 | require(_minters[msg.sender], "!minter"); 1326 | _mint(to, tokenId); 1327 | _setTokenURI(tokenId, MathwalletUtil.uintToString(tokenId)); 1328 | return true; 1329 | } 1330 | 1331 | /** 1332 | * @dev Function to safely mint tokens. 1333 | * @param to The address that will receive the minted token. 1334 | * @param tokenId The token id to mint. 1335 | * @return A boolean that indicates if the operation was successful. 1336 | */ 1337 | function safeMint(address to, uint256 tokenId) public returns (bool) { 1338 | require(_minters[msg.sender], "!minter"); 1339 | _safeMint(to, tokenId); 1340 | _setTokenURI(tokenId, MathwalletUtil.uintToString(tokenId)); 1341 | return true; 1342 | } 1343 | 1344 | /** 1345 | * @dev Function to safely mint tokens. 1346 | * @param to The address that will receive the minted token. 1347 | * @param tokenId The token id to mint. 1348 | * @param _data bytes data to send along with a safe transfer check. 1349 | * @return A boolean that indicates if the operation was successful. 1350 | */ 1351 | function safeMint( 1352 | address to, 1353 | uint256 tokenId, 1354 | bytes memory _data 1355 | ) public returns (bool) { 1356 | require(_minters[msg.sender], "!minter"); 1357 | _safeMint(to, tokenId, _data); 1358 | _setTokenURI(tokenId, MathwalletUtil.uintToString(tokenId)); 1359 | return true; 1360 | } 1361 | 1362 | function addMinter(address minter) public onlyGovernance { 1363 | _minters[minter] = true; 1364 | } 1365 | 1366 | function removeMinter(address minter) public onlyGovernance { 1367 | _minters[minter] = false; 1368 | } 1369 | 1370 | /** 1371 | * @dev Burns a specific ERC721 token. 1372 | * @param tokenId uint256 id of the ERC721 token to be burned. 1373 | */ 1374 | function burn(uint256 tokenId) external { 1375 | //solhint-disable-next-line max-line-length 1376 | require(_minters[msg.sender], "!minter"); 1377 | require( 1378 | _isApprovedOrOwner(_msgSender(), tokenId), 1379 | "caller is not owner nor approved" 1380 | ); 1381 | _burn(tokenId); 1382 | } 1383 | 1384 | 1385 | /** 1386 | * @dev Gets the list of token IDs of the requested owner. 1387 | * @param owner address owning the tokens 1388 | * @return uint256[] List of token IDs owned by the requested address 1389 | */ 1390 | function tokensOfOwner(address owner) public view returns (uint256[] memory) { 1391 | return _tokensOfOwner(owner); 1392 | } 1393 | 1394 | 1395 | } --------------------------------------------------------------------------------