|
@@ -0,0 +1,338 @@
|
|
|
+const { BN, expectEvent, constants, expectRevert } = require('@openzeppelin/test-helpers');
|
|
|
+const { expect } = require('chai');
|
|
|
+const { keccakFromString, bufferToHex } = require('ethereumjs-util');
|
|
|
+
|
|
|
+const { shouldBehaveLikeERC721 } = require('../ERC721.behavior');
|
|
|
+
|
|
|
+const ERC721 = artifacts.require('$ERC721');
|
|
|
+const ERC721Wrapper = artifacts.require('$ERC721Wrapper');
|
|
|
+
|
|
|
+contract('ERC721Wrapper', function (accounts) {
|
|
|
+ const [initialHolder, anotherAccount, approvedAccount] = accounts;
|
|
|
+
|
|
|
+ const name = 'My Token';
|
|
|
+ const symbol = 'MTKN';
|
|
|
+ const firstTokenId = new BN(1);
|
|
|
+ const secondTokenId = new BN(2);
|
|
|
+
|
|
|
+ beforeEach(async function () {
|
|
|
+ this.underlying = await ERC721.new(name, symbol);
|
|
|
+ this.token = await ERC721Wrapper.new(`Wrapped ${name}`, `W${symbol}`, this.underlying.address);
|
|
|
+
|
|
|
+ await this.underlying.$_safeMint(initialHolder, firstTokenId);
|
|
|
+ await this.underlying.$_safeMint(initialHolder, secondTokenId);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('has a name', async function () {
|
|
|
+ expect(await this.token.name()).to.equal(`Wrapped ${name}`);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('has a symbol', async function () {
|
|
|
+ expect(await this.token.symbol()).to.equal(`W${symbol}`);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('has underlying', async function () {
|
|
|
+ expect(await this.token.underlying()).to.be.bignumber.equal(this.underlying.address);
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('depositFor', function () {
|
|
|
+ it('works with token approval', async function () {
|
|
|
+ await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
|
|
|
+
|
|
|
+ const { tx } = await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
+ from: initialHolder,
|
|
|
+ to: this.token.address,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: constants.ZERO_ADDRESS,
|
|
|
+ to: initialHolder,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('works with approval for all', async function () {
|
|
|
+ await this.underlying.setApprovalForAll(this.token.address, true, { from: initialHolder });
|
|
|
+
|
|
|
+ const { tx } = await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
+ from: initialHolder,
|
|
|
+ to: this.token.address,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: constants.ZERO_ADDRESS,
|
|
|
+ to: initialHolder,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('works sending to another account', async function () {
|
|
|
+ await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
|
|
|
+
|
|
|
+ const { tx } = await this.token.depositFor(anotherAccount, [firstTokenId], { from: initialHolder });
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
+ from: initialHolder,
|
|
|
+ to: this.token.address,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: constants.ZERO_ADDRESS,
|
|
|
+ to: anotherAccount,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('works with multiple tokens', async function () {
|
|
|
+ await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
|
|
|
+ await this.underlying.approve(this.token.address, secondTokenId, { from: initialHolder });
|
|
|
+
|
|
|
+ const { tx } = await this.token.depositFor(initialHolder, [firstTokenId, secondTokenId], { from: initialHolder });
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
+ from: initialHolder,
|
|
|
+ to: this.token.address,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: constants.ZERO_ADDRESS,
|
|
|
+ to: initialHolder,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
+ from: initialHolder,
|
|
|
+ to: this.token.address,
|
|
|
+ tokenId: secondTokenId,
|
|
|
+ });
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: constants.ZERO_ADDRESS,
|
|
|
+ to: initialHolder,
|
|
|
+ tokenId: secondTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('reverts with missing approval', async function () {
|
|
|
+ await expectRevert(
|
|
|
+ this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder }),
|
|
|
+ 'ERC721: caller is not token owner or approved',
|
|
|
+ );
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('withdrawTo', function () {
|
|
|
+ beforeEach(async function () {
|
|
|
+ await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
|
|
|
+ await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('works for an owner', async function () {
|
|
|
+ const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: initialHolder });
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
+ from: this.token.address,
|
|
|
+ to: initialHolder,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: initialHolder,
|
|
|
+ to: constants.ZERO_ADDRESS,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('works for an approved', async function () {
|
|
|
+ await this.token.approve(approvedAccount, firstTokenId, { from: initialHolder });
|
|
|
+
|
|
|
+ const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: approvedAccount });
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
+ from: this.token.address,
|
|
|
+ to: initialHolder,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: initialHolder,
|
|
|
+ to: constants.ZERO_ADDRESS,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('works for an approved for all', async function () {
|
|
|
+ await this.token.setApprovalForAll(approvedAccount, true, { from: initialHolder });
|
|
|
+
|
|
|
+ const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: approvedAccount });
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
+ from: this.token.address,
|
|
|
+ to: initialHolder,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: initialHolder,
|
|
|
+ to: constants.ZERO_ADDRESS,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it("doesn't work for a non-owner nor approved", async function () {
|
|
|
+ await expectRevert(
|
|
|
+ this.token.withdrawTo(initialHolder, [firstTokenId], { from: anotherAccount }),
|
|
|
+ 'ERC721Wrapper: caller is not token owner or approved',
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ it('works with multiple tokens', async function () {
|
|
|
+ await this.underlying.approve(this.token.address, secondTokenId, { from: initialHolder });
|
|
|
+ await this.token.depositFor(initialHolder, [secondTokenId], { from: initialHolder });
|
|
|
+
|
|
|
+ const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId, secondTokenId], { from: initialHolder });
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
+ from: this.token.address,
|
|
|
+ to: initialHolder,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: initialHolder,
|
|
|
+ to: constants.ZERO_ADDRESS,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('works to another account', async function () {
|
|
|
+ const { tx } = await this.token.withdrawTo(anotherAccount, [firstTokenId], { from: initialHolder });
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
|
|
|
+ from: this.token.address,
|
|
|
+ to: anotherAccount,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: initialHolder,
|
|
|
+ to: constants.ZERO_ADDRESS,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('onERC721Received', function () {
|
|
|
+ const WRAPPER_ACCEPT_MAGIC = bufferToHex(keccakFromString('WRAPPER_ACCEPT_MAGIC')).slice(0, 26); // Include 0x
|
|
|
+
|
|
|
+ const magicWithAddresss = address =>
|
|
|
+ web3.utils.encodePacked(
|
|
|
+ {
|
|
|
+ value: WRAPPER_ACCEPT_MAGIC,
|
|
|
+ type: 'bytes12',
|
|
|
+ },
|
|
|
+ {
|
|
|
+ value: address,
|
|
|
+ type: 'address',
|
|
|
+ },
|
|
|
+ );
|
|
|
+
|
|
|
+ it('only allows calls from underlying', async function () {
|
|
|
+ await expectRevert(
|
|
|
+ this.token.onERC721Received(
|
|
|
+ initialHolder,
|
|
|
+ this.token.address,
|
|
|
+ firstTokenId,
|
|
|
+ magicWithAddresss(anotherAccount), // Correct data
|
|
|
+ { from: anotherAccount },
|
|
|
+ ),
|
|
|
+ 'ERC721Wrapper: caller is not underlying',
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('when data length is > 0', function () {
|
|
|
+ it('reverts with arbitrary data', async function () {
|
|
|
+ await expectRevert(
|
|
|
+ this.underlying.methods['safeTransferFrom(address,address,uint256,bytes)'](
|
|
|
+ initialHolder,
|
|
|
+ this.token.address,
|
|
|
+ firstTokenId,
|
|
|
+ '0x0123',
|
|
|
+ {
|
|
|
+ from: initialHolder,
|
|
|
+ },
|
|
|
+ ),
|
|
|
+ 'ERC721Wrapper: Invalid data format',
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ it('reverts with the magic value and data length different to 32', async function () {
|
|
|
+ await expectRevert(
|
|
|
+ this.underlying.methods['safeTransferFrom(address,address,uint256,bytes)'](
|
|
|
+ initialHolder,
|
|
|
+ this.token.address,
|
|
|
+ firstTokenId,
|
|
|
+ WRAPPER_ACCEPT_MAGIC, // Reverts for any non-32 bytes value
|
|
|
+ {
|
|
|
+ from: initialHolder,
|
|
|
+ },
|
|
|
+ ),
|
|
|
+ 'ERC721Wrapper: Invalid data format',
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ it('mints token to specific holder with address after magic value', async function () {
|
|
|
+ const { tx } = await this.underlying.methods['safeTransferFrom(address,address,uint256,bytes)'](
|
|
|
+ initialHolder,
|
|
|
+ this.token.address,
|
|
|
+ firstTokenId,
|
|
|
+ magicWithAddresss(anotherAccount),
|
|
|
+ {
|
|
|
+ from: initialHolder,
|
|
|
+ },
|
|
|
+ );
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: constants.ZERO_ADDRESS,
|
|
|
+ to: anotherAccount,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('mints a token to from if no data is specified', async function () {
|
|
|
+ const { tx } = await this.underlying.safeTransferFrom(initialHolder, this.token.address, firstTokenId, {
|
|
|
+ from: initialHolder,
|
|
|
+ });
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: constants.ZERO_ADDRESS,
|
|
|
+ to: initialHolder,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('_recover', function () {
|
|
|
+ it('works if there is something to recover', async function () {
|
|
|
+ // Should use `transferFrom` to avoid `onERC721Received` minting
|
|
|
+ await this.underlying.transferFrom(initialHolder, this.token.address, firstTokenId, { from: initialHolder });
|
|
|
+
|
|
|
+ const { tx } = await this.token.$_recover(anotherAccount, firstTokenId);
|
|
|
+
|
|
|
+ await expectEvent.inTransaction(tx, this.token, 'Transfer', {
|
|
|
+ from: constants.ZERO_ADDRESS,
|
|
|
+ to: anotherAccount,
|
|
|
+ tokenId: firstTokenId,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ it('reverts if there is nothing to recover', async function () {
|
|
|
+ await expectRevert(
|
|
|
+ this.token.$_recover(initialHolder, firstTokenId),
|
|
|
+ 'ERC721Wrapper: wrapper is not token owner',
|
|
|
+ );
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ describe('ERC712 behavior', function () {
|
|
|
+ shouldBehaveLikeERC721('ERC721', ...accounts);
|
|
|
+ });
|
|
|
+});
|