Created
June 24, 2020 06:49
-
-
Save DomEscobar/d35bbc65d8c8cdebf5cdaa1dd338161c 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.6.6+commit.6c089d02.js&optimize=false&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
pragma solidity >=0.4.22 <0.7.0; | |
import "github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/IERC721.sol"; | |
import "github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol"; | |
contract ColorCard | |
{ | |
using SafeMath for uint256; | |
struct Color | |
{ | |
uint256 id; | |
uint256 r; | |
uint256 g; | |
uint256 b; | |
} | |
struct Holder | |
{ | |
address id; | |
string name; | |
Color[] colors; // tryed out without and relation map | |
} | |
event CreatedHolder(address indexed holder, string indexed name); | |
event CreateColor(address indexed holder, uint256 indexed colorId); | |
uint256 colorCount; | |
mapping(uint256 => Color) public colors; | |
mapping(address => Holder) public holders; | |
function createHolder(string memory _name) | |
public | |
{ | |
require(holders[msg.sender].id != msg.sender , "Holder does exists"); | |
Holder storage newHolder; | |
newHolder.id = msg.sender; | |
newHolder.name = _name; | |
holders[msg.sender] = newHolder; | |
emit CreatedHolder(msg.sender, _name); | |
} | |
function createColor(uint256 r, uint256 g, uint256 b) | |
public | |
validateColor(r, g, b) | |
checkRegistredHolder() | |
{ | |
require(!colorExists(r,g,b), "Color Exists"); | |
colorCount = colorCount.add(1); | |
Color memory newColor = Color(getColorId(r, g, b), r,g,b); | |
colors[newColor.id] = newColor; | |
holders[msg.sender].colors.push(newColor); | |
emit CreateColor(msg.sender, newColor.id); | |
} | |
function transferColor(address _to, uint256 _colorId) | |
public | |
checkRegistredHolder() | |
{ | |
require(holders[_to].id == _to , "Receipient does not exists"); | |
require(msg.sender != _to, "Receipient and sender is same address"); | |
removeFromSender(_colorId); | |
assignToRecipient(_to, _colorId); | |
} | |
function getColorId(uint256 r, uint256 g, uint256 b) | |
private pure | |
returns(uint256) | |
{ | |
return r * g * b; | |
} | |
function getColorsByAddress(address _address) public view | |
returns (uint256[] memory) | |
{ | |
uint256[] memory result = new uint256[](holders[_address].colors.length); | |
for (uint256 i = 0; i < holders[_address].colors.length; i++) { | |
result[i] = holders[_address].colors[i].id; | |
} | |
return result; | |
} | |
function getIndexOfColor(Color[] storage _colors, uint256 colorId) | |
private view | |
returns(uint256) | |
{ | |
for (uint256 i = 0; i < _colors.length; i++) | |
{ | |
if(_colors[i].id == colorId) | |
{ | |
return i; | |
} | |
} | |
return 9999; | |
} | |
function assignToRecipient(address _to, uint256 _colorId) private { | |
require(colors[_colorId].id != 0 , "Color does not exist"); | |
holders[_to].colors.push(colors[_colorId]); | |
} | |
function removeFromSender(uint256 colorId) private { | |
require(holders[msg.sender].colors[getIndexOfColor(holders[msg.sender].colors, colorId)].id != 0, "Sender is not color owner"); | |
holders[msg.sender].colors[getIndexOfColor(holders[msg.sender].colors, colorId)] = holders[msg.sender].colors[holders[msg.sender].colors.length - 1]; | |
holders[msg.sender].colors.pop(); | |
} | |
function colorExists(uint256 r, uint256 g, uint256 b) private view returns(bool) | |
{ | |
return colors[getColorId(r,g,b)].r != 0; | |
} | |
modifier validateColor(uint256 r, uint256 g, uint256 b) | |
{ | |
require(r >= 0 && r <= 255, "Invalid r value"); | |
require(g >= 0 && g <= 255, "Invalid r value"); | |
require(b>= 0 && b <= 255, "Invalid r value"); | |
_; | |
} | |
modifier checkRegistredHolder() | |
{ | |
require(holders[msg.sender].id == msg.sender , "Holder does not exists"); | |
_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment