Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query from link #2

Open
DenisCarriere opened this issue Sep 7, 2019 · 1 comment

Comments

@NatPDeveloper

This comment has been minimized.

Copy link

commented Sep 12, 2019

  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,
  };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.