discovery.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict'
  2. const debug = require('debug')('joystream:runtime:discovery')
  3. /*
  4. * Add discovery related functionality to the substrate API.
  5. */
  6. class DiscoveryApi {
  7. static async create(base) {
  8. const ret = new DiscoveryApi()
  9. ret.base = base
  10. await DiscoveryApi.init()
  11. return ret
  12. }
  13. static async init() {
  14. debug('Init')
  15. }
  16. /*
  17. * Get Bootstrap endpoints
  18. */
  19. async getBootstrapEndpoints() {
  20. return this.base.api.query.discovery.bootstrapEndpoints()
  21. }
  22. /*
  23. * Set Bootstrap endpoints, requires the sudo account to be provided and unlocked
  24. */
  25. async setBootstrapEndpoints(sudoAccount, endpoints) {
  26. const tx = this.base.api.tx.discovery.setBootstrapEndpoints(endpoints)
  27. // make sudo call
  28. return this.base.signAndSend(sudoAccount, this.base.api.tx.sudo.sudo(tx))
  29. }
  30. /*
  31. * Get AccountInfo of a storage provider
  32. */
  33. async getAccountInfo(storageProviderId) {
  34. const info = await this.base.api.query.discovery.accountInfoByStorageProviderId(storageProviderId)
  35. // Not an Option so we use default value check to know if info was found
  36. return info.expires_at.eq(0) ? null : info
  37. }
  38. /*
  39. * Set AccountInfo of our storage provider
  40. */
  41. async setAccountInfo(ipnsId) {
  42. const roleAccountId = this.base.identities.key.address
  43. const storageProviderId = this.base.storageProviderId
  44. const isProvider = await this.base.workers.isStorageProvider(storageProviderId)
  45. if (isProvider) {
  46. const tx = this.base.api.tx.discovery.setIpnsId(storageProviderId, ipnsId)
  47. return this.base.signAndSend(roleAccountId, tx)
  48. }
  49. throw new Error('Cannot set AccountInfo, id is not a storage provider')
  50. }
  51. /*
  52. * Clear AccountInfo of our storage provider
  53. */
  54. async unsetAccountInfo() {
  55. const roleAccountId = this.base.identities.key.address
  56. const storageProviderId = this.base.storageProviderId
  57. const tx = this.base.api.tx.discovery.unsetIpnsId(storageProviderId)
  58. return this.base.signAndSend(roleAccountId, tx)
  59. }
  60. }
  61. module.exports = {
  62. DiscoveryApi,
  63. }