ERC721Token.test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. import assertRevert from '../../helpers/assertRevert';
  2. const BigNumber = web3.BigNumber;
  3. const ERC721Token = artifacts.require('ERC721TokenMock.sol');
  4. require('chai')
  5. .use(require('chai-as-promised'))
  6. .use(require('chai-bignumber')(BigNumber))
  7. .should();
  8. contract('ERC721Token', accounts => {
  9. let token = null;
  10. const _firstTokenId = 1;
  11. const _secondTokenId = 2;
  12. const _unknownTokenId = 3;
  13. const _creator = accounts[0];
  14. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  15. beforeEach(async function () {
  16. token = await ERC721Token.new({ from: _creator });
  17. await token.mint(_creator, _firstTokenId, { from: _creator });
  18. await token.mint(_creator, _secondTokenId, { from: _creator });
  19. });
  20. describe('totalSupply', function () {
  21. it('has a total supply equivalent to the inital supply', async function () {
  22. const totalSupply = await token.totalSupply();
  23. totalSupply.should.be.bignumber.equal(2);
  24. });
  25. });
  26. describe('balanceOf', function () {
  27. describe('when the given address owns some tokens', function () {
  28. it('returns the amount of tokens owned by the given address', async function () {
  29. const balance = await token.balanceOf(_creator);
  30. balance.should.be.bignumber.equal(2);
  31. });
  32. });
  33. describe('when the given address does not own any tokens', function () {
  34. it('returns 0', async function () {
  35. const balance = await token.balanceOf(accounts[1]);
  36. balance.should.be.bignumber.equal(0);
  37. });
  38. });
  39. });
  40. describe('ownerOf', function () {
  41. describe('when the given token ID was tracked by this token', function () {
  42. const tokenId = _firstTokenId;
  43. it('returns the owner of the given token ID', async function () {
  44. const owner = await token.ownerOf(tokenId);
  45. owner.should.be.equal(_creator);
  46. });
  47. });
  48. describe('when the given token ID was not tracked by this token', function () {
  49. const tokenId = _unknownTokenId;
  50. it('reverts', async function () {
  51. await assertRevert(token.ownerOf(tokenId));
  52. });
  53. });
  54. });
  55. describe('mint', function () {
  56. describe('when the given token ID was not tracked by this contract', function () {
  57. const tokenId = _unknownTokenId;
  58. describe('when the given address is not the zero address', function () {
  59. const to = accounts[1];
  60. it('mints the given token ID to the given address', async function () {
  61. const previousBalance = await token.balanceOf(to);
  62. await token.mint(to, tokenId);
  63. const owner = await token.ownerOf(tokenId);
  64. owner.should.be.equal(to);
  65. const balance = await token.balanceOf(to);
  66. balance.should.be.bignumber.equal(previousBalance + 1);
  67. });
  68. it('adds that token to the token list of the owner', async function () {
  69. await token.mint(to, tokenId);
  70. const tokens = await token.tokensOf(to);
  71. tokens.length.should.be.equal(1);
  72. tokens[0].should.be.bignumber.equal(tokenId);
  73. });
  74. it('emits a transfer event', async function () {
  75. const { logs } = await token.mint(to, tokenId);
  76. logs.length.should.be.equal(1);
  77. logs[0].event.should.be.eq('Transfer');
  78. logs[0].args._from.should.be.equal(ZERO_ADDRESS);
  79. logs[0].args._to.should.be.equal(to);
  80. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  81. });
  82. });
  83. describe('when the given address is the zero address', function () {
  84. const to = ZERO_ADDRESS;
  85. it('reverts', async function () {
  86. await assertRevert(token.mint(to, tokenId));
  87. });
  88. });
  89. });
  90. describe('when the given token ID was already tracked by this contract', function () {
  91. const tokenId = _firstTokenId;
  92. it('reverts', async function () {
  93. await assertRevert(token.mint(accounts[1], tokenId));
  94. });
  95. });
  96. });
  97. describe('burn', function () {
  98. describe('when the given token ID was tracked by this contract', function () {
  99. const tokenId = _firstTokenId;
  100. describe('when the msg.sender owns given token', function () {
  101. const sender = _creator;
  102. it('burns the given token ID and adjusts the balance of the owner', async function () {
  103. const previousBalance = await token.balanceOf(sender);
  104. await token.burn(tokenId, { from: sender });
  105. await assertRevert(token.ownerOf(tokenId));
  106. const balance = await token.balanceOf(sender);
  107. balance.should.be.bignumber.equal(previousBalance - 1);
  108. });
  109. it('removes that token from the token list of the owner', async function () {
  110. await token.burn(tokenId, { from: sender });
  111. const tokens = await token.tokensOf(sender);
  112. tokens.length.should.be.equal(1);
  113. tokens[0].should.be.bignumber.equal(_secondTokenId);
  114. });
  115. it('emits a burn event', async function () {
  116. const { logs } = await token.burn(tokenId, { from: sender });
  117. logs.length.should.be.equal(1);
  118. logs[0].event.should.be.eq('Transfer');
  119. logs[0].args._from.should.be.equal(sender);
  120. logs[0].args._to.should.be.equal(ZERO_ADDRESS);
  121. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  122. });
  123. describe('when there is an approval for the given token ID', function () {
  124. beforeEach(async function () {
  125. await token.approve(accounts[1], tokenId, { from: sender });
  126. });
  127. it('clears the approval', async function () {
  128. await token.burn(tokenId, { from: sender });
  129. const approvedAccount = await token.approvedFor(tokenId);
  130. approvedAccount.should.be.equal(ZERO_ADDRESS);
  131. });
  132. it('emits an approval event', async function () {
  133. const { logs } = await token.burn(tokenId, { from: sender });
  134. logs.length.should.be.equal(2);
  135. logs[0].event.should.be.eq('Approval');
  136. logs[0].args._owner.should.be.equal(sender);
  137. logs[0].args._approved.should.be.equal(ZERO_ADDRESS);
  138. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  139. });
  140. });
  141. });
  142. describe('when the msg.sender does not own given token', function () {
  143. const sender = accounts[1];
  144. it('reverts', async function () {
  145. await assertRevert(token.burn(tokenId, { from: sender }));
  146. });
  147. });
  148. });
  149. describe('when the given token ID was not tracked by this contract', function () {
  150. const tokenID = _unknownTokenId;
  151. it('reverts', async function () {
  152. await assertRevert(token.burn(tokenID, { from: _creator }));
  153. });
  154. });
  155. });
  156. describe('transfer', function () {
  157. describe('when the address to transfer the token to is not the zero address', function () {
  158. const to = accounts[1];
  159. describe('when the given token ID was tracked by this token', function () {
  160. const tokenId = _firstTokenId;
  161. describe('when the msg.sender is the owner of the given token ID', function () {
  162. const sender = _creator;
  163. it('transfers the ownership of the given token ID to the given address', async function () {
  164. await token.transfer(to, tokenId, { from: sender });
  165. const newOwner = await token.ownerOf(tokenId);
  166. newOwner.should.be.equal(to);
  167. });
  168. it('clears the approval for the token ID', async function () {
  169. await token.approve(accounts[2], tokenId, { from: sender });
  170. await token.transfer(to, tokenId, { from: sender });
  171. const approvedAccount = await token.approvedFor(tokenId);
  172. approvedAccount.should.be.equal(ZERO_ADDRESS);
  173. });
  174. it('emits an approval and transfer events', async function () {
  175. const { logs } = await token.transfer(to, tokenId, { from: sender });
  176. logs.length.should.be.equal(2);
  177. logs[0].event.should.be.eq('Approval');
  178. logs[0].args._owner.should.be.equal(sender);
  179. logs[0].args._approved.should.be.equal(ZERO_ADDRESS);
  180. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  181. logs[1].event.should.be.eq('Transfer');
  182. logs[1].args._from.should.be.equal(sender);
  183. logs[1].args._to.should.be.equal(to);
  184. logs[1].args._tokenId.should.be.bignumber.equal(tokenId);
  185. });
  186. it('adjusts owners balances', async function () {
  187. const previousBalance = await token.balanceOf(sender);
  188. await token.transfer(to, tokenId, { from: sender });
  189. const newOwnerBalance = await token.balanceOf(to);
  190. newOwnerBalance.should.be.bignumber.equal(1);
  191. const previousOwnerBalance = await token.balanceOf(_creator);
  192. previousOwnerBalance.should.be.bignumber.equal(previousBalance - 1);
  193. });
  194. it('adds the token to the tokens list of the new owner', async function () {
  195. await token.transfer(to, tokenId, { from: sender });
  196. const tokenIDs = await token.tokensOf(to);
  197. tokenIDs.length.should.be.equal(1);
  198. tokenIDs[0].should.be.bignumber.equal(tokenId);
  199. });
  200. });
  201. describe('when the msg.sender is not the owner of the given token ID', function () {
  202. const sender = accounts[2];
  203. it('reverts', async function () {
  204. await assertRevert(token.transfer(to, tokenId, { from: sender }));
  205. });
  206. });
  207. });
  208. describe('when the given token ID was not tracked by this token', function () {
  209. let tokenId = _unknownTokenId;
  210. it('reverts', async function () {
  211. await assertRevert(token.transfer(to, tokenId, { from: _creator }));
  212. });
  213. });
  214. });
  215. describe('when the address to transfer the token to is the zero address', function () {
  216. const to = ZERO_ADDRESS;
  217. it('reverts', async function () {
  218. await assertRevert(token.transfer(to, 0, { from: _creator }));
  219. });
  220. });
  221. });
  222. describe('approve', function () {
  223. describe('when the given token ID was already tracked by this contract', function () {
  224. const tokenId = _firstTokenId;
  225. describe('when the sender owns the given token ID', function () {
  226. const sender = _creator;
  227. describe('when the address that receives the approval is the 0 address', function () {
  228. const to = ZERO_ADDRESS;
  229. describe('when there was no approval for the given token ID before', function () {
  230. it('clears the approval for that token', async function () {
  231. await token.approve(to, tokenId, { from: sender });
  232. const approvedAccount = await token.approvedFor(tokenId);
  233. approvedAccount.should.be.equal(to);
  234. });
  235. it('does not emit an approval event', async function () {
  236. const { logs } = await token.approve(to, tokenId, { from: sender });
  237. logs.length.should.be.equal(0);
  238. });
  239. });
  240. describe('when the given token ID was approved for another account', function () {
  241. beforeEach(async function () {
  242. await token.approve(accounts[2], tokenId, { from: sender });
  243. });
  244. it('clears the approval for the token ID', async function () {
  245. await token.approve(to, tokenId, { from: sender });
  246. const approvedAccount = await token.approvedFor(tokenId);
  247. approvedAccount.should.be.equal(to);
  248. });
  249. it('emits an approval event', async function () {
  250. const { logs } = await token.approve(to, tokenId, { from: sender });
  251. logs.length.should.be.equal(1);
  252. logs[0].event.should.be.eq('Approval');
  253. logs[0].args._owner.should.be.equal(sender);
  254. logs[0].args._approved.should.be.equal(to);
  255. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  256. });
  257. });
  258. });
  259. describe('when the address that receives the approval is not the 0 address', function () {
  260. describe('when the address that receives the approval is different than the owner', function () {
  261. const to = accounts[1];
  262. describe('when there was no approval for the given token ID before', function () {
  263. it('approves the token ID to the given address', async function () {
  264. await token.approve(to, tokenId, { from: sender });
  265. const approvedAccount = await token.approvedFor(tokenId);
  266. approvedAccount.should.be.equal(to);
  267. });
  268. it('emits an approval event', async function () {
  269. const { logs } = await token.approve(to, tokenId, { from: sender });
  270. logs.length.should.be.equal(1);
  271. logs[0].event.should.be.eq('Approval');
  272. logs[0].args._owner.should.be.equal(sender);
  273. logs[0].args._approved.should.be.equal(to);
  274. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  275. });
  276. });
  277. describe('when the given token ID was approved for the same account', function () {
  278. beforeEach(async function () {
  279. await token.approve(to, tokenId, { from: sender });
  280. });
  281. it('keeps the approval to the given address', async function () {
  282. await token.approve(to, tokenId, { from: sender });
  283. const approvedAccount = await token.approvedFor(tokenId);
  284. approvedAccount.should.be.equal(to);
  285. });
  286. it('emits an approval event', async function () {
  287. const { logs } = await token.approve(to, tokenId, { from: sender });
  288. logs.length.should.be.equal(1);
  289. logs[0].event.should.be.eq('Approval');
  290. logs[0].args._owner.should.be.equal(sender);
  291. logs[0].args._approved.should.be.equal(to);
  292. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  293. });
  294. });
  295. describe('when the given token ID was approved for another account', function () {
  296. beforeEach(async function () {
  297. await token.approve(accounts[2], tokenId, { from: sender });
  298. });
  299. it('changes the approval to the given address', async function () {
  300. await token.approve(to, tokenId, { from: sender });
  301. const approvedAccount = await token.approvedFor(tokenId);
  302. approvedAccount.should.be.equal(to);
  303. });
  304. it('emits an approval event', async function () {
  305. const { logs } = await token.approve(to, tokenId, { from: sender });
  306. logs.length.should.be.equal(1);
  307. logs[0].event.should.be.eq('Approval');
  308. logs[0].args._owner.should.be.equal(sender);
  309. logs[0].args._approved.should.be.equal(to);
  310. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  311. });
  312. });
  313. });
  314. describe('when the address that receives the approval is the owner', function () {
  315. const to = _creator;
  316. describe('when there was no approval for the given token ID before', function () {
  317. it('reverts', async function () {
  318. await assertRevert(token.approve(to, tokenId, { from: sender }));
  319. });
  320. });
  321. describe('when the given token ID was approved for another account', function () {
  322. beforeEach(async function () {
  323. await token.approve(accounts[2], tokenId, { from: sender });
  324. });
  325. it('reverts', async function () {
  326. await assertRevert(token.approve(to, tokenId, { from: sender }));
  327. });
  328. });
  329. });
  330. });
  331. });
  332. describe('when the sender does not own the given token ID', function () {
  333. const sender = accounts[1];
  334. it('reverts', async function () {
  335. await assertRevert(token.approve(accounts[2], tokenId, { from: sender }));
  336. });
  337. });
  338. });
  339. describe('when the given token ID was not tracked by the contract before', function () {
  340. const tokenId = _unknownTokenId;
  341. it('reverts', async function () {
  342. await assertRevert(token.approve(accounts[1], tokenId, { from: _creator }));
  343. });
  344. });
  345. });
  346. describe('takeOwnership', function () {
  347. describe('when the given token ID was already tracked by this contract', function () {
  348. const tokenId = _firstTokenId;
  349. describe('when the sender has the approval for the token ID', function () {
  350. const sender = accounts[1];
  351. beforeEach(async function () {
  352. await token.approve(sender, tokenId, { from: _creator });
  353. });
  354. it('transfers the ownership of the given token ID to the given address', async function () {
  355. await token.takeOwnership(tokenId, { from: sender });
  356. const newOwner = await token.ownerOf(tokenId);
  357. newOwner.should.be.equal(sender);
  358. });
  359. it('clears the approval for the token ID', async function () {
  360. await token.takeOwnership(tokenId, { from: sender });
  361. const approvedAccount = await token.approvedFor(tokenId);
  362. approvedAccount.should.be.equal(ZERO_ADDRESS);
  363. });
  364. it('emits an approval and transfer events', async function () {
  365. const { logs } = await token.takeOwnership(tokenId, { from: sender });
  366. logs.length.should.be.equal(2);
  367. logs[0].event.should.be.eq('Approval');
  368. logs[0].args._owner.should.be.equal(_creator);
  369. logs[0].args._approved.should.be.equal(ZERO_ADDRESS);
  370. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  371. logs[1].event.should.be.eq('Transfer');
  372. logs[1].args._from.should.be.equal(_creator);
  373. logs[1].args._to.should.be.equal(sender);
  374. logs[1].args._tokenId.should.be.bignumber.equal(tokenId);
  375. });
  376. it('adjusts owners balances', async function () {
  377. const previousBalance = await token.balanceOf(_creator);
  378. await token.takeOwnership(tokenId, { from: sender });
  379. const newOwnerBalance = await token.balanceOf(sender);
  380. newOwnerBalance.should.be.bignumber.equal(1);
  381. const previousOwnerBalance = await token.balanceOf(_creator);
  382. previousOwnerBalance.should.be.bignumber.equal(previousBalance - 1);
  383. });
  384. it('adds the token to the tokens list of the new owner', async function () {
  385. await token.takeOwnership(tokenId, { from: sender });
  386. const tokenIDs = await token.tokensOf(sender);
  387. tokenIDs.length.should.be.equal(1);
  388. tokenIDs[0].should.be.bignumber.equal(tokenId);
  389. });
  390. });
  391. describe('when the sender does not have an approval for the token ID', function () {
  392. const sender = accounts[1];
  393. it('reverts', async function () {
  394. await assertRevert(token.takeOwnership(tokenId, { from: sender }));
  395. });
  396. });
  397. describe('when the sender is already the owner of the token', function () {
  398. const sender = _creator;
  399. it('reverts', async function () {
  400. await assertRevert(token.takeOwnership(tokenId, { from: sender }));
  401. });
  402. });
  403. });
  404. describe('when the given token ID was not tracked by the contract before', function () {
  405. const tokenId = _unknownTokenId;
  406. it('reverts', async function () {
  407. await assertRevert(token.takeOwnership(tokenId, { from: _creator }));
  408. });
  409. });
  410. });
  411. });