Use API in Your Dapp

Dapps, or decentralized applications, are built on blockchain technology, enabling users to interact with smart contracts deployed on a specific blockchain protocol.

Each Dapp requires a blockchain API endpoint to carry out its functions. This endpoint serves as a bridge, facilitating communication between your Dapps and the underlying blockchain protocol.

Here, we'll use Ethereum as an example to demonstrate how to utilize the InfStones Fast API endpoint within a Dapp.

1 Set Up Environment

a) You will need Node.js and npm installed. Create a new directory for your project and initialize a new npm project:

mkdir my-dapp
cd my-dapp
npm init -y

b) Install the web3.js library, which will be used to interact with Ethereum:

npm install web3

2 Connect to Protocol Network

Use InfStones Ethereum API endpoint to connect to Ethereum network.

Create a file index.js and import the web3 library:

const Web3 = require('web3');

const providerUrl = "<ethereum_endpoint>"; // Replace this with your Ethereum Fast API endpoint

const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl));

3 Interact with Protocol Network

Now you can interact with Ethereum network using web3.js. Here are some examples:

3.1 Query Balance

const address = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"; // Example address

web3.eth.getBalance(address).then(balance => {
  console.log(`Balance: ${web3.utils.fromWei(balance, 'ether')} ETH`);
});

3.2 Send Transaction

🚧

Make sure to have the private key of the sending address. It's crucial to keep this secure.

const privateKey = '<your_private_key>';
const account = web3.eth.accounts.privateKeyToAccount(privateKey);

const tx = {
  from:'<sender_address>',
  to: '<recipient_address>',
  value: web3.utils.toWei('0.1', 'ether'),
  gas: 2000000
};

web3.eth.accounts.signTransaction(tx, privateKey).then(signedTx => {
  web3.eth.sendSignedTransaction(signedTx.rawTransaction).on('receipt', receipt => {
    console.log('Transaction sent successfully:', receipt);
  });
});

4 Build Your Dapp

You can now build your Dapp using this connection, sending and receiving transactions, deploying smart contracts, and more.

Remember to keep all sensitive information like private keys and InfStones Fast API endpoints secure, especially in production environments.

5 Supported Protocols

We are now supporting using the API in your Dapp on the following blockchain protocols.