ERC721BasicToken.behavior.js 21 KB

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