Skip to content

Commit b351c5f

Browse files
committed
tests
1 parent b734cf2 commit b351c5f

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
6+
const SPONSOR_AMOUNT = '12345'
7+
8+
describe('sponsorship-sponsor', () => {
9+
10+
it('happy path', async () => {
11+
const client = createTestClient(await fetchPrivateKeyWithGas())
12+
const stream = await client.createStream('/test')
13+
const sponsorshipContract = await _operatorContractUtils.deploySponsorshipContract({
14+
streamId: stream.id,
15+
deployer: new Wallet(await fetchPrivateKeyWithGas()).connect(_operatorContractUtils.getProvider())
16+
})
17+
18+
const sponsorer = await generateWalletWithGasAndTokens()
19+
const sponsorshipAddress: string = await sponsorshipContract.getAddress()
20+
await runCommand(`internal sponsorship-sponsor ${sponsorshipAddress} ${SPONSOR_AMOUNT}`, {
21+
privateKey: sponsorer.privateKey
22+
})
23+
24+
const remainingWei = await sponsorshipContract.connect(sponsorer, _operatorContractUtils.getProvider()).remainingWei()
25+
expect(remainingWei).toEqual(parseEther(SPONSOR_AMOUNT))
26+
await client.destroy()
27+
})
28+
})

0 commit comments

Comments
 (0)