ERC721BasicToken.behaviour.js 21 KB

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