|
| 1 | +import { expectThrow } from "./expectThrow"; |
| 2 | +import BN = require("bn.js"); |
| 3 | +import { ExchangeTestUtil } from "./testExchangeUtil"; |
| 4 | + |
| 5 | +contract("UserStakingPool", (accounts: string[]) => { |
| 6 | + const owner1 = accounts[0]; |
| 7 | + const owner2 = accounts[1]; |
| 8 | + const owner3 = accounts[2]; |
| 9 | + const emptyAddr = "0x0000000000000000000000000000000000000000"; |
| 10 | + |
| 11 | + let userstaking: any; |
| 12 | + let protocolfee: any; |
| 13 | + let exchangeTestUtil: ExchangeTestUtil; |
| 14 | + |
| 15 | + before(async () => { |
| 16 | + exchangeTestUtil = new ExchangeTestUtil(); |
| 17 | + await exchangeTestUtil.initialize(accounts); |
| 18 | + userstaking = exchangeTestUtil.userstakingpool; |
| 19 | + protocolfee = exchangeTestUtil.protocolfeevault; |
| 20 | + userstaking.setProtocolFeeVault(protocolfee.address); |
| 21 | + }); |
| 22 | + |
| 23 | + describe("stakeA", () => { |
| 24 | + it("should not stake if dont have any LRC", async () => { |
| 25 | + await expectThrow( |
| 26 | + userstaking.stake(500, { from: owner1 }), |
| 27 | + "TRANSFER_FAILURE" |
| 28 | + ); |
| 29 | + }); |
| 30 | + it("should not withdraw if haven't staked", async () => { |
| 31 | + await expectThrow( |
| 32 | + userstaking.withdraw(500, { from: owner1 }), |
| 33 | + "INSUFFICIENT_FUND" |
| 34 | + ); |
| 35 | + }); |
| 36 | + it("set User A balance as 1000", async () => { |
| 37 | + //const amount = new BN(web3.utils.toWei("1000", "ether")); |
| 38 | + const amount = new BN(1000); |
| 39 | + await exchangeTestUtil.setBalanceAndApprove( |
| 40 | + owner1, |
| 41 | + "LRC", |
| 42 | + amount, |
| 43 | + userstaking.address |
| 44 | + ); |
| 45 | + }); |
| 46 | + it("should get user staking equal to staked", async () => { |
| 47 | + await userstaking.stake(1000, { from: owner1 }); |
| 48 | + const userstaked = await userstaking.getUserStaking(owner1); |
| 49 | + assert.equal( |
| 50 | + userstaked.stakeAmount, |
| 51 | + 1000, |
| 52 | + "User staking should equal to expected" |
| 53 | + ); |
| 54 | + }); |
| 55 | + it("should get total staking equal to staked", async () => { |
| 56 | + const userstaked = await userstaking.getTotalStaking(); |
| 57 | + assert.equal(userstaked, 1000, "Total staking should equal to expected"); |
| 58 | + }); |
| 59 | + it("should NEED_TO_WAIT when withdraw", async () => { |
| 60 | + await expectThrow( |
| 61 | + userstaking.withdraw(500, { from: owner1 }), |
| 62 | + "NEED_TO_WAIT" |
| 63 | + ); |
| 64 | + }); |
| 65 | + it("advance block timestampe after MIN_WITHDRAW_DELAY", async () => { |
| 66 | + await exchangeTestUtil.advanceBlockTimestamp(90 * 24 * 60 * 60); |
| 67 | + }); |
| 68 | + it("set protocolfee as 100", async () => { |
| 69 | + const amount = new BN(100); |
| 70 | + await exchangeTestUtil.transferBalance( |
| 71 | + protocolfee.address, |
| 72 | + "LRC", |
| 73 | + amount |
| 74 | + ); |
| 75 | + }); |
| 76 | + it("should get claimed equal as expected", async () => { |
| 77 | + const userclaimed = await userstaking.claim({ from: owner1 }); |
| 78 | + const eventArr: any = await exchangeTestUtil.getEventsFromContract( |
| 79 | + userstaking, |
| 80 | + "LRCRewarded", |
| 81 | + web3.eth.blockNumber |
| 82 | + ); |
| 83 | + const items = eventArr.map((eventObj: any) => { |
| 84 | + const reward = eventObj.args.amount; |
| 85 | + // 70% as staking reward |
| 86 | + assert.equal(reward, 70, "User claimed should equal to expected"); |
| 87 | + }); |
| 88 | + }); |
| 89 | + it("should get withdrawed equal as expected", async () => { |
| 90 | + await userstaking.withdraw(0, { from: owner1 }); |
| 91 | + const eventArr: any = await exchangeTestUtil.getEventsFromContract( |
| 92 | + userstaking, |
| 93 | + "LRCWithdrawn", |
| 94 | + web3.eth.blockNumber |
| 95 | + ); |
| 96 | + const items = eventArr.map((eventObj: any) => { |
| 97 | + const withdraw = eventObj.args.amount; |
| 98 | + assert.equal(withdraw, 1070, "User withdraw should equal to expected"); |
| 99 | + }); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe("stakeA&B&C", () => { |
| 104 | + it("set User A balance as 1000", async () => { |
| 105 | + //const amount = new BN(web3.utils.toWei("1000", "ether")); |
| 106 | + const amount = new BN(1000); |
| 107 | + await exchangeTestUtil.setBalanceAndApprove( |
| 108 | + owner1, |
| 109 | + "LRC", |
| 110 | + amount, |
| 111 | + userstaking.address |
| 112 | + ); |
| 113 | + }); |
| 114 | + it("set User B balance as 1000", async () => { |
| 115 | + const amount = new BN(1000); |
| 116 | + await exchangeTestUtil.setBalanceAndApprove( |
| 117 | + owner2, |
| 118 | + "LRC", |
| 119 | + amount, |
| 120 | + userstaking.address |
| 121 | + ); |
| 122 | + }); |
| 123 | + it("set User C balance as 1000", async () => { |
| 124 | + const amount = new BN(1000); |
| 125 | + await exchangeTestUtil.setBalanceAndApprove( |
| 126 | + owner3, |
| 127 | + "LRC", |
| 128 | + amount, |
| 129 | + userstaking.address |
| 130 | + ); |
| 131 | + }); |
| 132 | + it("should get user A staking equal to staked", async () => { |
| 133 | + await userstaking.stake(1000, { from: owner1 }); |
| 134 | + const userstaked = await userstaking.getUserStaking(owner1); |
| 135 | + assert.equal( |
| 136 | + userstaked.stakeAmount, |
| 137 | + 1000, |
| 138 | + "User staking should equal to expected" |
| 139 | + ); |
| 140 | + }); |
| 141 | + it("advance block timestampe after 30 days", async () => { |
| 142 | + await exchangeTestUtil.advanceBlockTimestamp(30 * 24 * 60 * 60); |
| 143 | + }); |
| 144 | + it("should get user B staking equal to staked", async () => { |
| 145 | + await userstaking.stake(1000, { from: owner2 }); |
| 146 | + const userstaked = await userstaking.getUserStaking(owner2); |
| 147 | + assert.equal( |
| 148 | + userstaked.stakeAmount, |
| 149 | + 1000, |
| 150 | + "User staking should equal to expected" |
| 151 | + ); |
| 152 | + }); |
| 153 | + it("advance block timestampe after 30 days", async () => { |
| 154 | + await exchangeTestUtil.advanceBlockTimestamp(30 * 24 * 60 * 60); |
| 155 | + }); |
| 156 | + it("should get user C staking equal to staked", async () => { |
| 157 | + await userstaking.stake(1000, { from: owner3 }); |
| 158 | + const userstaked = await userstaking.getUserStaking(owner3); |
| 159 | + assert.equal( |
| 160 | + userstaked.stakeAmount, |
| 161 | + 1000, |
| 162 | + "User staking should equal to expected" |
| 163 | + ); |
| 164 | + }); |
| 165 | + it("advance block timestampe after MIN_WITHDRAW_DELAY: 90 days", async () => { |
| 166 | + await exchangeTestUtil.advanceBlockTimestamp(90 * 24 * 60 * 60); |
| 167 | + }); |
| 168 | + it("set protocolfee as 120", async () => { |
| 169 | + const amount = new BN(120); |
| 170 | + await exchangeTestUtil.transferBalance( |
| 171 | + protocolfee.address, |
| 172 | + "LRC", |
| 173 | + amount |
| 174 | + ); |
| 175 | + }); |
| 176 | + it("should get user A claimed equal as expected", async () => { |
| 177 | + const userclaimed = await userstaking.claim({ from: owner1 }); |
| 178 | + const eventArr: any = await exchangeTestUtil.getEventsFromContract( |
| 179 | + userstaking, |
| 180 | + "LRCRewarded", |
| 181 | + web3.eth.blockNumber |
| 182 | + ); |
| 183 | + const items = eventArr.map((eventObj: any) => { |
| 184 | + const reward = eventObj.args.amount; |
| 185 | + // 120 * 70% * (180 / (180 + 120 + 90)) == 35 as staking reward |
| 186 | + // as A's staking time is 30 + 30 + 90 days, B's staking time is 30 + 90 days and C's staking time is 90 days |
| 187 | + |
| 188 | + // 34 for round down when div |
| 189 | + assert(reward == 35 || reward == 34, "User claimed should equal to expected"); |
| 190 | + }); |
| 191 | + }); |
| 192 | + it("should get user B claimed equal as expected", async () => { |
| 193 | + const userclaimed = await userstaking.claim({ from: owner2 }); |
| 194 | + const eventArr: any = await exchangeTestUtil.getEventsFromContract( |
| 195 | + userstaking, |
| 196 | + "LRCRewarded", |
| 197 | + web3.eth.blockNumber |
| 198 | + ); |
| 199 | + const items = eventArr.map((eventObj: any) => { |
| 200 | + const reward = eventObj.args.amount; |
| 201 | + // 120 * 70% * (180 / (180 + 120 + 90)) == 28 as staking reward |
| 202 | + assert(reward == 27 || reward == 28 || reward == 29, "User claimed should equal to expected"); |
| 203 | + }); |
| 204 | + }); |
| 205 | + it("should get user C claimed equal as expected", async () => { |
| 206 | + const userclaimed = await userstaking.claim({ from: owner3 }); |
| 207 | + const eventArr: any = await exchangeTestUtil.getEventsFromContract( |
| 208 | + userstaking, |
| 209 | + "LRCRewarded", |
| 210 | + web3.eth.blockNumber |
| 211 | + ); |
| 212 | + const items = eventArr.map((eventObj: any) => { |
| 213 | + const reward = eventObj.args.amount; |
| 214 | + // 120 * 70% * (180 / (180 + 120 + 90)) == 21 as staking reward |
| 215 | + assert(reward == 20 || reward == 21 || reward == 22, "User claimed should equal to expected"); |
| 216 | + }); |
| 217 | + }); |
| 218 | + }); |
| 219 | +}); |
0 commit comments