Skip to content

Instantly share code, notes, and snippets.

@soenkeba
Created October 27, 2021 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save soenkeba/5df8ee90d600e8e3c9ceee0a6a3a23a0 to your computer and use it in GitHub Desktop.
Save soenkeba/5df8ee90d600e8e3c9ceee0a6a3a23a0 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.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
contract sciencefaction is ERC1155 {
uint256 public constant DECENTRAFISH = 0x01559ae4021a4dcafee8abc36a5139b96d58cbba2fe6cd347c4ed6668011aac5;
uint256 public constant NFThistory = 0x01559ae4021a99b0d373d7bc8a80504bad782367abe12c21373c83adc6bf6a7e;
uint256 public constant IPFSforscher = 0x01559ae4021a9c84fa506cb76691054c2b92f655222cde2437d77ed21c083c4d;
constructor() public ERC1155("ipfs://f0{id}") {
_mint(msg.sender, DECENTRAFISH, 1, "");
_mint(msg.sender, NFThistory, 1, "");
_mint(msg.sender, IPFSforscher, 1, "");
}
function mint(
address to,
uint256 id,
uint256 amount,
bytes memory data
) public virtual {
_mint(msg.sender, id, amount, data);
}
function uint2hexstr(uint256 i) public pure returns (string memory) {
if (i == 0) return "0";
uint j = i;
uint length;
while (j != 0) {
length++;
j = j >> 4;
}
uint mask = 15;
bytes memory bstr = new bytes(length);
uint k = length;
while (i != 0) {
uint curr = (i & mask);
bstr[--k] = curr > 9 ?
bytes1(uint8(55 + curr)) :
bytes1(uint8(48 + curr)); // 55 = 65 - 10
i = i >> 4;
}
return string(bstr);
}
function uri(uint256 _tokenID) override public view returns (string memory) {
string memory hexstringtokenID;
hexstringtokenID = uint2hexstr(_tokenID);
return string(
abi.encodePacked(
"ipfs://f0",
hexstringtokenID)
);
}
}
@chrisalmeida
Copy link

Hi, @soenkeba. What would it take to convert the result from below into a v0 CID within the same contract?

hexstringtokenID = uint2hexstr(_tokenID);

@weeliem
Copy link

weeliem commented Dec 30, 2021

Hi sir, may i know how can i apply this idea into my dapp marketplace which has a form to create new NFT token? And the frontend is actually built using VueJS.

Does that mean i need to convert the IPFS CID to hex format upon upload using nodejs? I can't find any nodejs library which allowed us to convert the CID into hex.

@jamesmccomish
Copy link

jamesmccomish commented Mar 12, 2022

+1 on above.. I've spent quite a while trying to work how to get the CID into hex in js but haven't managed.

I checked https://cid.ipfs.io/ and I see a 'DIGEST (HEX)' for my CID which I think could be the correct value but I'm not sure. Is there some library out there that can help?

PS. Very cool work around to the fixed erc1155 uri limitation!

@tarassh
Copy link

tarassh commented Apr 22, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment