EOSIO / eosjs

General purpose library for the EOSIO blockchain.
http://eosio.github.io/eosjs
MIT License
1.43k stars 463 forks source link

[HELP] How to wrap the fetch of eosjs? #381

Closed shtse8 closed 5 years ago

shtse8 commented 6 years ago

I want to convert eosjs to socket. ATM, I can do it by overloading the global fetch method. How could I do it nicely? Thanks.

tbfleming commented 6 years ago

Option 1:

Pass it as an optional argument into:

const rpc = new eosjs.Rpc.JsonRpc('http://127.0.0.1:8000', { fetch: mySocketFetch });

Option 2:

Make your class reproduce JsonRpc's interface and pass it into:

const api = new eosjs.Api({ rpc: mySocketRpc, signatureProvider });
shtse8 commented 6 years ago

How about browser version? I can't find eosjs.

tbfleming commented 6 years ago

If you're using the pattern in https://github.com/EOSIO/eosjs/blob/master/docs/3.-Browsers.md :

const rpc = new eosjs_jsonrpc.JsonRpc('http://127.0.0.1:8000', { fetch: mySocketFetch });

or

const api = new eosjs.Api({ rpc: mySocketRpc, signatureProvider });
shtse8 commented 6 years ago

How about I am using this one?

https://cdn.jsdelivr.net/npm/eosjs@16.0.6/lib/eos.min.js

The pattern seems completely different:

  <script>
  /** Transactions are only valid on the selected chain. */
  chain = {
    main: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', // main network
    jungle: '038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca', // jungle testnet
    sys: 'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f' // local developer
  }

  /**
    Other httpEndpoint's: https://www.eosdocs.io/resources/apiendpoints
  */
  eos = Eos({
    keyProvider: '',// private key
    httpEndpoint: 'https://api.hkeos.com',
    chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
  });

  /**
    Sign and broadcast a transaction.

    @example updateProducerVote('myaccount', 'proxyaccount', ['respectedbp'])
  */
  async function updateProducerVote(voter, proxy = '', producers = []) {
    return eos.voteproducer(voter, proxy, producers)
  }

  </script>
tbfleming commented 6 years ago

I don't know if that version has a direct way.

shtse8 commented 6 years ago

What version should I use? v16.0.8?

tbfleming commented 6 years ago

v20.0.0beta1 works as I described above. I don’t know if 16 supports what you need or not.

shtse8 commented 6 years ago

That's great! Let me try.