Home Reference Source

packages/oo7-substrate/src/storageBond.js

  1. const XXH = require('xxhashjs');
  2. const { SubscriptionBond } = require('./subscriptionBond')
  3. const { VecU8 } = require('./types')
  4. const { stringToBytes, hexToBytes, toLEHex } = require('./utils')
  5. const { decode } = require('./codec');
  6.  
  7. function storageKey(prefixString, arg) {
  8. let loc = new VecU8([...stringToBytes(prefixString), ...arg]);
  9. return '0x' + toLEHex(XXH.h64(loc.buffer, 0), 8) + toLEHex(XXH.h64(loc.buffer, 1), 8);
  10. }
  11.  
  12. class StorageBond extends SubscriptionBond {
  13. constructor (prefix, type, args = [], defaultValue = null) {
  14. super('state_storage', [[ storageKey(prefix, args) ]], r => {
  15. let raw = hexToBytes(r.changes[0][1]);
  16. return raw.length > 0 ? decode(raw, type) : defaultValue
  17. })
  18. }
  19. }
  20.  
  21. module.exports = { storageKey, StorageBond }