Address.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. const { balance, ether, expectRevert, send, expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const AddressImpl = artifacts.require('AddressImpl');
  4. const EtherReceiver = artifacts.require('EtherReceiverMock');
  5. const CallReceiverMock = artifacts.require('CallReceiverMock');
  6. contract('Address', function (accounts) {
  7. const [ recipient, other ] = accounts;
  8. beforeEach(async function () {
  9. this.mock = await AddressImpl.new();
  10. });
  11. describe('isContract', function () {
  12. it('returns false for account address', async function () {
  13. expect(await this.mock.isContract(other)).to.equal(false);
  14. });
  15. it('returns true for contract address', async function () {
  16. const contract = await AddressImpl.new();
  17. expect(await this.mock.isContract(contract.address)).to.equal(true);
  18. });
  19. });
  20. describe('sendValue', function () {
  21. beforeEach(async function () {
  22. this.recipientTracker = await balance.tracker(recipient);
  23. });
  24. context('when sender contract has no funds', function () {
  25. it('sends 0 wei', async function () {
  26. await this.mock.sendValue(other, 0);
  27. expect(await this.recipientTracker.delta()).to.be.bignumber.equal('0');
  28. });
  29. it('reverts when sending non-zero amounts', async function () {
  30. await expectRevert(this.mock.sendValue(other, 1), 'Address: insufficient balance');
  31. });
  32. });
  33. context('when sender contract has funds', function () {
  34. const funds = ether('1');
  35. beforeEach(async function () {
  36. await send.ether(other, this.mock.address, funds);
  37. });
  38. it('sends 0 wei', async function () {
  39. await this.mock.sendValue(recipient, 0);
  40. expect(await this.recipientTracker.delta()).to.be.bignumber.equal('0');
  41. });
  42. it('sends non-zero amounts', async function () {
  43. await this.mock.sendValue(recipient, funds.subn(1));
  44. expect(await this.recipientTracker.delta()).to.be.bignumber.equal(funds.subn(1));
  45. });
  46. it('sends the whole balance', async function () {
  47. await this.mock.sendValue(recipient, funds);
  48. expect(await this.recipientTracker.delta()).to.be.bignumber.equal(funds);
  49. expect(await balance.current(this.mock.address)).to.be.bignumber.equal('0');
  50. });
  51. it('reverts when sending more than the balance', async function () {
  52. await expectRevert(this.mock.sendValue(recipient, funds.addn(1)), 'Address: insufficient balance');
  53. });
  54. context('with contract recipient', function () {
  55. beforeEach(async function () {
  56. this.contractRecipient = await EtherReceiver.new();
  57. });
  58. it('sends funds', async function () {
  59. const tracker = await balance.tracker(this.contractRecipient.address);
  60. await this.contractRecipient.setAcceptEther(true);
  61. await this.mock.sendValue(this.contractRecipient.address, funds);
  62. expect(await tracker.delta()).to.be.bignumber.equal(funds);
  63. });
  64. it('reverts on recipient revert', async function () {
  65. await this.contractRecipient.setAcceptEther(false);
  66. await expectRevert(
  67. this.mock.sendValue(this.contractRecipient.address, funds),
  68. 'Address: unable to send value, recipient may have reverted',
  69. );
  70. });
  71. });
  72. });
  73. });
  74. describe('functionCall', function () {
  75. beforeEach(async function () {
  76. this.contractRecipient = await CallReceiverMock.new();
  77. });
  78. context('with valid contract receiver', function () {
  79. it('calls the requested function', async function () {
  80. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  81. name: 'mockFunction',
  82. type: 'function',
  83. inputs: [],
  84. }, []);
  85. const receipt = await this.mock.functionCall(this.contractRecipient.address, abiEncodedCall);
  86. expectEvent(receipt, 'CallReturnValue', { data: '0x1234' });
  87. await expectEvent.inTransaction(receipt.tx, CallReceiverMock, 'MockFunctionCalled');
  88. });
  89. it('reverts when the called function reverts with no reason', async function () {
  90. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  91. name: 'mockFunctionRevertsNoReason',
  92. type: 'function',
  93. inputs: [],
  94. }, []);
  95. await expectRevert(
  96. this.mock.functionCall(this.contractRecipient.address, abiEncodedCall),
  97. 'Address: low-level call failed',
  98. );
  99. });
  100. it('reverts when the called function reverts, bubbling up the revert reason', async function () {
  101. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  102. name: 'mockFunctionRevertsReason',
  103. type: 'function',
  104. inputs: [],
  105. }, []);
  106. await expectRevert(
  107. this.mock.functionCall(this.contractRecipient.address, abiEncodedCall),
  108. 'CallReceiverMock: reverting',
  109. );
  110. });
  111. it('reverts when the called function runs out of gas', async function () {
  112. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  113. name: 'mockFunctionOutOfGas',
  114. type: 'function',
  115. inputs: [],
  116. }, []);
  117. await expectRevert(
  118. this.mock.functionCall(this.contractRecipient.address, abiEncodedCall, { gas: '100000' }),
  119. 'Address: low-level call failed',
  120. );
  121. });
  122. it('reverts when the called function throws', async function () {
  123. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  124. name: 'mockFunctionThrows',
  125. type: 'function',
  126. inputs: [],
  127. }, []);
  128. await expectRevert.unspecified(
  129. this.mock.functionCall(this.contractRecipient.address, abiEncodedCall),
  130. );
  131. });
  132. it('reverts when function does not exist', async function () {
  133. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  134. name: 'mockFunctionDoesNotExist',
  135. type: 'function',
  136. inputs: [],
  137. }, []);
  138. await expectRevert(
  139. this.mock.functionCall(this.contractRecipient.address, abiEncodedCall),
  140. 'Address: low-level call failed',
  141. );
  142. });
  143. });
  144. context('with non-contract receiver', function () {
  145. it('reverts when address is not a contract', async function () {
  146. const [ recipient ] = accounts;
  147. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  148. name: 'mockFunction',
  149. type: 'function',
  150. inputs: [],
  151. }, []);
  152. await expectRevert(this.mock.functionCall(recipient, abiEncodedCall), 'Address: call to non-contract');
  153. });
  154. });
  155. });
  156. describe('functionCallWithValue', function () {
  157. beforeEach(async function () {
  158. this.contractRecipient = await CallReceiverMock.new();
  159. });
  160. context('with zero value', function () {
  161. it('calls the requested function', async function () {
  162. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  163. name: 'mockFunction',
  164. type: 'function',
  165. inputs: [],
  166. }, []);
  167. const receipt = await this.mock.functionCallWithValue(this.contractRecipient.address, abiEncodedCall, 0);
  168. expectEvent(receipt, 'CallReturnValue', { data: '0x1234' });
  169. await expectEvent.inTransaction(receipt.tx, CallReceiverMock, 'MockFunctionCalled');
  170. });
  171. });
  172. context('with non-zero value', function () {
  173. const amount = ether('1.2');
  174. it('reverts if insufficient sender balance', async function () {
  175. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  176. name: 'mockFunction',
  177. type: 'function',
  178. inputs: [],
  179. }, []);
  180. await expectRevert(
  181. this.mock.functionCallWithValue(this.contractRecipient.address, abiEncodedCall, amount),
  182. 'Address: insufficient balance for call',
  183. );
  184. });
  185. it('calls the requested function with existing value', async function () {
  186. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  187. name: 'mockFunction',
  188. type: 'function',
  189. inputs: [],
  190. }, []);
  191. const tracker = await balance.tracker(this.contractRecipient.address);
  192. await send.ether(other, this.mock.address, amount);
  193. const receipt = await this.mock.functionCallWithValue(this.contractRecipient.address, abiEncodedCall, amount);
  194. expect(await tracker.delta()).to.be.bignumber.equal(amount);
  195. expectEvent(receipt, 'CallReturnValue', { data: '0x1234' });
  196. await expectEvent.inTransaction(receipt.tx, CallReceiverMock, 'MockFunctionCalled');
  197. });
  198. it('calls the requested function with transaction funds', async function () {
  199. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  200. name: 'mockFunction',
  201. type: 'function',
  202. inputs: [],
  203. }, []);
  204. const tracker = await balance.tracker(this.contractRecipient.address);
  205. expect(await balance.current(this.mock.address)).to.be.bignumber.equal('0');
  206. const receipt = await this.mock.functionCallWithValue(
  207. this.contractRecipient.address, abiEncodedCall, amount, { from: other, value: amount },
  208. );
  209. expect(await tracker.delta()).to.be.bignumber.equal(amount);
  210. expectEvent(receipt, 'CallReturnValue', { data: '0x1234' });
  211. await expectEvent.inTransaction(receipt.tx, CallReceiverMock, 'MockFunctionCalled');
  212. });
  213. it('reverts when calling non-payable functions', async function () {
  214. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  215. name: 'mockFunctionNonPayable',
  216. type: 'function',
  217. inputs: [],
  218. }, []);
  219. await send.ether(other, this.mock.address, amount);
  220. await expectRevert(
  221. this.mock.functionCallWithValue(this.contractRecipient.address, abiEncodedCall, amount),
  222. 'Address: low-level call with value failed',
  223. );
  224. });
  225. });
  226. });
  227. describe('functionStaticCall', function () {
  228. beforeEach(async function () {
  229. this.contractRecipient = await CallReceiverMock.new();
  230. });
  231. it('calls the requested function', async function () {
  232. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  233. name: 'mockStaticFunction',
  234. type: 'function',
  235. inputs: [],
  236. }, []);
  237. const receipt = await this.mock.functionStaticCall(this.contractRecipient.address, abiEncodedCall);
  238. expectEvent(receipt, 'CallReturnValue', { data: '0x1234' });
  239. });
  240. it('reverts on a non-static function', async function () {
  241. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  242. name: 'mockFunction',
  243. type: 'function',
  244. inputs: [],
  245. }, []);
  246. await expectRevert(
  247. this.mock.functionStaticCall(this.contractRecipient.address, abiEncodedCall),
  248. 'Address: low-level static call failed',
  249. );
  250. });
  251. it('bubbles up revert reason', async function () {
  252. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  253. name: 'mockFunctionRevertsReason',
  254. type: 'function',
  255. inputs: [],
  256. }, []);
  257. await expectRevert(
  258. this.mock.functionStaticCall(this.contractRecipient.address, abiEncodedCall),
  259. 'CallReceiverMock: reverting',
  260. );
  261. });
  262. it('reverts when address is not a contract', async function () {
  263. const [ recipient ] = accounts;
  264. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  265. name: 'mockFunction',
  266. type: 'function',
  267. inputs: [],
  268. }, []);
  269. await expectRevert(
  270. this.mock.functionStaticCall(recipient, abiEncodedCall),
  271. 'Address: static call to non-contract',
  272. );
  273. });
  274. });
  275. describe('functionDelegateCall', function () {
  276. beforeEach(async function () {
  277. this.contractRecipient = await CallReceiverMock.new();
  278. });
  279. it('delegate calls the requested function', async function () {
  280. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  281. name: 'mockFunctionWritesStorage',
  282. type: 'function',
  283. inputs: [],
  284. }, []);
  285. const receipt = await this.mock.functionDelegateCall(this.contractRecipient.address, abiEncodedCall);
  286. expectEvent(receipt, 'CallReturnValue', { data: '0x1234' });
  287. expect(await this.mock.sharedAnswer()).to.equal('42');
  288. });
  289. it('bubbles up revert reason', async function () {
  290. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  291. name: 'mockFunctionRevertsReason',
  292. type: 'function',
  293. inputs: [],
  294. }, []);
  295. await expectRevert(
  296. this.mock.functionDelegateCall(this.contractRecipient.address, abiEncodedCall),
  297. 'CallReceiverMock: reverting',
  298. );
  299. });
  300. it('reverts when address is not a contract', async function () {
  301. const [ recipient ] = accounts;
  302. const abiEncodedCall = web3.eth.abi.encodeFunctionCall({
  303. name: 'mockFunction',
  304. type: 'function',
  305. inputs: [],
  306. }, []);
  307. await expectRevert(
  308. this.mock.functionDelegateCall(recipient, abiEncodedCall),
  309. 'Address: delegate call to non-contract',
  310. );
  311. });
  312. });
  313. });