Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created November 20, 2018 06:31
Show Gist options
  • Select an option

  • Save miguelmota/c2a12cb4ed938d2d6f93146903390065 to your computer and use it in GitHub Desktop.

Select an option

Save miguelmota/c2a12cb4ed938d2d6f93146903390065 to your computer and use it in GitHub Desktop.
JavaScript Scatter EOSIO (eosjs) example
const ScatterJS = require('scatterjs-core').default
const ScatterEOS = require('scatterjs-plugin-eosjs').default
const Eos = require('eosjs')
ScatterJS.plugins(new ScatterEOS())
const connectionOptions = {
initTimeout: 10000
}
const network = {
blockchain: 'eos',
protocol: 'https',
host: 'api-kylin.eosasia.one',
port: 443,
chainId: '5fff1dae8dc8e2fc4d5b23b2c7665c97f9e9d8edf2b6485a86ba311c25639191'
}
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');
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 transactionOptions = {
authorization: [`${account.name}@${account.authority}`]
};
// example of sending EOS
eos.transfer(account.name, 'helloworld54', `1.0000 EOS`, 'memo', transactionOptions).then(trx => {
console.log(`Transaction ID: ${trx.transaction_id}`);
}).catch(error => {
console.error(error);
});
// example of pushing an action
eos.transaction({
actions: [{
account: 'helloworld54',
name: 'placeorder',
authorization: [{
actor: 'myaccount123',
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)
})
})
@ekkis

ekkis commented Jan 27, 2020

Copy link
Copy Markdown

Miguel, thank you for putting this together. It's truly shameful that the Scatter authors cannot provide simple documentation on their product. I have a question: eos.transaction is exactly what I need but is eos in this case just the same as Eos.api? or is a Scatter thing, because I don't find docs on it at all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment