Skip to content

Commit

Permalink
move generateWalletWithGasAndTokens() to test-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
teogeb committed Dec 20, 2024
1 parent 8ee7a8a commit 5d68316
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 63 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import {
Stream, StreamrClient, _operatorContractUtils
} from '@streamr/sdk'
import { fastPrivateKey, fetchPrivateKeyWithGas } from '@streamr/test-utils'
import { fastPrivateKey, fetchPrivateKeyWithGas, generateWalletWithGasAndTokens } from '@streamr/test-utils'
import { StreamPartID, toEthereumAddress, until } from '@streamr/utils'
import { parseEther } from 'ethers'
import { MaintainTopologyHelper } from '../../../../src/plugins/operator/MaintainTopologyHelper'
import { MaintainTopologyService } from '../../../../src/plugins/operator/MaintainTopologyService'
import { OperatorFleetState } from '../../../../src/plugins/operator/OperatorFleetState'
import { StreamPartAssignments } from '../../../../src/plugins/operator/StreamPartAssignments'
import { formCoordinationStreamId } from '../../../../src/plugins/operator/formCoordinationStreamId'
import { createClient, createTestStream } from '../../../utils'
import { parseEther } from 'ethers'

const {
delegate,
deployOperatorContract,
deploySponsorshipContract,
generateWalletWithGasAndTokens,
stake
} = _operatorContractUtils

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
StreamPermission,
_operatorContractUtils
} from '@streamr/sdk'
import { fastPrivateKey, fetchPrivateKeyWithGas } from '@streamr/test-utils'
import { fastPrivateKey, fetchPrivateKeyWithGas, generateWalletWithGasAndTokens } from '@streamr/test-utils'
import { EthereumAddress, collect, toEthereumAddress, toStreamPartID, until } from '@streamr/utils'
import { parseEther, Wallet } from 'ethers'
import { Wallet, parseEther } from 'ethers'
import { cloneDeep, set } from 'lodash'
import { Broker, createBroker } from '../../../../src/broker'
import { formCoordinationStreamId } from '../../../../src/plugins/operator/formCoordinationStreamId'
Expand All @@ -15,7 +15,6 @@ import { createClient, createTestStream, formConfig, startBroker } from '../../.
const {
delegate,
deploySponsorshipContract,
generateWalletWithGasAndTokens,
setupOperatorContract,
sponsor,
stake
Expand All @@ -32,7 +31,8 @@ describe('OperatorPlugin', () => {

beforeAll(async () => {
const deployment = (await setupOperatorContract({
nodeCount: 1
nodeCount: 1,
generateWalletWithGasAndTokens
}))
brokerWallet = deployment.nodeWallets[0]
operatorWallet = deployment.operatorWallet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Operator, _operatorContractUtils } from '@streamr/sdk'
import { toEthereumAddress, until } from '@streamr/utils'
import { announceNodeToContract } from '../../../../src/plugins/operator/announceNodeToContract'
import { createClient } from '../../../utils'
import { generateWalletWithGasAndTokens } from '@streamr/test-utils'

const TIMEOUT = 30 * 1000

Expand All @@ -11,7 +12,8 @@ describe('announceNodeToContract', () => {

beforeEach(async () => {
const { operatorContract, nodeWallets } = await _operatorContractUtils.setupOperatorContract({
nodeCount: 1
nodeCount: 1,
generateWalletWithGasAndTokens
})
operator = createClient(nodeWallets[0].privateKey).getOperator(toEthereumAddress(await operatorContract.getAddress()))
}, TIMEOUT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { _operatorContractUtils } from '@streamr/sdk'
import { fastPrivateKey } from '@streamr/test-utils'
import { fastPrivateKey, generateWalletWithGasAndTokens } from '@streamr/test-utils'
import { collect, toEthereumAddress } from '@streamr/utils'
import { announceNodeToStream } from '../../../../src/plugins/operator/announceNodeToStream'
import { formCoordinationStreamId } from '../../../../src/plugins/operator/formCoordinationStreamId'
Expand All @@ -11,7 +11,8 @@ describe('announceNodeToStream', () => {

it('publishes to stream', async () => {
const { operatorContract, nodeWallets } = await _operatorContractUtils.setupOperatorContract({
nodeCount: 1
nodeCount: 1,
generateWalletWithGasAndTokens
})
const operatorContractAddress = toEthereumAddress(await operatorContract.getAddress())
const nodeWallet = nodeWallets[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SetupOperatorContractOpts,
_operatorContractUtils,
} from '@streamr/sdk'
import { generateWalletWithGasAndTokens } from '@streamr/test-utils'
import { Logger, toEthereumAddress, until } from '@streamr/utils'
import { Contract, parseEther } from 'ethers'
import { checkOperatorValueBreach } from '../../../../src/plugins/operator/checkOperatorValueBreach'
Expand All @@ -11,7 +12,6 @@ import { createClient, createTestStream } from '../../../utils'
const {
delegate,
deploySponsorshipContract,
generateWalletWithGasAndTokens,
getProvider,
setupOperatorContract,
sponsor,
Expand Down Expand Up @@ -40,13 +40,14 @@ describe('checkOperatorValueBreach', () => {
deployConfig = {
operatorConfig: {
operatorsCutPercentage: 10
}
},
generateWalletWithGasAndTokens
}
}, 60 * 1000)

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 { operatorContract: watcherOperatorContract, nodeWallets: watcherWallets } = await setupOperatorContract({ nodeCount: 1, ...deployConfig, generateWalletWithGasAndTokens })
const { operatorWallet, operatorContract } = await setupOperatorContract(deployConfig)
const sponsorer = await generateWalletWithGasAndTokens()
await delegate(operatorWallet, await operatorContract.getAddress(), parseEther('20000'))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { _operatorContractUtils } from '@streamr/sdk'
import { fetchPrivateKeyWithGas } from '@streamr/test-utils'
import { fetchPrivateKeyWithGas, generateWalletWithGasAndTokens } from '@streamr/test-utils'
import { Logger, multiplyWeiAmount, toEthereumAddress, until } from '@streamr/utils'
import { parseEther } from 'ethers'
import { maintainOperatorValue } from '../../../../src/plugins/operator/maintainOperatorValue'
Expand All @@ -8,7 +8,6 @@ import { createClient, createTestStream } from '../../../utils'
const {
delegate,
deploySponsorshipContract,
generateWalletWithGasAndTokens,
setupOperatorContract,
sponsor,
stake
Expand Down Expand Up @@ -41,7 +40,8 @@ describe('maintainOperatorValue', () => {
nodeCount: 1,
operatorConfig: {
operatorsCutPercentage: 10
}
},
generateWalletWithGasAndTokens
})
const sponsorer = await generateWalletWithGasAndTokens()
const sponsorship = await deploySponsorshipContract({ earningsPerSecond: parseEther('100'), streamId, deployer: operatorWallet })
Expand Down
8 changes: 4 additions & 4 deletions packages/node/test/smoke/inspect.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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, multiplyWeiAmount } from '@streamr/utils'
import { fetchPrivateKeyWithGas, generateWalletWithGasAndTokens } from '@streamr/test-utils'
import { Logger, multiplyWeiAmount, StreamID, TheGraphClient, until, wait } from '@streamr/utils'
import { Contract, JsonRpcProvider, parseEther, Wallet } from 'ethers'
import { Broker, createBroker } from '../../src/broker'
import { createClient, createTestStream, formConfig } from '../utils'
Expand Down Expand Up @@ -38,7 +38,6 @@ import { OperatorPluginConfig } from './../../src/plugins/operator/OperatorPlugi
const {
setupOperatorContract,
getProvider,
generateWalletWithGasAndTokens,
deploySponsorshipContract,
delegate,
stake,
Expand Down Expand Up @@ -95,7 +94,8 @@ const createOperator = async (
nodeCount: 1,
operatorConfig: {
metadata: JSON.stringify({ redundancyFactor: 1 })
}
},
generateWalletWithGasAndTokens
})
await delegate(operator.operatorWallet, await operator.operatorContract.getAddress(), DELEGATE_AMOUNT)
await stake(operator.operatorContract, sponsorshipAddress, STAKE_AMOUNT)
Expand Down
6 changes: 3 additions & 3 deletions packages/node/test/smoke/profit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { config as CHAIN_CONFIG } from '@streamr/config'
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 { fetchPrivateKeyWithGas, generateWalletWithGasAndTokens } from '@streamr/test-utils'
import { multiplyWeiAmount, until, WeiAmount } from '@streamr/utils'
import { Contract, Wallet, parseEther } from 'ethers'
import { createClient, createTestStream, startBroker } from '../utils'
Expand Down Expand Up @@ -35,7 +35,6 @@ import { createClient, createTestStream, startBroker } from '../utils'
const {
setupOperatorContract,
getProvider,
generateWalletWithGasAndTokens,
deploySponsorshipContract,
sponsor,
delegate,
Expand Down Expand Up @@ -104,7 +103,8 @@ describe('profit', () => {
nodeCount: 1,
operatorConfig: {
operatorsCutPercentage: OPERATORS_CUT_PERCENTAGE
}
},
generateWalletWithGasAndTokens
}))
sponsorshipContract = await deploySponsorshipContract({
earningsPerSecond: EARNINGS_PER_SECOND,
Expand Down
42 changes: 9 additions & 33 deletions packages/sdk/src/contracts/operatorContractUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { config as CHAIN_CONFIG } from '@streamr/config'
import { Logger, multiplyWeiAmount, retry, WeiAmount } from '@streamr/utils'
import { Contract, EventLog, JsonRpcProvider, Provider, Wallet, ZeroAddress, parseEther } from 'ethers'
import { Logger, multiplyWeiAmount, WeiAmount } from '@streamr/utils'
import { Contract, EventLog, JsonRpcProvider, parseEther, Provider, Wallet, ZeroAddress } from 'ethers'
import { range } from 'lodash'
import { SignerWithProvider } from '../Authentication'
import type { DATAv2 as DATATokenContract } from '../ethereumArtifacts/DATAv2'
import DATATokenArtifact from '../ethereumArtifacts/DATAv2Abi.json'
import type { Operator as OperatorContract } from '../ethereumArtifacts/Operator'
import OperatorArtifact from '../ethereumArtifacts/OperatorAbi.json'
import type { OperatorFactory as OperatorFactoryContract } from '../ethereumArtifacts/OperatorFactory'
Expand All @@ -10,10 +13,6 @@ import type { Sponsorship as SponsorshipContract } from '../ethereumArtifacts/Sp
import SponsorshipArtifact from '../ethereumArtifacts/SponsorshipAbi.json'
import type { SponsorshipFactory as SponsorshipFactoryContract } from '../ethereumArtifacts/SponsorshipFactory'
import SponsorshipFactoryArtifact from '../ethereumArtifacts/SponsorshipFactoryAbi.json'
import type { DATAv2 as DATATokenContract } from '../ethereumArtifacts/DATAv2'
import DATATokenArtifact from '../ethereumArtifacts/DATAv2Abi.json'
import { SignerWithProvider } from '../Authentication'
import crypto from 'crypto'

const TEST_CHAIN_CONFIG = CHAIN_CONFIG.dev2
const FRACTION_MAX = parseEther('1')
Expand All @@ -28,6 +27,7 @@ export interface SetupOperatorContractOpts {
operatorsCutPercentage?: number
metadata?: string
}
generateWalletWithGasAndTokens: () => Promise<Wallet & SignerWithProvider>
}

/**
Expand All @@ -43,9 +43,9 @@ export interface SetupOperatorContractReturnType {
const logger = new Logger(module)

export async function setupOperatorContract(
opts?: SetupOperatorContractOpts
opts: SetupOperatorContractOpts
): Promise<SetupOperatorContractReturnType> {
const operatorWallet = await generateWalletWithGasAndTokens()
const operatorWallet = await opts.generateWalletWithGasAndTokens()
const operatorContract = await deployOperatorContract({
deployer: operatorWallet,
operatorsCutPercentage: opts?.operatorConfig?.operatorsCutPercentage,
Expand All @@ -54,7 +54,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())
nodeWallets.push(await opts.generateWalletWithGasAndTokens())
}
await (await operatorContract.setNodeAddresses(nodeWallets.map((w) => w.address))).wait()
}
Expand Down Expand Up @@ -161,30 +161,6 @@ export const getTestAdminWallet = (adminKey?: string, provider?: Provider): Wall
return new Wallet(adminKey ?? TEST_CHAIN_CONFIG.adminPrivateKey).connect(provider ?? getProvider())
}

export async function generateWalletWithGasAndTokens(): Promise<Wallet & SignerWithProvider> {
const provider = getProvider()
const privateKey = crypto.randomBytes(32).toString('hex')
const newWallet = new Wallet(privateKey)
const adminWallet = getTestAdminWallet()
const token = getTestTokenContract().connect(adminWallet)
await retry(
async () => {
await (await token.mint(newWallet.address, parseEther('1000000'))).wait()
await (await adminWallet.sendTransaction({
to: newWallet.address,
value: parseEther('1')
})).wait()
},
(message: string, err: any) => {
logger.debug(message, { err })
},
'Token minting',
10,
100
)
return newWallet.connect(provider) as (Wallet & SignerWithProvider)
}

export const delegate = async (delegator: Wallet, operatorContractAddress: string, amount: WeiAmount, token?: DATATokenContract): Promise<void> => {
logger.debug('Delegate', { amount: amount.toString() })
// onTokenTransfer: the tokens are delegated on behalf of the given data address
Expand Down
2 changes: 0 additions & 2 deletions packages/sdk/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ import {
stake,
unstake,
getProvider,
generateWalletWithGasAndTokens,
DeploySponsorshipContractOpts,
getTestTokenContract,
getTestAdminWallet,
Expand All @@ -122,7 +121,6 @@ const _operatorContractUtils = {
stake,
unstake,
getProvider,
generateWalletWithGasAndTokens,
deployOperatorContract,
getTestTokenContract,
getTestAdminWallet,
Expand Down
8 changes: 5 additions & 3 deletions packages/sdk/test/end-to-end/Operator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { config as CHAIN_CONFIG } from '@streamr/config'
import { fetchPrivateKeyWithGas } from '@streamr/test-utils'
import { fetchPrivateKeyWithGas, generateWalletWithGasAndTokens } from '@streamr/test-utils'
import { Logger, TheGraphClient, toEthereumAddress, until } from '@streamr/utils'
import { Contract, parseEther, Wallet } from 'ethers'
import { StreamrClient } from '../../src/StreamrClient'
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('Operator', () => {
const concurrentTasks = await Promise.all([
createStream(),
createStream(),
setupOperatorContract({ nodeCount: 1 })
setupOperatorContract({ nodeCount: 1, generateWalletWithGasAndTokens })
])
streamId1 = concurrentTasks[0]
streamId2 = concurrentTasks[1]
Expand Down Expand Up @@ -135,7 +135,9 @@ describe('Operator', () => {

it('flag', async () => {
const flagger = deployedOperator
const target = await setupOperatorContract()
const target = await setupOperatorContract({
generateWalletWithGasAndTokens
})

await sponsor(flagger.operatorWallet, await sponsorship2.getAddress(), parseEther('50000'))

Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"author": "Streamr Network AG <[email protected]>",
"license": "Apache-2.0",
"dependencies": {
"@streamr/config": "^5.4.1",
"@streamr/utils": "102.0.0-beta.0",
"cors": "^2.8.5",
"ethers": "^6.13.0",
Expand Down
Loading

0 comments on commit 5d68316

Please sign in to comment.