storage.ts 4.7 KB

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