|
| 1 | +const {accounts, contract, web3} = require("@openzeppelin/test-environment") |
| 2 | +const {createSnapshot, restoreSnapshot} = require("./helpers/snapshot") |
| 3 | + |
| 4 | +const KeepRegistry = contract.fromArtifact("KeepRegistry") |
| 5 | +const ETHStaking = contract.fromArtifact("ETHStaking") |
| 6 | +const ETHBonding = contract.fromArtifact("ETHBonding") |
| 7 | +const TestEtherReceiver = contract.fromArtifact("TestEtherReceiver") |
| 8 | + |
| 9 | +const {expectEvent, expectRevert} = require("@openzeppelin/test-helpers") |
| 10 | + |
| 11 | +const BN = web3.utils.BN |
| 12 | + |
| 13 | +const chai = require("chai") |
| 14 | +chai.use(require("bn-chai")(BN)) |
| 15 | +const expect = chai.expect |
| 16 | + |
| 17 | +describe("ETHBonding", function () { |
| 18 | + let registry |
| 19 | + let ethStaking |
| 20 | + let ethBonding |
| 21 | + let etherReceiver |
| 22 | + |
| 23 | + let operator |
| 24 | + let authorizer |
| 25 | + let beneficiary |
| 26 | + let stakeOwner |
| 27 | + let bondCreator |
| 28 | + let sortitionPool |
| 29 | + |
| 30 | + before(async () => { |
| 31 | + operator = accounts[1] |
| 32 | + authorizer = accounts[2] |
| 33 | + beneficiary = accounts[3] |
| 34 | + stakeOwner = accounts[4] |
| 35 | + bondCreator = accounts[5] |
| 36 | + sortitionPool = accounts[6] |
| 37 | + |
| 38 | + registry = await KeepRegistry.new() |
| 39 | + ethStaking = await ETHStaking.new(registry.address) |
| 40 | + ethBonding = await ETHBonding.new(registry.address, ethStaking.address) |
| 41 | + etherReceiver = await TestEtherReceiver.new() |
| 42 | + |
| 43 | + await registry.approveOperatorContract(bondCreator) |
| 44 | + |
| 45 | + await ethStaking.stake(operator, beneficiary, authorizer, { |
| 46 | + from: stakeOwner, |
| 47 | + }) |
| 48 | + |
| 49 | + await ethStaking.authorizeOperatorContract(operator, bondCreator, { |
| 50 | + from: authorizer, |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + beforeEach(async () => { |
| 55 | + await createSnapshot() |
| 56 | + }) |
| 57 | + |
| 58 | + afterEach(async () => { |
| 59 | + await restoreSnapshot() |
| 60 | + }) |
| 61 | + |
| 62 | + describe("withdraw", async () => { |
| 63 | + const value = new BN(1000) |
| 64 | + |
| 65 | + beforeEach(async () => { |
| 66 | + await ethBonding.deposit(operator, {value: value}) |
| 67 | + }) |
| 68 | + |
| 69 | + it("can be called by operator", async () => { |
| 70 | + await ethBonding.withdraw(value, operator, {from: operator}) |
| 71 | + // ok, no reverts |
| 72 | + }) |
| 73 | + |
| 74 | + it("can be called by stake owner", async () => { |
| 75 | + await ethBonding.withdraw(value, operator, {from: stakeOwner}) |
| 76 | + // ok, no reverts |
| 77 | + }) |
| 78 | + |
| 79 | + it("cannot be called by authorizer", async () => { |
| 80 | + await expectRevert( |
| 81 | + ethBonding.withdraw(value, operator, {from: authorizer}), |
| 82 | + "Only operator or the owner is allowed to withdraw bond" |
| 83 | + ) |
| 84 | + }) |
| 85 | + |
| 86 | + it("cannot be called by beneficiary", async () => { |
| 87 | + await expectRevert( |
| 88 | + ethBonding.withdraw(value, operator, {from: beneficiary}), |
| 89 | + "Only operator or the owner is allowed to withdraw bond" |
| 90 | + ) |
| 91 | + }) |
| 92 | + |
| 93 | + it("cannot be called by third party", async () => { |
| 94 | + const thirdParty = accounts[7] |
| 95 | + |
| 96 | + await expectRevert( |
| 97 | + ethBonding.withdraw(value, operator, {from: thirdParty}), |
| 98 | + "Only operator or the owner is allowed to withdraw bond" |
| 99 | + ) |
| 100 | + }) |
| 101 | + |
| 102 | + it("transfers unbonded value to beneficiary", async () => { |
| 103 | + const expectedUnbonded = 0 |
| 104 | + |
| 105 | + const expectedBeneficiaryBalance = web3.utils |
| 106 | + .toBN(await web3.eth.getBalance(beneficiary)) |
| 107 | + .add(value) |
| 108 | + |
| 109 | + await ethBonding.withdraw(value, operator, {from: operator}) |
| 110 | + |
| 111 | + const unbonded = await ethBonding.availableUnbondedValue( |
| 112 | + operator, |
| 113 | + bondCreator, |
| 114 | + sortitionPool |
| 115 | + ) |
| 116 | + expect(unbonded).to.eq.BN(expectedUnbonded, "invalid unbonded value") |
| 117 | + |
| 118 | + const actualBeneficiaryBalance = await web3.eth.getBalance(beneficiary) |
| 119 | + expect(actualBeneficiaryBalance).to.eq.BN( |
| 120 | + expectedBeneficiaryBalance, |
| 121 | + "invalid beneficiary balance" |
| 122 | + ) |
| 123 | + }) |
| 124 | + |
| 125 | + it("emits event", async () => { |
| 126 | + const value = new BN(90) |
| 127 | + |
| 128 | + const receipt = await ethBonding.withdraw(value, operator, { |
| 129 | + from: operator, |
| 130 | + }) |
| 131 | + expectEvent(receipt, "UnbondedValueWithdrawn", { |
| 132 | + operator: operator, |
| 133 | + beneficiary: beneficiary, |
| 134 | + amount: value, |
| 135 | + }) |
| 136 | + }) |
| 137 | + |
| 138 | + it("reverts if insufficient unbonded value", async () => { |
| 139 | + const invalidValue = value.add(new BN(1)) |
| 140 | + |
| 141 | + await expectRevert( |
| 142 | + ethBonding.withdraw(invalidValue, operator, {from: operator}), |
| 143 | + "Insufficient unbonded value" |
| 144 | + ) |
| 145 | + }) |
| 146 | + |
| 147 | + it("reverts if transfer fails", async () => { |
| 148 | + const operator2 = accounts[7] |
| 149 | + |
| 150 | + await etherReceiver.setShouldFail(true) |
| 151 | + |
| 152 | + await ethStaking.stake(operator2, etherReceiver.address, authorizer, { |
| 153 | + from: stakeOwner, |
| 154 | + }) |
| 155 | + |
| 156 | + await ethBonding.deposit(operator2, {value: value}) |
| 157 | + |
| 158 | + await expectRevert( |
| 159 | + ethBonding.withdraw(value, operator2, {from: operator2}), |
| 160 | + "Transfer failed" |
| 161 | + ) |
| 162 | + }) |
| 163 | + }) |
| 164 | +}) |
0 commit comments