|
| 1 | +import { _operatorContractUtils } from '@streamr/sdk' |
| 2 | +import { fetchPrivateKeyWithGas, generateWalletWithGasAndTokens } from '@streamr/test-utils' |
| 3 | +import { createTestClient, runCommand } from './utils' |
| 4 | +import { parseEther, Wallet } from 'ethers' |
| 5 | +import { wait } from '@streamr/utils' |
| 6 | + |
| 7 | +const DELEGATION_AMOUNT = '20000' |
| 8 | +const STAKE_AMOUNT = '10000' |
| 9 | +const SELF_DELEGATION_AMOUNT = '100000' |
| 10 | +const MINIMUM_DELEGATION_SECONDS = 1 // the config value defined in StreamrEnvDeployer in network-contracts repo |
| 11 | + |
| 12 | +describe('operator', () => { |
| 13 | + |
| 14 | + it('happy path', async () => { |
| 15 | + const client = createTestClient(await fetchPrivateKeyWithGas()) |
| 16 | + const stream = await client.createStream('/test') |
| 17 | + const sponsorshipContract = await _operatorContractUtils.deploySponsorshipContract({ |
| 18 | + streamId: stream.id, |
| 19 | + deployer: new Wallet(await fetchPrivateKeyWithGas()).connect(_operatorContractUtils.getProvider()) |
| 20 | + }) |
| 21 | + const sponsorshipAddress: string = await sponsorshipContract.getAddress() |
| 22 | + const operator = await generateWalletWithGasAndTokens() |
| 23 | + const operatorContract = await _operatorContractUtils.deployOperatorContract({ |
| 24 | + deployer: operator |
| 25 | + }) |
| 26 | + await _operatorContractUtils.delegate(operator, await operatorContract.getAddress(), parseEther(SELF_DELEGATION_AMOUNT)) |
| 27 | + const delegator = await generateWalletWithGasAndTokens() |
| 28 | + const operatorAddress: string = await operatorContract.getAddress() |
| 29 | + |
| 30 | + // delegate |
| 31 | + await runCommand(`internal operator-delegate ${operatorAddress} ${DELEGATION_AMOUNT}`, { |
| 32 | + privateKey: delegator.privateKey |
| 33 | + }) |
| 34 | + expect(await operatorContract.balanceInData(await delegator.getAddress())).toEqual(parseEther(DELEGATION_AMOUNT)) |
| 35 | + |
| 36 | + // stake |
| 37 | + await runCommand(`internal operator-stake ${operatorAddress} ${sponsorshipAddress} ${STAKE_AMOUNT}`, { |
| 38 | + privateKey: operator.privateKey |
| 39 | + }) |
| 40 | + expect(await operatorContract.totalStakedIntoSponsorshipsWei()).toEqual(parseEther(STAKE_AMOUNT)) |
| 41 | + |
| 42 | + // unstake |
| 43 | + await runCommand(`internal operator-unstake ${operatorAddress} ${sponsorshipAddress}`, { |
| 44 | + privateKey: operator.privateKey |
| 45 | + }) |
| 46 | + expect(await operatorContract.totalStakedIntoSponsorshipsWei()).toEqual(0n) |
| 47 | + |
| 48 | + // undelegate |
| 49 | + await wait(MINIMUM_DELEGATION_SECONDS) |
| 50 | + await runCommand(`internal operator-undelegate ${operatorAddress} ${DELEGATION_AMOUNT}`, { |
| 51 | + privateKey: delegator.privateKey |
| 52 | + }) |
| 53 | + expect(await operatorContract.balanceInData(await delegator.getAddress())).toEqual(0n) |
| 54 | + |
| 55 | + await client.destroy() |
| 56 | + }, 10 * 1000) |
| 57 | +}) |
0 commit comments