|
| 1 | +const { expect } = require('chai'); |
| 2 | + |
| 3 | +describe('ERC721ABurnable', function () { |
| 4 | + |
| 5 | + beforeEach(async function () { |
| 6 | + this.ERC721ABurnable = await ethers.getContractFactory('ERC721ABurnableMock'); |
| 7 | + this.token = await this.ERC721ABurnable.deploy('Azuki', 'AZUKI'); |
| 8 | + await this.token.deployed(); |
| 9 | + }); |
| 10 | + |
| 11 | + beforeEach(async function () { |
| 12 | + const [owner, addr1, addr2] = await ethers.getSigners(); |
| 13 | + this.owner = owner; |
| 14 | + this.addr1 = addr1; |
| 15 | + this.addr2 = addr2; |
| 16 | + this.numTestTokens = 10; |
| 17 | + this.burnedTokenId = 5; |
| 18 | + await this.token['safeMint(address,uint256)'](this.addr1.address, this.numTestTokens); |
| 19 | + await this.token.connect(this.addr1).burn(this.burnedTokenId); |
| 20 | + }); |
| 21 | + |
| 22 | + it('changes exists', async function () { |
| 23 | + expect(await this.token.exists(this.burnedTokenId)).to.be.false; |
| 24 | + }); |
| 25 | + |
| 26 | + it('cannot burn a non-existing token', async function () { |
| 27 | + const query = this.token.connect(this.addr1).burn(this.numTestTokens); |
| 28 | + await expect(query).to.be.revertedWith( |
| 29 | + 'OwnerQueryForNonexistentToken' |
| 30 | + ); |
| 31 | + }); |
| 32 | + |
| 33 | + it('cannot burn a burned token', async function () { |
| 34 | + const query = this.token.connect(this.addr1).burn(this.burnedTokenId); |
| 35 | + await expect(query).to.be.revertedWith( |
| 36 | + 'OwnerQueryForNonexistentToken' |
| 37 | + ); |
| 38 | + }) |
| 39 | + |
| 40 | + it('cannot transfer a burned token', async function () { |
| 41 | + const query = this.token.connect(this.addr1) |
| 42 | + .transferFrom(this.addr1.address, this.addr2.address, this.burnedTokenId); |
| 43 | + await expect(query).to.be.revertedWith( |
| 44 | + 'OwnerQueryForNonexistentToken' |
| 45 | + ); |
| 46 | + }) |
| 47 | + |
| 48 | + it('reduces totalSupply', async function () { |
| 49 | + const supplyBefore = await this.token.totalSupply(); |
| 50 | + for (let i = 0; i < 2; ++i) { |
| 51 | + await this.token.connect(this.addr1).burn(i); |
| 52 | + expect(supplyBefore - (await this.token.totalSupply())).to.equal(i + 1); |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + it('adjusts owners tokens by index', async function () { |
| 57 | + const n = await this.token.totalSupply(); |
| 58 | + for (let i = 0; i < this.burnedTokenId; ++i) { |
| 59 | + expect(await this.token.tokenByIndex(i)).to.be.equal(i); |
| 60 | + } |
| 61 | + for (let i = this.burnedTokenId; i < n; ++i) { |
| 62 | + expect(await this.token.tokenByIndex(i)).to.be.equal(i + 1); |
| 63 | + } |
| 64 | + // tokenIds of addr1: [0,1,2,3,4,6,7,8,9] |
| 65 | + expect(await this.token.tokenOfOwnerByIndex(this.addr1.address, 2)) |
| 66 | + .to.be.equal(2); |
| 67 | + await this.token.connect(this.addr1).burn(2); |
| 68 | + // tokenIds of addr1: [0,1,3,4,6,7,8,9] |
| 69 | + expect(await this.token.tokenOfOwnerByIndex(this.addr1.address, 2)) |
| 70 | + .to.be.equal(3); |
| 71 | + await this.token.connect(this.addr1).burn(0); |
| 72 | + // tokenIds of addr1: [1,3,4,6,7,8,9] |
| 73 | + expect(await this.token.tokenOfOwnerByIndex(this.addr1.address, 2)) |
| 74 | + .to.be.equal(4); |
| 75 | + await this.token.connect(this.addr1).burn(3); |
| 76 | + // tokenIds of addr1: [1,4,6,7,8,9] |
| 77 | + expect(await this.token.tokenOfOwnerByIndex(this.addr1.address, 2)) |
| 78 | + .to.be.equal(6); |
| 79 | + }) |
| 80 | + |
| 81 | + it('adjusts owners balances', async function () { |
| 82 | + expect(await this.token.balanceOf(this.addr1.address)) |
| 83 | + .to.be.equal(this.numTestTokens - 1); |
| 84 | + }); |
| 85 | + |
| 86 | + it('adjusts token by index', async function () { |
| 87 | + const n = await this.token.totalSupply(); |
| 88 | + for (let i = 0; i < this.burnedTokenId; ++i) { |
| 89 | + expect(await this.token.tokenByIndex(i)).to.be.equal(i); |
| 90 | + } |
| 91 | + for (let i = this.burnedTokenId; i < n; ++i) { |
| 92 | + expect(await this.token.tokenByIndex(i)).to.be.equal(i + 1); |
| 93 | + } |
| 94 | + await expect(this.token.tokenByIndex(n)).to.be.revertedWith( |
| 95 | + 'TokenIndexOutOfBounds' |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('ownerships correctly set', async function () { |
| 100 | + it('with token before previously burnt token transfered and burned', async function () { |
| 101 | + const tokenIdToBurn = this.burnedTokenId - 1; |
| 102 | + await this.token.connect(this.addr1) |
| 103 | + .transferFrom(this.addr1.address, this.addr2.address, tokenIdToBurn); |
| 104 | + expect(await this.token.ownerOf(tokenIdToBurn)).to.be.equal(this.addr2.address); |
| 105 | + await this.token.connect(this.addr2).burn(tokenIdToBurn); |
| 106 | + for (let i = 0; i < this.numTestTokens; ++i) { |
| 107 | + if (i == tokenIdToBurn || i == this.burnedTokenId) { |
| 108 | + await expect(this.token.ownerOf(i)).to.be.revertedWith( |
| 109 | + 'OwnerQueryForNonexistentToken' |
| 110 | + ); |
| 111 | + } else { |
| 112 | + expect(await this.token.ownerOf(i)).to.be.equal(this.addr1.address); |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + it('with token after previously burnt token transfered and burned', async function () { |
| 118 | + const tokenIdToBurn = this.burnedTokenId + 1; |
| 119 | + await this.token.connect(this.addr1) |
| 120 | + .transferFrom(this.addr1.address, this.addr2.address, tokenIdToBurn); |
| 121 | + expect(await this.token.ownerOf(tokenIdToBurn)).to.be.equal(this.addr2.address); |
| 122 | + await this.token.connect(this.addr2).burn(tokenIdToBurn); |
| 123 | + for (let i = 0; i < this.numTestTokens; ++i) { |
| 124 | + if (i == tokenIdToBurn || i == this.burnedTokenId) { |
| 125 | + await expect(this.token.ownerOf(i)).to.be.revertedWith( |
| 126 | + 'OwnerQueryForNonexistentToken' |
| 127 | + ) |
| 128 | + } else { |
| 129 | + expect(await this.token.ownerOf(i)).to.be.equal(this.addr1.address); |
| 130 | + } |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + it('with first token burned', async function () { |
| 135 | + await this.token.connect(this.addr1).burn(0); |
| 136 | + for (let i = 0; i < this.numTestTokens; ++i) { |
| 137 | + if (i == 0 || i == this.burnedTokenId) { |
| 138 | + await expect(this.token.ownerOf(i)).to.be.revertedWith( |
| 139 | + 'OwnerQueryForNonexistentToken' |
| 140 | + ) |
| 141 | + } else { |
| 142 | + expect(await this.token.ownerOf(i)).to.be.equal(this.addr1.address); |
| 143 | + } |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + it('with last token burned', async function () { |
| 148 | + await expect(this.token.ownerOf(this.numTestTokens)).to.be.revertedWith( |
| 149 | + 'OwnerQueryForNonexistentToken' |
| 150 | + ) |
| 151 | + await this.token.connect(this.addr1).burn(this.numTestTokens - 1); |
| 152 | + await expect(this.token.ownerOf(this.numTestTokens - 1)).to.be.revertedWith( |
| 153 | + 'OwnerQueryForNonexistentToken' |
| 154 | + ) |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
0 commit comments