StakingGasUsage.js 4.25 KB
Newer Older
XFT-dev's avatar
XFT-dev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
const MasterChef = artifacts.require('MasterChef');
const StakingRewards = artifacts.require('StakingRewards');
const TokenMock = artifacts.require('TokenMock');

const {time, BN, ether} = require('@openzeppelin/test-helpers');
const {use, expect} = require('chai');

function bn(params) {
  return new BN(params.toString());
}

use(require('chai-bn')(BN));

const PID = 0;

const ALLOCATE_POINT_SUSHI = ether('1000');
const ALLOCATE_POINT_XFT = ether('1000');


contract('DeltaHubStaking', ([owner, dev, wallet1, wallet2, wallet3, wallet4, wallet5]) => {
  let slp;
  let sushi;
  let xft;
  let onsen;
  let staking;

  beforeEach(async () => {
    slp = await TokenMock.new('SLP', 'SLP', ether('10000000'), {from: owner});
    sushi = await TokenMock.new('SUSHI', 'SUSHI', ether('10000000'), {from: owner});
    xft = await TokenMock.new('XFT', 'XFT', ether('10000000'), {from: owner});
    onsen = await MasterChef.new(
      sushi.address,
      dev,
      ether('0.01'),
      await time.latestBlock(),
      await time.latestBlock() + 2592000, // apx. 12 month
      {from: owner},
    );

    staking = await StakingRewards.new(
      xft.address,
      sushi.address,
      slp.address,
      onsen.address,
      {from: owner},
    );

    await onsen.add(ALLOCATE_POINT_SUSHI, slp.address, false, {from: owner});
    await sushi.mint(onsen.address, ALLOCATE_POINT_SUSHI);

    await xft.transfer(staking.address, ALLOCATE_POINT_XFT, {from: owner});
    await staking.notifyRewardAmount(ALLOCATE_POINT_XFT, {from: owner});

    await slp.transfer(wallet1, ether('100000'), {from: owner});
    await slp.transfer(wallet2, ether('100000'), {from: owner});
    await slp.transfer(wallet3, ether('100000'), {from: owner});
    await slp.transfer(wallet4, ether('100000'), {from: owner});
    await slp.transfer(wallet5, ether('100000'), {from: owner});

    await slp.approve(staking.address, new BN(2).pow(new BN(255)), {from: wallet1});
    await slp.approve(staking.address, new BN(2).pow(new BN(255)), {from: wallet2});
    await slp.approve(staking.address, new BN(2).pow(new BN(255)), {from: wallet3});
    await slp.approve(staking.address, new BN(2).pow(new BN(255)), {from: wallet4});
    await slp.approve(staking.address, new BN(2).pow(new BN(255)), {from: wallet5});
  });

  describe('test gas usage', () => {
    beforeEach( async () => {
      await slp.approve(onsen.address, new BN(2).pow(new BN(255)), {from: wallet1});
      await slp.approve(onsen.address, new BN(2).pow(new BN(255)), {from: wallet2});
      await slp.approve(onsen.address, new BN(2).pow(new BN(255)), {from: wallet3});
    });

    it('should stake to onsen contract', async () => {
      const amount = ether('2');
      let totalStaked = await bn(0);

      for (let i = 1; i < 50; i++) {
        const stakeAmount = await bn(amount).mul(bn(i));

        await onsen.deposit(PID, stakeAmount, {from: wallet1});
        await onsen.deposit(PID, stakeAmount, {from: wallet2});
        await onsen.deposit(PID, stakeAmount, {from: wallet3});

        await staking.stake(stakeAmount, {from: wallet1});
        await staking.stake(stakeAmount, {from: wallet2});
        await staking.stake(stakeAmount, {from: wallet3});
        totalStaked = await totalStaked.add(stakeAmount);
      }

      for (let i = 1; i < 50; i++) {
        const stakeAmount = await bn(amount).mul(bn(i));

        await staking.withdraw(stakeAmount, {from: wallet1});
        await staking.withdraw(stakeAmount, {from: wallet2});
        await staking.withdraw(stakeAmount, {from: wallet3});

        await onsen.withdraw(PID, stakeAmount, {from: wallet1});
        await onsen.withdraw(PID, stakeAmount, {from: wallet2});
        await onsen.withdraw(PID, stakeAmount, {from: wallet3});
      }
    });

    it('should withdraw from onsen contract', async () => {
      await staking.stake(ether('100'), {from: wallet1});
      expect((await staking.userInfo(wallet1)).amount).to.be.a.bignumber.equal(ether('100'));
      expect((await onsen.userInfo(PID, staking.address)).amount).to.be.a.bignumber.equal(ether('100'));

      await staking.withdraw(ether('100'), {from: wallet1});
      expect((await staking.userInfo(wallet1)).amount).to.be.a.bignumber.equal('0');
      expect((await onsen.userInfo(PID, staking.address)).amount).to.be.a.bignumber.equal('0');
    });
  });
});