Add support for tokenURI in token contracts (#8)

This commit is contained in:
Gerardo Nardelli 2019-10-04 10:55:36 -03:00 committed by Alexander Kolotov
parent 138dd3932c
commit d7ccc3e7a4
6 changed files with 123 additions and 2 deletions

View File

@ -222,4 +222,8 @@ contract KittyBase is KittyAccessControl {
require(secs < cooldowns[0]);
secondsPerBlock = secs;
}
function _exists(uint256 _tokenId) internal view returns (bool) {
return kittyIndexToOwner[_tokenId] != address(0);
}
}

View File

@ -1,11 +1,12 @@
pragma solidity 0.4.24;
import "./KittyMinting.sol";
import "../simpleKitty/URIMetadata.sol";
/// @title CryptoKitties: Collectible, breedable, and oh-so-adorable cats on the Ethereum blockchain.
/// @author Axiom Zen (https://www.axiomzen.co)
/// @dev The main CryptoKitties contract, keeps track of kittens so they don't wander around and get lost.
contract KittyCore is KittyMinting {
contract KittyCore is KittyMinting, URIMetadata {
// This is the main CryptoKitties contract. In order to keep our code seperated into logical sections,
// we've broken it up in two ways. First, we have several seperately-instantiated sibling contracts
@ -142,4 +143,9 @@ contract KittyCore is KittyMinting {
cfoAddress.send(balance - subtractFees);
}
}
function tokenURI(uint256 _tokenId) external view returns (string) {
require(_exists(_tokenId));
return _tokenURI(_tokenId);
}
}

View File

@ -2,8 +2,9 @@ pragma solidity 0.4.24;
import "./SimpleKittyCore.sol";
import "./BridgeRole.sol";
import "./URIMetadata.sol";
contract SimpleBridgeKitty is BridgeRole, SimpleKittyCore {
contract SimpleBridgeKitty is BridgeRole, SimpleKittyCore, URIMetadata {
event Death(uint256 kittyId);
function mint(
@ -46,4 +47,9 @@ contract SimpleBridgeKitty is BridgeRole, SimpleKittyCore {
ownershipTokenCount[msg.sender]--;
emit Death(_tokenId);
}
function tokenURI(uint256 _tokenId) external view returns (string) {
require(_exists(_tokenId));
return _tokenURI(_tokenId);
}
}

View File

@ -138,4 +138,8 @@ contract SimpleKittyBase {
return _tokenId;
}
function _exists(uint256 _tokenId) internal view returns (bool) {
return kittyIndexToOwner[_tokenId] != address(0);
}
}

View File

@ -0,0 +1,40 @@
pragma solidity 0.4.24;
contract URIMetadata {
function _tokenURI(uint256 _tokenId) internal pure returns (string) {
return strConcat("https://api.cryptokitties.co/kitties/", uintToString(_tokenId));
}
function uintToString(uint256 i) internal pure returns (string) {
if (i == 0) return "0";
uint256 j = i;
uint256 length;
while (j != 0) {
length++;
j /= 10;
}
bytes memory bstr = new bytes(length);
uint256 k = length - 1;
while (i != 0) {
bstr[k--] = bytes1(48 + (i % 10));
i /= 10;
}
return string(bstr);
}
function strConcat(string _a, string _b) internal pure returns (string) {
bytes memory _ba = bytes(_a);
bytes memory _bb = bytes(_b);
string memory ab = new string(_ba.length + _bb.length);
bytes memory bab = bytes(ab);
uint256 k = 0;
uint256 i = 0;
for (i = 0; i < _ba.length; i++) {
bab[k++] = _ba[i];
}
for (i = 0; i < _bb.length; i++) {
bab[k++] = _bb[i];
}
return string(bab);
}
}

View File

@ -384,4 +384,65 @@ contract('SimpleBridgeKitty', accounts => {
})
})
})
describe('tokenURI', () => {
it('should return string URI of token', async () => {
// should fail if token does not exists
await expectRevert.unspecified(token.tokenURI(1))
await token.mint(
1,
isReady,
cooldownIndex,
nextActionAt,
siringWithId,
birthTime,
matronId,
sireId,
generation,
genes,
user,
{ from: owner }
)
expect(await token.tokenURI(1)).to.be.equal('https://api.cryptokitties.co/kitties/1')
await expectRevert.unspecified(token.tokenURI(12345))
await token.mint(
12345,
isReady,
cooldownIndex,
nextActionAt,
siringWithId,
birthTime,
matronId,
sireId,
generation,
genes,
user,
{ from: owner }
)
expect(await token.tokenURI(12345)).to.be.equal('https://api.cryptokitties.co/kitties/12345')
await expectRevert.unspecified(token.tokenURI(1714022))
await token.mint(
1714022,
isReady,
cooldownIndex,
nextActionAt,
siringWithId,
birthTime,
matronId,
sireId,
generation,
genes,
user,
{ from: owner }
)
expect(await token.tokenURI(1714022)).to.be.equal('https://api.cryptokitties.co/kitties/1714022')
})
})
})