storage.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { ApiPromise } from "@polkadot/api";
  2. import type { ITuple } from "@polkadot/types/types";
  3. import {
  4. Bag,
  5. BagId,
  6. Cid,
  7. DynamicBagType,
  8. DynamicBagCreationPolicy,
  9. DataObject,
  10. DataObjectId,
  11. StorageBucket,
  12. StorageBucketId,
  13. DistributionBucket,
  14. DistributionBucketIndex,
  15. DistributionBucketFamily,
  16. DistributionBucketFamilyId,
  17. } from "@joystream/types/storage";
  18. import { Hash } from "@polkadot/types/interfaces";
  19. export const uploadingBlocked = (api: ApiPromise): Promise<boolean> =>
  20. api.query.storage.uploadingBlocked() as Promise<boolean>;
  21. export const getBlacklist = (api: ApiPromise): Promise<Cid[]> =>
  22. api.query.storage.blacklist();
  23. export const getBlacklistSize = (api: ApiPromise): Promise<number> =>
  24. api.query.storage.currentBlacklistSize();
  25. export const maxStorageBucketsPerBag = (api: ApiPromise): Promise<number> =>
  26. api.query.storage.storageBucketsPerBagLimit();
  27. export const maxDistributionBucketsPerBag = (
  28. api: ApiPromise
  29. ): Promise<number> => api.query.storage.distributionBucketsPerBagLimit();
  30. export const maxStorageBucketObjects = (api: ApiPromise): Promise<number> =>
  31. api.query.storage.voucherMaxObjectsNumberLimit();
  32. export const maxStorageBucketSize = (api: ApiPromise): Promise<number> =>
  33. api.query.storage.voucherMaxObjectsSizeLimit();
  34. export const feePerMegabyte = (api: ApiPromise): Promise<Bag> =>
  35. api.query.storage.dataObjectPerMegabyteFee();
  36. export const getDynamicBagPolicy = (
  37. api: ApiPromise,
  38. type?: DynamicBagType | "Member" | "Channel" | number
  39. ): Promise<
  40. DynamicBagCreationPolicy | Map<DynamicBagType, DynamicBagCreationPolicy>
  41. > =>
  42. type
  43. ? api.query.storage.dynamicBagCreationPolicies(type)
  44. : api.query.storage.dynamicBagCreationPolicies.entries();
  45. export const getNextDataObject = (api: ApiPromise): Promise<DataObjectId> =>
  46. api.query.storage.nextDataObjectId() as Promise<DataObjectId>;
  47. export const getNextStorageBucket = (
  48. api: ApiPromise
  49. ): Promise<StorageBucketId> =>
  50. api.query.storage.nextStorageBucketId() as Promise<StorageBucketId>;
  51. export const getNextDistributionFamily = (
  52. api: ApiPromise
  53. ): Promise<DistributionBucketFamilyId> =>
  54. api.query.storage.nextDistributionBucketFamilyId() as Promise<DistributionBucketFamilyId>;
  55. // video + cover are contained in one bag
  56. export const getBag = (
  57. api: ApiPromise,
  58. bagId: BagId | { Static: any } | { Dynamic: any } | string | number
  59. ): Promise<Bag> => api.query.storage.bags(bagId);
  60. export const getBags = async (api: ApiPromise): Promise<Map<BagId, Bag>> =>
  61. api.query.storage.bags.entries();
  62. export const getObject = (
  63. api: ApiPromise,
  64. bagId: BagId | { Static: any } | { Dynamic: any } | string | number,
  65. objectId: DataObjectId | number
  66. ): Promise<Map<DataObjectId, DataObject>> =>
  67. api.query.storage.dataObjectsById(bagId, objectId);
  68. export const getBagObjects = (
  69. api: ApiPromise,
  70. bagId: BagId | { Static: any } | { Dynamic: any } | string | number
  71. ): Promise<Map<[BagId, DataObjectId], DataObject>> =>
  72. api.query.storage.dataObjectsById.entries(bagId);
  73. // storage
  74. export const getStorageBucket = (
  75. api: ApiPromise,
  76. bucketId: StorageBucketId | Hash,
  77. hash?: Hash
  78. ): Promise<StorageBucket> =>
  79. (hash
  80. ? api.query.storage.storageBucketById.at(hash, bucketId)
  81. : api.query.storage.storageBucketById(bucketId)) as Promise<StorageBucket>;
  82. export const getStorageBuckets = (
  83. api: ApiPromise
  84. ): Promise<Map<StorageBucketId, StorageBucket>> =>
  85. api.query.storage.storageBucketById.entries();
  86. // distribution
  87. export const getDistributionFamilyNumber = (api: ApiPromise): Promise<number> =>
  88. api.query.storage.distributionBucketFamilyNumber();
  89. export const getDistributionFamilyBuckets = (
  90. api: ApiPromise,
  91. familyId: DistributionBucketFamilyId | number
  92. ): Promise<Map<DistributionBucketIndex, DistributionBucket>> =>
  93. api.query.storage.distributionBucketByFamilyIdById.entries(
  94. familyId
  95. ) as Promise<Map<DistributionBucketIndex, DistributionBucket>>;
  96. export const getDistributionFamilyBucket = (
  97. api: ApiPromise,
  98. familyId: DistributionBucketFamilyId | number,
  99. bucketIndex: DistributionBucketIndex | number
  100. ): Promise<DistributionBucket> =>
  101. api.query.storage.distributionBucketByFamilyIdById(
  102. familyId,
  103. bucketIndex
  104. ) as Promise<DistributionBucket>;
  105. export const getDistributionFamilies = (
  106. api: ApiPromise
  107. ): Promise<Map<DistributionBucketFamilyId, DistributionBucketFamily>> =>
  108. api.query.storage.distributionBucketFamilyById.entries();
  109. export const getDistributionFamily = async (
  110. api: ApiPromise,
  111. familyId: DistributionBucketFamilyId | number
  112. ): Promise<DistributionBucketFamily> =>
  113. (await api.query.storage.distributionBucketFamilyById(
  114. familyId
  115. )) as DistributionBucketFamily;