|
| 1 | +import { expect } from 'chai' |
| 2 | +import { constants } from 'ethers' |
| 3 | + |
| 4 | +import { GRTWithdrawHelper } from '../../build/typechain/contracts/GRTWithdrawHelper' |
| 5 | +import { GraphToken } from '../../build/typechain/contracts/GraphToken' |
| 6 | +import { Staking } from '../../build/typechain/contracts/Staking' |
| 7 | + |
| 8 | +import { NetworkFixture } from '../lib/fixtures' |
| 9 | +import * as deployment from '../lib/deployment' |
| 10 | +import { |
| 11 | + deriveChannelKey, |
| 12 | + getAccounts, |
| 13 | + randomAddress, |
| 14 | + randomHexBytes, |
| 15 | + toGRT, |
| 16 | + Account, |
| 17 | +} from '../lib/testHelpers' |
| 18 | + |
| 19 | +const { AddressZero } = constants |
| 20 | + |
| 21 | +describe('WithdrawHelper', () => { |
| 22 | + let cmc: Account |
| 23 | + let governor: Account |
| 24 | + let indexer: Account |
| 25 | + |
| 26 | + let fixture: NetworkFixture |
| 27 | + |
| 28 | + let grt: GraphToken |
| 29 | + let staking: Staking |
| 30 | + let withdrawHelper: GRTWithdrawHelper |
| 31 | + |
| 32 | + function createWithdrawData(callData: string) { |
| 33 | + return { |
| 34 | + amount: 0, |
| 35 | + assetId: grt.address, |
| 36 | + callData, |
| 37 | + callTo: withdrawHelper.address, |
| 38 | + channelAddress: randomAddress(), |
| 39 | + nonce: 1, |
| 40 | + recipient: withdrawHelper.address, |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + before(async function () { |
| 45 | + ;[cmc, governor, indexer] = await getAccounts() |
| 46 | + |
| 47 | + fixture = new NetworkFixture() |
| 48 | + ;({ grt, staking } = await fixture.load(governor.signer)) |
| 49 | + withdrawHelper = ((await deployment.deployContract( |
| 50 | + 'GRTWithdrawHelper', |
| 51 | + governor.signer, |
| 52 | + grt.address, |
| 53 | + )) as unknown) as GRTWithdrawHelper |
| 54 | + |
| 55 | + // Give some funds to the indexer and approve staking contract to use funds on indexer behalf |
| 56 | + const indexerTokens = toGRT('100000') |
| 57 | + await grt.connect(governor.signer).mint(indexer.address, indexerTokens) |
| 58 | + await grt.connect(indexer.signer).approve(staking.address, indexerTokens) |
| 59 | + |
| 60 | + // Give some funds to the CMC multisig fake account |
| 61 | + const cmcTokens = toGRT('2000') |
| 62 | + await grt.connect(governor.signer).mint(cmc.address, cmcTokens) |
| 63 | + |
| 64 | + // Allow WithdrawHelper to call the Staking contract |
| 65 | + await staking.connect(governor.signer).setAssetHolder(withdrawHelper.address, true) |
| 66 | + }) |
| 67 | + |
| 68 | + beforeEach(async function () { |
| 69 | + await fixture.setUp() |
| 70 | + }) |
| 71 | + |
| 72 | + afterEach(async function () { |
| 73 | + await fixture.tearDown() |
| 74 | + }) |
| 75 | + |
| 76 | + describe('execute withdrawal', function () { |
| 77 | + it('withdraw tokens from the CMC to staking contract through WithdrawHelper', async function () { |
| 78 | + // Generate test data |
| 79 | + const channelKey = deriveChannelKey() |
| 80 | + const allocationID = channelKey.address |
| 81 | + const subgraphDeploymentID = randomHexBytes(32) |
| 82 | + const metadata = randomHexBytes(32) |
| 83 | + |
| 84 | + // Setup staking |
| 85 | + const stakeTokens = toGRT('100000') |
| 86 | + await staking.connect(indexer.signer).stake(stakeTokens) |
| 87 | + await staking |
| 88 | + .connect(indexer.signer) |
| 89 | + .allocate( |
| 90 | + subgraphDeploymentID, |
| 91 | + stakeTokens, |
| 92 | + allocationID, |
| 93 | + metadata, |
| 94 | + await channelKey.generateProof(indexer.address), |
| 95 | + ) |
| 96 | + |
| 97 | + // Initiate a withdrawal |
| 98 | + // For the purpose of the test we skip the CMC and call WithdrawHelper |
| 99 | + // directly. Transfer tokens from the CMC -> WithdrawHelper being the recipient |
| 100 | + const actualAmount = toGRT('2000') // <- withdraw amount |
| 101 | + await grt.connect(cmc.signer).transfer(withdrawHelper.address, actualAmount) |
| 102 | + |
| 103 | + // Simulate callTo from the CMC to the WithdrawHelper |
| 104 | + const callData = await withdrawHelper.getCallData({ |
| 105 | + staking: staking.address, |
| 106 | + allocationID, |
| 107 | + returnAddress: randomAddress(), |
| 108 | + }) |
| 109 | + const withdrawData = { |
| 110 | + amount: 0, |
| 111 | + assetId: grt.address, |
| 112 | + callData, |
| 113 | + callTo: withdrawHelper.address, |
| 114 | + channelAddress: cmc.address, |
| 115 | + nonce: 1, |
| 116 | + recipient: withdrawHelper.address, |
| 117 | + } |
| 118 | + await withdrawHelper.connect(cmc.signer).execute(withdrawData, actualAmount) |
| 119 | + |
| 120 | + // Allocation must have collected the withdrawn tokens |
| 121 | + const allocation = await staking.allocations(allocationID) |
| 122 | + expect(allocation.collectedFees).eq(actualAmount) |
| 123 | + |
| 124 | + // CMC should not have more funds |
| 125 | + expect(await grt.balanceOf(cmc.address)).eq(0) |
| 126 | + }) |
| 127 | + |
| 128 | + it('withdraw tokens from the CMC to staking contract through WithdrawHelper (invalid allocation)', async function () { |
| 129 | + // Use an invalid allocation |
| 130 | + const allocationID = '0xfefefefefefefefefefefefefefefefefefefefe' |
| 131 | + const returnAddress = randomAddress() |
| 132 | + |
| 133 | + // Initiate a withdrawal |
| 134 | + // For the purpose of the test we skip the CMC and call WithdrawHelper |
| 135 | + // directly. Transfer tokens from the CMC -> WithdrawHelper being the recipient |
| 136 | + const actualAmount = toGRT('2000') // <- withdraw amount |
| 137 | + await grt.connect(cmc.signer).transfer(withdrawHelper.address, actualAmount) |
| 138 | + |
| 139 | + // Simulate callTo from the CMC to the WithdrawHelper |
| 140 | + const callData = await withdrawHelper.getCallData({ |
| 141 | + staking: staking.address, |
| 142 | + allocationID, |
| 143 | + returnAddress, |
| 144 | + }) |
| 145 | + const withdrawData = { |
| 146 | + amount: 0, |
| 147 | + assetId: grt.address, |
| 148 | + callData, |
| 149 | + callTo: withdrawHelper.address, |
| 150 | + channelAddress: cmc.address, |
| 151 | + nonce: 1, |
| 152 | + recipient: withdrawHelper.address, |
| 153 | + } |
| 154 | + |
| 155 | + // This reverts! |
| 156 | + await withdrawHelper.connect(cmc.signer).execute(withdrawData, actualAmount) |
| 157 | + |
| 158 | + // There should not be collected fees |
| 159 | + const allocation = await staking.allocations(allocationID) |
| 160 | + expect(allocation.collectedFees).eq(0) |
| 161 | + |
| 162 | + // CMC should have the funds returned |
| 163 | + expect(await grt.balanceOf(returnAddress)).eq(actualAmount) |
| 164 | + }) |
| 165 | + |
| 166 | + it('reject collect data with no staking address', async function () { |
| 167 | + // Simulate callTo from the CMC to the WithdrawHelper |
| 168 | + const callData = await withdrawHelper.getCallData({ |
| 169 | + staking: AddressZero, |
| 170 | + allocationID: randomAddress(), |
| 171 | + returnAddress: randomAddress(), |
| 172 | + }) |
| 173 | + const tx = withdrawHelper.execute(createWithdrawData(callData), toGRT('100')) |
| 174 | + await expect(tx).revertedWith('GRTWithdrawHelper: !staking') |
| 175 | + }) |
| 176 | + |
| 177 | + it('reject collect data with no allocation', async function () { |
| 178 | + // Simulate callTo from the CMC to the WithdrawHelper |
| 179 | + const callData = await withdrawHelper.getCallData({ |
| 180 | + staking: randomAddress(), |
| 181 | + allocationID: AddressZero, |
| 182 | + returnAddress: randomAddress(), |
| 183 | + }) |
| 184 | + const tx = withdrawHelper.execute(createWithdrawData(callData), toGRT('100')) |
| 185 | + await expect(tx).revertedWith('GRTWithdrawHelper: !allocationID') |
| 186 | + }) |
| 187 | + }) |
| 188 | +}) |
0 commit comments