EOS-Nation / dapp-client-js

DAPP JavaScript/TypeScript Client Library
MIT License
1 stars 0 forks source link

Add query from link #2

Open DenisCarriere opened 5 years ago

DenisCarriere commented 5 years ago

https://cmichel.io/how-to-fetch-table-indexes-using-eosjs/

NatPDeveloper commented 5 years ago
    const accountHexLE = getTableBoundsForName(`cardgame1112`).lower_bound;
    const serviceHexLE = getTableBoundsForName(`ipfsservice1`).lower_bound;
    const packageHexLE = getTableBoundsForName(`package1`).lower_bound;
    const providerHexLE = getTableBoundsForName(`heliosselene`);
    const boundsAccountext = {
      lower_bound: `${accountHexLE}${`0`.repeat(16)}${providerHexLE.lower_bound}${serviceHexLE}`,
      upper_bound: `${accountHexLE}${`0`.repeat(16)}${providerHexLE.upper_bound}${serviceHexLE}`,
    }
    const boundsPackage = {
      lower_bound: `${packageHexLE}${`0`.repeat(16)}${providerHexLE.lower_bound}${serviceHexLE}`,
      upper_bound: `${packageHexLE}${`0`.repeat(16)}${providerHexLE.upper_bound}${serviceHexLE}`,
    };
    const accountextnfo = JSON.stringify({
      code: 'dappservices',
      scope: 'DAPP',
      table: 'accountext',
      key_type: `sha256`,
      index_position: 2,
      json: true,
      limit: 9999,
      ...boundsAccountext,
    });
    const packageInfo = JSON.stringify({
      code: 'dappservices',
      scope: 'dappservices',
      table: 'package',
      key_type: `sha256`,
      index_position: 2,
      json: true,
      limit: 9999,
      ...boundsPackage,
    });
    axios.post(this.state.kylinEndpoint, accountextnfo)
    .then(response => {
      response.data.rows.forEach( e => {
        if(e.provider === this.state.queryProvider)
          this.setState({
            packageName: e.package,
            service:  e.service,
            provider:  e.provider,
            dappStaked:  e.balance,
          });
      })
    });
    axios.post(this.state.kylinEndpoint, packageInfo)
      .then(response => {
        response.data.rows.forEach( e => {
          if(e.provider === this.state.queryProvider)
            this.setState({
              quota:  e.quota.substring(0, e.quota.length - 6),
              packagePeriod:  `${e.package_period} seconds`,
            });
        })
      });
  }

...

import Long from 'long';

const nameToValue = (name) => {
    const charmap = '.12345abcdefghijklmnopqrstuvwxyz';
    const charidx = ch => {
      const idx = charmap.indexOf(ch);
      if (idx === -1) throw new TypeError(`Invalid character: '${ch}'`);

      return idx;
    };
    if (typeof name !== 'string') throw new TypeError('name parameter is a required string');

    if (name.length > 12) throw new TypeError('A name can be up to 12 characters long');

    let bitstr = '';
    for (let i = 0; i <= 12; i++) {
      // process all 64 bits (even if name is short)
      const c = i < name.length ? charidx(name[i]) : 0;
      const bitlen = i < 12 ? 5 : 4;
      let bits = Number(c).toString(2);
      if (bits.length > bitlen) {
        throw new TypeError('Invalid name ' + name);
      }
      bits = '0'.repeat(bitlen - bits.length) + bits;
      bitstr += bits;
    }

    return Long.fromString(bitstr, true, 2);
}

const bytesToHex = (bytes) => {
    let leHex = '';
    for (const b of bytes) {
      const n = Number(b).toString(16);
      leHex += (n.length === 1 ? '0' : '') + n;
    }
    return leHex;
}

export default function getTableBoundsForName(name, asLittleEndianHex = true) {
    const nameValue = nameToValue(name);
    const nameValueP1 = nameValue.add(1);

    if(!asLittleEndianHex) {
      return {
        lower_bound: nameValue.toString(),
        upper_bound: nameValueP1.toString()
      };
    }

    const lowerBound = bytesToHex(nameValue.toBytesLE());
    const upperBound = bytesToHex(nameValueP1.toBytesLE());

    return {
      lower_bound: lowerBound,
      upper_bound: upperBound,
    };
}