Unverified Commit 956d6632 by Francisco Giordano Committed by GitHub

ERC1155 feature pending tasks (#2014)

* Initial ERC1155 implementation with some tests (#1803)

* Initial ERC1155 implementation with some tests

* Remove mocked isERC1155TokenReceiver

* Revert reason edit nit

* Remove parameters associated with isERC1155TokenReceiver call

* Add tests for approvals and single transfers

* Add tests for transferring to contracts

* Add tests for batch transfers

* Make expectEvent.inTransaction tests async

* Renamed "owner" to "account" and "holder"

* Document unspecified balanceOfBatch reversion on zero behavior

* Ensure accounts can't set their own operator status

* Specify descriptive messages for underflow errors

* Bring SafeMath.add calls in line with OZ style

* Explicitly prevent _burn on the zero account

* Implement batch minting/burning

* Refactored operator approval check into isApprovedForAll calls

* Renamed ERC1155TokenReceiver to ERC1155Receiver

* Added ERC1155Holder

* Fix lint issues

* Migrate tests to @openzeppelin/test-environment

* Port ERC 1155 branch to Solidity 0.6 (and current master) (#2130)

* port ERC1155 to Solidity 0.6

* make ERC1155 constructor more similar to ERC721 one

* also migrate mock contracts to Solidity 0.6

* mark all non-view functions as virtual

Co-authored-by: Alan Lu <alanlu1023@gmail.com>
Co-authored-by: Nicolás Venturo <nicolas.venturo@gmail.com>
Co-authored-by: Robert Kaiser <kairo@kairo.at>
parent 0c7b2ec0
pragma solidity ^0.6.0;
import "../token/ERC1155/ERC1155.sol";
/**
* @title ERC1155Mock
* This mock just publicizes internal functions for testing purposes
*/
contract ERC1155Mock is ERC1155 {
function mint(address to, uint256 id, uint256 value, bytes memory data) public {
_mint(to, id, value, data);
}
function mintBatch(address to, uint256[] memory ids, uint256[] memory values, bytes memory data) public {
_mintBatch(to, ids, values, data);
}
function burn(address owner, uint256 id, uint256 value) public {
_burn(owner, id, value);
}
function burnBatch(address owner, uint256[] memory ids, uint256[] memory values) public {
_burnBatch(owner, ids, values);
}
function doSafeTransferAcceptanceCheck(address operator, address from, address to, uint256 id, uint256 value, bytes memory data) public {
_doSafeTransferAcceptanceCheck(operator, from, to, id, value, data);
}
function doSafeBatchTransferAcceptanceCheck(address operator, address from, address to, uint256[] memory ids, uint256[] memory values, bytes memory data) public {
_doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, values, data);
}
}
pragma solidity ^0.6.0;
import "../token/ERC1155/IERC1155Receiver.sol";
import "./ERC165Mock.sol";
contract ERC1155ReceiverMock is IERC1155Receiver, ERC165Mock {
bytes4 private _recRetval;
bool private _recReverts;
bytes4 private _batRetval;
bool private _batReverts;
event Received(address operator, address from, uint256 id, uint256 value, bytes data, uint256 gas);
event BatchReceived(address operator, address from, uint256[] ids, uint256[] values, bytes data, uint256 gas);
constructor (
bytes4 recRetval,
bool recReverts,
bytes4 batRetval,
bool batReverts
)
public
{
_recRetval = recRetval;
_recReverts = recReverts;
_batRetval = batRetval;
_batReverts = batReverts;
}
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
)
external
override
returns(bytes4)
{
require(!_recReverts, "ERC1155ReceiverMock: reverting on receive");
emit Received(operator, from, id, value, data, gasleft());
return _recRetval;
}
function onERC1155BatchReceived(
address operator,
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
)
external
override
returns(bytes4)
{
require(!_batReverts, "ERC1155ReceiverMock: reverting on batch receive");
emit BatchReceived(operator, from, ids, values, data, gasleft());
return _batRetval;
}
}
pragma solidity ^0.6.0;
import "./ERC1155Receiver.sol";
contract ERC1155Holder is ERC1155Receiver {
function onERC1155Received(address, address, uint256, uint256, bytes calldata) external override virtual returns (bytes4)
{
return this.onERC1155Received.selector;
}
function onERC1155BatchReceived(address, address, uint256[] calldata, uint256[] calldata, bytes calldata) external override virtual returns (bytes4)
{
return this.onERC1155BatchReceived.selector;
}
}
pragma solidity ^0.6.0;
import "./IERC1155Receiver.sol";
import "../../introspection/ERC165.sol";
abstract contract ERC1155Receiver is ERC165, IERC1155Receiver {
constructor() public {
_registerInterface(
ERC1155Receiver(0).onERC1155Received.selector ^
ERC1155Receiver(0).onERC1155BatchReceived.selector
);
}
}
pragma solidity ^0.6.0;
import "../../introspection/IERC165.sol";
/**
@title ERC-1155 Multi Token Standard basic interface
@dev See https://eips.ethereum.org/EIPS/eip-1155
*/
abstract contract IERC1155 is IERC165 {
event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);
event TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values);
event ApprovalForAll(address indexed account, address indexed operator, bool approved);
event URI(string value, uint256 indexed id);
function balanceOf(address account, uint256 id) public view virtual returns (uint256);
function balanceOfBatch(address[] memory accounts, uint256[] memory ids) public view virtual returns (uint256[] memory);
function setApprovalForAll(address operator, bool approved) external virtual;
function isApprovedForAll(address account, address operator) external view virtual returns (bool);
function safeTransferFrom(address from, address to, uint256 id, uint256 value, bytes calldata data) external virtual;
function safeBatchTransferFrom(address from, address to, uint256[] calldata ids, uint256[] calldata values, bytes calldata data) external virtual;
}
pragma solidity ^0.6.0;
import "../../introspection/IERC165.sol";
/**
@title ERC-1155 Multi Token Receiver Interface
@dev See https://eips.ethereum.org/EIPS/eip-1155
*/
interface IERC1155Receiver is IERC165 {
/**
@dev Handles the receipt of a single ERC1155 token type. This function is
called at the end of a `safeTransferFrom` after the balance has been updated.
To accept the transfer, this must return
`bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
(i.e. 0xf23a6e61, or its own function selector).
@param operator The address which initiated the transfer (i.e. msg.sender)
@param from The address which previously owned the token
@param id The ID of the token being transferred
@param value The amount of tokens being transferred
@param data Additional data with no specified format
@return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` if transfer is allowed
*/
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
)
external
returns(bytes4);
/**
@dev Handles the receipt of a multiple ERC1155 token types. This function
is called at the end of a `safeBatchTransferFrom` after the balances have
been updated. To accept the transfer(s), this must return
`bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
(i.e. 0xbc197c81, or its own function selector).
@param operator The address which initiated the batch transfer (i.e. msg.sender)
@param from The address which previously owned the token
@param ids An array containing ids of each token being transferred (order and length must match values array)
@param values An array containing amounts of each token being transferred (order and length must match ids array)
@param data Additional data with no specified format
@return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` if transfer is allowed
*/
function onERC1155BatchReceived(
address operator,
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
)
external
returns(bytes4);
}
---
sections:
- title: Core
contracts:
- IERC1155
- ERC1155
- IERC1155Receiver
---
This set of interfaces and contracts are all related to the [ERC1155 Multi Token Standard](https://eips.ethereum.org/EIPS/eip-1155).
The EIP consists of two interfaces which fulfill different roles, found here as `IERC1155` and `IERC1155Receiver`. Only `IERC1155` is required for a contract to be ERC1155 compliant. The basic functionality is implemented in `ERC1155`.
...@@ -27,6 +27,14 @@ const INTERFACES = { ...@@ -27,6 +27,14 @@ const INTERFACES = {
'symbol()', 'symbol()',
'tokenURI(uint256)', 'tokenURI(uint256)',
], ],
ERC1155: [
'balanceOf(address,uint256)',
'balanceOfBatch(address[],uint256[])',
'setApprovalForAll(address,bool)',
'isApprovedForAll(address,address)',
'safeTransferFrom(address,address,uint256,uint256,bytes)',
'safeBatchTransferFrom(address,address,uint256[],uint256[],bytes)',
],
}; };
const INTERFACE_IDS = {}; const INTERFACE_IDS = {};
......
const { accounts, contract } = require('@openzeppelin/test-environment');
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;
const { expect } = require('chai');
const { shouldBehaveLikeERC1155 } = require('./ERC1155.behavior');
const ERC1155Mock = contract.fromArtifact('ERC1155Mock');
describe('ERC1155', function () {
const [creator, tokenHolder, tokenBatchHolder, ...otherAccounts] = accounts;
beforeEach(async function () {
this.token = await ERC1155Mock.new({ from: creator });
});
shouldBehaveLikeERC1155(otherAccounts);
describe('internal functions', function () {
const tokenId = new BN(1990);
const mintAmount = new BN(9001);
const burnAmount = new BN(3000);
const tokenBatchIds = [new BN(2000), new BN(2010), new BN(2020)];
const mintAmounts = [new BN(5000), new BN(10000), new BN(42195)];
const burnAmounts = [new BN(5000), new BN(9001), new BN(195)];
const data = '0xcafebabe';
describe('_mint(address, uint256, uint256, bytes memory)', function () {
it('reverts with a null destination address', async function () {
await expectRevert(
this.token.mint(ZERO_ADDRESS, tokenId, mintAmount, data),
'ERC1155: mint to the zero address'
);
});
context('with minted tokens', function () {
beforeEach(async function () {
({ logs: this.logs } = await this.token.mint(
tokenHolder,
tokenId,
mintAmount,
data,
{ from: creator }
));
});
it('emits a TransferSingle event', function () {
expectEvent.inLogs(this.logs, 'TransferSingle', {
operator: creator,
from: ZERO_ADDRESS,
to: tokenHolder,
id: tokenId,
value: mintAmount,
});
});
it('credits the minted amount of tokens', async function () {
expect(await this.token.balanceOf(
tokenHolder,
tokenId
)).to.be.bignumber.equal(mintAmount);
});
});
});
describe('_mintBatch(address, uint256[] memory, uint256[] memory, bytes memory)', function () {
it('reverts with a null destination address', async function () {
await expectRevert(
this.token.mintBatch(ZERO_ADDRESS, tokenBatchIds, mintAmounts, data),
'ERC1155: batch mint to the zero address'
);
});
it('reverts if length of inputs do not match', async function () {
await expectRevert(
this.token.mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts.slice(1), data),
'ERC1155: minted IDs and values must have same lengths'
);
});
context('with minted batch of tokens', function () {
beforeEach(async function () {
({ logs: this.logs } = await this.token.mintBatch(
tokenBatchHolder,
tokenBatchIds,
mintAmounts,
data,
{ from: creator }
));
});
it('emits a TransferBatch event', function () {
expectEvent.inLogs(this.logs, 'TransferBatch', {
operator: creator,
from: ZERO_ADDRESS,
to: tokenBatchHolder,
// ids: tokenBatchIds,
// values: mintAmounts,
});
});
it('credits the minted batch of tokens', async function () {
const holderBatchBalances = await this.token.balanceOfBatch(
new Array(tokenBatchIds.length).fill(tokenBatchHolder),
tokenBatchIds
);
for (let i = 0; i < holderBatchBalances.length; i++) {
expect(holderBatchBalances[i]).to.be.bignumber.equal(mintAmounts[i]);
}
});
});
});
describe('_burn(address, uint256, uint256)', function () {
it('reverts when burning the zero account\'s tokens', async function () {
await expectRevert(
this.token.burn(ZERO_ADDRESS, tokenId, mintAmount),
'ERC1155: attempting to burn tokens on zero account'
);
});
it('reverts when burning a non-existent token id', async function () {
await expectRevert(
this.token.burn(tokenHolder, tokenId, mintAmount),
'ERC1155: attempting to burn more than balance'
);
});
context('with minted-then-burnt tokens', function () {
beforeEach(async function () {
await this.token.mint(tokenHolder, tokenId, mintAmount, data);
({ logs: this.logs } = await this.token.burn(
tokenHolder,
tokenId,
burnAmount,
{ from: creator }
));
});
it('emits a TransferSingle event', function () {
expectEvent.inLogs(this.logs, 'TransferSingle', {
operator: creator,
from: tokenHolder,
to: ZERO_ADDRESS,
id: tokenId,
value: burnAmount,
});
});
it('accounts for both minting and burning', async function () {
expect(await this.token.balanceOf(
tokenHolder,
tokenId
)).to.be.bignumber.equal(mintAmount.sub(burnAmount));
});
});
});
describe('_burnBatch(address, uint256[] memory, uint256[] memory)', function () {
it('reverts when burning the zero account\'s tokens', async function () {
await expectRevert(
this.token.burnBatch(ZERO_ADDRESS, tokenBatchIds, burnAmounts),
'ERC1155: attempting to burn batch of tokens on zero account'
);
});
it('reverts if length of inputs do not match', async function () {
await expectRevert(
this.token.burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts.slice(1)),
'ERC1155: burnt IDs and values must have same lengths'
);
});
it('reverts when burning a non-existent token id', async function () {
await expectRevert(
this.token.burnBatch(tokenBatchHolder, tokenBatchIds, burnAmounts),
'ERC1155: attempting to burn more than balance for some token'
);
});
context('with minted-then-burnt tokens', function () {
beforeEach(async function () {
await this.token.mintBatch(tokenBatchHolder, tokenBatchIds, mintAmounts, data);
({ logs: this.logs } = await this.token.burnBatch(
tokenBatchHolder,
tokenBatchIds,
burnAmounts,
{ from: creator }
));
});
it('emits a TransferBatch event', function () {
expectEvent.inLogs(this.logs, 'TransferBatch', {
operator: creator,
from: tokenBatchHolder,
to: ZERO_ADDRESS,
// ids: tokenBatchIds,
// values: burnAmounts,
});
});
it('accounts for both minting and burning', async function () {
const holderBatchBalances = await this.token.balanceOfBatch(
new Array(tokenBatchIds.length).fill(tokenBatchHolder),
tokenBatchIds
);
for (let i = 0; i < holderBatchBalances.length; i++) {
expect(holderBatchBalances[i]).to.be.bignumber.equal(mintAmounts[i].sub(burnAmounts[i]));
}
});
});
});
});
});
const { accounts, contract } = require('@openzeppelin/test-environment');
const { BN } = require('@openzeppelin/test-helpers');
const ERC1155Holder = contract.fromArtifact('ERC1155Holder');
const ERC1155Mock = contract.fromArtifact('ERC1155Mock');
const { expect } = require('chai');
describe('ERC1155Holder', function () {
const [creator] = accounts;
it('receives ERC1155 tokens', async function () {
const multiToken = await ERC1155Mock.new({ from: creator });
const multiTokenIds = [new BN(1), new BN(2), new BN(3)];
const multiTokenAmounts = [new BN(1000), new BN(2000), new BN(3000)];
await multiToken.mintBatch(creator, multiTokenIds, multiTokenAmounts, '0x', { from: creator });
const transferData = '0xf00dbabe';
const holder = await ERC1155Holder.new();
await multiToken.safeTransferFrom(
creator,
holder.address,
multiTokenIds[0],
multiTokenAmounts[0],
transferData,
{ from: creator },
);
expect(await multiToken.balanceOf(holder.address, multiTokenIds[0])).to.be.bignumber.equal(multiTokenAmounts[0]);
await multiToken.safeBatchTransferFrom(
creator,
holder.address,
multiTokenIds.slice(1),
multiTokenAmounts.slice(1),
transferData,
{ from: creator },
);
for (let i = 1; i < multiTokenIds.length; i++) {
expect(await multiToken.balanceOf(holder.address, multiTokenIds[i])).to.be.bignumber.equal(multiTokenAmounts[i]);
}
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment