GetScatter / scatter-js

Importable JavaScript library that allows web applications to directly interface with Scatter Desktop, Classic and Mobile.
MIT License
262 stars 148 forks source link

Errors initializing scatter.eos #136

Closed Sparke2 closed 5 years ago

Sparke2 commented 5 years ago

I'm trying to run the following code:

<header>Hello Scatter</header>

<script src='dist-web/eosjs-api.js'></script>
<script src='dist-web/eosjs-jsonrpc.js'></script>
<script src='dist-web/eosjs-jssig.js'></script>
<script src="https://cdn.scattercdn.com/file/scatter-cdn/js/latest/scatterjs-core.min.js"></script>
<script src="https://cdn.scattercdn.com/file/scatter-cdn/js/latest/scatterjs-plugin-eosjs.min.js"></script>
<script>

ScatterJS.plugins(new ScatterEOS());

  const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr";
  const signatureProvider = new eosjs_jssig.JsSignatureProvider([defaultPrivateKey]);

const connectionOptions = {
  initTimeout: 10000
}

const network = {
    blockchain:'eos',
    chainId:'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
    host:'api.eosio.cr',
    port:80,
    protocol:'https'
}

ScatterJS.scatter.connect('My-App', connectionOptions).then(connected => {
  if (!connected) {
    // User does not have Scatter installed/unlocked.
    return false;
  }

  const scatter = ScatterJS.scatter;
  const requiredFields = {
    accounts: [network]
  };

  scatter.getIdentity(requiredFields).then(async() => {

    const account = scatter.identity.accounts.find(x => x.blockchain === 'eos');
    console.log(account);
    const eosOptions = {
      expireInSeconds: 60
    }

    // Get a proxy reference to eosjs which you can use to sign transactions with a user's Scatter.
    //const eos = scatter.eos(network, Eos, eosOptions);

    //const eos = scatter.eos(network, Api, {rpc}, eosOptions);

    const rpc = new eosjs_jsonrpc.JsonRpc('https://api.eosnewyork.io');
    const Api = new eosjs_api.Api({ rpc, signatureProvider });
    eos = scatter.eos(network, Api, { rpc });

    const transactionOptions = {
      authorization: [`${account.name}@${account.authority}`]
    };

    // example of pushing an action
    eos.transaction({
      actions: [{
        account: 'dexaraniiznx',
        name: 'placeorder',
        authorization: [{
          actor: 'walletxxxxxx',
          permission: 'active',
        }],
        data: {
          acct: account.name,
          price: 10,
          amount: 100,
        },
      }]
    }, {
      broadcast: true,
      sign: true
    })
    .then(trx => {
      console.log(`Transaction ID: ${trx.transaction_id}`);
    }).catch(error => {
      console.error(error);
    });
  })
  .catch(err => {
    console.error(err)
  })
})
</script>

I'm getting this error which is related to the initialization of eos = scatter.eos(network, Api, { rpc });

TypeError: e is not a function
    at t.eos (index.js:46)
    at test-index.html:59

I need a minimal viable example of sending a transaction with Scatter in plain JS without any requirejs, angular, react, vue or any extra dependencies.

I'm trying to use this example https://gist.github.com/miguelmota/c2a12cb4ed938d2d6f93146903390065 but I don't know what to do with the Eos arg because I'm not using require or eosjs library.

Whenever I'm replacing it with any declarations using api and rpc args as it is shown in this example

       this.eos = scatter.eos(network, Api, { rpc });

then it throws a similar error:

test-index.html:92 TypeError: e is not a constructor
    at t.eos (index.js:70)
    at test-index.html:58
Dexaran commented 5 years ago

You can declare scatter.eos like this:


const network = {
    blockchain:'eos',
    chainId:'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
    host:'fn.eossweden.se',
    port:443,
    protocol:'https'
}

ScatterJS.scatter.connect('eosio').then(function(connect) {
    const rpc = new eosjs_jsonrpc.JsonRpc('https://fn.eossweden.se:443');
    const eos = scatter.eos(network, eosjs_api.Api, { rpc });
});