Networks are the foundation of ScatterJS. They tell Scatter which blockchain nodes you're going to be working with.
const network = ScatterJS.Network.fromJson({
blockchain: 'eos',
chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
host: 'nodes.get-scatter.com',
port: 443,
protocol: 'https'
});
Connect to an Available Wallet
Once you are connected, you can then call API methods on ScatterJS.
ScatterJS.connect('MyAppName', { network }).then(connected => {
if (!connected) return false;
// ScatterJS.someMethod();
});
You can see the full @scatterjs/core API docs here.
Getting Blockchain Accounts
Login with the network passed into ScatterJS.connect
.
ScatterJS.login().then(...);
Login with multiple networks.
ScatterJS.login({ accounts: [network1, network2] }).then(...);
Logout.
ScatterJS.logout().then(...);
After a successful login, the "Identity" will be available at ScatterJS.identity
. If a user refreshes the page and has already logged in, the ScatterJS.identity
property will be auto-filled.
Get Blockchain Accounts from the Identity
Because accounts are nested within the Identity, there is an easy method for fetching them.
const account = ScatterJS.account('eos');
// Result: { name: '...', authority: 'active', publicKey: '...', blockchain: 'eos', chainId: '...' }
const account = ScatterJS.account('eth');
// Result: { address: '...', blockchain: 'eth', chainId: '1' }
const account = ScatterJS.account('trx');
// Result: { address: '...', blockchain: 'trx', chainId: '1' }
From the Identity.
const account = ScatterJS.identity.accounts.find(x => {
return x.blockchain === 'eos';
});
Using Blockchain Wrappers
Blockchain wrappers wrap the actual blockchain libraries (eosjs, tronweb, web3, etc) that you pass in. That way you don't have to relearn any APIs or be forced to use any specific version.
You can click on the libraries below to go directly to their respective GitHub pages:
- [email protected] (@scatterjs/eosjs)
import Eos from 'eosjs';
const eos = ScatterJS.eos(network, Eos, eosjsOptions);
const result = await eos.transfer(...);
- [email protected] (@scatterjs/eosjs2)
import { JsonRpc, Api } from 'eosjs'
const rpc = new JsonRpc(network.fullhost());
const eos = ScatterJS.eos(network, Api, { rpc });
const result = await eos.transact({...});
- tronweb
import TronWeb from 'tronweb';
const httpProvider = new TronWeb.providers.HttpProvider(network.fullhost());
let tron = new TronWeb(httpProvider, httpProvider, network.fullhost());
tron.setDefaultBlock('latest');
tron = ScatterJS.trx(network, tron);
const result = await tron.trx.sendTransaction(...);
- web3
import Web3 from 'web3';
const web3 = ScatterJS.web3(network, Web3);
const result = await web3.eth.sendTransaction(...);
- fio
import Fio from '@fioprotocol/fiojs';
const api = ScatterJS.fio(network, Fio, {
textEncoder: new TextEncoder(),
textDecoder: new TextDecoder(),
});
const transactionOptions = await api.getTransactionOptions();
const tx = await api.transact(Object.assign(transactionOptions, { actions: [...] }));
const result = await fetch('HOST/v1/chain/push_transaction', { body: JSON.stringify(tx), method: 'POST' }).then(x => x.json());
NodeJS and Babel/Webpack Issues
If you're having trouble packaging or compiling your project, you probably need to add a Babel transpiler.
npm i -D @babel/runtime <-- run this command and it should compile.
Fetch Issues
If you're having trouble with fetch not being defined in your NodeJS environment, you can add it by doing:
npm i -S isomorphic-fetch
and then require('isomorphic-fetch'); at the start of your application
What Now?
Head over to the Scatter Developer Documentation to learn about all the amazing things you can do with ScatterJS.
This article has been written in Markdown format, which allows for easy formatting and readability. It covers the basic concepts of using ScatterJS, including connecting to a wallet, logging in, getting blockchain accounts, and using blockchain wrappers. It also touches on common issues that developers may encounter when working with NodeJS and Babel/Webpack.