Ontosight® – Biweekly NewsletterJune 17th – June 30th, 2024 –Read More
dApp development on EOS Blockchain using EOSJS and scatter
As I have been diving deep into the development of EOS dApp, I have come across so many good articles. Here, I am aggregating all the knowledge I got after doing some research. In this article, I will be explaining how to use EOSJS and scatter together. I am assuming you have a basic understanding about smart contracts and how to deploy them on EOS blockchain as I’ll be skipping that part in this article.
What are we building?
We are building a simple todo dApp. We will write a smart contract for the CRUD (create, read, update, and delete) operations and will interact with deployed contract using EOSJS and scatter. CRUD operations include creating, completing, removing and getting todos. We will use Jungle Testnet for deploying our smart contract.
Pre requisite knowledge
- EOS
- EOSJS
- Scatter
Scatter setup
Scatter is used to sign transactions for blockchain and provide personal information to applications without ever exposing your keys. For setting up your scatter wallet, follow this video. In scatter settings, you have to add the jungle testnet in networks with the following details:
Domain or IP: dev.cryptolions.io // It might be changed, so check for the latest one
Port: 38888
chainId:038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca
After adding the network configuration, now import your private key into the wallet by going into key pair section then click on new. Fill in the key information according to the form as shown in the pic below.
Now you should add an identity using your key pair. Go to identities section and add or edit existing identity if it is of no use. In the identity section, select the network and then key pair, it will ask you to add your account associated with that key on the chain net. You should add the account with active permission.
Your scatter is all set up and ready to be used in our dApp.
Smart contract
For deploying the todo smart contract, follow this article and deploy it on Jungle Testnet. Make sure you are able to interact with Testnet from the command line as mentioned in the article.
Interacting with the Testnet
I am using ReactJS for the frontend part. The complete logic and flow is in a single file named index.jsx inside the src folder. Following is the configuration object:
const EOS_CONFIG = {
contractName: “xyz”, // Contract name
contractSender: “xyz”, // User executing the contract (should be paired with private key)
network: {
protocol: “http”,
blockchain: “eos”,
host: “dev.cryptolions.io”,
port: 38888,
chainId: “038f4b0fc8ff18a4f0842a8f0564611f6e96e8535901dd45e43ac8691a1c4dca” // get this using http://dev.cryptolions.io:38888/v1/chain/get_info
},
eosOptions: {}
};
Interacting with scatter:
document.addEventListener(`scatterLoaded`, this.onScatterLoad);
onScatterLoad = () => {
const scatter = window.scatter;
window.scatter = null;
// Here, we are connecting scatter with eosjs so that the transactions can be signed using keys present in scatter
this.eosClient = scatter.eos(
EOS_CONFIG.network,
EOS,
EOS_CONFIG.eosOptions,
EOS_CONFIG.network.protocol
);
// scatter object to collect the information present in wallet like accounts or public key
this.scatter = scatter;
// to load the data present in our table
this.loadTodos();
};
Now, in this object we have two references EOSClient and scatter which we will be using to interact with EOS blockchain and wallet respectively.
I am adding the code for one functionality to get the stored data (all the todos) using EOSClient, the rest of the functionalities you can check in the src/index.jsx
this.eosClient.getTableRows({
code: EOS_CONFIG.contractName,
scope: EOS_CONFIG.contractName,
table: “todos”,
json: true
}).then(data => {
this.setState({ todos: data.rows });
}).catch(e => {
console.error(e);
});
}
To get the account, use getIdentity() of scatter object:
accounts: [config.EOS_CONFIG.network]
});
That’s it.
Conclusion
One of the biggest advantages of this is you don’t have to maintain a wallet on your machine, scatter manages everything for you. There are other methods also to host the wallet but scatter gives the responsibility to end user and nothing private needs to be handled by the developer.
Resources and references
- To do contract deployment (https://medium.com/r/?url=https%3A%2F%2Fsteemit.com%2Feos%2F%40eos-asia%2Fpart-2-building-a-to-do-list-with-eos-or-working-with-persistent-data-in-eos)
- Scatter setup (https://medium.com/r/?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DQcbCf5mm_Ek)
- Todo contract without scatter integration (https://medium.com/r/?url=https%3A%2F%2Fgithub.com%2Feosasia%2Feos-todo)
- EOS Stack Exchange (https://medium.com/r/?url=https%3A%2F%2Feosio.stackexchange.com%2F)
- EOS developers (https://medium.com/r/?url=https%3A%2F%2Fdevelopers.eos.io%2F)
Featured Blogs
Machine learning as an indispensable tool for Biopharma
The cost of developing a new drug roughly doubles every nine years (inflation-adjusted) aka Eroom’s law. As the volume of data…
Find biological associations between ‘never thought before to be linked’
There was a time when science depended on manual efforts by scientists and researchers. Then, came an avalanche of data…
Find key opinion leaders and influencers to drive your therapy’s
Collaboration with key opinion leaders and influencers becomes crucial at various stages of the drug development chain. When a pharmaceutical…
Impact of AI and Digitalization on R&D in Biopharmaceutical Industry
Data are not the new gold – but the ability to put them together in a relevant and analyzable way…
Why AI Is a Practical Solution for Pharma
Artificial intelligence, or AI, is gaining more attention in the pharma space these days. At one time evoking images from…
How can AI help in Transforming the Drug Development Cycle?
Artificial intelligence (AI) is transforming the pharmaceutical industry with extraordinary innovations that are automating processes at every stage of drug…
How Will AI Disrupt the Pharma Industry?
There is a lot of buzz these days about how artificial intelligence (AI) is going to disrupt the pharmaceutical industry….
Revolutionizing Drug Discovery with AI-Powered Solutions
Drug discovery plays a key role in the pharma and biotech industries. Discovering unmet needs, pinpointing the target, identifying the…
Leveraging the Role of AI for More Successful Clinical Trials
The pharmaceutical industry spends billions on R&D each year. Clinical trials require tremendous amounts of effort, from identifying sites and…
Understanding the Language of Life Sciences
Training algorithms to identify and extract Life Sciences-specific data The English dictionary is full of words and definitions that can be…
Understanding the Computer Vision Technology
The early 1970s introduced the world to the idea of computer vision, a promising technology automating tasks that would otherwise…
AI Is All Hype If We Don’t Have Access to
Summary: AI could potentially speed drug discovery and save time in rejecting treatments that are unlikely to yield worthwhile resultsAI has…