Created
November 20, 2018 06:31
-
-
Save miguelmota/c2a12cb4ed938d2d6f93146903390065 to your computer and use it in GitHub Desktop.
JavaScript Scatter EOSIO (eosjs) example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 iseos
in this case just the same asEos.api
? or is a Scatter thing, because I don't find docs on it at all