Skip to content

Commit

Permalink
User token metadata to store hero property
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchuangportal committed Jul 23, 2018
1 parent ed82e3a commit 61df85b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
24 changes: 16 additions & 8 deletions contracts/CryptoHerosToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,38 @@ import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
contract CryptoHerosToken is ERC721Token, Ownable {
mapping (uint256 => address) internal tokenOwner;

uint nonce = 0;
string[] public heros;

constructor(string name, string symbol) public
ERC721Token(name, symbol)
{ }

function initHeros(string heroURI) public onlyOwner {
heros.push(heroURI);
}
/**
* Only owner can mint
*/
function mint(address _to, string _uri) public onlyOwner {
function mint() public {
require(heros.length > 0);
uint256 _tokenId = totalSupply();
tokenOwner[_tokenId] = _to;
super._mint(_to, _tokenId);
super._setTokenURI(_tokenId, _uri);
tokenOwner[_tokenId] = msg.sender;
super._mint(msg.sender, _tokenId);
super._setTokenURI(_tokenId, heros[rand(0, heros.length)]);
}

function burn(uint256 _tokenId) public onlyOwner {
tokenOwner[_tokenId] = address(0);
super._burn(ownerOf(_tokenId), _tokenId);
}

function setTokenURI(uint256 _tokenId, string _uri) public onlyOwnerOf(_tokenId) {
super._setTokenURI(_tokenId, _uri);
}

function getOwnedTokens(address _owner) external view returns (uint256[]) {
return ownedTokens[_owner];
}

function rand(uint min, uint max) private returns (uint){
nonce++;
return uint(sha3(nonce))%(min+max)-min;
}
}
40 changes: 29 additions & 11 deletions test/cryptoHerosToken.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,40 @@ contract("CryptoHeros token", accounts => {
assert.equal(owner, accounts[0]);
});

it("Should init hero", async () => {
let instance = await CryptoHerosToken.deployed();
let result = await instance.initHeros('{name: "Portal Network", description: "A gateway to the decentralized world"}');
assert.equal(result.receipt.status, '0x1');
let result2 = await instance.initHeros('{name: "Portal Network 2", description: "A gateway to the decentralized world"}');
assert.equal(result2.receipt.status, '0x1');
let result3 = await instance.initHeros('{name: "Portal Network 3", description: "A gateway to the decentralized world"}');
assert.equal(result3.receipt.status, '0x1');
//assert.equal(owner, accounts[0]);
});

describe("Should mint crypto heros", () => {
it("Creates crypto heros with specified URI", async () => {
let instance = await CryptoHerosToken.deployed();

let token = await instance.mint(accounts[1], '{name: "Portal Network", description: "A gateway to the decentralized world"}');
let tokenURI = await instance.tokenURI(0);
assert.deepEqual(tokenURI, '{name: "Portal Network", description: "A gateway to the decentralized world"}');
for (let i=0;i<10;i++) {
await instance.mint({from: accounts[1]});
}
//let hero = await instance.heros();
//console.log(hero);
//let heros = await instance.heros(0);
//assert.deepEqual(heros, '{name: "Portal Network", description: "A gateway to the decentralized world"}');
});

it("Set crypto heros token uri", async () => {
it("Get crypto heros token uri", async () => {
let instance = await CryptoHerosToken.deployed();
await assertRevert(instance.setTokenURI(0, '{name: "0", description: "Do not work!"}', {from: accounts[2]}));
await instance.setTokenURI(0, '{name: "0", description: "Do not work!"}', {from: accounts[1]});
//await assertRevert(instance.setTokenURI(0, '{name: "0", description: "Do not work!"}', {from: accounts[2]}));
//await instance.setTokenURI(0, '{name: "0", description: "Do not work!"}', {from: accounts[1]});
let tokenURI;
for (let i=0;i<10;i++) {
tokenURI = await instance.tokenURI(i);
console.log(tokenURI);
}

let tokenURI = await instance.tokenURI(0);
assert.equal(tokenURI, '{name: "0", description: "Do not work!"}');
//assert.equal(tokenURI, '{name: "0", description: "Do not work!"}');
});

it("Get token owner", async () => {
Expand All @@ -57,8 +75,8 @@ contract("CryptoHeros token", accounts => {
let owner = await instance.ownerOf(0);

let ownedTokens = await instance.getOwnedTokens(owner);
console.log(ownedTokens);
//assert.equal(ownedTokens, [0]);
console.log(ownedTokens.toString());
//assert.equal(ownedTokens, 0);
})

it("Should transfer ownership", async () => {
Expand Down

0 comments on commit 61df85b

Please sign in to comment.