All files / src/modules/debug stamps.ts

96.82% Statements 61/63
0% Branches 0/2
100% Functions 13/13
96.66% Lines 58/60

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 20033x                 33x 33x 33x 33x 33x 33x 33x   33x 33x   33x 1x           1x 1785x   1785x                       33x 1x           1x 8x   1x 8x 8x 8x 8x 8x 8x   8x                                       33x       78x           48x   48x 48x 48x 48x 48x 48x   48x                                     33x       1x           1x   1x         65536x 65536x             33x           3x   3x       3x       3x               3x   3x     33x         4x           4x   4x     33x 3x           3x   3x    
import { Types } from 'cafe-utility'
import type {
  BeeRequestOptions,
  GlobalPostageBatch,
  NumberString,
  PostageBatch,
  PostageBatchBuckets,
  PostageBatchOptions,
} from '../../types'
import { Duration } from '../../utils/duration'
import { http } from '../../utils/http'
import { Size } from '../../utils/size'
import { getStampEffectiveBytes, getStampTheoreticalBytes, getStampUsage } from '../../utils/stamps'
import { asNumberString } from '../../utils/type'
import { BatchId, EthAddress } from '../../utils/typed-bytes'
import { normalizeBatchTTL } from '../../utils/workaround'
 
const STAMPS_ENDPOINT = 'stamps'
const BATCHES_ENDPOINT = 'batches'
 
export async function getGlobalPostageBatches(requestOptions: BeeRequestOptions): Promise<GlobalPostageBatch[]> {
  const response = await http<unknown>(requestOptions, {
    method: 'get',
    url: `${BATCHES_ENDPOINT}`,
    responseType: 'json',
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
  const batches = Types.asArray(body.batches, { name: 'batches' }).map(x => Types.asObject(x, { name: 'batch' }))
 
  return batches.map(x => ({
    batchID: new BatchId(Types.asString(x.batchID, { name: 'batchID' })),
    batchTTL: Types.asNumber(x.batchTTL, { name: 'batchTTL' }),
    bucketDepth: Types.asNumber(x.bucketDepth, { name: 'bucketDepth' }),
    depth: Types.asNumber(x.depth, { name: 'depth' }),
    immutable: Types.asBoolean(x.immutable, { name: 'immutable' }),
    owner: new EthAddress(Types.asString(x.owner, { name: 'owner' })),
    start: Types.asNumber(x.start, { name: 'start' }),
    value: asNumberString(x.value, { name: 'value' }),
  }))
}
 
export async function getAllPostageBatches(requestOptions: BeeRequestOptions): Promise<PostageBatch[]> {
  const response = await http<unknown>(requestOptions, {
    method: 'get',
    url: `${STAMPS_ENDPOINT}`,
    responseType: 'json',
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
  const stamps = Types.asArray(body.stamps, { name: 'stamps' }).map(x => Types.asObject(x, { name: 'stamp' }))
 
  return stamps.map(x => {
    const utilization = Types.asNumber(x.utilization, { name: 'utilization' })
    const depth = Types.asNumber(x.depth, { name: 'depth' })
    const bucketDepth = Types.asNumber(x.bucketDepth, { name: 'bucketDepth' })
    const usage = getStampUsage(utilization, depth, bucketDepth)
    const batchTTL = normalizeBatchTTL(Types.asNumber(x.batchTTL, { name: 'batchTTL' }))
    const duration = Duration.fromSeconds(batchTTL)
 
    return {
      batchID: new BatchId(Types.asString(x.batchID, { name: 'batchID' })),
      utilization,
      usable: Types.asBoolean(x.usable, { name: 'usable' }),
      label: Types.asEmptiableString(x.label, { name: 'label' }),
      depth,
      amount: asNumberString(x.amount, { name: 'amount' }),
      bucketDepth,
      blockNumber: Types.asNumber(x.blockNumber, { name: 'blockNumber' }),
      immutableFlag: Types.asBoolean(x.immutableFlag, { name: 'immutableFlag' }),
      usage,
      usageText: `${Math.round(usage * 100)}%`,
      size: Size.fromBytes(getStampEffectiveBytes(depth)),
      remainingSize: Size.fromBytes(Math.ceil(getStampEffectiveBytes(depth) * (1 - usage))),
      theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(depth)),
      duration,
    }
  })
}
 
export async function getPostageBatch(
  requestOptions: BeeRequestOptions,
  postageBatchId: BatchId,
): Promise<PostageBatch> {
  const response = await http<unknown>(requestOptions, {
    method: 'get',
    url: `${STAMPS_ENDPOINT}/${postageBatchId}`,
    responseType: 'json',
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  const utilization = Types.asNumber(body.utilization, { name: 'utilization' })
  const depth = Types.asNumber(body.depth, { name: 'depth' })
  const bucketDepth = Types.asNumber(body.bucketDepth, { name: 'bucketDepth' })
  const usage = getStampUsage(utilization, depth, bucketDepth)
  const batchTTL = normalizeBatchTTL(Types.asNumber(body.batchTTL, { name: 'batchTTL' }))
  const duration = Duration.fromSeconds(batchTTL)
 
  return {
    batchID: new BatchId(Types.asString(body.batchID, { name: 'batchID' })),
    utilization,
    usable: Types.asBoolean(body.usable, { name: 'usable' }),
    label: Types.asEmptiableString(body.label, { name: 'label' }),
    depth,
    amount: asNumberString(body.amount, { name: 'amount' }),
    bucketDepth,
    blockNumber: Types.asNumber(body.blockNumber, { name: 'blockNumber' }),
    immutableFlag: Types.asBoolean(body.immutableFlag, { name: 'immutableFlag' }),
    usage,
    usageText: `${Math.round(usage * 100)}%`,
    size: Size.fromBytes(getStampEffectiveBytes(depth)),
    remainingSize: Size.fromBytes(Math.ceil(getStampEffectiveBytes(depth) * (1 - usage))),
    theoreticalSize: Size.fromBytes(getStampTheoreticalBytes(depth)),
    duration,
  }
}
 
export async function getPostageBatchBuckets(
  requestOptions: BeeRequestOptions,
  postageBatchId: BatchId,
): Promise<PostageBatchBuckets> {
  const response = await http<unknown>(requestOptions, {
    method: 'get',
    url: `${STAMPS_ENDPOINT}/${postageBatchId}/buckets`,
    responseType: 'json',
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  return {
    depth: Types.asNumber(body.depth, { name: 'depth' }),
    bucketDepth: Types.asNumber(body.bucketDepth, { name: 'bucketDepth' }),
    bucketUpperBound: Types.asNumber(body.bucketUpperBound, { name: 'bucketUpperBound' }),
    buckets: Types.asArray(body.buckets, { name: 'buckets' })
      .map(x => Types.asObject(x, { name: 'bucket' }))
      .map(x => ({
        bucketID: Types.asNumber(x.bucketID, { name: 'bucketID' }),
        collisions: Types.asNumber(x.collisions, { name: 'collisions' }),
      })),
  }
}
 
export async function createPostageBatch(
  requestOptions: BeeRequestOptions,
  amount: NumberString,
  depth: number,
  options?: PostageBatchOptions,
): Promise<BatchId> {
  const headers: Record<string, string> = {}
 
  Iif (options?.gasPrice) {
    headers['gas-price'] = options.gasPrice.toString()
  }
 
  Iif (options?.immutableFlag !== undefined) {
    headers.immutable = String(options.immutableFlag)
  }
 
  const response = await http<unknown>(requestOptions, {
    method: 'post',
    url: `${STAMPS_ENDPOINT}/${amount}/${depth}`,
    responseType: 'json',
    params: { label: options?.label },
    headers,
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  return new BatchId(Types.asString(body.batchID, { name: 'batchID' }))
}
 
export async function topUpBatch(
  requestOptions: BeeRequestOptions,
  id: BatchId,
  amount: NumberString,
): Promise<BatchId> {
  const response = await http<unknown>(requestOptions, {
    method: 'patch',
    url: `${STAMPS_ENDPOINT}/topup/${id}/${amount}`,
    responseType: 'json',
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  return new BatchId(Types.asString(body.batchID, { name: 'batchID' }))
}
 
export async function diluteBatch(requestOptions: BeeRequestOptions, id: BatchId, depth: number): Promise<BatchId> {
  const response = await http<unknown>(requestOptions, {
    method: 'patch',
    url: `${STAMPS_ENDPOINT}/dilute/${id}/${depth}`,
    responseType: 'json',
  })
 
  const body = Types.asObject(response.data, { name: 'response.data' })
 
  return new BatchId(Types.asString(body.batchID, { name: 'batchID' }))
}