All files / src/modules/debug stake.ts

100% Statements 20/20
100% Branches 0/0
100% Functions 3/3
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 8533x   33x 33x 33x 33x 33x   33x 33x             33x 4x           4x   4x                   33x         2x             2x   2x               33x 1x           1x   1x                                    
import { Types } from 'cafe-utility'
import { BeeRequestOptions, NumberString, RedistributionState, TransactionOptions } from '../../types'
import { prepareRequestHeaders } from '../../utils/headers'
import { http } from '../../utils/http'
import { BZZ, DAI } from '../../utils/tokens'
import { asNumberString } from '../../utils/type'
import { TransactionId } from '../../utils/typed-bytes'
 
const STAKE_ENDPOINT = 'stake'
const REDISTRIBUTION_ENDPOINT = 'redistributionstate'
 
/**
 * Gets the staked amount
 *
 * @param requestOptions Options for making requests
 */
export async function getStake(requestOptions: BeeRequestOptions): Promise<BZZ> {
  const response = await http<unknown>(requestOptions, {
    method: 'get',
    responseType: 'json',
    url: `${STAKE_ENDPOINT}`,
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  return BZZ.fromPLUR(asNumberString(body.stakedAmount, { name: 'stakedAmount' }))
}
 
/**
 * Stake given amount of tokens.
 *
 * @param requestOptions Options for making requests
 * @param amount
 * @param options
 */
export async function stake(
  requestOptions: BeeRequestOptions,
  amount: NumberString | string | bigint,
  options?: TransactionOptions,
): Promise<TransactionId> {
  const repsonse = await http<unknown>(requestOptions, {
    method: 'post',
    responseType: 'json',
    url: `${STAKE_ENDPOINT}/${amount}`,
    headers: prepareRequestHeaders(null, options),
  })
 
  const body = Types.asObject(repsonse.data, { name: 'response.data' })
 
  return new TransactionId(Types.asHexString(body.txHash, { name: 'txHash' }))
}
 
/**
 * Get current status of node in redistribution game
 *
 * @param requestOptions Options for making requests
 */
export async function getRedistributionState(requestOptions: BeeRequestOptions): Promise<RedistributionState> {
  const response = await http<unknown>(requestOptions, {
    method: 'get',
    responseType: 'json',
    url: REDISTRIBUTION_ENDPOINT,
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  return {
    minimumGasFunds: DAI.fromWei(asNumberString(body.minimumGasFunds, { name: 'minimumGasFunds' })),
    hasSufficientFunds: Types.asBoolean(body.hasSufficientFunds, { name: 'hasSufficientFunds' }),
    isFrozen: Types.asBoolean(body.isFrozen, { name: 'isFrozen' }),
    isFullySynced: Types.asBoolean(body.isFullySynced, { name: 'isFullySynced' }),
    phase: Types.asString(body.phase, { name: 'phase' }),
    round: Types.asNumber(body.round, { name: 'round' }),
    lastWonRound: Types.asNumber(body.lastWonRound, { name: 'lastWonRound' }),
    lastPlayedRound: Types.asNumber(body.lastPlayedRound, { name: 'lastPlayedRound' }),
    lastFrozenRound: Types.asNumber(body.lastFrozenRound, { name: 'lastFrozenRound' }),
    lastSelectedRound: Types.asNumber(body.lastSelectedRound, { name: 'lastSelectedRound' }),
    lastSampleDurationSeconds: Types.asNumber(body.lastSampleDurationSeconds, { name: 'lastSampleDurationSeconds' }),
    block: Types.asNumber(body.block, { name: 'block' }),
    reward: BZZ.fromPLUR(asNumberString(body.reward, { name: 'reward' })),
    fees: DAI.fromWei(asNumberString(body.fees, { name: 'fees' })),
    isHealthy: Types.asBoolean(body.isHealthy, { name: 'isHealthy' }),
  }
}