discovery.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 ret.init()
  11. return ret
  12. }
  13. 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
  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(
  29. sudoAccount,
  30. this.base.api.tx.sudo.sudo(tx)
  31. )
  32. }
  33. /*
  34. * Get AccountInfo of a storage provider
  35. */
  36. async getAccountInfo (storageProviderId) {
  37. const info = await this.base.api.query.discovery.accountInfoByStorageProviderId(storageProviderId)
  38. // Not an Option so we use default value check to know if info was found
  39. return info.expires_at.eq(0) ? null : info
  40. }
  41. /*
  42. * Set AccountInfo of our storage provider
  43. */
  44. async setAccountInfo (ipnsId) {
  45. const roleAccountId = this.base.identities.key.address
  46. const storageProviderId = this.base.storageProviderId
  47. const isProvider = await this.base.workers.isStorageProvider(storageProviderId)
  48. if (isProvider) {
  49. const tx = this.base.api.tx.discovery.setIpnsId(storageProviderId, ipnsId)
  50. return this.base.signAndSend(roleAccountId, tx)
  51. } else {
  52. throw new Error('Cannot set AccountInfo, id is not a storage provider')
  53. }
  54. }
  55. /*
  56. * Clear AccountInfo of our storage provider
  57. */
  58. async unsetAccountInfo () {
  59. const roleAccountId = this.base.identities.key.address
  60. const storageProviderId = this.base.storageProviderId
  61. var tx = this.base.api.tx.discovery.unsetIpnsId(storageProviderId)
  62. return this.base.signAndSend(roleAccountId, tx)
  63. }
  64. }
  65. module.exports = {
  66. DiscoveryApi
  67. }