ERC721BasicToken.behaviour.js 21 KB

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