|
| 1 | +import { _operatorContractUtils, StreamrClient } from '@streamr/sdk' |
| 2 | +import { collect, Logger, scheduleAtInterval, WeiAmount } from '@streamr/utils' |
| 3 | +import { Schema } from 'ajv' |
| 4 | +import { formatEther, parseEther, Wallet } from 'ethers' |
| 5 | +import sample from 'lodash/sample' |
| 6 | +import { Plugin } from '../../Plugin' |
| 7 | +import PLUGIN_CONFIG_SCHEMA from './config.schema.json' |
| 8 | + |
| 9 | +export interface AutostakerPluginConfig { |
| 10 | + operatorContractAddress: string |
| 11 | + // TODO is it possible implement this without exposing the private key here? |
| 12 | + // e.g. by configuring so that operator nodes can stake behalf of the operator? |
| 13 | + operatorOwnerPrivateKey: string |
| 14 | + runIntervalInMs: number |
| 15 | +} |
| 16 | + |
| 17 | +const STAKE_AMOUNT: WeiAmount = parseEther('10000') |
| 18 | + |
| 19 | +const logger = new Logger(module) |
| 20 | + |
| 21 | +export class AutostakerPlugin extends Plugin<AutostakerPluginConfig> { |
| 22 | + |
| 23 | + private abortController: AbortController = new AbortController() |
| 24 | + |
| 25 | + async start(streamrClient: StreamrClient): Promise<void> { |
| 26 | + logger.info('Start autostaker plugin') |
| 27 | + scheduleAtInterval(async () => { |
| 28 | + try { |
| 29 | + await this.runActions(streamrClient) |
| 30 | + } catch (err) { |
| 31 | + logger.warn('Error while running autostaker actions', { err }) |
| 32 | + } |
| 33 | + }, this.pluginConfig.runIntervalInMs, false, this.abortController.signal) |
| 34 | + } |
| 35 | + |
| 36 | + private async runActions(streamrClient: StreamrClient): Promise<void> { |
| 37 | + logger.info('Run autostaker actions') |
| 38 | + const provider = (await streamrClient.getSigner()).provider |
| 39 | + const operatorContract = _operatorContractUtils.getOperatorContract(this.pluginConfig.operatorContractAddress) |
| 40 | + .connect(new Wallet(this.pluginConfig.operatorOwnerPrivateKey, provider)) |
| 41 | + // 30000 DELEGOINTEJA |
| 42 | + // 10000 niistä steikattu = totalStakedIntoSponsorshipsWei() |
| 43 | + // 123 on saatu jo tuottoja, ja withdrawattu sponsorshipeistä |
| 44 | + // 20000 = X |
| 45 | + // valueWithoutEarnings() = 30123 = joka voisi olla nimeltä esim. totalBalanceInData() |
| 46 | + const stakedAmount = await operatorContract.totalStakedIntoSponsorshipsWei() |
| 47 | + const availableBalance = (await operatorContract.valueWithoutEarnings()) - stakedAmount // tämä on ihan sama, mutta toi valueWithoutEarnings(), voi käyttää kumpaa vaan await getTestTokenContract().connect(provider).balanceOf(await operatorContract.getAddress()) // = 20123 |
| 48 | + logger.info(`Available balance: ${formatEther(availableBalance)} (staked=${formatEther(stakedAmount)})`) |
| 49 | + // TODO is there a better way to get the client? Maybe we should add StreamrClient#getTheGraphClient() |
| 50 | + // TODO what are good where consitions for the sponsorships query |
| 51 | + // @ts-expect-error private |
| 52 | + const queryResult = streamrClient.theGraphClient.queryEntities((lastId: string, pageSize: number) => { |
| 53 | + return { |
| 54 | + query: ` |
| 55 | + { |
| 56 | + sponsorships( |
| 57 | + where: { |
| 58 | + projectedInsolvency_gt: ${Math.floor(Date.now() / 1000)}, |
| 59 | + spotAPY_gt: 0 |
| 60 | + id_gt: "${lastId}" |
| 61 | + }, |
| 62 | + first: ${pageSize} |
| 63 | + ) { |
| 64 | + id |
| 65 | + } |
| 66 | + } |
| 67 | + ` |
| 68 | + } |
| 69 | + }) |
| 70 | + const sponsorships: { id: string }[] = await collect(queryResult) |
| 71 | + logger.info(`Available sponsorships: ${sponsorships.map((s) => s.id).join(',')}`) |
| 72 | + if ((sponsorships.length) > 0 && (availableBalance >= STAKE_AMOUNT)) { |
| 73 | + const targetSponsorship = sample(sponsorships)! |
| 74 | + logger.info(`Stake ${formatEther(STAKE_AMOUNT)} to ${targetSponsorship.id}`) |
| 75 | + await _operatorContractUtils.stake( |
| 76 | + operatorContract, |
| 77 | + targetSponsorship.id, |
| 78 | + STAKE_AMOUNT |
| 79 | + ) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + async stop(): Promise<void> { |
| 84 | + logger.info('Stop autostaker plugin') |
| 85 | + this.abortController.abort() |
| 86 | + } |
| 87 | + |
| 88 | + // eslint-disable-next-line class-methods-use-this |
| 89 | + override getConfigSchema(): Schema { |
| 90 | + return PLUGIN_CONFIG_SCHEMA |
| 91 | + } |
| 92 | +} |
0 commit comments