Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom URI's for ERC721WithData Contract #60

Merged
merged 3 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions samples/solidity/contracts/ERC721WithData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

pragma solidity ^0.8.0;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if "NoData" and "WithData" are not the best names at this point. Really "NoData" is "the minimum viable ERC implementation that can still work somewhat with FireFly" and "WithData" is "all the extra recommended stuff for full FireFly functionality".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like @jimthematrix suggested, we can turn the NoData contracts into just ERC20 and ERC721. What are thoughts for the WithData contracts being called ERC721FireFly? 🤷🏻

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still prefer "WithData" since it's more specific to the difference compared to nodata. plus giving it the "firefly" suffix makes it sounds as though they are supposed to be the contract of choice for using with FireFly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback guys, I've wrote up your comments in this issue: #63. Going to go ahead and merge this PR.

import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
import '@openzeppelin/contracts/utils/Context.sol';
import '@openzeppelin/contracts/access/Ownable.sol';
import '@openzeppelin/contracts/utils/Context.sol';
import "@openzeppelin/contracts/utils/Strings.sol";
import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
import './IERC721WithData.sol';

/**
Expand All @@ -23,7 +24,17 @@ import './IERC721WithData.sol';
* This is a sample only and NOT a reference implementation.
shorsher marked this conversation as resolved.
Show resolved Hide resolved
*/
contract ERC721WithData is Context, Ownable, ERC721, IERC721WithData {
constructor(string memory name, string memory symbol) ERC721(name, symbol) {}
string private _baseTokenURI;
// Optional mapping for token URIs
mapping (uint256 => string) private _tokenURIs;

constructor(
string memory name,
string memory symbol,
string memory baseTokenURI
) ERC721(name, symbol) {
_baseTokenURI = baseTokenURI;
}

function supportsInterface(
shorsher marked this conversation as resolved.
Show resolved Hide resolved
bytes4 interfaceId
Expand All @@ -39,6 +50,24 @@ contract ERC721WithData is Context, Ownable, ERC721, IERC721WithData {
bytes calldata data
) external override onlyOwner {
_safeMint(to, tokenId, data);
_setTokenURI(tokenId, string(abi.encodePacked(_baseURI(), Strings.toString(tokenId))));
}

function mintWithURI(
address to,
uint256 tokenId,
bytes calldata data,
string memory tokenURI_
) external {
shorsher marked this conversation as resolved.
Show resolved Hide resolved
_safeMint(to, tokenId, data);

// If there is no tokenURI passed, concatenate the tokenID to the base URI
bytes memory tempURITest = bytes(tokenURI_);
if (tempURITest.length == 0) {
_setTokenURI(tokenId, string(abi.encodePacked(_baseURI(), Strings.toString(tokenId))));
} else {
_setTokenURI(tokenId, tokenURI_);
}
}

function transferWithData(
Expand Down Expand Up @@ -76,6 +105,23 @@ contract ERC721WithData is Context, Ownable, ERC721, IERC721WithData {
}

function _baseURI() internal view virtual override returns (string memory) {
return 'firefly://token/';
bytes memory tempURITest = bytes(_baseTokenURI);
if (tempURITest.length == 0) {
shorsher marked this conversation as resolved.
Show resolved Hide resolved
return 'firefly://token/';
} else {
return _baseTokenURI;
}
}

function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
require(_exists(tokenId), "ERC721WithData: Token does not exist");

string memory uri = _tokenURIs[tokenId];
return uri;
}

function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
shorsher marked this conversation as resolved.
Show resolved Hide resolved
require(_exists(tokenId), "ERC721WithData: Token does not exist");
_tokenURIs[tokenId] = _tokenURI;
}
}
5 changes: 3 additions & 2 deletions samples/solidity/contracts/TokenFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ contract TokenFactory is Context {
string memory name,
shorsher marked this conversation as resolved.
Show resolved Hide resolved
string memory symbol,
bool is_fungible,
bytes calldata data
bytes calldata data,
string memory uri
shorsher marked this conversation as resolved.
Show resolved Hide resolved
) external virtual {
if (is_fungible) {
ERC20WithData erc20 = new ERC20WithData(name, symbol);
erc20.transferOwnership(_msgSender());
emit TokenPoolCreation(address(erc20), name, symbol, true, data);
} else {
ERC721WithData erc721 = new ERC721WithData(name, symbol);
ERC721WithData erc721 = new ERC721WithData(name, symbol, uri);
erc721.transferOwnership(_msgSender());
emit TokenPoolCreation(address(erc721), name, symbol, false, data);
}
Expand Down
6 changes: 4 additions & 2 deletions samples/solidity/test/ERC721WithData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('ERC721WithData - Unit Tests', function () {
deployedERC721WithData = await Factory.connect(deployerSignerA).deploy(
contractName,
contractSymbol,
""
);
await deployedERC721WithData.deployed();
});
Expand All @@ -37,18 +38,19 @@ describe('ERC721WithData - Unit Tests', function () {
expect(await deployedERC721WithData.symbol()).to.equal(contractSymbol);
});

it('Mint - Deployer should mint tokens to itself successfully', async function () {
it('Mint - Should mint successfully with a custom URI', async function () {
expect(await deployedERC721WithData.balanceOf(deployerSignerA.address)).to.equal(0);
// Signer A mint token 721 to Signer A (Allowed)
await expect(
deployedERC721WithData
.connect(deployerSignerA)
.mintWithData(deployerSignerA.address, 721, '0x00'),
.mintWithURI(deployerSignerA.address, 721, '0x00', "ipfs://CID"),
)
.to.emit(deployedERC721WithData, 'Transfer')
.withArgs(ZERO_ADDRESS, deployerSignerA.address, 721);

expect(await deployedERC721WithData.balanceOf(deployerSignerA.address)).to.equal(1);
expect(await deployedERC721WithData.tokenURI(721)).to.equal('ipfs://CID');
});

it('Mint - Non-deployer of contract should not be able to mint tokens', async function () {
Expand Down
19 changes: 15 additions & 4 deletions samples/solidity/test/TokenFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { TokenFactory } from '../typechain';
describe('TokenFactory - Unit Tests', function () {
const contractName = 'testName';
const contractSymbol = 'testSymbol';
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
const ONE_ADDRESS = '0x1111111111111111111111111111111111111111';
let deployedTokenFactory: TokenFactory;
let Factory;

Expand All @@ -24,7 +22,7 @@ describe('TokenFactory - Unit Tests', function () {
});

it('Create - Should deploy a new ERC20 contract', async function () {
const tx = await deployedTokenFactory.create(contractName, contractSymbol, true, '0x00');
const tx = await deployedTokenFactory.create(contractName, contractSymbol, true, '0x00', '');
expect(tx).to.emit(deployedTokenFactory, 'TokenPoolCreation');
const receipt = await tx.wait();
const event = receipt.events?.find(e => e.event === 'TokenPoolCreation');
Expand All @@ -37,7 +35,20 @@ describe('TokenFactory - Unit Tests', function () {
});

it('Create - Should deploy a new ERC721 contract', async function () {
const tx = await deployedTokenFactory.create(contractName, contractSymbol, false, '0x00');
const tx = await deployedTokenFactory.create(contractName, contractSymbol, false, '0x00', '');
expect(tx).to.emit(deployedTokenFactory, 'TokenPoolCreation');
const receipt = await tx.wait();
const event = receipt.events?.find(e => e.event === 'TokenPoolCreation');
expect(event).to.exist;
if (event) {
expect(event.args).to.have.length(5);
expect(event.args?.[0]).to.be.properAddress;
expect(event.args?.slice(1)).to.eql([contractName, contractSymbol, false, '0x00']);
}
});

it('Create - Should deploy a new ERC721 contract with a custom URI', async function () {
const tx = await deployedTokenFactory.create(contractName, contractSymbol, false, '0x00', 'testURI');
expect(tx).to.emit(deployedTokenFactory, 'TokenPoolCreation');
const receipt = await tx.wait();
const event = receipt.events?.find(e => e.event === 'TokenPoolCreation');
Expand Down