github.com/codingfuture/orig-energi3@v0.8.4/energi/contracts/test/MasternodeTokenV1.spec.js (about)

     1  // Copyright 2019 The Energi Core Authors
     2  // This file is part of the Energi Core library.
     3  //
     4  // The Energi Core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The Energi Core library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Energi Governance system is the fundamental part of Energi Core.
    18  
    19  'use strict';
    20  
    21  const MockProxy = artifacts.require('MockProxy');
    22  const MockContract = artifacts.require('MockContract');
    23  const MasternodeTokenV1 = artifacts.require('MasternodeTokenV1');
    24  const IMasternodeToken = artifacts.require('IMasternodeToken');
    25  const StorageMasternodeTokenV1 = artifacts.require('StorageMasternodeTokenV1');
    26  
    27  const common = require('./common');
    28  
    29  contract("MasternodeTokenV1", async accounts => {
    30      const s = {
    31          artifacts,
    32          accounts,
    33          assert,
    34          it,
    35          web3,
    36          storage: null,
    37      };
    38  
    39      const COLLATERAL_1 = web3.utils.toWei('10000', 'ether');
    40      const COLLATERAL_2 = web3.utils.toWei('20000', 'ether');
    41      const COLLATERAL_3 = web3.utils.toWei('30000', 'ether');
    42      const COLLATERAL_4 = web3.utils.toWei('40000', 'ether');
    43      const COLLATERAL_7 = web3.utils.toWei('70000', 'ether');
    44      const COLLATERAL_9 = web3.utils.toWei('90000', 'ether');
    45      const COLLATERAL_10 = web3.utils.toWei('100000', 'ether');
    46      const COLLATERAL_13 = web3.utils.toWei('130000', 'ether');
    47      const check_age = async (age) => {
    48          const bn = await web3.eth.getBlockNumber();
    49          expect(age.toString()).equal(bn.toString());
    50      };
    51  
    52      before(async () => {
    53          s.orig = await MasternodeTokenV1.deployed();
    54          s.proxy = await MockProxy.at(await s.orig.proxy());
    55          s.registry_proxy = await MockProxy.at(await s.orig.registry_proxy());
    56          s.fake = await MockContract.new(s.proxy.address);
    57          s.proxy_abi = await MasternodeTokenV1.at(s.proxy.address);
    58          s.token_abi = await IMasternodeToken.at(s.proxy.address);
    59          await s.proxy.setImpl(s.orig.address);
    60          s.storage = await StorageMasternodeTokenV1.at(await s.proxy_abi.v1storage());
    61          Object.freeze(s);
    62      });
    63  
    64      after(async () => {
    65          const impl = await MasternodeTokenV1.new(s.proxy.address, s.registry_proxy.address);
    66          await s.proxy.setImpl(impl.address);
    67  
    68          await s.fake.testDrain(COLLATERAL_3, {from: accounts[1]});
    69          await s.fake.testDrain(COLLATERAL_1, {from: accounts[0]});
    70      });
    71      
    72      describe('common pre', () => common.govPreTests(s) );
    73  
    74      //---
    75      describe('ERC20', () => {
    76          it('should emit Transfer in c-tor', async () => {
    77              const tmp = await MasternodeTokenV1.new(s.proxy.address, s.registry_proxy.address);
    78  
    79              const evt = await tmp.getPastEvents('Transfer', common.evt_last_block);
    80              expect(evt).lengthOf(1);
    81              expect(evt[0].args).deep.include({
    82                  '__length__': 3,
    83                  'from': '0x0000000000000000000000000000000000000000',
    84                  'to': '0x0000000000000000000000000000000000000000',
    85                  'value': web3.utils.toBN('0'),
    86              });
    87          });
    88  
    89          it('should support totalSupply()', async () => {
    90              const res = await s.token_abi.totalSupply();
    91              assert.equal(res.valueOf(), 0);
    92          });
    93  
    94          it('should support name()', async () => {
    95              const res = await s.token_abi.name();
    96              assert.equal(res, "Masternode Collateral");
    97          });
    98  
    99          it('should support symbol()', async () => {
   100              const res = await s.token_abi.symbol();
   101              assert.equal(res, "MNGR");
   102          });
   103  
   104          it('should support decimals()', async () => {
   105              const res = await s.token_abi.decimals();
   106              assert.equal(res.valueOf(), 22);
   107          });
   108  
   109          it('should support balanceOf()', async () => {
   110              const res = await s.token_abi.balanceOf(s.fake.address);
   111              assert.equal(res.valueOf(), 0);
   112          });
   113  
   114          it('should support allowance()', async () => {
   115              const res = await s.token_abi.allowance(s.fake.address, s.fake.address);
   116              assert.equal(res.valueOf(), 0);
   117          });
   118  
   119          it('should refuse transfer()', async () => {
   120              try {
   121                  await s.token_abi.transfer(s.fake.address, '0');
   122                  assert.fail("It must fail");
   123              } catch (e) {
   124                  assert.match(e.message, /Not allowed/);
   125              }
   126          });
   127  
   128          it('should refuse transferFrom()', async () => {
   129              try {
   130                  await s.token_abi.transferFrom(s.fake.address, s.fake.address, '0');
   131                  assert.fail("It must fail");
   132              } catch (e) {
   133                  assert.match(e.message, /Not allowed/);
   134              }
   135          });
   136  
   137          it('should refuse approve()', async () => {
   138              try {
   139                  await s.token_abi.approve(s.fake.address, '0');
   140                  assert.fail("It must fail");
   141              } catch (e) {
   142                  assert.match(e.message, /Not allowed/);
   143              }
   144          });
   145      });
   146  
   147      //---
   148      describe('Primary', () => {
   149          it('should support balanceInfo()', async () => {
   150              const res = await s.token_abi.balanceInfo(s.fake.address);
   151              assert.equal(res['0'].valueOf(), 0);
   152          });
   153  
   154          it('should allow depositCollateral()', async () => {
   155              const { logs } = await s.token_abi.depositCollateral({
   156                  from: accounts[0],
   157                  value: COLLATERAL_1,
   158              });
   159              assert.equal(logs.length, 1);
   160              const res = await s.token_abi.balanceInfo(accounts[0]);
   161              assert.equal(res['0'].valueOf(), COLLATERAL_1);
   162              await check_age(res['1']);
   163  
   164              const res2 = await s.token_abi.balanceOf(accounts[0]);
   165              assert.equal(res2.valueOf(), COLLATERAL_1);
   166  
   167              const res3 = await s.token_abi.totalSupply();
   168              assert.equal(res3.valueOf(), COLLATERAL_1);
   169  
   170              const evt = await s.orig.getPastEvents('Transfer', common.evt_last_block);
   171              expect(evt).lengthOf(1);
   172              expect(evt[0].args).deep.include({
   173                  '__length__': 3,
   174                  'from': '0x0000000000000000000000000000000000000000',
   175                  'to': accounts[0],
   176                  'value': web3.utils.toBN(COLLATERAL_1),
   177              });
   178          });
   179  
   180          it('should correctly reflect last block', async () => {
   181              const res1 = await s.token_abi.balanceInfo(accounts[0]);
   182              await common.moveTime(web3, 3600);
   183  
   184              const res2 = await s.token_abi.balanceInfo(accounts[0]);
   185              assert.equal(res2['0'].valueOf(), COLLATERAL_1);
   186              assert.equal(res1['1'].toString(), res2['1'].toString());
   187          });
   188          
   189          it('should allow depositCollateral() direct', async () => {
   190              const { logs } = await s.orig.depositCollateral({
   191                  from: accounts[0],
   192                  value: COLLATERAL_2,
   193              });
   194              assert.equal(logs.length, 1);
   195              const res = await s.token_abi.balanceInfo(accounts[0]);
   196              assert.equal(res['0'].valueOf(), COLLATERAL_3);
   197              await check_age(res['1']);
   198  
   199              const res2 = await s.token_abi.balanceOf(accounts[0]);
   200              assert.equal(res2.valueOf(), COLLATERAL_3);
   201  
   202              const total = await s.token_abi.totalSupply();
   203  
   204              assert.equal(total.valueOf(), COLLATERAL_3);
   205  
   206              const evt = await s.orig.getPastEvents('Transfer', common.evt_last_block);
   207              expect(evt).lengthOf(1);
   208              expect(evt[0].args).deep.include({
   209                  '__length__': 3,
   210                  'from': '0x0000000000000000000000000000000000000000',
   211                  'to': accounts[0],
   212                  'value': web3.utils.toBN(COLLATERAL_2),
   213              });
   214          });
   215  
   216          it('should refuse depositCollateral() not a multiple of', async () => {
   217              try {
   218                  await s.token_abi.depositCollateral({
   219                      from: accounts[0],
   220                      value: web3.utils.toWei('10001', 'ether'),
   221                  });
   222                  assert.fail("It must fail");
   223              } catch (e) {
   224                  assert.match(e.message, /Not a multiple/);
   225              }
   226  
   227              const evt = await s.orig.getPastEvents('Transfer', common.evt_last_block);
   228              expect(evt).lengthOf(0);
   229          });
   230  
   231          it('should allow depositCollateral() - max', async () => {
   232              const { logs } = await s.token_abi.depositCollateral({
   233                  from: accounts[0],
   234                  value: COLLATERAL_7,
   235              });
   236              assert.equal(logs.length, 1);
   237              const res = await s.token_abi.balanceInfo(accounts[0]);
   238              assert.equal(res['0'].valueOf(), COLLATERAL_10);
   239              await check_age(res['1']);
   240  
   241              const res2 = await s.token_abi.balanceOf(accounts[0]);
   242              assert.equal(res2.valueOf(), COLLATERAL_10);
   243  
   244              const total = await s.token_abi.totalSupply();
   245              assert.equal(total.valueOf(), COLLATERAL_10);
   246          });
   247  
   248          it('should refuse to depositCollateral() over max', async () => {
   249              try {
   250                  await s.token_abi.depositCollateral({
   251                      from: accounts[0],
   252                      value: web3.utils.toWei(COLLATERAL_1, 'ether'),
   253                  });
   254                  assert.fail("It must fail");
   255              } catch (e) {
   256                  assert.match(e.message, /Too much/);
   257              }
   258          });
   259  
   260          it('should allow depositCollateral() another account', async () => {
   261              const { logs } = await s.orig.depositCollateral({
   262                  from: accounts[1],
   263                  value: COLLATERAL_3,
   264              });
   265              assert.equal(logs.length, 1);
   266  
   267              const res = await s.token_abi.balanceInfo(accounts[1]);
   268              assert.equal(res['0'].valueOf(), COLLATERAL_3);
   269              await check_age(res['1']);
   270  
   271              const res2 = await s.token_abi.balanceOf(accounts[1]);
   272              assert.equal(res2.valueOf(), COLLATERAL_3);
   273  
   274              const total = await s.token_abi.totalSupply();
   275              assert.equal(total.valueOf(), COLLATERAL_13);
   276          });
   277  
   278          it('should allow withdrawCollateral()', async () => {
   279              const { logs } = await s.token_abi.withdrawCollateral(COLLATERAL_9, {
   280                  from: accounts[0],
   281              });
   282              assert.equal(logs.length, 1);
   283              const res = await s.token_abi.balanceInfo(accounts[0]);
   284              assert.equal(res['0'].valueOf(), COLLATERAL_1);
   285              await check_age(res['1']);
   286  
   287              const total = await s.token_abi.totalSupply();
   288              assert.equal(total.valueOf(), COLLATERAL_4);
   289  
   290              const evt = await s.orig.getPastEvents('Transfer', common.evt_last_block);
   291              expect(evt).lengthOf(1);
   292              expect(evt[0].args).deep.include({
   293                  '__length__': 3,
   294                  'from': accounts[0],
   295                  'to': '0x0000000000000000000000000000000000000000',
   296                  'value': web3.utils.toBN(COLLATERAL_9),
   297              });
   298          });
   299  
   300          it('should refuse withdrawCollateral() over balance', async () => {
   301              try {
   302                  await s.token_abi.withdrawCollateral(COLLATERAL_2, {
   303                      from: accounts[0],
   304                  });
   305                  assert.fail("It must fail");
   306              } catch (e) {
   307                  assert.match(e.message, /Not enough/);
   308              }
   309  
   310              const evt = await s.orig.getPastEvents('Transfer', common.evt_last_block);
   311              expect(evt).lengthOf(0);
   312          });
   313  
   314          it('should refuse setBalance() on s.storage', async () => {
   315              try {
   316                  await s.storage.setBalance(s.fake.address, COLLATERAL_1, COLLATERAL_1);
   317                  assert.fail("It must fail");
   318              } catch (e) {
   319                  assert.match(e.message, /Not owner/);
   320              }
   321          });
   322      });
   323  
   324      //---
   325      describe('common post', () => common.govPostTests(s) );
   326  });