ERC721BasicToken.behaviour.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. import assertRevert from '../../helpers/assertRevert';
  2. import decodeLogs from '../../helpers/decodeLogs';
  3. import sendTransaction from '../../helpers/sendTransaction';
  4. import _ from 'lodash';
  5. const ERC721Receiver = artifacts.require('ERC721ReceiverMock.sol');
  6. const BigNumber = web3.BigNumber;
  7. require('chai')
  8. .use(require('chai-as-promised'))
  9. .use(require('chai-bignumber')(BigNumber))
  10. .should();
  11. export default 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 = '0xf0b9e5ba';
  18. describe('like a 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. describe('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. describe('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. describe('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. describe('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. describe('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. describe('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. describe('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('emits an approval and transfer events', async function () {
  97. logs.length.should.be.equal(2);
  98. logs[0].event.should.be.eq('Approval');
  99. logs[0].args._owner.should.be.equal(owner);
  100. logs[0].args._approved.should.be.equal(ZERO_ADDRESS);
  101. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  102. logs[1].event.should.be.eq('Transfer');
  103. logs[1].args._from.should.be.equal(owner);
  104. logs[1].args._to.should.be.equal(this.to);
  105. logs[1].args._tokenId.should.be.bignumber.equal(tokenId);
  106. });
  107. } else {
  108. it('emits only a transfer event', async function () {
  109. logs.length.should.be.equal(1);
  110. logs[0].event.should.be.eq('Transfer');
  111. logs[0].args._from.should.be.equal(owner);
  112. logs[0].args._to.should.be.equal(this.to);
  113. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  114. });
  115. }
  116. it('adjusts owners balances', async function () {
  117. const newOwnerBalance = await this.token.balanceOf(this.to);
  118. newOwnerBalance.should.be.bignumber.equal(1);
  119. const previousOwnerBalance = await this.token.balanceOf(owner);
  120. previousOwnerBalance.should.be.bignumber.equal(1);
  121. });
  122. it('adjusts owners tokens by index', async function () {
  123. if (!this.token.tokenOfOwnerByIndex) return;
  124. const newOwnerToken = await this.token.tokenOfOwnerByIndex(this.to, 0);
  125. newOwnerToken.toNumber().should.be.equal(tokenId);
  126. const previousOwnerToken = await this.token.tokenOfOwnerByIndex(owner, 0);
  127. previousOwnerToken.toNumber().should.not.be.equal(tokenId);
  128. });
  129. };
  130. const shouldTransferTokensByUsers = function (transferFunction) {
  131. describe('when called by the owner', function () {
  132. beforeEach(async function () {
  133. ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: owner }));
  134. });
  135. transferWasSuccessful({ owner, tokenId, approved });
  136. });
  137. describe('when called by the approved individual', function () {
  138. beforeEach(async function () {
  139. ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: approved }));
  140. });
  141. transferWasSuccessful({ owner, tokenId, approved });
  142. });
  143. describe('when called by the operator', function () {
  144. beforeEach(async function () {
  145. ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: operator }));
  146. });
  147. transferWasSuccessful({ owner, tokenId, approved });
  148. });
  149. describe('when called by the owner without an approved user', function () {
  150. beforeEach(async function () {
  151. await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
  152. ({ logs } = await transferFunction.call(this, owner, this.to, tokenId, { from: operator }));
  153. });
  154. transferWasSuccessful({ owner, tokenId, approved: null });
  155. });
  156. describe('when sent to the owner', function () {
  157. beforeEach(async function () {
  158. ({ logs } = await transferFunction.call(this, owner, owner, tokenId, { from: owner }));
  159. });
  160. it('keeps ownership of the token', async function () {
  161. const newOwner = await this.token.ownerOf(tokenId);
  162. newOwner.should.be.equal(owner);
  163. });
  164. it('clears the approval for the token ID', async function () {
  165. const approvedAccount = await this.token.getApproved(tokenId);
  166. approvedAccount.should.be.equal(ZERO_ADDRESS);
  167. });
  168. it('emits an approval and transfer events', async function () {
  169. logs.length.should.be.equal(2);
  170. logs[0].event.should.be.eq('Approval');
  171. logs[0].args._owner.should.be.equal(owner);
  172. logs[0].args._approved.should.be.equal(ZERO_ADDRESS);
  173. logs[0].args._tokenId.should.be.bignumber.equal(tokenId);
  174. logs[1].event.should.be.eq('Transfer');
  175. logs[1].args._from.should.be.equal(owner);
  176. logs[1].args._to.should.be.equal(owner);
  177. logs[1].args._tokenId.should.be.bignumber.equal(tokenId);
  178. });
  179. it('keeps the owner balance', async function () {
  180. const ownerBalance = await this.token.balanceOf(owner);
  181. ownerBalance.should.be.bignumber.equal(2);
  182. });
  183. it('keeps same tokens by index', async function () {
  184. if (!this.token.tokenOfOwnerByIndex) return;
  185. const tokensListed = await Promise.all(_.range(2).map(i => this.token.tokenOfOwnerByIndex(owner, i)));
  186. tokensListed.map(t => t.toNumber()).should.have.members([firstTokenId, secondTokenId]);
  187. });
  188. });
  189. describe('when the address of the previous owner is incorrect', function () {
  190. it('reverts', async function () {
  191. await assertRevert(transferFunction.call(this, unauthorized, this.to, tokenId, { from: owner }));
  192. });
  193. });
  194. describe('when the sender is not authorized for the token id', function () {
  195. it('reverts', async function () {
  196. await assertRevert(transferFunction.call(this, owner, this.to, tokenId, { from: unauthorized }));
  197. });
  198. });
  199. describe('when the given token ID does not exist', function () {
  200. it('reverts', async function () {
  201. await assertRevert(transferFunction.call(this, owner, this.to, unknownTokenId, { from: owner }));
  202. });
  203. });
  204. describe('when the address to transfer the token to is the zero address', function () {
  205. it('reverts', async function () {
  206. await assertRevert(transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }));
  207. });
  208. });
  209. };
  210. describe('via transferFrom', function () {
  211. shouldTransferTokensByUsers(function (from, to, tokenId, opts) {
  212. return this.token.transferFrom(from, to, tokenId, opts);
  213. });
  214. });
  215. describe('via safeTransferFrom', function () {
  216. const safeTransferFromWithData = function (from, to, tokenId, opts) {
  217. return sendTransaction(
  218. this.token,
  219. 'safeTransferFrom',
  220. 'address,address,uint256,bytes',
  221. [from, to, tokenId, data],
  222. opts
  223. );
  224. };
  225. const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
  226. return this.token.safeTransferFrom(from, to, tokenId, opts);
  227. };
  228. const shouldTransferSafely = function (transferFun, data) {
  229. describe('to a user account', function () {
  230. shouldTransferTokensByUsers(transferFun);
  231. });
  232. describe('to a valid receiver contract', function () {
  233. beforeEach(async function () {
  234. this.receiver = await ERC721Receiver.new(RECEIVER_MAGIC_VALUE, false);
  235. this.to = this.receiver.address;
  236. });
  237. shouldTransferTokensByUsers(transferFun);
  238. it('should call onERC721Received', async function () {
  239. const result = await transferFun.call(this, owner, this.to, tokenId, { from: owner });
  240. result.receipt.logs.length.should.be.equal(3);
  241. const [log] = decodeLogs([result.receipt.logs[2]], ERC721Receiver, this.receiver.address);
  242. log.event.should.be.eq('Received');
  243. log.args._address.should.be.equal(owner);
  244. log.args._tokenId.toNumber().should.be.equal(tokenId);
  245. log.args._data.should.be.equal(data);
  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.eq('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. describe('when clearing approval', function () {
  302. describe('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. it('does not emit an approval event', async function () {
  308. logs.length.should.be.equal(0);
  309. });
  310. });
  311. describe('when there was a prior approval', function () {
  312. beforeEach(async function () {
  313. await this.token.approve(to, tokenId, { from: sender });
  314. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: sender }));
  315. });
  316. itClearsApproval();
  317. itEmitsApprovalEvent(ZERO_ADDRESS);
  318. });
  319. });
  320. describe('when approving a non-zero address', function () {
  321. describe('when there was no prior approval', function () {
  322. beforeEach(async function () {
  323. ({ logs } = await this.token.approve(to, tokenId, { from: sender }));
  324. });
  325. itApproves(to);
  326. itEmitsApprovalEvent(to);
  327. });
  328. describe('when there was a prior approval to the same address', function () {
  329. beforeEach(async function () {
  330. await this.token.approve(to, tokenId, { from: sender });
  331. ({ logs } = await this.token.approve(to, tokenId, { from: sender }));
  332. });
  333. itApproves(to);
  334. itEmitsApprovalEvent(to);
  335. });
  336. describe('when there was a prior approval to a different address', function () {
  337. beforeEach(async function () {
  338. await this.token.approve(accounts[2], tokenId, { from: sender });
  339. ({ logs } = await this.token.approve(to, tokenId, { from: sender }));
  340. });
  341. itApproves(to);
  342. itEmitsApprovalEvent(to);
  343. });
  344. });
  345. describe('when the address that receives the approval is the owner', function () {
  346. it('reverts', async function () {
  347. await assertRevert(this.token.approve(sender, tokenId, { from: sender }));
  348. });
  349. });
  350. describe('when the sender does not own the given token ID', function () {
  351. it('reverts', async function () {
  352. await assertRevert(this.token.approve(to, tokenId, { from: accounts[2] }));
  353. });
  354. });
  355. describe('when the sender is approved for the given token ID', function () {
  356. it('reverts', async function () {
  357. await this.token.approve(accounts[2], tokenId, { from: sender });
  358. await assertRevert(this.token.approve(to, tokenId, { from: accounts[2] }));
  359. });
  360. });
  361. describe('when the sender is an operator', function () {
  362. const operator = accounts[2];
  363. beforeEach(async function () {
  364. await this.token.setApprovalForAll(operator, true, { from: sender });
  365. ({ logs } = await this.token.approve(to, tokenId, { from: operator }));
  366. });
  367. itApproves(to);
  368. itEmitsApprovalEvent(to);
  369. });
  370. describe('when the given token ID does not exist', function () {
  371. it('reverts', async function () {
  372. await assertRevert(this.token.approve(to, unknownTokenId, { from: sender }));
  373. });
  374. });
  375. });
  376. describe('setApprovalForAll', function () {
  377. const sender = creator;
  378. describe('when the operator willing to approve is not the owner', function () {
  379. const operator = accounts[1];
  380. describe('when there is no operator approval set by the sender', function () {
  381. it('approves the operator', async function () {
  382. await this.token.setApprovalForAll(operator, true, { from: sender });
  383. const isApproved = await this.token.isApprovedForAll(sender, operator);
  384. isApproved.should.be.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.eq('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.be.true;
  393. });
  394. });
  395. describe('when the operator was set as not approved', function () {
  396. beforeEach(async function () {
  397. await this.token.setApprovalForAll(operator, false, { from: sender });
  398. });
  399. it('approves the operator', async function () {
  400. await this.token.setApprovalForAll(operator, true, { from: sender });
  401. const isApproved = await this.token.isApprovedForAll(sender, operator);
  402. isApproved.should.be.true;
  403. });
  404. it('emits an approval event', async function () {
  405. const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender });
  406. logs.length.should.be.equal(1);
  407. logs[0].event.should.be.eq('ApprovalForAll');
  408. logs[0].args._owner.should.be.equal(sender);
  409. logs[0].args._operator.should.be.equal(operator);
  410. logs[0].args._approved.should.be.true;
  411. });
  412. it('can unset the operator approval', async function () {
  413. await this.token.setApprovalForAll(operator, false, { from: sender });
  414. const isApproved = await this.token.isApprovedForAll(sender, operator);
  415. isApproved.should.be.false;
  416. });
  417. });
  418. describe('when the operator was already approved', function () {
  419. beforeEach(async function () {
  420. await this.token.setApprovalForAll(operator, true, { from: sender });
  421. });
  422. it('keeps the approval to the given address', async function () {
  423. await this.token.setApprovalForAll(operator, true, { from: sender });
  424. const isApproved = await this.token.isApprovedForAll(sender, operator);
  425. isApproved.should.be.true;
  426. });
  427. it('emits an approval event', async function () {
  428. const { logs } = await this.token.setApprovalForAll(operator, true, { from: sender });
  429. logs.length.should.be.equal(1);
  430. logs[0].event.should.be.eq('ApprovalForAll');
  431. logs[0].args._owner.should.be.equal(sender);
  432. logs[0].args._operator.should.be.equal(operator);
  433. logs[0].args._approved.should.be.true;
  434. });
  435. });
  436. });
  437. describe('when the operator is the owner', function () {
  438. const operator = creator;
  439. it('reverts', async function () {
  440. await assertRevert(this.token.setApprovalForAll(operator, true, { from: sender }));
  441. });
  442. });
  443. });
  444. });
  445. };