ERC721BasicToken.behavior.js 21 KB

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