Home Reference Source

packages/oo7-substrate/src/types.js

  1. const { toLE } = require('./utils')
  2.  
  3. class VecU8 extends Uint8Array {
  4. toJSON() {
  5. return { _type: 'VecU8', data: Array.from(this) }
  6. }
  7. }
  8.  
  9. class AccountId extends Uint8Array {
  10. toJSON() {
  11. return { _type: 'AccountId', data: Array.from(this) }
  12. }
  13. }
  14.  
  15. class Hash extends Uint8Array {
  16. toJSON() {
  17. return { _type: 'Hash', data: Array.from(this) }
  18. }
  19. }
  20.  
  21. class Signature extends Uint8Array {
  22. toJSON() {
  23. return { _type: "Signature", data: Array.from(this) }
  24. }
  25. }
  26.  
  27. class VoteThreshold extends String {
  28. toJSON() {
  29. return { _type: 'VoteThreshold', data: this + ''}
  30. }
  31. }
  32.  
  33. class BlockNumber extends Number {
  34. toJSON() {
  35. return { _type: 'BlockNumber', data: this+0 }
  36. }
  37. }
  38.  
  39. class AccountIndex extends Number {
  40. toJSON() {
  41. return { _type: 'AccountIndex', data: this+0 }
  42. }
  43. }
  44.  
  45. class Tuple extends Array {
  46. toJSON() {
  47. return { _type: 'Tuple', data: Array.from(this) }
  48. }
  49. }
  50.  
  51. class SlashPreference extends Number {
  52. toJSON() { return { _type: 'SlashPreference', data: this+0 } }
  53. }
  54.  
  55. class Perbill extends Number {
  56. toJSON() { return { _type: 'Perbill', data: this+0 } }
  57. }
  58.  
  59. class Permill extends Number {
  60. toJSON() { return { _type: 'Permill', data: this+0 } }
  61. }
  62.  
  63. class Moment extends Date {
  64. constructor(seconds) {
  65. super(seconds * 1000)
  66. this.number = seconds
  67. }
  68. toJSON() {
  69. return { _type: 'Moment', data: this.number }
  70. }
  71. }
  72.  
  73. class Balance extends Number {
  74. toJSON() { return { _type: 'Balance', data: this+0 } }
  75. add(b) { return new Balance(this + b) }
  76. sub(b) { return new Balance(this - b) }
  77. }
  78.  
  79. class TransactionEra {
  80. constructor (period, phase) {
  81. if (typeof period === 'number' && typeof phase === 'number') {
  82. this.period = 2 << Math.min(15, Math.max(1, Math.ceil(Math.log2(period)) - 1))
  83. this.phase = phase % this.period
  84. }
  85. }
  86.  
  87. encode() {
  88. if (typeof this.period === 'number' && typeof this.phase === 'number') {
  89. let l = Math.min(15, Math.max(1, Math.ceil(Math.log2(this.period)) - 1))
  90. let factor = Math.max(1, this.period >> 12)
  91. let res = toLE((Math.floor(this.phase / factor) << 4) + l, 2)
  92. return res
  93. } else {
  94. return new Uint8Array([0])
  95. }
  96. }
  97. }
  98.  
  99. function reviver(key, bland) {
  100. if (typeof bland == 'object' && bland) {
  101. switch (bland._type) {
  102. case 'VecU8': return new VecU8(bland.data);
  103. case 'AccountId': return new AccountId(bland.data);
  104. case 'Hash': return new Hash(bland.data);
  105. case 'Signature': return new Signature(bland.data);
  106. case 'VoteThreshold': return new VoteThreshold(bland.data);
  107. case 'SlashPreference': return new SlashPreference(bland.data);
  108. case 'Perbill': return new Perbill(bland.data);
  109. case 'Permill': return new Permill(bland.data);
  110. case 'Moment': return new Moment(bland.data);
  111. case 'Tuple': return new Tuple(bland.data);
  112. case 'Balance': return new Balance(bland.data);
  113. case 'BlockNumber': return new BlockNumber(bland.data);
  114. case 'AccountIndex': return new AccountIndex(bland.data);
  115. }
  116. }
  117. return bland;
  118. }
  119.  
  120. module.exports = { VecU8, AccountId, Hash, Signature, VoteThreshold, SlashPreference, Moment, Balance,
  121. BlockNumber, AccountIndex, Tuple, TransactionEra, Perbill, Permill, reviver }