github.com/codingfuture/orig-energi3@v0.8.4/energi/contracts/test/StakerRewardV1.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 StakerRewardV1 = artifacts.require('StakerRewardV1');
    24  const IBlockReward = artifacts.require('IBlockReward');
    25  
    26  const common = require('./common');
    27  
    28  contract("StakerRewardV1", async accounts => {
    29      const s = {
    30          artifacts,
    31          accounts,
    32          assert,
    33          it,
    34          web3,
    35      };
    36  
    37      before(async () => {
    38          s.orig = await StakerRewardV1.deployed();
    39          s.proxy = await MockProxy.at(await s.orig.proxy());
    40          s.fake = await MockContract.new(s.proxy.address);
    41          s.proxy_abi = await StakerRewardV1.at(s.proxy.address);
    42          s.token_abi = await IBlockReward.at(s.proxy.address);
    43          await s.proxy.setImpl(s.orig.address);
    44          Object.freeze(s);
    45      });
    46  
    47      after(async () => {
    48          const impl = await StakerRewardV1.new(s.proxy.address);
    49          await s.proxy.setImpl(impl.address);
    50      });
    51  
    52      describe('common pre', () => common.govPreTests(s) );
    53  
    54      //---
    55      describe('Primary', () => {
    56          const { toBN, toWei } = web3.utils;
    57          const reward = toBN(toWei('2.28', 'ether'));
    58  
    59          it('should correctly getReward()', async () => {
    60              expect(toBN(await s.token_abi.getReward(0)).toString())
    61                  .equal(toBN(0).toString());
    62              expect(toBN(await s.token_abi.getReward(1)).toString())
    63                  .equal(toBN(reward).toString());
    64              expect(toBN(await s.token_abi.getReward(common.superblock_cycles)).toString())
    65                  .equal(toBN(reward).toString());
    66          });
    67  
    68          it('should reward()', async () => {
    69              const coinbase = (await web3.eth.getBlock('latest')).miner;
    70              const trunc = toBN(toWei('0.01', 'ether')); // truncate gas
    71              const eth_reward = toBN(toWei('2', 'ether')); // standard reward
    72  
    73              const bal_before = toBN(await web3.eth.getBalance(coinbase));
    74              await s.token_abi.reward({value: reward, from: accounts[1]});
    75              const bal_after = toBN(await web3.eth.getBalance(coinbase));
    76  
    77              expect(bal_after.sub(bal_before).div(trunc).mul(trunc).toString())
    78                  .equal(reward.add(eth_reward).toString());
    79          });
    80      });
    81  
    82      //---
    83      describe('common post', () => common.govPostTests(s) );
    84  });