From e4d110604b9a0fdd238c710ae2aa83b0d5da94f4 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 18:09:10 +0200 Subject: [PATCH 01/21] use bigint --- .../operator/MaintainTopologyService.test.ts | 6 ++-- .../plugins/operator/OperatorPlugin.test.ts | 14 ++++---- .../operator/checkOperatorValueBreach.test.ts | 14 ++++---- .../operator/maintainOperatorValue.test.ts | 7 ++-- packages/node/test/smoke/inspect.test.ts | 13 +++---- packages/node/test/smoke/profit.test.ts | 34 +++++++++---------- .../src/contracts/operatorContractUtils.ts | 22 ++++++------ packages/sdk/test/end-to-end/Operator.test.ts | 20 +++++------ 8 files changed, 67 insertions(+), 63 deletions(-) diff --git a/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts b/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts index 085ae2ef6c..4ca4a71e65 100644 --- a/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts +++ b/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts @@ -70,8 +70,8 @@ describe('MaintainTopologyService', () => { const sponsorship1 = await deploySponsorshipContract({ deployer: operatorWallet, streamId: stream1.id }) const sponsorship2 = await deploySponsorshipContract({ deployer: operatorWallet, streamId: stream2.id }) const operatorContract = await deployOperatorContract({ deployer: operatorWallet }) - await delegate(operatorWallet, await operatorContract.getAddress(), 20000) - await stake(operatorContract, await sponsorship1.getAddress(), 10000) + await delegate(operatorWallet, await operatorContract.getAddress(), 20000n) + await stake(operatorContract, await sponsorship1.getAddress(), 10000n) const createOperatorFleetState = OperatorFleetState.createOperatorFleetStateBuilder( client, @@ -104,7 +104,7 @@ describe('MaintainTopologyService', () => { return containsAll(await getSubscribedStreamPartIds(client), await stream1.getStreamParts()) }, 10000, 1000) - await stake(operatorContract, await sponsorship2.getAddress(), 10000) + await stake(operatorContract, await sponsorship2.getAddress(), 10000n) await until(async () => { return containsAll(await getSubscribedStreamPartIds(client), [ ...await stream1.getStreamParts(), diff --git a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts index 1da3b9cc74..5ab4e95907 100644 --- a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts +++ b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts @@ -21,6 +21,8 @@ const { stake } = _operatorContractUtils +const SPONSOR_AMOUNT = 10000n + describe('OperatorPlugin', () => { let broker: Broker @@ -54,9 +56,9 @@ describe('OperatorPlugin', () => { const sponsorer = await generateWalletWithGasAndTokens() const sponsorship1 = await deploySponsorshipContract({ streamId: stream.id, deployer: sponsorer }) - await sponsor(sponsorer, await sponsorship1.getAddress(), 10000) - await delegate(operatorWallet, await operatorContract.getAddress(), 10000) - await stake(operatorContract, await sponsorship1.getAddress(), 10000) + await sponsor(sponsorer, await sponsorship1.getAddress(), SPONSOR_AMOUNT) + await delegate(operatorWallet, await operatorContract.getAddress(), SPONSOR_AMOUNT) + await stake(operatorContract, await sponsorship1.getAddress(), SPONSOR_AMOUNT) const publisher = createClient(fastPrivateKey()) await stream.grantPermissions({ @@ -112,9 +114,9 @@ describe('OperatorPlugin', () => { const sponsorer = await generateWalletWithGasAndTokens() const sponsorship1 = await deploySponsorshipContract({ streamId: stream.id, deployer: sponsorer }) - await sponsor(sponsorer, await sponsorship1.getAddress(), 10000) - await delegate(operatorWallet, await operatorContract.getAddress(), 10000) - await stake(operatorContract, await sponsorship1.getAddress(), 10000) + await sponsor(sponsorer, await sponsorship1.getAddress(), SPONSOR_AMOUNT) + await delegate(operatorWallet, await operatorContract.getAddress(), SPONSOR_AMOUNT) + await stake(operatorContract, await sponsorship1.getAddress(), SPONSOR_AMOUNT) const operatorContractAddress = await operatorContract.getAddress() broker = await startBroker({ diff --git a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts index c5e6289308..19573fcf2f 100644 --- a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts +++ b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts @@ -49,13 +49,13 @@ describe('checkOperatorValueBreach', () => { const { operatorContract: watcherOperatorContract, nodeWallets: watcherWallets } = await setupOperatorContract({ nodeCount: 1, ...deployConfig }) const { operatorWallet, operatorContract } = await setupOperatorContract(deployConfig) const sponsorer = await generateWalletWithGasAndTokens() - await delegate(operatorWallet, await operatorContract.getAddress(), 20000) - const sponsorship1 = await deploySponsorshipContract({ earningsPerSecond: 100, streamId, deployer: operatorWallet }) - await sponsor(sponsorer, await sponsorship1.getAddress(), 25000) - await stake(operatorContract, await sponsorship1.getAddress(), 10000) - const sponsorship2 = await deploySponsorshipContract({ earningsPerSecond: 200, streamId, deployer: operatorWallet }) - await sponsor(sponsorer, await sponsorship2.getAddress(), 25000) - await stake(operatorContract, await sponsorship2.getAddress(), 10000) + await delegate(operatorWallet, await operatorContract.getAddress(), 20000n) + const sponsorship1 = await deploySponsorshipContract({ earningsPerSecond: 100n, streamId, deployer: operatorWallet }) + await sponsor(sponsorer, await sponsorship1.getAddress(), 25000n) + await stake(operatorContract, await sponsorship1.getAddress(), 10000n) + const sponsorship2 = await deploySponsorshipContract({ earningsPerSecond: 200n, streamId, deployer: operatorWallet }) + await sponsor(sponsorer, await sponsorship2.getAddress(), 25000n) + await stake(operatorContract, await sponsorship2.getAddress(), 10000n) const valueBeforeWithdraw = await operatorContract.valueWithoutEarnings() const streamrConfigAddress = await operatorContract.streamrConfig() const streamrConfig = new Contract(streamrConfigAddress, streamrConfigABI, getProvider()) as unknown as StreamrConfig diff --git a/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts b/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts index 0db255bf55..6a0b61ef86 100644 --- a/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts +++ b/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts @@ -16,7 +16,8 @@ const { const logger = new Logger(module) -const STAKE_AMOUNT = 10000 +const SPONSOR_AMOUNT = 25000n +const STAKE_AMOUNT = 10000n const SAFETY_FRACTION = 0.5 // 50% describe('maintainOperatorValue', () => { @@ -44,8 +45,8 @@ describe('maintainOperatorValue', () => { } }) const sponsorer = await generateWalletWithGasAndTokens() - const sponsorship = await deploySponsorshipContract({ earningsPerSecond: 100, streamId, deployer: operatorWallet }) - await sponsor(sponsorer, await sponsorship.getAddress(), 25000) + const sponsorship = await deploySponsorshipContract({ earningsPerSecond: 100n, streamId, deployer: operatorWallet }) + await sponsor(sponsorer, await sponsorship.getAddress(), SPONSOR_AMOUNT) await delegate(operatorWallet, await operatorContract.getAddress(), STAKE_AMOUNT) await stake(operatorContract, await sponsorship.getAddress(), STAKE_AMOUNT) const operator = createClient(nodeWallets[0].privateKey).getOperator(toEthereumAddress(await operatorContract.getAddress())) diff --git a/packages/node/test/smoke/inspect.test.ts b/packages/node/test/smoke/inspect.test.ts index 2345fb5326..f45f6566f4 100644 --- a/packages/node/test/smoke/inspect.test.ts +++ b/packages/node/test/smoke/inspect.test.ts @@ -62,11 +62,12 @@ const CLOSE_EXPIRED_FLAGS_MAX_AGE = 30 * 1000 const VALID_OPERATOR_COUNT = 3 // one flagger and at least two voters are needed (see VoteKickPolicy.sol:166) const MAX_TEST_RUN_TIME = 15 * 60 * 1000 -const DELEGATE_WEI = 50000 -const STAKE_WEI = 10000 -const REVIEWER_REWARD_WEI = 700 -const FLAGGER_REWARD_WEI = 900 +const DELEGATE_WEI = 50000n +const STAKE_WEI = 10000n +const REVIEWER_REWARD_WEI = 700n +const FLAGGER_REWARD_WEI = 900n const SLASHING_FRACTION = 0.25 +const SLASHING_WEI = BigInt(SLASHING_FRACTION * Number(STAKE_WEI)) // two operators and a sponsorship which have been created in dev-chain init const PRE_BAKED_OPERATORS = [{ @@ -221,7 +222,7 @@ describe('inspect', () => { logger.info('Setup sponsorship') const streamId = await createStream() const sponsorer = await generateWalletWithGasAndTokens() - const sponsorship = await deploySponsorshipContract({ earningsPerSecond: 0, streamId, deployer: sponsorer }) + const sponsorship = await deploySponsorshipContract({ earningsPerSecond: 0n, streamId, deployer: sponsorer }) logger.info('Create operators') freeriderOperator = await createOperator({}, await sponsorship.getAddress(), true) const CONFIG = { @@ -312,7 +313,7 @@ describe('inspect', () => { // assert slashing and rewards const token = getTestTokenContract().connect(getProvider()) - expect(await getTokenBalance(freeriderOperator.contractAddress, token)).toEqual(DELEGATE_WEI - SLASHING_FRACTION * STAKE_WEI) + expect(await getTokenBalance(freeriderOperator.contractAddress, token)).toEqual(DELEGATE_WEI - SLASHING_WEI) expect(await getTokenBalance(flags[0].flagger, token)).toEqual(DELEGATE_WEI - STAKE_WEI + FLAGGER_REWARD_WEI) for (const voter of flags[0].votes.map((vote) => vote.voter)) { expect(await getTokenBalance(voter, token)).toEqual(DELEGATE_WEI - STAKE_WEI + REVIEWER_REWARD_WEI) diff --git a/packages/node/test/smoke/profit.test.ts b/packages/node/test/smoke/profit.test.ts index 25022dd3a9..dc66c8a843 100644 --- a/packages/node/test/smoke/profit.test.ts +++ b/packages/node/test/smoke/profit.test.ts @@ -46,16 +46,16 @@ const { getTestAdminWallet } = _operatorContractUtils -const SPONSOR_AMOUNT = 6000 -const OPERATOR_DELEGATED_AMOUNT = 5000 -const EXTERNAL_DELEGATED_AMOUNT = 5260 -const EARNINGS_PER_SECOND = 1000 +const SPONSOR_AMOUNT = 6000n +const OPERATOR_DELEGATED_AMOUNT = 5000n +const EXTERNAL_DELEGATED_AMOUNT = 5260n +const EARNINGS_PER_SECOND = 1000n const OPERATORS_CUT_PERCENTAGE = 10 const PROTOCOL_FEE_PERCENTAGE = 5 -const PROTOCOL_FEE = SPONSOR_AMOUNT * (PROTOCOL_FEE_PERCENTAGE / 100) +const PROTOCOL_FEE = BigInt(Number(SPONSOR_AMOUNT) * (PROTOCOL_FEE_PERCENTAGE / 100)) const TOTAL_PROFIT = SPONSOR_AMOUNT - PROTOCOL_FEE const TOTAL_DELEGATED = OPERATOR_DELEGATED_AMOUNT + EXTERNAL_DELEGATED_AMOUNT -const OPERATORS_CUT = TOTAL_PROFIT * (OPERATORS_CUT_PERCENTAGE / 100) +const OPERATORS_CUT = BigInt(Number(TOTAL_PROFIT) * (OPERATORS_CUT_PERCENTAGE / 100)) const OPERATOR_PROFIT_WHEN_NO_WITHDRAWALS = (TOTAL_PROFIT - OPERATORS_CUT) * OPERATOR_DELEGATED_AMOUNT / TOTAL_DELEGATED + OPERATORS_CUT const DELEGATOR_PROFIT_WHEN_NO_WITHDRAWALS = (TOTAL_PROFIT - OPERATORS_CUT) * EXTERNAL_DELEGATED_AMOUNT / TOTAL_DELEGATED // If the operator doesn't make any withdrawals during the sponsorship period, the profit is split between @@ -63,7 +63,7 @@ const DELEGATOR_PROFIT_WHEN_NO_WITHDRAWALS = (TOTAL_PROFIT - OPERATORS_CUT) * EX // the operator gets a larger share of the profit. This happens because the operator's delegated amount // grows by both their profit share and their cut of the total profit, while the external delegator's amount // only grows by their profit share. -const PROFIT_INACCURACY = 50 +const PROFIT_INACCURACY = 50n describe('profit', () => { @@ -75,20 +75,20 @@ describe('profit', () => { let sponsorshipContract: Sponsorship const getBalances = async (): Promise<{ - operator: number - delegator: number - sponsor: number - admin: number - operatorContract: number + operator: bigint + delegator: bigint + sponsor: bigint + admin: bigint + operatorContract: bigint }> => { const dataToken = getTestTokenContract().connect(getProvider()) const adminWallet = getTestAdminWallet() return { - operator: Number(formatEther(await dataToken.balanceOf(operatorWallet.address))), - delegator: Number(formatEther(await dataToken.balanceOf(delegatorWallet.address))), - sponsor: Number(formatEther(await dataToken.balanceOf(sponsorWallet.address))), - admin: Number(formatEther(await dataToken.balanceOf(adminWallet.address))), - operatorContract: Number(formatEther(await dataToken.balanceOf(await operatorContract.getAddress()))), + operator: BigInt(formatEther(await dataToken.balanceOf(operatorWallet.address))), + delegator: BigInt(formatEther(await dataToken.balanceOf(delegatorWallet.address))), + sponsor: BigInt(formatEther(await dataToken.balanceOf(sponsorWallet.address))), + admin: BigInt(formatEther(await dataToken.balanceOf(adminWallet.address))), + operatorContract: BigInt(formatEther(await dataToken.balanceOf(await operatorContract.getAddress()))), } } diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 32689d265c..0760ce3e1a 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -136,7 +136,7 @@ export interface DeploySponsorshipContractOpts { deployer: Wallet metadata?: string minOperatorCount?: number - earningsPerSecond?: number + earningsPerSecond?: bigint chainConfig?: { contracts: { SponsorshipFactory: string @@ -222,19 +222,19 @@ export async function generateWalletWithGasAndTokens(opts?: GenerateWalletWithGa return newWallet.connect(provider) as (Wallet & SignerWithProvider) } -export const delegate = async (delegator: Wallet, operatorContractAddress: string, amount: number, token?: TestTokenContract): Promise => { - logger.debug('Delegate', { amount }) +export const delegate = async (delegator: Wallet, operatorContractAddress: string, amountWei: bigint, token?: TestTokenContract): Promise => { + logger.debug('Delegate', { amountWei }) // onTokenTransfer: the tokens are delegated on behalf of the given data address // eslint-disable-next-line max-len // https://github.com/streamr-dev/network-contracts/blob/01ec980cfe576e25e8c9acc08a57e1e4769f3e10/packages/network-contracts/contracts/OperatorTokenomics/Operator.sol#L233 - await transferTokens(delegator, operatorContractAddress, amount, delegator.address, token) + await transferTokens(delegator, operatorContractAddress, amountWei, delegator.address, token) } -export const undelegate = async (delegator: Wallet, operatorContract: OperatorContract, amount: number): Promise => { +export const undelegate = async (delegator: Wallet, operatorContract: OperatorContract, amount: bigint): Promise => { await (await operatorContract.connect(delegator).undelegate(parseEther(amount.toString()))).wait() } -export const stake = async (operatorContract: OperatorContract, sponsorshipContractAddress: string, amount: number): Promise => { +export const stake = async (operatorContract: OperatorContract, sponsorshipContractAddress: string, amount: bigint): Promise => { logger.debug('Stake', { amount }) await (await operatorContract.stake(sponsorshipContractAddress, parseEther(amount.toString()))).wait() } @@ -244,15 +244,15 @@ export const unstake = async (operatorContract: OperatorContract, sponsorshipCon await (await operatorContract.unstake(sponsorshipContractAddress)).wait() } -export const sponsor = async (sponsorer: Wallet, sponsorshipContractAddress: string, amount: number, token?: TestTokenContract): Promise => { - logger.debug('Sponsor', { amount }) +export const sponsor = async (sponsorer: Wallet, sponsorshipContractAddress: string, amountWei: bigint, token?: TestTokenContract): Promise => { + logger.debug('Sponsor', { amountWei }) // eslint-disable-next-line max-len // https://github.com/streamr-dev/network-contracts/blob/01ec980cfe576e25e8c9acc08a57e1e4769f3e10/packages/network-contracts/contracts/OperatorTokenomics/Sponsorship.sol#L139 - await transferTokens(sponsorer, sponsorshipContractAddress, amount, undefined, token) + await transferTokens(sponsorer, sponsorshipContractAddress, amountWei, undefined, token) } -export const transferTokens = async (from: Wallet, to: string, amount: number, data?: string, token?: TestTokenContract): Promise => { - const tx = await ((token ?? getTestTokenContract()).connect(from).transferAndCall(to, parseEther(amount.toString()), data ?? '0x')) +export const transferTokens = async (from: Wallet, to: string, amountWei: bigint, data?: string, token?: TestTokenContract): Promise => { + const tx = await ((token ?? getTestTokenContract()).connect(from).transferAndCall(to, parseEther(amountWei.toString()), data ?? '0x')) await tx.wait() } diff --git a/packages/sdk/test/end-to-end/Operator.test.ts b/packages/sdk/test/end-to-end/Operator.test.ts index 3a85a43bf4..69ff7d15f2 100644 --- a/packages/sdk/test/end-to-end/Operator.test.ts +++ b/packages/sdk/test/end-to-end/Operator.test.ts @@ -80,8 +80,8 @@ describe('Operator', () => { }, 90 * 1000) it('getStakedOperators', async () => { - await delegate(deployedOperator.operatorWallet, await deployedOperator.operatorContract.getAddress(), 20000) - await stake(deployedOperator.operatorContract, await sponsorship1.getAddress(), 10000) + await delegate(deployedOperator.operatorWallet, await deployedOperator.operatorContract.getAddress(), 20000n) + await stake(deployedOperator.operatorContract, await sponsorship1.getAddress(), 10000n) const dummyOperator = await getOperator(deployedOperator.nodeWallets[0], deployedOperator) const randomOperatorAddress = sample(await dummyOperator.getStakedOperators()) expect(randomOperatorAddress).toBeDefined() @@ -105,9 +105,9 @@ describe('Operator', () => { it('getSponsorships, getOperatorsInSponsorship', async () => { const operatorContractAddress = toEthereumAddress(await deployedOperator.operatorContract.getAddress()) - await delegate(deployedOperator.operatorWallet, operatorContractAddress, 20000) - await stake(deployedOperator.operatorContract, await sponsorship1.getAddress(), 10000) - await stake(deployedOperator.operatorContract, await sponsorship2.getAddress(), 10000) + await delegate(deployedOperator.operatorWallet, operatorContractAddress, 20000n) + await stake(deployedOperator.operatorContract, await sponsorship1.getAddress(), 10000n) + await stake(deployedOperator.operatorContract, await sponsorship2.getAddress(), 10000n) const operator = await getOperator(undefined, deployedOperator) @@ -138,12 +138,12 @@ describe('Operator', () => { const flagger = deployedOperator const target = await setupOperatorContract() - await sponsor(flagger.operatorWallet, await sponsorship2.getAddress(), 50000) + await sponsor(flagger.operatorWallet, await sponsorship2.getAddress(), 50000n) - await delegate(flagger.operatorWallet, await flagger.operatorContract.getAddress(), 20000) - await delegate(target.operatorWallet, await target.operatorContract.getAddress(), 30000) - await stake(flagger.operatorContract, await sponsorship2.getAddress(), 15000) - await stake(target.operatorContract, await sponsorship2.getAddress(), 25000) + await delegate(flagger.operatorWallet, await flagger.operatorContract.getAddress(), 20000n) + await delegate(target.operatorWallet, await target.operatorContract.getAddress(), 30000n) + await stake(flagger.operatorContract, await sponsorship2.getAddress(), 15000n) + await stake(target.operatorContract, await sponsorship2.getAddress(), 25000n) const contractFacade = await getOperator(deployedOperator.nodeWallets[0], flagger) await contractFacade.flag( From 6321d0c966d9baf75261c14fb06399b33a9a8b48 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 18:41:03 +0200 Subject: [PATCH 02/21] rm DeploySponsorshipContractOpts#metadata --- packages/sdk/src/contracts/operatorContractUtils.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 0760ce3e1a..41a4988db1 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -134,7 +134,6 @@ export async function deployOperatorContract(opts: DeployOperatorContractOpts): export interface DeploySponsorshipContractOpts { streamId: string deployer: Wallet - metadata?: string minOperatorCount?: number earningsPerSecond?: bigint chainConfig?: { @@ -158,7 +157,7 @@ export async function deploySponsorshipContract(opts: DeploySponsorshipContractO const sponsorshipDeployTx = await sponsorshipFactory.deploySponsorship( (opts.minOperatorCount ?? 1).toString(), opts.streamId, - opts.metadata ?? '{}', + '{}', [ chainConfig.contracts.SponsorshipStakeWeightedAllocationPolicy, chainConfig.contracts.SponsorshipDefaultLeavePolicy, From 22227d99c5aab256dda50ad21baa569a7f366b22 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 18:50:29 +0200 Subject: [PATCH 03/21] rename generateWalletWithGasAndTokens() -> createTestWallet(), rm parameter --- .../operator/MaintainTopologyService.test.ts | 4 ++-- .../plugins/operator/OperatorPlugin.test.ts | 6 +++--- .../operator/checkOperatorValueBreach.test.ts | 4 ++-- .../operator/maintainOperatorValue.test.ts | 4 ++-- packages/node/test/smoke/inspect.test.ts | 4 ++-- packages/node/test/smoke/profit.test.ts | 6 +++--- .../sdk/src/contracts/operatorContractUtils.ts | 18 ++++-------------- packages/sdk/src/exports.ts | 4 ++-- 8 files changed, 20 insertions(+), 30 deletions(-) diff --git a/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts b/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts index 4ca4a71e65..03f5542646 100644 --- a/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts +++ b/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts @@ -14,7 +14,7 @@ const { delegate, deployOperatorContract, deploySponsorshipContract, - generateWalletWithGasAndTokens, + createTestWallet, stake } = _operatorContractUtils @@ -65,7 +65,7 @@ describe('MaintainTopologyService', () => { }) it('happy path', async () => { - const operatorWallet = await generateWalletWithGasAndTokens() + const operatorWallet = await createTestWallet() const [stream1, stream2] = await setUpStreams() const sponsorship1 = await deploySponsorshipContract({ deployer: operatorWallet, streamId: stream1.id }) const sponsorship2 = await deploySponsorshipContract({ deployer: operatorWallet, streamId: stream2.id }) diff --git a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts index 5ab4e95907..e800bfeb55 100644 --- a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts +++ b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts @@ -15,7 +15,7 @@ import { createClient, createTestStream, formConfig, startBroker } from '../../. const { delegate, deploySponsorshipContract, - generateWalletWithGasAndTokens, + createTestWallet, setupOperatorContract, sponsor, stake @@ -54,7 +54,7 @@ describe('OperatorPlugin', () => { const subscriber = createClient(await fetchPrivateKeyWithGas()) const stream = await createTestStream(subscriber, module) - const sponsorer = await generateWalletWithGasAndTokens() + const sponsorer = await createTestWallet() const sponsorship1 = await deploySponsorshipContract({ streamId: stream.id, deployer: sponsorer }) await sponsor(sponsorer, await sponsorship1.getAddress(), SPONSOR_AMOUNT) await delegate(operatorWallet, await operatorContract.getAddress(), SPONSOR_AMOUNT) @@ -112,7 +112,7 @@ describe('OperatorPlugin', () => { const client = createClient(await fetchPrivateKeyWithGas()) const stream = await createTestStream(client, module) - const sponsorer = await generateWalletWithGasAndTokens() + const sponsorer = await createTestWallet() const sponsorship1 = await deploySponsorshipContract({ streamId: stream.id, deployer: sponsorer }) await sponsor(sponsorer, await sponsorship1.getAddress(), SPONSOR_AMOUNT) await delegate(operatorWallet, await operatorContract.getAddress(), SPONSOR_AMOUNT) diff --git a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts index 19573fcf2f..17c6f621ab 100644 --- a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts +++ b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts @@ -11,7 +11,7 @@ import { createClient, createTestStream } from '../../../utils' const { delegate, deploySponsorshipContract, - generateWalletWithGasAndTokens, + createTestWallet, getProvider, setupOperatorContract, sponsor, @@ -48,7 +48,7 @@ describe('checkOperatorValueBreach', () => { // eslint-disable-next-line max-len const { operatorContract: watcherOperatorContract, nodeWallets: watcherWallets } = await setupOperatorContract({ nodeCount: 1, ...deployConfig }) const { operatorWallet, operatorContract } = await setupOperatorContract(deployConfig) - const sponsorer = await generateWalletWithGasAndTokens() + const sponsorer = await createTestWallet() await delegate(operatorWallet, await operatorContract.getAddress(), 20000n) const sponsorship1 = await deploySponsorshipContract({ earningsPerSecond: 100n, streamId, deployer: operatorWallet }) await sponsor(sponsorer, await sponsorship1.getAddress(), 25000n) diff --git a/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts b/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts index 6a0b61ef86..bbf6161a08 100644 --- a/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts +++ b/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts @@ -8,7 +8,7 @@ import { createClient, createTestStream } from '../../../utils' const { delegate, deploySponsorshipContract, - generateWalletWithGasAndTokens, + createTestWallet, setupOperatorContract, sponsor, stake @@ -44,7 +44,7 @@ describe('maintainOperatorValue', () => { operatorsCutPercent: 10 } }) - const sponsorer = await generateWalletWithGasAndTokens() + const sponsorer = await createTestWallet() const sponsorship = await deploySponsorshipContract({ earningsPerSecond: 100n, streamId, deployer: operatorWallet }) await sponsor(sponsorer, await sponsorship.getAddress(), SPONSOR_AMOUNT) await delegate(operatorWallet, await operatorContract.getAddress(), STAKE_AMOUNT) diff --git a/packages/node/test/smoke/inspect.test.ts b/packages/node/test/smoke/inspect.test.ts index f45f6566f4..9f06dad1b8 100644 --- a/packages/node/test/smoke/inspect.test.ts +++ b/packages/node/test/smoke/inspect.test.ts @@ -39,7 +39,7 @@ import { OperatorPluginConfig } from './../../src/plugins/operator/OperatorPlugi const { setupOperatorContract, getProvider, - generateWalletWithGasAndTokens, + createTestWallet, deploySponsorshipContract, delegate, stake, @@ -221,7 +221,7 @@ describe('inspect', () => { await streamrConfig.setSlashingFraction(parseEther(String(SLASHING_FRACTION))) logger.info('Setup sponsorship') const streamId = await createStream() - const sponsorer = await generateWalletWithGasAndTokens() + const sponsorer = await createTestWallet() const sponsorship = await deploySponsorshipContract({ earningsPerSecond: 0n, streamId, deployer: sponsorer }) logger.info('Create operators') freeriderOperator = await createOperator({}, await sponsorship.getAddress(), true) diff --git a/packages/node/test/smoke/profit.test.ts b/packages/node/test/smoke/profit.test.ts index dc66c8a843..0e9213991d 100644 --- a/packages/node/test/smoke/profit.test.ts +++ b/packages/node/test/smoke/profit.test.ts @@ -35,7 +35,7 @@ import { createClient, createTestStream, startBroker } from '../utils' const { setupOperatorContract, getProvider, - generateWalletWithGasAndTokens, + createTestWallet, deploySponsorshipContract, sponsor, delegate, @@ -111,8 +111,8 @@ describe('profit', () => { streamId, deployer: operatorWallet // could be any wallet with gas }) - sponsorWallet = await generateWalletWithGasAndTokens() - delegatorWallet = await generateWalletWithGasAndTokens() + sponsorWallet = await createTestWallet() + delegatorWallet = await createTestWallet() const streamrConfig = new Contract( CHAIN_CONFIG.dev2.contracts.StreamrConfig, streamrConfigABI diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 41a4988db1..7bae1f249e 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -53,9 +53,7 @@ const logger = new Logger(module) export async function setupOperatorContract( opts?: SetupOperatorContractOpts ): Promise { - const operatorWallet = await generateWalletWithGasAndTokens({ - chainConfig: opts?.chainConfig - }) + const operatorWallet = await createTestWallet() const operatorContract = await deployOperatorContract({ chainConfig: opts?.chainConfig ?? TEST_CHAIN_CONFIG, deployer: operatorWallet, @@ -65,9 +63,7 @@ export async function setupOperatorContract( const nodeWallets: (Wallet & SignerWithProvider)[] = [] if ((opts?.nodeCount !== undefined) && (opts?.nodeCount > 0)) { for (const _ of range(opts.nodeCount)) { - nodeWallets.push(await generateWalletWithGasAndTokens({ - chainConfig: opts?.chainConfig - })) + nodeWallets.push(await createTestWallet()) } await (await operatorContract.setNodeAddresses(nodeWallets.map((w) => w.address))).wait() } @@ -191,18 +187,12 @@ export const getTestAdminWallet = (adminKey?: string, provider?: Provider): Wall return new Wallet(adminKey ?? TEST_CHAIN_CONFIG.adminPrivateKey).connect(provider ?? getProvider()) } -interface GenerateWalletWithGasAndTokensOpts { - chainConfig?: { contracts: { DATA: string } } -} - -export async function generateWalletWithGasAndTokens(opts?: GenerateWalletWithGasAndTokensOpts): Promise { +export async function createTestWallet(): Promise { const provider = getProvider() const privateKey = crypto.randomBytes(32).toString('hex') const newWallet = new Wallet(privateKey) const adminWallet = getTestAdminWallet() - const token = (opts?.chainConfig !== undefined) - ? new Contract(opts.chainConfig.contracts.DATA, TestTokenArtifact, adminWallet) as unknown as TestTokenContract - : getTestTokenContract().connect(adminWallet) + const token = getTestTokenContract().connect(adminWallet) await retry( async () => { await (await token.mint(newWallet.address, parseEther('1000000'))).wait() diff --git a/packages/sdk/src/exports.ts b/packages/sdk/src/exports.ts index 379f5e92f8..9d78ba94ca 100644 --- a/packages/sdk/src/exports.ts +++ b/packages/sdk/src/exports.ts @@ -104,7 +104,7 @@ import { stake, unstake, getProvider, - generateWalletWithGasAndTokens, + createTestWallet, DeploySponsorshipContractOpts, getTestTokenContract, getTestAdminWallet, @@ -124,7 +124,7 @@ const _operatorContractUtils = { stake, unstake, getProvider, - generateWalletWithGasAndTokens, + createTestWallet, deployOperatorContract, getTestTokenContract, getTestAdminWallet, From 0561854acd295605a55e18d5b5ce1955d8411fa0 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 18:57:17 +0200 Subject: [PATCH 04/21] rename setupOperatorContract() -> setupTestOperatorContract() --- .../plugins/operator/OperatorPlugin.test.ts | 4 ++-- .../plugins/operator/announceNodeToContract.test.ts | 2 +- .../plugins/operator/announceNodeToStream.test.ts | 2 +- .../plugins/operator/checkOperatorValueBreach.test.ts | 10 +++++----- .../plugins/operator/maintainOperatorValue.test.ts | 4 ++-- packages/node/test/smoke/inspect.test.ts | 4 ++-- packages/node/test/smoke/profit.test.ts | 4 ++-- packages/sdk/src/contracts/operatorContractUtils.ts | 6 +++--- packages/sdk/src/exports.ts | 8 ++++---- packages/sdk/test/end-to-end/Operator.test.ts | 6 +++--- 10 files changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts index e800bfeb55..3a1c57b2a3 100644 --- a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts +++ b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts @@ -16,7 +16,7 @@ const { delegate, deploySponsorshipContract, createTestWallet, - setupOperatorContract, + setupTestOperatorContract, sponsor, stake } = _operatorContractUtils @@ -31,7 +31,7 @@ describe('OperatorPlugin', () => { let operatorWallet: Wallet beforeAll(async () => { - const deployment = (await setupOperatorContract({ + const deployment = (await setupTestOperatorContract({ nodeCount: 1 })) brokerWallet = deployment.nodeWallets[0] diff --git a/packages/node/test/integration/plugins/operator/announceNodeToContract.test.ts b/packages/node/test/integration/plugins/operator/announceNodeToContract.test.ts index 977a7124b6..a13abbf4be 100644 --- a/packages/node/test/integration/plugins/operator/announceNodeToContract.test.ts +++ b/packages/node/test/integration/plugins/operator/announceNodeToContract.test.ts @@ -10,7 +10,7 @@ describe('announceNodeToContract', () => { let operator: Operator beforeEach(async () => { - const { operatorContract, nodeWallets } = await _operatorContractUtils.setupOperatorContract({ + const { operatorContract, nodeWallets } = await _operatorContractUtils.setupTestOperatorContract({ nodeCount: 1 }) operator = createClient(nodeWallets[0].privateKey).getOperator(toEthereumAddress(await operatorContract.getAddress())) diff --git a/packages/node/test/integration/plugins/operator/announceNodeToStream.test.ts b/packages/node/test/integration/plugins/operator/announceNodeToStream.test.ts index fa37e8dc2a..4cd906c981 100644 --- a/packages/node/test/integration/plugins/operator/announceNodeToStream.test.ts +++ b/packages/node/test/integration/plugins/operator/announceNodeToStream.test.ts @@ -10,7 +10,7 @@ const TIMEOUT = 40 * 1000 describe('announceNodeToStream', () => { it('publishes to stream', async () => { - const { operatorContract, nodeWallets } = await _operatorContractUtils.setupOperatorContract({ + const { operatorContract, nodeWallets } = await _operatorContractUtils.setupTestOperatorContract({ nodeCount: 1 }) const operatorContractAddress = toEthereumAddress(await operatorContract.getAddress()) diff --git a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts index 17c6f621ab..acbd72e464 100644 --- a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts +++ b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts @@ -1,6 +1,6 @@ import { Operator, StreamrConfig, streamrConfigABI } from '@streamr/network-contracts' import { - SetupOperatorContractOpts, + SetupTestOperatorContractOpts, _operatorContractUtils, } from '@streamr/sdk' import { Logger, toEthereumAddress, until } from '@streamr/utils' @@ -13,7 +13,7 @@ const { deploySponsorshipContract, createTestWallet, getProvider, - setupOperatorContract, + setupTestOperatorContract, sponsor, stake } = _operatorContractUtils @@ -31,7 +31,7 @@ const getEarnings = async (operatorContract: Operator): Promise => { describe('checkOperatorValueBreach', () => { let streamId: string - let deployConfig: SetupOperatorContractOpts + let deployConfig: SetupTestOperatorContractOpts beforeAll(async () => { const client = createClient(STREAM_CREATION_KEY) @@ -46,8 +46,8 @@ describe('checkOperatorValueBreach', () => { it('withdraws the other Operators earnings when they are above the limit', async () => { // eslint-disable-next-line max-len - const { operatorContract: watcherOperatorContract, nodeWallets: watcherWallets } = await setupOperatorContract({ nodeCount: 1, ...deployConfig }) - const { operatorWallet, operatorContract } = await setupOperatorContract(deployConfig) + const { operatorContract: watcherOperatorContract, nodeWallets: watcherWallets } = await setupTestOperatorContract({ nodeCount: 1, ...deployConfig }) + const { operatorWallet, operatorContract } = await setupTestOperatorContract(deployConfig) const sponsorer = await createTestWallet() await delegate(operatorWallet, await operatorContract.getAddress(), 20000n) const sponsorship1 = await deploySponsorshipContract({ earningsPerSecond: 100n, streamId, deployer: operatorWallet }) diff --git a/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts b/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts index bbf6161a08..17033f03e8 100644 --- a/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts +++ b/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts @@ -9,7 +9,7 @@ const { delegate, deploySponsorshipContract, createTestWallet, - setupOperatorContract, + setupTestOperatorContract, sponsor, stake } = _operatorContractUtils @@ -38,7 +38,7 @@ describe('maintainOperatorValue', () => { * in network-contracts), and the configured safe limit in this test is 50%, i.e. 2.5 tokens. */ it('withdraws sponsorship earnings when earnings are above the safe threshold', async () => { - const { operatorWallet, operatorContract, nodeWallets } = await setupOperatorContract({ + const { operatorWallet, operatorContract, nodeWallets } = await setupTestOperatorContract({ nodeCount: 1, operatorConfig: { operatorsCutPercent: 10 diff --git a/packages/node/test/smoke/inspect.test.ts b/packages/node/test/smoke/inspect.test.ts index 9f06dad1b8..c739d64245 100644 --- a/packages/node/test/smoke/inspect.test.ts +++ b/packages/node/test/smoke/inspect.test.ts @@ -37,7 +37,7 @@ import { OperatorPluginConfig } from './../../src/plugins/operator/OperatorPlugi */ const { - setupOperatorContract, + setupTestOperatorContract, getProvider, createTestWallet, deploySponsorshipContract, @@ -93,7 +93,7 @@ const createStream = async (): Promise => { const createOperator = async ( pluginConfig: Partial>, sponsorshipAddress: string, isFreerider: boolean ): Promise => { - const operator = await setupOperatorContract({ + const operator = await setupTestOperatorContract({ nodeCount: 1, operatorConfig: { metadata: JSON.stringify({ redundancyFactor: 1 }) diff --git a/packages/node/test/smoke/profit.test.ts b/packages/node/test/smoke/profit.test.ts index 0e9213991d..5cd45c6b75 100644 --- a/packages/node/test/smoke/profit.test.ts +++ b/packages/node/test/smoke/profit.test.ts @@ -33,7 +33,7 @@ import { createClient, createTestStream, startBroker } from '../utils' */ const { - setupOperatorContract, + setupTestOperatorContract, getProvider, createTestWallet, deploySponsorshipContract, @@ -100,7 +100,7 @@ describe('profit', () => { operatorWallet, operatorContract, nodeWallets: [operatorNodeWallet] - } = await setupOperatorContract({ + } = await setupTestOperatorContract({ nodeCount: 1, operatorConfig: { operatorsCutPercent: OPERATORS_CUT_PERCENTAGE diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 7bae1f249e..e8e7ca4c0e 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -21,7 +21,7 @@ const TEST_CHAIN_CONFIG = CHAIN_CONFIG.dev2 * @deprecated * @hidden */ -export interface SetupOperatorContractOpts { +export interface SetupTestOperatorContractOpts { nodeCount?: number chainConfig?: { contracts: { @@ -50,8 +50,8 @@ export interface SetupOperatorContractReturnType { const logger = new Logger(module) -export async function setupOperatorContract( - opts?: SetupOperatorContractOpts +export async function setupTestOperatorContract( + opts?: SetupTestOperatorContractOpts ): Promise { const operatorWallet = await createTestWallet() const operatorContract = await deployOperatorContract({ diff --git a/packages/sdk/src/exports.ts b/packages/sdk/src/exports.ts index 9d78ba94ca..9f1431d317 100644 --- a/packages/sdk/src/exports.ts +++ b/packages/sdk/src/exports.ts @@ -95,8 +95,8 @@ import { delegate, undelegate, deploySponsorshipContract, - setupOperatorContract, - SetupOperatorContractOpts, + setupTestOperatorContract, + SetupTestOperatorContractOpts, SetupOperatorContractReturnType, deployOperatorContract, DeployOperatorContractOpts, @@ -119,7 +119,7 @@ const _operatorContractUtils = { delegate, undelegate, deploySponsorshipContract, - setupOperatorContract, + setupTestOperatorContract, sponsor, stake, unstake, @@ -131,7 +131,7 @@ const _operatorContractUtils = { getOperatorContract } export { _operatorContractUtils } -export type { SetupOperatorContractOpts, SetupOperatorContractReturnType, DeployOperatorContractOpts, DeploySponsorshipContractOpts } +export type { SetupTestOperatorContractOpts, SetupOperatorContractReturnType, DeployOperatorContractOpts, DeploySponsorshipContractOpts } export type { IceServer, PeerDescriptor, PortRange } from '@streamr/dht' export type { Signer, Eip1193Provider, Overrides } from 'ethers' diff --git a/packages/sdk/test/end-to-end/Operator.test.ts b/packages/sdk/test/end-to-end/Operator.test.ts index 69ff7d15f2..035d15a71f 100644 --- a/packages/sdk/test/end-to-end/Operator.test.ts +++ b/packages/sdk/test/end-to-end/Operator.test.ts @@ -10,7 +10,7 @@ import { delegate, deploySponsorshipContract, getTestAdminWallet, - setupOperatorContract, + setupTestOperatorContract, sponsor, stake } from '../../src/contracts/operatorContractUtils' @@ -62,7 +62,7 @@ describe('Operator', () => { const concurrentTasks = await Promise.all([ createStream(), createStream(), - setupOperatorContract({ nodeCount: 1 }) + setupTestOperatorContract({ nodeCount: 1 }) ]) streamId1 = concurrentTasks[0] streamId2 = concurrentTasks[1] @@ -136,7 +136,7 @@ describe('Operator', () => { it('flag', async () => { const flagger = deployedOperator - const target = await setupOperatorContract() + const target = await setupTestOperatorContract() await sponsor(flagger.operatorWallet, await sponsorship2.getAddress(), 50000n) From f2f48ed39c7f2aff9874cd3627aab53cb4428df2 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 18:58:16 +0200 Subject: [PATCH 05/21] rm SetupTestOperatorContractOpts#chainConfig --- packages/sdk/src/contracts/operatorContractUtils.ts | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index e8e7ca4c0e..12ab8cedb2 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -23,15 +23,6 @@ const TEST_CHAIN_CONFIG = CHAIN_CONFIG.dev2 */ export interface SetupTestOperatorContractOpts { nodeCount?: number - chainConfig?: { - contracts: { - DATA: string - OperatorFactory: string - OperatorDefaultDelegationPolicy: string - OperatorDefaultExchangeRatePolicy: string - OperatorDefaultUndelegationPolicy: string - } - } operatorConfig?: { operatorsCutPercent?: number metadata?: string @@ -55,7 +46,7 @@ export async function setupTestOperatorContract( ): Promise { const operatorWallet = await createTestWallet() const operatorContract = await deployOperatorContract({ - chainConfig: opts?.chainConfig ?? TEST_CHAIN_CONFIG, + chainConfig: TEST_CHAIN_CONFIG, deployer: operatorWallet, operatorsCutPercent: opts?.operatorConfig?.operatorsCutPercent, metadata: opts?.operatorConfig?.metadata From ab18463464e4aa696cbd3f5d29714e87dcf6b48e Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 18:59:04 +0200 Subject: [PATCH 06/21] rename deployOperatorContract() -> deployTestOperatorContract() --- .../plugins/operator/MaintainTopologyService.test.ts | 4 ++-- packages/sdk/src/contracts/operatorContractUtils.ts | 6 +++--- packages/sdk/src/exports.ts | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts b/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts index 03f5542646..49a867e4f6 100644 --- a/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts +++ b/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts @@ -12,7 +12,7 @@ import { createClient, createTestStream } from '../../../utils' const { delegate, - deployOperatorContract, + deployTestOperatorContract, deploySponsorshipContract, createTestWallet, stake @@ -69,7 +69,7 @@ describe('MaintainTopologyService', () => { const [stream1, stream2] = await setUpStreams() const sponsorship1 = await deploySponsorshipContract({ deployer: operatorWallet, streamId: stream1.id }) const sponsorship2 = await deploySponsorshipContract({ deployer: operatorWallet, streamId: stream2.id }) - const operatorContract = await deployOperatorContract({ deployer: operatorWallet }) + const operatorContract = await deployTestOperatorContract({ deployer: operatorWallet }) await delegate(operatorWallet, await operatorContract.getAddress(), 20000n) await stake(operatorContract, await sponsorship1.getAddress(), 10000n) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 12ab8cedb2..65fe457e08 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -45,7 +45,7 @@ export async function setupTestOperatorContract( opts?: SetupTestOperatorContractOpts ): Promise { const operatorWallet = await createTestWallet() - const operatorContract = await deployOperatorContract({ + const operatorContract = await deployTestOperatorContract({ chainConfig: TEST_CHAIN_CONFIG, deployer: operatorWallet, operatorsCutPercent: opts?.operatorConfig?.operatorsCutPercent, @@ -65,7 +65,7 @@ export async function setupTestOperatorContract( * @deprecated * @hidden */ -export interface DeployOperatorContractOpts { +export interface DeployTestOperatorContractOpts { deployer: Wallet operatorsCutPercent?: number metadata?: string @@ -84,7 +84,7 @@ export interface DeployOperatorContractOpts { * @param opts.deployer should be the operator's Wallet * @returns Operator */ -export async function deployOperatorContract(opts: DeployOperatorContractOpts): Promise { +export async function deployTestOperatorContract(opts: DeployTestOperatorContractOpts): Promise { logger.debug('Deploying OperatorContract') const abi = OperatorFactoryArtifact const chainConfig = opts.chainConfig ?? CHAIN_CONFIG.dev2 diff --git a/packages/sdk/src/exports.ts b/packages/sdk/src/exports.ts index 9f1431d317..e7c1e7fe95 100644 --- a/packages/sdk/src/exports.ts +++ b/packages/sdk/src/exports.ts @@ -98,8 +98,8 @@ import { setupTestOperatorContract, SetupTestOperatorContractOpts, SetupOperatorContractReturnType, - deployOperatorContract, - DeployOperatorContractOpts, + deployTestOperatorContract, + DeployTestOperatorContractOpts, sponsor, stake, unstake, @@ -125,13 +125,13 @@ const _operatorContractUtils = { unstake, getProvider, createTestWallet, - deployOperatorContract, + deployTestOperatorContract, getTestTokenContract, getTestAdminWallet, getOperatorContract } export { _operatorContractUtils } -export type { SetupTestOperatorContractOpts, SetupOperatorContractReturnType, DeployOperatorContractOpts, DeploySponsorshipContractOpts } +export type { SetupTestOperatorContractOpts, SetupOperatorContractReturnType, DeployTestOperatorContractOpts, DeploySponsorshipContractOpts } export type { IceServer, PeerDescriptor, PortRange } from '@streamr/dht' export type { Signer, Eip1193Provider, Overrides } from 'ethers' From db94e848a50d3e714927e62223742fdeff2c5d33 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 19:00:30 +0200 Subject: [PATCH 07/21] rm DeployTestOperatorContractOpts#chainConfig --- .../sdk/src/contracts/operatorContractUtils.ts | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 65fe457e08..7908fa0ddb 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -46,7 +46,6 @@ export async function setupTestOperatorContract( ): Promise { const operatorWallet = await createTestWallet() const operatorContract = await deployTestOperatorContract({ - chainConfig: TEST_CHAIN_CONFIG, deployer: operatorWallet, operatorsCutPercent: opts?.operatorConfig?.operatorsCutPercent, metadata: opts?.operatorConfig?.metadata @@ -70,14 +69,6 @@ export interface DeployTestOperatorContractOpts { operatorsCutPercent?: number metadata?: string operatorTokenName?: string - chainConfig?: { - contracts: { - OperatorFactory: string - OperatorDefaultDelegationPolicy: string - OperatorDefaultExchangeRatePolicy: string - OperatorDefaultUndelegationPolicy: string - } - } } /** @@ -87,8 +78,7 @@ export interface DeployTestOperatorContractOpts { export async function deployTestOperatorContract(opts: DeployTestOperatorContractOpts): Promise { logger.debug('Deploying OperatorContract') const abi = OperatorFactoryArtifact - const chainConfig = opts.chainConfig ?? CHAIN_CONFIG.dev2 - const operatorFactory = new Contract(chainConfig.contracts.OperatorFactory, abi, opts.deployer) as unknown as OperatorFactoryContract + const operatorFactory = new Contract(TEST_CHAIN_CONFIG.contracts.OperatorFactory, abi, opts.deployer) as unknown as OperatorFactoryContract const contractAddress = await operatorFactory.operators(opts.deployer.address) if (contractAddress !== ZeroAddress) { throw new Error('Operator already has a contract') @@ -98,9 +88,9 @@ export async function deployTestOperatorContract(opts: DeployTestOperatorContrac opts.operatorTokenName ?? `OperatorToken-${Date.now()}`, opts.metadata ?? '', [ - chainConfig.contracts.OperatorDefaultDelegationPolicy, - chainConfig.contracts.OperatorDefaultExchangeRatePolicy, - chainConfig.contracts.OperatorDefaultUndelegationPolicy, + TEST_CHAIN_CONFIG.contracts.OperatorDefaultDelegationPolicy, + TEST_CHAIN_CONFIG.contracts.OperatorDefaultExchangeRatePolicy, + TEST_CHAIN_CONFIG.contracts.OperatorDefaultUndelegationPolicy, ], [ 0, 0, From 4f6327441c9ffec538ecab1cac829ecfa359d3d7 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 19:01:27 +0200 Subject: [PATCH 08/21] rename deploySponsorshipContract() -> deployTestSponsorshipContract() --- .../plugins/operator/MaintainTopologyService.test.ts | 6 +++--- .../integration/plugins/operator/OperatorPlugin.test.ts | 6 +++--- .../plugins/operator/checkOperatorValueBreach.test.ts | 6 +++--- .../plugins/operator/maintainOperatorValue.test.ts | 4 ++-- packages/node/test/smoke/inspect.test.ts | 4 ++-- packages/node/test/smoke/profit.test.ts | 4 ++-- packages/sdk/src/contracts/operatorContractUtils.ts | 4 ++-- packages/sdk/src/exports.ts | 8 ++++---- packages/sdk/test/end-to-end/Operator.test.ts | 6 +++--- 9 files changed, 24 insertions(+), 24 deletions(-) diff --git a/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts b/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts index 49a867e4f6..0102787fe3 100644 --- a/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts +++ b/packages/node/test/integration/plugins/operator/MaintainTopologyService.test.ts @@ -13,7 +13,7 @@ import { createClient, createTestStream } from '../../../utils' const { delegate, deployTestOperatorContract, - deploySponsorshipContract, + deployTestSponsorshipContract, createTestWallet, stake } = _operatorContractUtils @@ -67,8 +67,8 @@ describe('MaintainTopologyService', () => { it('happy path', async () => { const operatorWallet = await createTestWallet() const [stream1, stream2] = await setUpStreams() - const sponsorship1 = await deploySponsorshipContract({ deployer: operatorWallet, streamId: stream1.id }) - const sponsorship2 = await deploySponsorshipContract({ deployer: operatorWallet, streamId: stream2.id }) + const sponsorship1 = await deployTestSponsorshipContract({ deployer: operatorWallet, streamId: stream1.id }) + const sponsorship2 = await deployTestSponsorshipContract({ deployer: operatorWallet, streamId: stream2.id }) const operatorContract = await deployTestOperatorContract({ deployer: operatorWallet }) await delegate(operatorWallet, await operatorContract.getAddress(), 20000n) await stake(operatorContract, await sponsorship1.getAddress(), 10000n) diff --git a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts index 3a1c57b2a3..7b3de4a192 100644 --- a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts +++ b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts @@ -14,7 +14,7 @@ import { createClient, createTestStream, formConfig, startBroker } from '../../. const { delegate, - deploySponsorshipContract, + deployTestSponsorshipContract, createTestWallet, setupTestOperatorContract, sponsor, @@ -55,7 +55,7 @@ describe('OperatorPlugin', () => { const stream = await createTestStream(subscriber, module) const sponsorer = await createTestWallet() - const sponsorship1 = await deploySponsorshipContract({ streamId: stream.id, deployer: sponsorer }) + const sponsorship1 = await deployTestSponsorshipContract({ streamId: stream.id, deployer: sponsorer }) await sponsor(sponsorer, await sponsorship1.getAddress(), SPONSOR_AMOUNT) await delegate(operatorWallet, await operatorContract.getAddress(), SPONSOR_AMOUNT) await stake(operatorContract, await sponsorship1.getAddress(), SPONSOR_AMOUNT) @@ -113,7 +113,7 @@ describe('OperatorPlugin', () => { const stream = await createTestStream(client, module) const sponsorer = await createTestWallet() - const sponsorship1 = await deploySponsorshipContract({ streamId: stream.id, deployer: sponsorer }) + const sponsorship1 = await deployTestSponsorshipContract({ streamId: stream.id, deployer: sponsorer }) await sponsor(sponsorer, await sponsorship1.getAddress(), SPONSOR_AMOUNT) await delegate(operatorWallet, await operatorContract.getAddress(), SPONSOR_AMOUNT) await stake(operatorContract, await sponsorship1.getAddress(), SPONSOR_AMOUNT) diff --git a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts index acbd72e464..e0f60aeffc 100644 --- a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts +++ b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts @@ -10,7 +10,7 @@ import { createClient, createTestStream } from '../../../utils' const { delegate, - deploySponsorshipContract, + deployTestSponsorshipContract, createTestWallet, getProvider, setupTestOperatorContract, @@ -50,10 +50,10 @@ describe('checkOperatorValueBreach', () => { const { operatorWallet, operatorContract } = await setupTestOperatorContract(deployConfig) const sponsorer = await createTestWallet() await delegate(operatorWallet, await operatorContract.getAddress(), 20000n) - const sponsorship1 = await deploySponsorshipContract({ earningsPerSecond: 100n, streamId, deployer: operatorWallet }) + const sponsorship1 = await deployTestSponsorshipContract({ earningsPerSecond: 100n, streamId, deployer: operatorWallet }) await sponsor(sponsorer, await sponsorship1.getAddress(), 25000n) await stake(operatorContract, await sponsorship1.getAddress(), 10000n) - const sponsorship2 = await deploySponsorshipContract({ earningsPerSecond: 200n, streamId, deployer: operatorWallet }) + const sponsorship2 = await deployTestSponsorshipContract({ earningsPerSecond: 200n, streamId, deployer: operatorWallet }) await sponsor(sponsorer, await sponsorship2.getAddress(), 25000n) await stake(operatorContract, await sponsorship2.getAddress(), 10000n) const valueBeforeWithdraw = await operatorContract.valueWithoutEarnings() diff --git a/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts b/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts index 17033f03e8..82e4d713f2 100644 --- a/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts +++ b/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts @@ -7,7 +7,7 @@ import { createClient, createTestStream } from '../../../utils' const { delegate, - deploySponsorshipContract, + deployTestSponsorshipContract, createTestWallet, setupTestOperatorContract, sponsor, @@ -45,7 +45,7 @@ describe('maintainOperatorValue', () => { } }) const sponsorer = await createTestWallet() - const sponsorship = await deploySponsorshipContract({ earningsPerSecond: 100n, streamId, deployer: operatorWallet }) + const sponsorship = await deployTestSponsorshipContract({ earningsPerSecond: 100n, streamId, deployer: operatorWallet }) await sponsor(sponsorer, await sponsorship.getAddress(), SPONSOR_AMOUNT) await delegate(operatorWallet, await operatorContract.getAddress(), STAKE_AMOUNT) await stake(operatorContract, await sponsorship.getAddress(), STAKE_AMOUNT) diff --git a/packages/node/test/smoke/inspect.test.ts b/packages/node/test/smoke/inspect.test.ts index c739d64245..a0060fdc35 100644 --- a/packages/node/test/smoke/inspect.test.ts +++ b/packages/node/test/smoke/inspect.test.ts @@ -40,7 +40,7 @@ const { setupTestOperatorContract, getProvider, createTestWallet, - deploySponsorshipContract, + deployTestSponsorshipContract, delegate, stake, getTestTokenContract, @@ -222,7 +222,7 @@ describe('inspect', () => { logger.info('Setup sponsorship') const streamId = await createStream() const sponsorer = await createTestWallet() - const sponsorship = await deploySponsorshipContract({ earningsPerSecond: 0n, streamId, deployer: sponsorer }) + const sponsorship = await deployTestSponsorshipContract({ earningsPerSecond: 0n, streamId, deployer: sponsorer }) logger.info('Create operators') freeriderOperator = await createOperator({}, await sponsorship.getAddress(), true) const CONFIG = { diff --git a/packages/node/test/smoke/profit.test.ts b/packages/node/test/smoke/profit.test.ts index 5cd45c6b75..78a0cdfc21 100644 --- a/packages/node/test/smoke/profit.test.ts +++ b/packages/node/test/smoke/profit.test.ts @@ -36,7 +36,7 @@ const { setupTestOperatorContract, getProvider, createTestWallet, - deploySponsorshipContract, + deployTestSponsorshipContract, sponsor, delegate, undelegate, @@ -106,7 +106,7 @@ describe('profit', () => { operatorsCutPercent: OPERATORS_CUT_PERCENTAGE } })) - sponsorshipContract = await deploySponsorshipContract({ + sponsorshipContract = await deployTestSponsorshipContract({ earningsPerSecond: EARNINGS_PER_SECOND, streamId, deployer: operatorWallet // could be any wallet with gas diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 7908fa0ddb..7ea38d8360 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -108,7 +108,7 @@ export async function deployTestOperatorContract(opts: DeployTestOperatorContrac * @deprecated * @hidden */ -export interface DeploySponsorshipContractOpts { +export interface DeployTestSponsorshipContractOpts { streamId: string deployer: Wallet minOperatorCount?: number @@ -123,7 +123,7 @@ export interface DeploySponsorshipContractOpts { } } -export async function deploySponsorshipContract(opts: DeploySponsorshipContractOpts): Promise { +export async function deployTestSponsorshipContract(opts: DeployTestSponsorshipContractOpts): Promise { logger.debug('Deploying SponsorshipContract') const chainConfig = opts.chainConfig ?? CHAIN_CONFIG.dev2 const sponsorshipFactory = new Contract( diff --git a/packages/sdk/src/exports.ts b/packages/sdk/src/exports.ts index e7c1e7fe95..7beff559e6 100644 --- a/packages/sdk/src/exports.ts +++ b/packages/sdk/src/exports.ts @@ -94,7 +94,7 @@ export { import { delegate, undelegate, - deploySponsorshipContract, + deployTestSponsorshipContract, setupTestOperatorContract, SetupTestOperatorContractOpts, SetupOperatorContractReturnType, @@ -105,7 +105,7 @@ import { unstake, getProvider, createTestWallet, - DeploySponsorshipContractOpts, + DeployTestSponsorshipContractOpts, getTestTokenContract, getTestAdminWallet, getOperatorContract @@ -118,7 +118,7 @@ import { const _operatorContractUtils = { delegate, undelegate, - deploySponsorshipContract, + deployTestSponsorshipContract, setupTestOperatorContract, sponsor, stake, @@ -131,7 +131,7 @@ const _operatorContractUtils = { getOperatorContract } export { _operatorContractUtils } -export type { SetupTestOperatorContractOpts, SetupOperatorContractReturnType, DeployTestOperatorContractOpts, DeploySponsorshipContractOpts } +export type { SetupTestOperatorContractOpts, SetupOperatorContractReturnType, DeployTestOperatorContractOpts, DeployTestSponsorshipContractOpts } export type { IceServer, PeerDescriptor, PortRange } from '@streamr/dht' export type { Signer, Eip1193Provider, Overrides } from 'ethers' diff --git a/packages/sdk/test/end-to-end/Operator.test.ts b/packages/sdk/test/end-to-end/Operator.test.ts index 035d15a71f..889ea77eba 100644 --- a/packages/sdk/test/end-to-end/Operator.test.ts +++ b/packages/sdk/test/end-to-end/Operator.test.ts @@ -8,7 +8,7 @@ import { Operator } from '../../src/contracts/Operator' import { SetupOperatorContractReturnType, delegate, - deploySponsorshipContract, + deployTestSponsorshipContract, getTestAdminWallet, setupTestOperatorContract, sponsor, @@ -68,11 +68,11 @@ describe('Operator', () => { streamId2 = concurrentTasks[1] deployedOperator = concurrentTasks[2] - sponsorship1 = await deploySponsorshipContract({ + sponsorship1 = await deployTestSponsorshipContract({ streamId: streamId1, deployer: deployedOperator.operatorWallet }) - sponsorship2 = await deploySponsorshipContract({ + sponsorship2 = await deployTestSponsorshipContract({ streamId: streamId2, deployer: deployedOperator.operatorWallet }) From 953ec31adcc343fdfe408a3eeb094096f11b8e34 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 19:02:10 +0200 Subject: [PATCH 09/21] rm DeployTestSponsorshipContractOpts#chainConfig --- .../sdk/src/contracts/operatorContractUtils.ts | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 7ea38d8360..0a023fc6b5 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -113,21 +113,12 @@ export interface DeployTestSponsorshipContractOpts { deployer: Wallet minOperatorCount?: number earningsPerSecond?: bigint - chainConfig?: { - contracts: { - SponsorshipFactory: string - SponsorshipStakeWeightedAllocationPolicy: string - SponsorshipDefaultLeavePolicy: string - SponsorshipVoteKickPolicy: string - } - } } export async function deployTestSponsorshipContract(opts: DeployTestSponsorshipContractOpts): Promise { logger.debug('Deploying SponsorshipContract') - const chainConfig = opts.chainConfig ?? CHAIN_CONFIG.dev2 const sponsorshipFactory = new Contract( - chainConfig.contracts.SponsorshipFactory, + TEST_CHAIN_CONFIG.contracts.SponsorshipFactory, SponsorshipFactoryArtifact, opts.deployer ) as unknown as SponsorshipFactoryContract @@ -136,9 +127,9 @@ export async function deployTestSponsorshipContract(opts: DeployTestSponsorshipC opts.streamId, '{}', [ - chainConfig.contracts.SponsorshipStakeWeightedAllocationPolicy, - chainConfig.contracts.SponsorshipDefaultLeavePolicy, - chainConfig.contracts.SponsorshipVoteKickPolicy, + TEST_CHAIN_CONFIG.contracts.SponsorshipStakeWeightedAllocationPolicy, + TEST_CHAIN_CONFIG.contracts.SponsorshipDefaultLeavePolicy, + TEST_CHAIN_CONFIG.contracts.SponsorshipVoteKickPolicy, ], [ parseEther((opts.earningsPerSecond ?? 1).toString()).toString(), '0', From 8504f73368d7a061b2b96877acf52ba29a0282db Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 19:05:42 +0200 Subject: [PATCH 10/21] rename getProvide() -> getTestProvider() --- .../plugins/operator/checkOperatorValueBreach.test.ts | 4 ++-- packages/node/test/smoke/inspect.test.ts | 10 +++++----- packages/node/test/smoke/profit.test.ts | 4 ++-- packages/sdk/src/contracts/operatorContractUtils.ts | 6 +++--- packages/sdk/src/exports.ts | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts index e0f60aeffc..84013d25a9 100644 --- a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts +++ b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts @@ -12,7 +12,7 @@ const { delegate, deployTestSponsorshipContract, createTestWallet, - getProvider, + getTestProvider, setupTestOperatorContract, sponsor, stake @@ -58,7 +58,7 @@ describe('checkOperatorValueBreach', () => { await stake(operatorContract, await sponsorship2.getAddress(), 10000n) const valueBeforeWithdraw = await operatorContract.valueWithoutEarnings() const streamrConfigAddress = await operatorContract.streamrConfig() - const streamrConfig = new Contract(streamrConfigAddress, streamrConfigABI, getProvider()) as unknown as StreamrConfig + const streamrConfig = new Contract(streamrConfigAddress, streamrConfigABI, getTestProvider()) as unknown as StreamrConfig const allowedDifference = valueBeforeWithdraw * (await streamrConfig.maxAllowedEarningsFraction()) / ONE_ETHER const client = createClient(watcherWallets[0].privateKey) const operator = client.getOperator(toEthereumAddress(await watcherOperatorContract.getAddress())) diff --git a/packages/node/test/smoke/inspect.test.ts b/packages/node/test/smoke/inspect.test.ts index a0060fdc35..d2a376c231 100644 --- a/packages/node/test/smoke/inspect.test.ts +++ b/packages/node/test/smoke/inspect.test.ts @@ -38,7 +38,7 @@ import { OperatorPluginConfig } from './../../src/plugins/operator/OperatorPlugi const { setupTestOperatorContract, - getProvider, + getTestProvider, createTestWallet, deployTestSponsorshipContract, delegate, @@ -126,7 +126,7 @@ const createTheGraphClient = (): TheGraphClient => { const configureBlockchain = async (): Promise => { const MINING_INTERVAL = 1100 logger.info('Configure blockchain') - const provider = getProvider() as JsonRpcProvider + const provider = getTestProvider() as JsonRpcProvider await provider.send('evm_setAutomine', [true]) await createStream() // just some transaction await provider.send('evm_setAutomine', [false]) @@ -252,7 +252,7 @@ describe('inspect', () => { // select only offline nodes, but because of ETH-784 the reviewer set won't change). logger.info('Unstake pre-baked operators') for (const operator of PRE_BAKED_OPERATORS) { - const contract = getOperatorContract(operator.contractAddress).connect(new Wallet(operator.privateKey, getProvider())) as any + const contract = getOperatorContract(operator.contractAddress).connect(new Wallet(operator.privateKey, getTestProvider())) as any await (await contract.unstake(PRE_BAKED_SPONSORSHIP)).wait() } @@ -272,7 +272,7 @@ describe('inspect', () => { await operator.node.stop() } // revert to dev-chain default mining interval - await (getProvider() as JsonRpcProvider).send('evm_setIntervalMining', [DEV_CHAIN_DEFAULT_MINING_INTERVAL]) + await (getTestProvider() as JsonRpcProvider).send('evm_setIntervalMining', [DEV_CHAIN_DEFAULT_MINING_INTERVAL]) }) /* @@ -312,7 +312,7 @@ describe('inspect', () => { } // assert slashing and rewards - const token = getTestTokenContract().connect(getProvider()) + const token = getTestTokenContract().connect(getTestProvider()) expect(await getTokenBalance(freeriderOperator.contractAddress, token)).toEqual(DELEGATE_WEI - SLASHING_WEI) expect(await getTokenBalance(flags[0].flagger, token)).toEqual(DELEGATE_WEI - STAKE_WEI + FLAGGER_REWARD_WEI) for (const voter of flags[0].votes.map((vote) => vote.voter)) { diff --git a/packages/node/test/smoke/profit.test.ts b/packages/node/test/smoke/profit.test.ts index 78a0cdfc21..796c002fe5 100644 --- a/packages/node/test/smoke/profit.test.ts +++ b/packages/node/test/smoke/profit.test.ts @@ -34,7 +34,7 @@ import { createClient, createTestStream, startBroker } from '../utils' const { setupTestOperatorContract, - getProvider, + getTestProvider, createTestWallet, deployTestSponsorshipContract, sponsor, @@ -81,7 +81,7 @@ describe('profit', () => { admin: bigint operatorContract: bigint }> => { - const dataToken = getTestTokenContract().connect(getProvider()) + const dataToken = getTestTokenContract().connect(getTestProvider()) const adminWallet = getTestAdminWallet() return { operator: BigInt(formatEther(await dataToken.balanceOf(operatorWallet.address))), diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 0a023fc6b5..007042a2d0 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -144,7 +144,7 @@ export async function deployTestSponsorshipContract(opts: DeployTestSponsorshipC return newSponsorship } -export function getProvider(): Provider { +export function getTestProvider(): Provider { return new JsonRpcProvider(TEST_CHAIN_CONFIG.rpcEndpoints[0].url, undefined, { batchStallTime: 0, // Don't batch requests, send them immediately cacheTimeout: -1 // Do not employ result caching @@ -156,11 +156,11 @@ export function getTestTokenContract(): TestTokenContract { } export const getTestAdminWallet = (adminKey?: string, provider?: Provider): Wallet => { - return new Wallet(adminKey ?? TEST_CHAIN_CONFIG.adminPrivateKey).connect(provider ?? getProvider()) + return new Wallet(adminKey ?? TEST_CHAIN_CONFIG.adminPrivateKey).connect(provider ?? getTestProvider()) } export async function createTestWallet(): Promise { - const provider = getProvider() + const provider = getTestProvider() const privateKey = crypto.randomBytes(32).toString('hex') const newWallet = new Wallet(privateKey) const adminWallet = getTestAdminWallet() diff --git a/packages/sdk/src/exports.ts b/packages/sdk/src/exports.ts index 7beff559e6..ee7d0d3822 100644 --- a/packages/sdk/src/exports.ts +++ b/packages/sdk/src/exports.ts @@ -103,7 +103,7 @@ import { sponsor, stake, unstake, - getProvider, + getTestProvider, createTestWallet, DeployTestSponsorshipContractOpts, getTestTokenContract, @@ -123,7 +123,7 @@ const _operatorContractUtils = { sponsor, stake, unstake, - getProvider, + getTestProvider, createTestWallet, deployTestOperatorContract, getTestTokenContract, From be103775a31e4464dc954b414dc0594a9c2352f2 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 19:14:26 +0200 Subject: [PATCH 11/21] rm DeployTestOperatorContractOpts#operatorTokenName --- packages/sdk/src/contracts/operatorContractUtils.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 007042a2d0..e5a912cd32 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -68,7 +68,6 @@ export interface DeployTestOperatorContractOpts { deployer: Wallet operatorsCutPercent?: number metadata?: string - operatorTokenName?: string } /** @@ -85,7 +84,7 @@ export async function deployTestOperatorContract(opts: DeployTestOperatorContrac } const operatorReceipt = await (await operatorFactory.deployOperator( parseEther('1') * BigInt(opts.operatorsCutPercent ?? 0) / 100n, - opts.operatorTokenName ?? `OperatorToken-${Date.now()}`, + `OperatorToken-${Date.now()}`, opts.metadata ?? '', [ TEST_CHAIN_CONFIG.contracts.OperatorDefaultDelegationPolicy, From 0d7b645fd9ac9c0545819f92649be442ef97c041 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 19:15:36 +0200 Subject: [PATCH 12/21] rm DeployTestSponsorshipContractOpts#minOperatorCount --- packages/sdk/src/contracts/operatorContractUtils.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index e5a912cd32..35074730c8 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -110,7 +110,6 @@ export async function deployTestOperatorContract(opts: DeployTestOperatorContrac export interface DeployTestSponsorshipContractOpts { streamId: string deployer: Wallet - minOperatorCount?: number earningsPerSecond?: bigint } @@ -122,7 +121,7 @@ export async function deployTestSponsorshipContract(opts: DeployTestSponsorshipC opts.deployer ) as unknown as SponsorshipFactoryContract const sponsorshipDeployTx = await sponsorshipFactory.deploySponsorship( - (opts.minOperatorCount ?? 1).toString(), + '1', opts.streamId, '{}', [ From 1a034099c7652a381936a24995e243dc22e9c039 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 19:23:54 +0200 Subject: [PATCH 13/21] use Signer instead of Wallet --- .../sdk/src/contracts/operatorContractUtils.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 35074730c8..e36eee5afa 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -1,6 +1,6 @@ import { config as CHAIN_CONFIG } from '@streamr/config' import { Logger, retry } from '@streamr/utils' -import { Contract, EventLog, JsonRpcProvider, Provider, Wallet, ZeroAddress, parseEther } from 'ethers' +import { Contract, EventLog, JsonRpcProvider, Provider, Signer, Wallet, ZeroAddress, parseEther } from 'ethers' import { range } from 'lodash' import type { Operator as OperatorContract } from '../ethereumArtifacts/Operator' import OperatorArtifact from '../ethereumArtifacts/OperatorAbi.json' @@ -65,7 +65,7 @@ export async function setupTestOperatorContract( * @hidden */ export interface DeployTestOperatorContractOpts { - deployer: Wallet + deployer: Signer operatorsCutPercent?: number metadata?: string } @@ -78,7 +78,7 @@ export async function deployTestOperatorContract(opts: DeployTestOperatorContrac logger.debug('Deploying OperatorContract') const abi = OperatorFactoryArtifact const operatorFactory = new Contract(TEST_CHAIN_CONFIG.contracts.OperatorFactory, abi, opts.deployer) as unknown as OperatorFactoryContract - const contractAddress = await operatorFactory.operators(opts.deployer.address) + const contractAddress = await operatorFactory.operators(await opts.deployer.getAddress()) if (contractAddress !== ZeroAddress) { throw new Error('Operator already has a contract') } @@ -109,7 +109,7 @@ export async function deployTestOperatorContract(opts: DeployTestOperatorContrac */ export interface DeployTestSponsorshipContractOpts { streamId: string - deployer: Wallet + deployer: Signer earningsPerSecond?: bigint } @@ -181,15 +181,15 @@ export async function createTestWallet(): Promise { return newWallet.connect(provider) as (Wallet & SignerWithProvider) } -export const delegate = async (delegator: Wallet, operatorContractAddress: string, amountWei: bigint, token?: TestTokenContract): Promise => { +export const delegate = async (delegator: Signer, operatorContractAddress: string, amountWei: bigint, token?: TestTokenContract): Promise => { logger.debug('Delegate', { amountWei }) // onTokenTransfer: the tokens are delegated on behalf of the given data address // eslint-disable-next-line max-len // https://github.com/streamr-dev/network-contracts/blob/01ec980cfe576e25e8c9acc08a57e1e4769f3e10/packages/network-contracts/contracts/OperatorTokenomics/Operator.sol#L233 - await transferTokens(delegator, operatorContractAddress, amountWei, delegator.address, token) + await transferTokens(delegator, operatorContractAddress, amountWei, await delegator.getAddress(), token) } -export const undelegate = async (delegator: Wallet, operatorContract: OperatorContract, amount: bigint): Promise => { +export const undelegate = async (delegator: Signer, operatorContract: OperatorContract, amount: bigint): Promise => { await (await operatorContract.connect(delegator).undelegate(parseEther(amount.toString()))).wait() } @@ -203,14 +203,14 @@ export const unstake = async (operatorContract: OperatorContract, sponsorshipCon await (await operatorContract.unstake(sponsorshipContractAddress)).wait() } -export const sponsor = async (sponsorer: Wallet, sponsorshipContractAddress: string, amountWei: bigint, token?: TestTokenContract): Promise => { +export const sponsor = async (sponsorer: Signer, sponsorshipContractAddress: string, amountWei: bigint, token?: TestTokenContract): Promise => { logger.debug('Sponsor', { amountWei }) // eslint-disable-next-line max-len // https://github.com/streamr-dev/network-contracts/blob/01ec980cfe576e25e8c9acc08a57e1e4769f3e10/packages/network-contracts/contracts/OperatorTokenomics/Sponsorship.sol#L139 await transferTokens(sponsorer, sponsorshipContractAddress, amountWei, undefined, token) } -export const transferTokens = async (from: Wallet, to: string, amountWei: bigint, data?: string, token?: TestTokenContract): Promise => { +export const transferTokens = async (from: Signer, to: string, amountWei: bigint, data?: string, token?: TestTokenContract): Promise => { const tx = await ((token ?? getTestTokenContract()).connect(from).transferAndCall(to, parseEther(amountWei.toString()), data ?? '0x')) await tx.wait() } From 21b453ef100250b28b3d85e448aa8981ec6718be Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 19:35:13 +0200 Subject: [PATCH 14/21] rm parameters from getTestAdminWallet() --- packages/sdk/src/contracts/operatorContractUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index e36eee5afa..3af8590f4e 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -153,8 +153,8 @@ export function getTestTokenContract(): TestTokenContract { return new Contract(TEST_CHAIN_CONFIG.contracts.DATA, TestTokenArtifact) as unknown as TestTokenContract } -export const getTestAdminWallet = (adminKey?: string, provider?: Provider): Wallet => { - return new Wallet(adminKey ?? TEST_CHAIN_CONFIG.adminPrivateKey).connect(provider ?? getTestProvider()) +export const getTestAdminWallet = (): Wallet => { + return new Wallet(TEST_CHAIN_CONFIG.adminPrivateKey).connect(getTestProvider()) } export async function createTestWallet(): Promise { From 5caf1a4c7b238fdbad0ebbf9e8c4d6268a724b71 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 19:37:05 +0200 Subject: [PATCH 15/21] use StreamID --- .../plugins/operator/checkOperatorValueBreach.test.ts | 4 ++-- .../plugins/operator/maintainOperatorValue.test.ts | 4 ++-- packages/sdk/src/contracts/operatorContractUtils.ts | 4 ++-- packages/sdk/test/end-to-end/Operator.test.ts | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts index 84013d25a9..fce4bc73a7 100644 --- a/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts +++ b/packages/node/test/integration/plugins/operator/checkOperatorValueBreach.test.ts @@ -3,7 +3,7 @@ import { SetupTestOperatorContractOpts, _operatorContractUtils, } from '@streamr/sdk' -import { Logger, toEthereumAddress, until } from '@streamr/utils' +import { Logger, StreamID, toEthereumAddress, until } from '@streamr/utils' import { Contract } from 'ethers' import { checkOperatorValueBreach } from '../../../../src/plugins/operator/checkOperatorValueBreach' import { createClient, createTestStream } from '../../../utils' @@ -30,7 +30,7 @@ const getEarnings = async (operatorContract: Operator): Promise => { describe('checkOperatorValueBreach', () => { - let streamId: string + let streamId: StreamID let deployConfig: SetupTestOperatorContractOpts beforeAll(async () => { diff --git a/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts b/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts index 82e4d713f2..1cf165537f 100644 --- a/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts +++ b/packages/node/test/integration/plugins/operator/maintainOperatorValue.test.ts @@ -1,6 +1,6 @@ import { _operatorContractUtils } from '@streamr/sdk' import { fetchPrivateKeyWithGas } from '@streamr/test-utils' -import { Logger, toEthereumAddress, until } from '@streamr/utils' +import { Logger, StreamID, toEthereumAddress, until } from '@streamr/utils' import { multiply } from '../../../../src/helpers/multiply' import { maintainOperatorValue } from '../../../../src/plugins/operator/maintainOperatorValue' import { createClient, createTestStream } from '../../../utils' @@ -22,7 +22,7 @@ const SAFETY_FRACTION = 0.5 // 50% describe('maintainOperatorValue', () => { - let streamId: string + let streamId: StreamID beforeAll(async () => { logger.debug('Creating stream for the test') diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 3af8590f4e..622869be74 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -1,5 +1,5 @@ import { config as CHAIN_CONFIG } from '@streamr/config' -import { Logger, retry } from '@streamr/utils' +import { Logger, retry, StreamID } from '@streamr/utils' import { Contract, EventLog, JsonRpcProvider, Provider, Signer, Wallet, ZeroAddress, parseEther } from 'ethers' import { range } from 'lodash' import type { Operator as OperatorContract } from '../ethereumArtifacts/Operator' @@ -108,7 +108,7 @@ export async function deployTestOperatorContract(opts: DeployTestOperatorContrac * @hidden */ export interface DeployTestSponsorshipContractOpts { - streamId: string + streamId: StreamID deployer: Signer earningsPerSecond?: bigint } diff --git a/packages/sdk/test/end-to-end/Operator.test.ts b/packages/sdk/test/end-to-end/Operator.test.ts index 889ea77eba..8cd648a6b8 100644 --- a/packages/sdk/test/end-to-end/Operator.test.ts +++ b/packages/sdk/test/end-to-end/Operator.test.ts @@ -1,6 +1,6 @@ import { config as CHAIN_CONFIG } from '@streamr/config' import { fetchPrivateKeyWithGas } from '@streamr/test-utils' -import { Logger, TheGraphClient, toEthereumAddress, until } from '@streamr/utils' +import { Logger, StreamID, TheGraphClient, toEthereumAddress, until } from '@streamr/utils' import { Contract, Wallet } from 'ethers' import fetch from 'node-fetch' import { StreamrClient } from '../../src/StreamrClient' @@ -38,7 +38,7 @@ const createTheGraphClient = (): TheGraphClient => { }) } -async function createStream(): Promise { +async function createStream(): Promise { const client = createClient(await fetchPrivateKeyWithGas()) const streamId = (await client.createStream(`/${Date.now()}`)).id await client.destroy() @@ -52,8 +52,8 @@ const getOperator = async (wallet: Wallet | undefined, operator: SetupOperatorCo } describe('Operator', () => { - let streamId1: string - let streamId2: string + let streamId1: StreamID + let streamId2: StreamID let sponsorship1: SponsorshipContract let sponsorship2: SponsorshipContract let deployedOperator: SetupOperatorContractReturnType From 2758c014f1daf4eb86b371f24ee08fc2018072d2 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 19:53:48 +0200 Subject: [PATCH 16/21] use toEthereumAddress() --- .../plugins/operator/OperatorPlugin.test.ts | 4 ++-- packages/node/test/smoke/inspect.test.ts | 8 ++++---- packages/node/test/smoke/profit.test.ts | 12 ++++++------ .../sdk/src/contracts/operatorContractUtils.ts | 14 +++++++------- packages/sdk/test/end-to-end/Operator.test.ts | 18 +++++++++--------- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts index 7b3de4a192..e47c50003e 100644 --- a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts +++ b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts @@ -57,7 +57,7 @@ describe('OperatorPlugin', () => { const sponsorer = await createTestWallet() const sponsorship1 = await deployTestSponsorshipContract({ streamId: stream.id, deployer: sponsorer }) await sponsor(sponsorer, await sponsorship1.getAddress(), SPONSOR_AMOUNT) - await delegate(operatorWallet, await operatorContract.getAddress(), SPONSOR_AMOUNT) + await delegate(operatorWallet, toEthereumAddress(await operatorContract.getAddress()), SPONSOR_AMOUNT) await stake(operatorContract, await sponsorship1.getAddress(), SPONSOR_AMOUNT) const publisher = createClient(fastPrivateKey()) @@ -115,7 +115,7 @@ describe('OperatorPlugin', () => { const sponsorer = await createTestWallet() const sponsorship1 = await deployTestSponsorshipContract({ streamId: stream.id, deployer: sponsorer }) await sponsor(sponsorer, await sponsorship1.getAddress(), SPONSOR_AMOUNT) - await delegate(operatorWallet, await operatorContract.getAddress(), SPONSOR_AMOUNT) + await delegate(operatorWallet, toEthereumAddress(await operatorContract.getAddress()), SPONSOR_AMOUNT) await stake(operatorContract, await sponsorship1.getAddress(), SPONSOR_AMOUNT) const operatorContractAddress = await operatorContract.getAddress() diff --git a/packages/node/test/smoke/inspect.test.ts b/packages/node/test/smoke/inspect.test.ts index d2a376c231..8889b78c4c 100644 --- a/packages/node/test/smoke/inspect.test.ts +++ b/packages/node/test/smoke/inspect.test.ts @@ -2,7 +2,7 @@ import { config as CHAIN_CONFIG } from '@streamr/config' import { StreamrConfig, streamrConfigABI } from '@streamr/network-contracts' import { _operatorContractUtils } from '@streamr/sdk' import { fetchPrivateKeyWithGas } from '@streamr/test-utils' -import { Logger, StreamID, TheGraphClient, wait, until } from '@streamr/utils' +import { Logger, StreamID, TheGraphClient, wait, until, toEthereumAddress, EthereumAddress } from '@streamr/utils' import { Contract, formatEther, JsonRpcProvider, parseEther, Wallet } from 'ethers' import fetch from 'node-fetch' import { Broker, createBroker } from '../../src/broker' @@ -71,10 +71,10 @@ const SLASHING_WEI = BigInt(SLASHING_FRACTION * Number(STAKE_WEI)) // two operators and a sponsorship which have been created in dev-chain init const PRE_BAKED_OPERATORS = [{ - contractAddress: '0x8ac1cee54b9133ab7fe5418c826be60a6353d95e', + contractAddress: toEthereumAddress('0x8ac1cee54b9133ab7fe5418c826be60a6353d95e'), privateKey: '0x4059de411f15511a85ce332e7a428f36492ab4e87c7830099dadbf130f1896ae' }, { - contractAddress: '0xb63c856cf861a88f4fa8587716fdc4e69cdf9ef1', + contractAddress: toEthereumAddress('0xb63c856cf861a88f4fa8587716fdc4e69cdf9ef1'), privateKey: '0x5e98cce00cff5dea6b454889f359a4ec06b9fa6b88e9d69b86de8e1c81887da0' }] const PRE_BAKED_SPONSORSHIP = '0x5fb705aeb6f9a84499c202fc02c33d6f249dc26a' @@ -91,7 +91,7 @@ const createStream = async (): Promise => { } const createOperator = async ( - pluginConfig: Partial>, sponsorshipAddress: string, isFreerider: boolean + pluginConfig: Partial>, sponsorshipAddress: EthereumAddress, isFreerider: boolean ): Promise => { const operator = await setupTestOperatorContract({ nodeCount: 1, diff --git a/packages/node/test/smoke/profit.test.ts b/packages/node/test/smoke/profit.test.ts index 796c002fe5..a7114f146c 100644 --- a/packages/node/test/smoke/profit.test.ts +++ b/packages/node/test/smoke/profit.test.ts @@ -3,7 +3,7 @@ import type { Operator, Sponsorship } from '@streamr/network-contracts' import { StreamrConfig, streamrConfigABI } from '@streamr/network-contracts' import { _operatorContractUtils } from '@streamr/sdk' import { fetchPrivateKeyWithGas } from '@streamr/test-utils' -import { until } from '@streamr/utils' +import { until, toEthereumAddress } from '@streamr/utils' import { Contract, Wallet, formatEther, parseEther } from 'ethers' import { createClient, createTestStream, startBroker } from '../utils' @@ -123,10 +123,10 @@ describe('profit', () => { it('happy path', async () => { const beforeBalances = await getBalances() - await sponsor(sponsorWallet, await sponsorshipContract.getAddress(), SPONSOR_AMOUNT) - await delegate(operatorWallet, await operatorContract.getAddress(), OPERATOR_DELEGATED_AMOUNT) - await delegate(delegatorWallet, await operatorContract.getAddress(), EXTERNAL_DELEGATED_AMOUNT) - await stake(operatorContract, await sponsorshipContract.getAddress(), OPERATOR_DELEGATED_AMOUNT + EXTERNAL_DELEGATED_AMOUNT) + await sponsor(sponsorWallet, toEthereumAddress(await sponsorshipContract.getAddress()), SPONSOR_AMOUNT) + await delegate(operatorWallet, toEthereumAddress(await operatorContract.getAddress()), OPERATOR_DELEGATED_AMOUNT) + await delegate(delegatorWallet, toEthereumAddress(await operatorContract.getAddress()), EXTERNAL_DELEGATED_AMOUNT) + await stake(operatorContract, toEthereumAddress(await sponsorshipContract.getAddress()), OPERATOR_DELEGATED_AMOUNT + EXTERNAL_DELEGATED_AMOUNT) const broker = await startBroker({ privateKey: operatorNodeWallet.privateKey, @@ -152,7 +152,7 @@ describe('profit', () => { }) await broker.stop() - await unstake(operatorContract, await sponsorshipContract.getAddress()) + await unstake(operatorContract, toEthereumAddress(await sponsorshipContract.getAddress())) await undelegate( delegatorWallet, operatorContract, diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 622869be74..22b56c218e 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -1,5 +1,5 @@ import { config as CHAIN_CONFIG } from '@streamr/config' -import { Logger, retry, StreamID } from '@streamr/utils' +import { EthereumAddress, Logger, retry, StreamID } from '@streamr/utils' import { Contract, EventLog, JsonRpcProvider, Provider, Signer, Wallet, ZeroAddress, parseEther } from 'ethers' import { range } from 'lodash' import type { Operator as OperatorContract } from '../ethereumArtifacts/Operator' @@ -181,7 +181,7 @@ export async function createTestWallet(): Promise { return newWallet.connect(provider) as (Wallet & SignerWithProvider) } -export const delegate = async (delegator: Signer, operatorContractAddress: string, amountWei: bigint, token?: TestTokenContract): Promise => { +export const delegate = async (delegator: Signer, operatorContractAddress: EthereumAddress, amountWei: bigint, token?: TestTokenContract): Promise => { logger.debug('Delegate', { amountWei }) // onTokenTransfer: the tokens are delegated on behalf of the given data address // eslint-disable-next-line max-len @@ -193,28 +193,28 @@ export const undelegate = async (delegator: Signer, operatorContract: OperatorCo await (await operatorContract.connect(delegator).undelegate(parseEther(amount.toString()))).wait() } -export const stake = async (operatorContract: OperatorContract, sponsorshipContractAddress: string, amount: bigint): Promise => { +export const stake = async (operatorContract: OperatorContract, sponsorshipContractAddress: EthereumAddress, amount: bigint): Promise => { logger.debug('Stake', { amount }) await (await operatorContract.stake(sponsorshipContractAddress, parseEther(amount.toString()))).wait() } -export const unstake = async (operatorContract: OperatorContract, sponsorshipContractAddress: string): Promise => { +export const unstake = async (operatorContract: OperatorContract, sponsorshipContractAddress: EthereumAddress): Promise => { logger.debug('Unstake') await (await operatorContract.unstake(sponsorshipContractAddress)).wait() } -export const sponsor = async (sponsorer: Signer, sponsorshipContractAddress: string, amountWei: bigint, token?: TestTokenContract): Promise => { +export const sponsor = async (sponsorer: Signer, sponsorshipContractAddress: EthereumAddress, amountWei: bigint, token?: TestTokenContract): Promise => { logger.debug('Sponsor', { amountWei }) // eslint-disable-next-line max-len // https://github.com/streamr-dev/network-contracts/blob/01ec980cfe576e25e8c9acc08a57e1e4769f3e10/packages/network-contracts/contracts/OperatorTokenomics/Sponsorship.sol#L139 await transferTokens(sponsorer, sponsorshipContractAddress, amountWei, undefined, token) } -export const transferTokens = async (from: Signer, to: string, amountWei: bigint, data?: string, token?: TestTokenContract): Promise => { +export const transferTokens = async (from: Signer, to: EthereumAddress, amountWei: bigint, data?: string, token?: TestTokenContract): Promise => { const tx = await ((token ?? getTestTokenContract()).connect(from).transferAndCall(to, parseEther(amountWei.toString()), data ?? '0x')) await tx.wait() } -export const getOperatorContract = (operatorAddress: string): OperatorContract => { +export const getOperatorContract = (operatorAddress: EthereumAddress): OperatorContract => { return new Contract(operatorAddress, OperatorArtifact) as unknown as OperatorContract } diff --git a/packages/sdk/test/end-to-end/Operator.test.ts b/packages/sdk/test/end-to-end/Operator.test.ts index 8cd648a6b8..3d45ed381f 100644 --- a/packages/sdk/test/end-to-end/Operator.test.ts +++ b/packages/sdk/test/end-to-end/Operator.test.ts @@ -80,8 +80,8 @@ describe('Operator', () => { }, 90 * 1000) it('getStakedOperators', async () => { - await delegate(deployedOperator.operatorWallet, await deployedOperator.operatorContract.getAddress(), 20000n) - await stake(deployedOperator.operatorContract, await sponsorship1.getAddress(), 10000n) + await delegate(deployedOperator.operatorWallet, toEthereumAddress(await deployedOperator.operatorContract.getAddress()), 20000n) + await stake(deployedOperator.operatorContract, toEthereumAddress(await sponsorship1.getAddress()), 10000n) const dummyOperator = await getOperator(deployedOperator.nodeWallets[0], deployedOperator) const randomOperatorAddress = sample(await dummyOperator.getStakedOperators()) expect(randomOperatorAddress).toBeDefined() @@ -106,8 +106,8 @@ describe('Operator', () => { it('getSponsorships, getOperatorsInSponsorship', async () => { const operatorContractAddress = toEthereumAddress(await deployedOperator.operatorContract.getAddress()) await delegate(deployedOperator.operatorWallet, operatorContractAddress, 20000n) - await stake(deployedOperator.operatorContract, await sponsorship1.getAddress(), 10000n) - await stake(deployedOperator.operatorContract, await sponsorship2.getAddress(), 10000n) + await stake(deployedOperator.operatorContract, toEthereumAddress(await sponsorship1.getAddress()), 10000n) + await stake(deployedOperator.operatorContract, toEthereumAddress(await sponsorship2.getAddress()), 10000n) const operator = await getOperator(undefined, deployedOperator) @@ -138,12 +138,12 @@ describe('Operator', () => { const flagger = deployedOperator const target = await setupTestOperatorContract() - await sponsor(flagger.operatorWallet, await sponsorship2.getAddress(), 50000n) + await sponsor(flagger.operatorWallet, toEthereumAddress(await sponsorship2.getAddress()), 50000n) - await delegate(flagger.operatorWallet, await flagger.operatorContract.getAddress(), 20000n) - await delegate(target.operatorWallet, await target.operatorContract.getAddress(), 30000n) - await stake(flagger.operatorContract, await sponsorship2.getAddress(), 15000n) - await stake(target.operatorContract, await sponsorship2.getAddress(), 25000n) + await delegate(flagger.operatorWallet, toEthereumAddress(await flagger.operatorContract.getAddress()), 20000n) + await delegate(target.operatorWallet, toEthereumAddress(await target.operatorContract.getAddress()), 30000n) + await stake(flagger.operatorContract, toEthereumAddress(await sponsorship2.getAddress()), 15000n) + await stake(target.operatorContract, toEthereumAddress( await sponsorship2.getAddress()), 25000n) const contractFacade = await getOperator(deployedOperator.nodeWallets[0], flagger) await contractFacade.flag( From 796b58be7a727dbfd1f01e1d9d3363f18267d4a3 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 21:28:51 +0200 Subject: [PATCH 17/21] do not export transferTokens() --- packages/sdk/src/contracts/operatorContractUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 22b56c218e..7dce6948fc 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -210,7 +210,7 @@ export const sponsor = async (sponsorer: Signer, sponsorshipContractAddress: Eth await transferTokens(sponsorer, sponsorshipContractAddress, amountWei, undefined, token) } -export const transferTokens = async (from: Signer, to: EthereumAddress, amountWei: bigint, data?: string, token?: TestTokenContract): Promise => { +const transferTokens = async (from: Signer, to: EthereumAddress, amountWei: bigint, data?: string, token?: TestTokenContract): Promise => { const tx = await ((token ?? getTestTokenContract()).connect(from).transferAndCall(to, parseEther(amountWei.toString()), data ?? '0x')) await tx.wait() } From f7dd0a0677ce0daba7501cadaad1e6da09e53c34 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 22:08:57 +0200 Subject: [PATCH 18/21] rename SetupOperatorContractReturnType --- packages/sdk/src/contracts/operatorContractUtils.ts | 4 ++-- packages/sdk/src/exports.ts | 4 ++-- packages/sdk/test/end-to-end/Operator.test.ts | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 7dce6948fc..402091e675 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -33,7 +33,7 @@ export interface SetupTestOperatorContractOpts { * @deprecated * @hidden */ -export interface SetupOperatorContractReturnType { +export interface SetupTestOperatorContractReturnType { operatorWallet: Wallet operatorContract: OperatorContract nodeWallets: (Wallet & SignerWithProvider)[] @@ -43,7 +43,7 @@ const logger = new Logger(module) export async function setupTestOperatorContract( opts?: SetupTestOperatorContractOpts -): Promise { +): Promise { const operatorWallet = await createTestWallet() const operatorContract = await deployTestOperatorContract({ deployer: operatorWallet, diff --git a/packages/sdk/src/exports.ts b/packages/sdk/src/exports.ts index ee7d0d3822..4efe57731f 100644 --- a/packages/sdk/src/exports.ts +++ b/packages/sdk/src/exports.ts @@ -97,7 +97,7 @@ import { deployTestSponsorshipContract, setupTestOperatorContract, SetupTestOperatorContractOpts, - SetupOperatorContractReturnType, + SetupTestOperatorContractReturnType, deployTestOperatorContract, DeployTestOperatorContractOpts, sponsor, @@ -131,7 +131,7 @@ const _operatorContractUtils = { getOperatorContract } export { _operatorContractUtils } -export type { SetupTestOperatorContractOpts, SetupOperatorContractReturnType, DeployTestOperatorContractOpts, DeployTestSponsorshipContractOpts } +export type { SetupTestOperatorContractOpts, SetupTestOperatorContractReturnType, DeployTestOperatorContractOpts, DeployTestSponsorshipContractOpts } export type { IceServer, PeerDescriptor, PortRange } from '@streamr/dht' export type { Signer, Eip1193Provider, Overrides } from 'ethers' diff --git a/packages/sdk/test/end-to-end/Operator.test.ts b/packages/sdk/test/end-to-end/Operator.test.ts index 3d45ed381f..d25c5e68df 100644 --- a/packages/sdk/test/end-to-end/Operator.test.ts +++ b/packages/sdk/test/end-to-end/Operator.test.ts @@ -6,7 +6,7 @@ import fetch from 'node-fetch' import { StreamrClient } from '../../src/StreamrClient' import { Operator } from '../../src/contracts/Operator' import { - SetupOperatorContractReturnType, + SetupTestOperatorContractReturnType, delegate, deployTestSponsorshipContract, getTestAdminWallet, @@ -45,7 +45,7 @@ async function createStream(): Promise { return streamId } -const getOperator = async (wallet: Wallet | undefined, operator: SetupOperatorContractReturnType): Promise => { +const getOperator = async (wallet: Wallet | undefined, operator: SetupTestOperatorContractReturnType): Promise => { const client = createClient(wallet?.privateKey) const contractAddress = toEthereumAddress(await operator.operatorContract.getAddress()) return client.getOperator(contractAddress) @@ -56,7 +56,7 @@ describe('Operator', () => { let streamId2: StreamID let sponsorship1: SponsorshipContract let sponsorship2: SponsorshipContract - let deployedOperator: SetupOperatorContractReturnType + let deployedOperator: SetupTestOperatorContractReturnType beforeAll(async () => { const concurrentTasks = await Promise.all([ From 03b2e9c4a2a5d9c362c58aa01804a94f8d97fbd8 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 22:35:12 +0200 Subject: [PATCH 19/21] eslint --- packages/node/test/smoke/profit.test.ts | 14 +++++++++----- .../sdk/src/contracts/operatorContractUtils.ts | 17 ++++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/node/test/smoke/profit.test.ts b/packages/node/test/smoke/profit.test.ts index a7114f146c..cbad42634f 100644 --- a/packages/node/test/smoke/profit.test.ts +++ b/packages/node/test/smoke/profit.test.ts @@ -9,11 +9,11 @@ import { createClient, createTestStream, startBroker } from '../utils' /* * The test needs these dependencies: - * - dev-chain in Docker: + * - dev-chain in Docker: * streamr-docker-dev start dev-chain-fast deploy-network-subgraphs-fastchain * - DHT entry point: * /bin/run-entry-point.sh - * + * * Given: * - one sponsorship * - one operator who mines the sponsorship by running one node @@ -58,7 +58,7 @@ const TOTAL_DELEGATED = OPERATOR_DELEGATED_AMOUNT + EXTERNAL_DELEGATED_AMOUNT const OPERATORS_CUT = BigInt(Number(TOTAL_PROFIT) * (OPERATORS_CUT_PERCENTAGE / 100)) const OPERATOR_PROFIT_WHEN_NO_WITHDRAWALS = (TOTAL_PROFIT - OPERATORS_CUT) * OPERATOR_DELEGATED_AMOUNT / TOTAL_DELEGATED + OPERATORS_CUT const DELEGATOR_PROFIT_WHEN_NO_WITHDRAWALS = (TOTAL_PROFIT - OPERATORS_CUT) * EXTERNAL_DELEGATED_AMOUNT / TOTAL_DELEGATED -// If the operator doesn't make any withdrawals during the sponsorship period, the profit is split between +// If the operator doesn't make any withdrawals during the sponsorship period, the profit is split between // the operator and the delegator based on their respective delegated amounts. However, if there are withdrawals, // the operator gets a larger share of the profit. This happens because the operator's delegated amount // grows by both their profit share and their cut of the total profit, while the external delegator's amount @@ -126,8 +126,12 @@ describe('profit', () => { await sponsor(sponsorWallet, toEthereumAddress(await sponsorshipContract.getAddress()), SPONSOR_AMOUNT) await delegate(operatorWallet, toEthereumAddress(await operatorContract.getAddress()), OPERATOR_DELEGATED_AMOUNT) await delegate(delegatorWallet, toEthereumAddress(await operatorContract.getAddress()), EXTERNAL_DELEGATED_AMOUNT) - await stake(operatorContract, toEthereumAddress(await sponsorshipContract.getAddress()), OPERATOR_DELEGATED_AMOUNT + EXTERNAL_DELEGATED_AMOUNT) - + await stake( + operatorContract, + toEthereumAddress(await sponsorshipContract.getAddress()), + OPERATOR_DELEGATED_AMOUNT + EXTERNAL_DELEGATED_AMOUNT + ) + const broker = await startBroker({ privateKey: operatorNodeWallet.privateKey, extraPlugins: { diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 402091e675..21b9202ea7 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -181,7 +181,12 @@ export async function createTestWallet(): Promise { return newWallet.connect(provider) as (Wallet & SignerWithProvider) } -export const delegate = async (delegator: Signer, operatorContractAddress: EthereumAddress, amountWei: bigint, token?: TestTokenContract): Promise => { +export const delegate = async ( + delegator: Signer, + operatorContractAddress: EthereumAddress, + amountWei: bigint, + token?: TestTokenContract +): Promise => { logger.debug('Delegate', { amountWei }) // onTokenTransfer: the tokens are delegated on behalf of the given data address // eslint-disable-next-line max-len @@ -189,7 +194,8 @@ export const delegate = async (delegator: Signer, operatorContractAddress: Ether await transferTokens(delegator, operatorContractAddress, amountWei, await delegator.getAddress(), token) } -export const undelegate = async (delegator: Signer, operatorContract: OperatorContract, amount: bigint): Promise => { +export const undelegate = async (delegator: Signer, operatorContract: OperatorContract, amount: bigint +): Promise => { await (await operatorContract.connect(delegator).undelegate(parseEther(amount.toString()))).wait() } @@ -203,7 +209,12 @@ export const unstake = async (operatorContract: OperatorContract, sponsorshipCon await (await operatorContract.unstake(sponsorshipContractAddress)).wait() } -export const sponsor = async (sponsorer: Signer, sponsorshipContractAddress: EthereumAddress, amountWei: bigint, token?: TestTokenContract): Promise => { +export const sponsor = async ( + sponsorer: Signer, + sponsorshipContractAddress: EthereumAddress, + amountWei: bigint, + token?: TestTokenContract +): Promise => { logger.debug('Sponsor', { amountWei }) // eslint-disable-next-line max-len // https://github.com/streamr-dev/network-contracts/blob/01ec980cfe576e25e8c9acc08a57e1e4769f3e10/packages/network-contracts/contracts/OperatorTokenomics/Sponsorship.sol#L139 From 869fbe9cb55971eadc9a8dffd5f83eb58c44a1eb Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Thu, 28 Nov 2024 22:59:32 +0200 Subject: [PATCH 20/21] style --- packages/sdk/src/contracts/operatorContractUtils.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/sdk/src/contracts/operatorContractUtils.ts b/packages/sdk/src/contracts/operatorContractUtils.ts index 21b9202ea7..2563104ef5 100644 --- a/packages/sdk/src/contracts/operatorContractUtils.ts +++ b/packages/sdk/src/contracts/operatorContractUtils.ts @@ -194,8 +194,7 @@ export const delegate = async ( await transferTokens(delegator, operatorContractAddress, amountWei, await delegator.getAddress(), token) } -export const undelegate = async (delegator: Signer, operatorContract: OperatorContract, amount: bigint -): Promise => { +export const undelegate = async (delegator: Signer, operatorContract: OperatorContract, amount: bigint): Promise => { await (await operatorContract.connect(delegator).undelegate(parseEther(amount.toString()))).wait() } From d4ff1470c991801e7a21993964d507f2ddd9cc29 Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Tue, 17 Dec 2024 15:33:47 +0200 Subject: [PATCH 21/21] fix merge 2e2e164b2tr --- .../integration/plugins/operator/OperatorPlugin.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts index 375652bc0f..1ad64d87bc 100644 --- a/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts +++ b/packages/node/test/integration/plugins/operator/OperatorPlugin.test.ts @@ -114,10 +114,10 @@ describe('OperatorPlugin', () => { const stream = await createTestStream(client, module) const sponsorer = await createTestWallet() - const sponsorship1 = await deployTestSponsorshipContract({ streamId: stream.id, deployer: sponsorer }) - await sponsor(sponsorer, await sponsorship1.getAddress(), SPONSOR_AMOUNT) + const sponsorship = await deployTestSponsorshipContract({ streamId: stream.id, deployer: sponsorer }) + await sponsor(sponsorer, await sponsorship.getAddress(), SPONSOR_AMOUNT) await delegate(operatorWallet, toEthereumAddress(await operatorContract.getAddress()), SPONSOR_AMOUNT) - await stake(operatorContract, await sponsorship1.getAddress(), SPONSOR_AMOUNT) + await stake(operatorContract, await sponsorship.getAddress(), SPONSOR_AMOUNT) const operatorContractAddress = await operatorContract.getAddress() broker = await startBroker({