ERC721BasicToken.behavior.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior');
  2. const { assertRevert } = require('../../helpers/assertRevert');
  3. const { decodeLogs } = require('../../helpers/decodeLogs');
  4. const { sendTransaction } = require('../../helpers/sendTransaction');
  5. const _ = require('lodash');
  6. const ERC721Receiver = artifacts.require('ERC721ReceiverMock.sol');
  7. const BigNumber = web3.BigNumber;
  8. require('chai')
  9. .use(require('chai-bignumber')(BigNumber))
  10. .should();
  11. function shouldBehaveLikeERC721BasicToken (accounts) {
  12. const firstTokenId = 1;
  13. const secondTokenId = 2;
  14. const unknownTokenId = 3;
  15. const creator = accounts[0];
  16. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  17. const RECEIVER_MAGIC_VALUE = '0x150b7a02';
  18. describe('like an ERC721BasicToken', function () {
  19. beforeEach(async function () {
  20. await this.token.mint(creator, firstTokenId, { from: creator });
  21. await this.token.mint(creator, secondTokenId, { from: creator });
  22. });
  23. describe('balanceOf', function () {
  24. context('when the given address owns some tokens', function () {
  25. it('returns the amount of tokens owned by the given address', async function () {
  26. (await this.token.balanceOf(creator)).should.be.bignumber.equal(2);
  27. });
  28. });
  29. context('when the given address does not own any tokens', function () {
  30. it('returns 0', async function () {
  31. (await this.token.balanceOf(accounts[1])).should.be.bignumber.equal(0);
  32. });
  33. });
  34. context('when querying the zero address', function () {
  35. it('throws', async function () {
  36. await assertRevert(this.token.balanceOf(0));
  37. });
  38. });
  39. });
  40. describe('ownerOf', function () {
  41. context('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. (await this.token.ownerOf(tokenId)).should.be.equal(creator);
  45. });
  46. });
  47. context('when the given token ID was not tracked by this token', function () {
  48. const tokenId = unknownTokenId;
  49. it('reverts', async function () {
  50. await assertRevert(this.token.ownerOf(tokenId));
  51. });
  52. });
  53. });
  54. describe('transfers', function () {
  55. const owner = accounts[0];
  56. const approved = accounts[2];
  57. const operator = accounts[3];
  58. const unauthorized = accounts[4];
  59. const tokenId = firstTokenId;
  60. const data = '0x42';
  61. let logs = null;
  62. beforeEach(async function () {
  63. this.to = accounts[1];
  64. await this.token.approve(approved, tokenId, { from: owner });
  65. await this.token.setApprovalForAll(operator, true, { from: owner });
  66. });
  67. const transferWasSuccessful = function ({ owner, tokenId, approved }) {
  68. it('transfers the ownership of the given token ID to the given address', async function () {
  69. (await this.token.ownerOf(tokenId)).should.be.equal(this.to);
  70. });
  71. it('clears the approval for the token ID', async function () {
  72. (await this.token.getApproved(tokenId)).should.be.equal(ZERO_ADDRESS);
  73. });
  74. if (approved) {
  75. it('emit only a transfer event', async function () {
  76. logs.length.should.be.equal(1);
  77. logs[0].event.should.be.equal('Transfer');
  78. logs[0].args._from.should.be.equal(owner);
  79. logs[0].args._to.should.be.equal(this.to);
  80. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  81. });
  82. } else {
  83. it('emits only a transfer event', async function () {
  84. logs.length.should.be.equal(1);
  85. logs[0].event.should.be.equal('Transfer');
  86. logs[0].args._from.should.be.equal(owner);
  87. logs[0].args._to.should.be.equal(this.to);
  88. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  89. });
  90. }
  91. it('adjusts owners balances', async function () {
  92. (await this.token.balanceOf(this.to)).should.be.bignumber.equal(1);
  93. (await this.token.balanceOf(owner)).should.be.bignumber.equal(1);
  94. });
  95. it('adjusts owners tokens by index', async function () {
  96. if (!this.token.tokenOfOwnerByIndex) return;
  97. (await this.token.tokenOfOwnerByIndex(this.to, 0)).toNumber().should.be.equal(tokenId);
  98. (await this.token.tokenOfOwnerByIndex(owner, 0)).toNumber().should.not.be.eq(tokenId);
  99. });
  100. };
  101. const shouldTransferTokensByUsers = function (transferFunction) {
  102. context('when called by the owner', function () {
  103. beforeEach(async function () {
  104. ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: owner }));
  105. });
  106. transferWasSuccessful({ owner, tokenId, approved });
  107. });
  108. context('when called by the approved individual', function () {
  109. beforeEach(async function () {
  110. ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: approved }));
  111. });
  112. transferWasSuccessful({ owner, tokenId, approved });
  113. });
  114. context('when called by the operator', function () {
  115. beforeEach(async function () {
  116. ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: operator }));
  117. });
  118. transferWasSuccessful({ owner, tokenId, approved });
  119. });
  120. context('when called by the owner without an approved user', function () {
  121. beforeEach(async function () {
  122. await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
  123. ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: operator }));
  124. });
  125. transferWasSuccessful({ owner, tokenId, approved: null });
  126. });
  127. context('when sent to the owner', function () {
  128. beforeEach(async function () {
  129. ({ logs } = await transferFunction.call(this, owner, owner, tokenId, { from: owner }));
  130. });
  131. it('keeps ownership of the token', async function () {
  132. (await this.token.ownerOf(tokenId)).should.be.equal(owner);
  133. });
  134. it('clears the approval for the token ID', async function () {
  135. (await this.token.getApproved(tokenId)).should.be.equal(ZERO_ADDRESS);
  136. });
  137. it('emits only a transfer event', async function () {
  138. logs.length.should.be.equal(1);
  139. logs[0].event.should.be.equal('Transfer');
  140. logs[0].args._from.should.be.equal(owner);
  141. logs[0].args._to.should.be.equal(owner);
  142. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  143. });
  144. it('keeps the owner balance', async function () {
  145. (await this.token.balanceOf(owner)).should.be.bignumber.equal(2);
  146. });
  147. it('keeps same tokens by index', async function () {
  148. if (!this.token.tokenOfOwnerByIndex) return;
  149. const tokensListed = await Promise.all(_.range(2).map(i => this.token.tokenOfOwnerByIndex(owner, i)));
  150. tokensListed.map(t => t.toNumber()).should.have.members([firstTokenId, secondTokenId]);
  151. });
  152. });
  153. context('when the address of the previous owner is incorrect', function () {
  154. it('reverts', async function () {
  155. await assertRevert(transferFunction.call(this, unauthorized, this.to, tokenId, { from: owner }));
  156. });
  157. });
  158. context('when the sender is not authorized for the token id', function () {
  159. it('reverts', async function () {
  160. await assertRevert(transferFunction.call(this, owner, this.to, tokenId, { from: unauthorized }));
  161. });
  162. });
  163. context('when the given token ID does not exist', function () {
  164. it('reverts', async function () {
  165. await assertRevert(transferFunction.call(this, owner, this.to, unknownTokenId, { from: owner }));
  166. });
  167. });
  168. context('when the address to transfer the token to is the zero address', function () {
  169. it('reverts', async function () {
  170. await assertRevert(transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }));
  171. });
  172. });
  173. };
  174. describe('via transferFrom', function () {
  175. shouldTransferTokensByUsers(function (from, to, tokenId, opts) {
  176. return this.token.transferFrom(from, to, tokenId, opts);
  177. });
  178. });
  179. describe('via safeTransferFrom', function () {
  180. const safeTransferFromWithData = function (from, to, tokenId, opts) {
  181. return sendTransaction(
  182. this.token,
  183. 'safeTransferFrom',
  184. 'address,address,uint256,bytes',
  185. [from, to, tokenId, data],
  186. opts
  187. );
  188. };
  189. const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
  190. return this.token.safeTransferFrom(from, to, tokenId, opts);
  191. };
  192. const shouldTransferSafely = function (transferFun, data) {
  193. describe('to a user account', function () {
  194. shouldTransferTokensByUsers(transferFun);
  195. });
  196. describe('to a valid receiver contract', function () {
  197. beforeEach(async function () {
  198. this.receiver = await ERC721Receiver.new(RECEIVER_MAGIC_VALUE, false);
  199. this.to = this.receiver.address;
  200. });
  201. shouldTransferTokensByUsers(transferFun);
  202. it('should call onERC721Received', async function () {
  203. const result = await transferFun.call(this, owner, this.to, tokenId, { from: owner });
  204. result.receipt.logs.length.should.be.equal(2);
  205. const [log] = decodeLogs([result.receipt.logs[1]], ERC721Receiver, this.receiver.address);
  206. log.event.should.be.equal('Received');
  207. log.args._operator.should.be.equal(owner);
  208. log.args._from.should.be.equal(owner);
  209. log.args._tokenId.toNumber().should.be.equal(tokenId);
  210. log.args._data.should.be.equal(data);
  211. });
  212. it('should call onERC721Received from approved', async function () {
  213. const result = await transferFun.call(this, owner, this.to, tokenId, { from: approved });
  214. result.receipt.logs.length.should.be.equal(2);
  215. const [log] = decodeLogs([result.receipt.logs[1]], ERC721Receiver, this.receiver.address);
  216. log.event.should.be.equal('Received');
  217. log.args._operator.should.be.equal(approved);
  218. log.args._from.should.be.equal(owner);
  219. log.args._tokenId.toNumber().should.be.equal(tokenId);
  220. log.args._data.should.be.equal(data);
  221. });
  222. describe('with an invalid token id', function () {
  223. it('reverts', async function () {
  224. await assertRevert(
  225. transferFun.call(
  226. this,
  227. owner,
  228. this.to,
  229. unknownTokenId,
  230. { from: owner },
  231. )
  232. );
  233. });
  234. });
  235. });
  236. };
  237. describe('with data', function () {
  238. shouldTransferSafely(safeTransferFromWithData, data);
  239. });
  240. describe('without data', function () {
  241. shouldTransferSafely(safeTransferFromWithoutData, '0x');
  242. });
  243. describe('to a receiver contract returning unexpected value', function () {
  244. it('reverts', async function () {
  245. const invalidReceiver = await ERC721Receiver.new('0x42', false);
  246. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  247. });
  248. });
  249. describe('to a receiver contract that throws', function () {
  250. it('reverts', async function () {
  251. const invalidReceiver = await ERC721Receiver.new(RECEIVER_MAGIC_VALUE, true);
  252. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  253. });
  254. });
  255. describe('to a contract that does not implement the required function', function () {
  256. it('reverts', async function () {
  257. const invalidReceiver = this.token;
  258. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  259. });
  260. });
  261. });
  262. });
  263. describe('approve', function () {
  264. const tokenId = firstTokenId;
  265. const sender = creator;
  266. const to = accounts[1];
  267. let logs = null;
  268. const itClearsApproval = function () {
  269. it('clears approval for the token', async function () {
  270. (await this.token.getApproved(tokenId)).should.be.equal(ZERO_ADDRESS);
  271. });
  272. };
  273. const itApproves = function (address) {
  274. it('sets the approval for the target address', async function () {
  275. (await this.token.getApproved(tokenId)).should.be.equal(address);
  276. });
  277. };
  278. const itEmitsApprovalEvent = function (address) {
  279. it('emits an approval event', async function () {
  280. logs.length.should.be.equal(1);
  281. logs[0].event.should.be.equal('Approval');
  282. logs[0].args._owner.should.be.equal(sender);
  283. logs[0].args._approved.should.be.equal(address);
  284. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  285. });
  286. };
  287. context('when clearing approval', function () {
  288. context('when there was no prior approval', function () {
  289. beforeEach(async function () {
  290. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: sender }));
  291. });
  292. itClearsApproval();
  293. itEmitsApprovalEvent(ZERO_ADDRESS);
  294. });
  295. context('when there was a prior approval', function () {
  296. beforeEach(async function () {
  297. await this.token.approve(to, tokenId, { from: sender });
  298. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: sender }));
  299. });
  300. itClearsApproval();
  301. itEmitsApprovalEvent(ZERO_ADDRESS);
  302. });
  303. });
  304. context('when approving a non-zero address', function () {
  305. context('when there was no prior approval', function () {
  306. beforeEach(async function () {
  307. ({ logs } = await this.token.approve(to, tokenId, { from: sender }));
  308. });
  309. itApproves(to);
  310. itEmitsApprovalEvent(to);
  311. });
  312. context('when there was a prior approval to the same address', function () {
  313. beforeEach(async function () {
  314. await this.token.approve(to, tokenId, { from: sender });
  315. ({ logs } = await this.token.approve(to, tokenId, { from: sender }));
  316. });
  317. itApproves(to);
  318. itEmitsApprovalEvent(to);
  319. });
  320. context('when there was a prior approval to a different address', function () {
  321. beforeEach(async function () {
  322. await this.token.approve(accounts[2], tokenId, { from: sender });
  323. ({ logs } = await this.token.approve(to, tokenId, { from: sender }));
  324. });
  325. itApproves(to);
  326. itEmitsApprovalEvent(to);
  327. });
  328. });
  329. context('when the address that receives the approval is the owner', function () {
  330. it('reverts', async function () {
  331. await assertRevert(this.token.approve(sender, tokenId, { from: sender }));
  332. });
  333. });
  334. context('when the sender does not own the given token ID', function () {
  335. it('reverts', async function () {
  336. await assertRevert(this.token.approve(to, tokenId, { from: accounts[2] }));
  337. });
  338. });
  339. context('when the sender is approved for the given token ID', function () {
  340. it('reverts', async function () {
  341. await this.token.approve(accounts[2], tokenId, { from: sender });
  342. await assertRevert(this.token.approve(to, tokenId, { from: accounts[2] }));
  343. });
  344. });
  345. context('when the sender is an operator', function () {
  346. const operator = accounts[2];
  347. beforeEach(async function () {
  348. await this.token.setApprovalForAll(operator, true, { from: sender });
  349. ({ logs } = await this.token.approve(to, tokenId, { from: operator }));
  350. });
  351. itApproves(to);
  352. itEmitsApprovalEvent(to);
  353. });
  354. context('when the given token ID does not exist', function () {
  355. it('reverts', async function () {
  356. await assertRevert(this.token.approve(to, unknownTokenId, { from: sender }));
  357. });
  358. });
  359. });
  360. describe('setApprovalForAll', function () {
  361. const sender = creator;
  362. context('when the operator willing to approve is not the owner', function () {
  363. const operator = accounts[1];
  364. context('when there is no operator approval set by the sender', function () {
  365. it('approves the operator', async function () {
  366. await this.token.setApprovalForAll(operator, true, { from: sender });
  367. (await this.token.isApprovedForAll(sender, operator)).should.equal(true);
  368. });
  369. it('emits an approval event', async function () {
  370. const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender });
  371. logs.length.should.be.equal(1);
  372. logs[0].event.should.be.equal('ApprovalForAll');
  373. logs[0].args._owner.should.be.equal(sender);
  374. logs[0].args._operator.should.be.equal(operator);
  375. logs[0].args._approved.should.equal(true);
  376. });
  377. });
  378. context('when the operator was set as not approved', function () {
  379. beforeEach(async function () {
  380. await this.token.setApprovalForAll(operator, false, { from: sender });
  381. });
  382. it('approves the operator', async function () {
  383. await this.token.setApprovalForAll(operator, true, { from: sender });
  384. (await this.token.isApprovedForAll(sender, operator)).should.equal(true);
  385. });
  386. it('emits an approval event', async function () {
  387. const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender });
  388. logs.length.should.be.equal(1);
  389. logs[0].event.should.be.equal('ApprovalForAll');
  390. logs[0].args._owner.should.be.equal(sender);
  391. logs[0].args._operator.should.be.equal(operator);
  392. logs[0].args._approved.should.equal(true);
  393. });
  394. it('can unset the operator approval', async function () {
  395. await this.token.setApprovalForAll(operator, false, { from: sender });
  396. (await this.token.isApprovedForAll(sender, operator)).should.equal(false);
  397. });
  398. });
  399. context('when the operator was already approved', function () {
  400. beforeEach(async function () {
  401. await this.token.setApprovalForAll(operator, true, { from: sender });
  402. });
  403. it('keeps the approval to the given address', async function () {
  404. await this.token.setApprovalForAll(operator, true, { from: sender });
  405. (await this.token.isApprovedForAll(sender, operator)).should.equal(true);
  406. });
  407. it('emits an approval event', async function () {
  408. const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender });
  409. logs.length.should.be.equal(1);
  410. logs[0].event.should.be.equal('ApprovalForAll');
  411. logs[0].args._owner.should.be.equal(sender);
  412. logs[0].args._operator.should.be.equal(operator);
  413. logs[0].args._approved.should.equal(true);
  414. });
  415. });
  416. });
  417. context('when the operator is the owner', function () {
  418. const operator = creator;
  419. it('reverts', async function () {
  420. await assertRevert(this.token.setApprovalForAll(operator, true, { from: sender }));
  421. });
  422. });
  423. });
  424. shouldSupportInterfaces([
  425. 'ERC165',
  426. 'ERC721',
  427. ]);
  428. });
  429. }
  430. module.exports = {
  431. shouldBehaveLikeERC721BasicToken,
  432. };