ERC721BasicToken.behaviour.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 = '0xf0b9e5ba';
  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._address.should.be.equal(owner);
  237. log.args._tokenId.toNumber().should.be.equal(tokenId);
  238. log.args._data.should.be.equal(data);
  239. });
  240. describe('with an invalid token id', function () {
  241. it('reverts', async function () {
  242. await assertRevert(
  243. transferFun.call(
  244. this,
  245. owner,
  246. this.to,
  247. unknownTokenId,
  248. { from: owner },
  249. )
  250. );
  251. });
  252. });
  253. });
  254. };
  255. describe('with data', function () {
  256. shouldTransferSafely(safeTransferFromWithData, data);
  257. });
  258. describe('without data', function () {
  259. shouldTransferSafely(safeTransferFromWithoutData, '0x');
  260. });
  261. describe('to a receiver contract returning unexpected value', function () {
  262. it('reverts', async function () {
  263. const invalidReceiver = await ERC721Receiver.new('0x42', false);
  264. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  265. });
  266. });
  267. describe('to a receiver contract that throws', function () {
  268. it('reverts', async function () {
  269. const invalidReceiver = await ERC721Receiver.new(RECEIVER_MAGIC_VALUE, true);
  270. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  271. });
  272. });
  273. describe('to a contract that does not implement the required function', function () {
  274. it('reverts', async function () {
  275. const invalidReceiver = this.token;
  276. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  277. });
  278. });
  279. });
  280. });
  281. describe('approve', function () {
  282. const tokenId = firstTokenId;
  283. const sender = creator;
  284. const to = accounts[1];
  285. let logs = null;
  286. const itClearsApproval = function () {
  287. it('clears approval for the token', async function () {
  288. const approvedAccount = await this.token.getApproved(tokenId);
  289. approvedAccount.should.be.equal(ZERO_ADDRESS);
  290. });
  291. };
  292. const itApproves = function (address) {
  293. it('sets the approval for the target address', async function () {
  294. const approvedAccount = await this.token.getApproved(tokenId);
  295. approvedAccount.should.be.equal(address);
  296. });
  297. };
  298. const itEmitsApprovalEvent = function (address) {
  299. it('emits an approval event', async function () {
  300. logs.length.should.be.equal(1);
  301. logs[0].event.should.be.eq('Approval');
  302. logs[0].args._owner.should.be.equal(sender);
  303. logs[0].args._approved.should.be.equal(address);
  304. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  305. });
  306. };
  307. context('when clearing approval', function () {
  308. context('when there was no prior approval', function () {
  309. beforeEach(async function () {
  310. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: sender }));
  311. });
  312. itClearsApproval();
  313. itEmitsApprovalEvent(ZERO_ADDRESS);
  314. });
  315. context('when there was a prior approval', function () {
  316. beforeEach(async function () {
  317. await this.token.approve(to, tokenId, { from: sender });
  318. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: sender }));
  319. });
  320. itClearsApproval();
  321. itEmitsApprovalEvent(ZERO_ADDRESS);
  322. });
  323. });
  324. context('when approving a non-zero address', function () {
  325. context('when there was no prior approval', function () {
  326. beforeEach(async function () {
  327. ({ logs } = await this.token.approve(to, tokenId, { from: sender }));
  328. });
  329. itApproves(to);
  330. itEmitsApprovalEvent(to);
  331. });
  332. context('when there was a prior approval to the same address', function () {
  333. beforeEach(async function () {
  334. await this.token.approve(to, tokenId, { from: sender });
  335. ({ logs } = await this.token.approve(to, tokenId, { from: sender }));
  336. });
  337. itApproves(to);
  338. itEmitsApprovalEvent(to);
  339. });
  340. context('when there was a prior approval to a different address', function () {
  341. beforeEach(async function () {
  342. await this.token.approve(accounts[2], tokenId, { from: sender });
  343. ({ logs } = await this.token.approve(to, tokenId, { from: sender }));
  344. });
  345. itApproves(to);
  346. itEmitsApprovalEvent(to);
  347. });
  348. });
  349. context('when the address that receives the approval is the owner', function () {
  350. it('reverts', async function () {
  351. await assertRevert(this.token.approve(sender, tokenId, { from: sender }));
  352. });
  353. });
  354. context('when the sender does not own the given token ID', function () {
  355. it('reverts', async function () {
  356. await assertRevert(this.token.approve(to, tokenId, { from: accounts[2] }));
  357. });
  358. });
  359. context('when the sender is approved for the given token ID', function () {
  360. it('reverts', async function () {
  361. await this.token.approve(accounts[2], tokenId, { from: sender });
  362. await assertRevert(this.token.approve(to, tokenId, { from: accounts[2] }));
  363. });
  364. });
  365. context('when the sender is an operator', function () {
  366. const operator = accounts[2];
  367. beforeEach(async function () {
  368. await this.token.setApprovalForAll(operator, true, { from: sender });
  369. ({ logs } = await this.token.approve(to, tokenId, { from: operator }));
  370. });
  371. itApproves(to);
  372. itEmitsApprovalEvent(to);
  373. });
  374. context('when the given token ID does not exist', function () {
  375. it('reverts', async function () {
  376. await assertRevert(this.token.approve(to, unknownTokenId, { from: sender }));
  377. });
  378. });
  379. });
  380. describe('setApprovalForAll', function () {
  381. const sender = creator;
  382. context('when the operator willing to approve is not the owner', function () {
  383. const operator = accounts[1];
  384. context('when there is no operator approval set by the sender', function () {
  385. it('approves the operator', async function () {
  386. await this.token.setApprovalForAll(operator, true, { from: sender });
  387. const isApproved = await this.token.isApprovedForAll(sender, operator);
  388. isApproved.should.be.true;
  389. });
  390. it('emits an approval event', async function () {
  391. const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender });
  392. logs.length.should.be.equal(1);
  393. logs[0].event.should.be.eq('ApprovalForAll');
  394. logs[0].args._owner.should.be.equal(sender);
  395. logs[0].args._operator.should.be.equal(operator);
  396. logs[0].args._approved.should.be.true;
  397. });
  398. });
  399. context('when the operator was set as not approved', function () {
  400. beforeEach(async function () {
  401. await this.token.setApprovalForAll(operator, false, { from: sender });
  402. });
  403. it('approves the operator', async function () {
  404. await this.token.setApprovalForAll(operator, true, { from: sender });
  405. const isApproved = await this.token.isApprovedForAll(sender, operator);
  406. isApproved.should.be.true;
  407. });
  408. it('emits an approval event', async function () {
  409. const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender });
  410. logs.length.should.be.equal(1);
  411. logs[0].event.should.be.eq('ApprovalForAll');
  412. logs[0].args._owner.should.be.equal(sender);
  413. logs[0].args._operator.should.be.equal(operator);
  414. logs[0].args._approved.should.be.true;
  415. });
  416. it('can unset the operator approval', async function () {
  417. await this.token.setApprovalForAll(operator, false, { from: sender });
  418. const isApproved = await this.token.isApprovedForAll(sender, operator);
  419. isApproved.should.be.false;
  420. });
  421. });
  422. context('when the operator was already approved', function () {
  423. beforeEach(async function () {
  424. await this.token.setApprovalForAll(operator, true, { from: sender });
  425. });
  426. it('keeps the approval to the given address', async function () {
  427. await this.token.setApprovalForAll(operator, true, { from: sender });
  428. const isApproved = await this.token.isApprovedForAll(sender, operator);
  429. isApproved.should.be.true;
  430. });
  431. it('emits an approval event', async function () {
  432. const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender });
  433. logs.length.should.be.equal(1);
  434. logs[0].event.should.be.eq('ApprovalForAll');
  435. logs[0].args._owner.should.be.equal(sender);
  436. logs[0].args._operator.should.be.equal(operator);
  437. logs[0].args._approved.should.be.true;
  438. });
  439. });
  440. });
  441. context('when the operator is the owner', function () {
  442. const operator = creator;
  443. it('reverts', async function () {
  444. await assertRevert(this.token.setApprovalForAll(operator, true, { from: sender }));
  445. });
  446. });
  447. });
  448. shouldSupportInterfaces([
  449. 'ERC165',
  450. 'ERC721',
  451. 'ERC721Exists',
  452. ]);
  453. });
  454. };