Read and Write to Contracts

Examples of how to Read and Write to Charged Particles contracts using Web3.js

Read and Write: Web3.js

To help your interact with the contracts our repository of the subgraph contains the ABI that you will need to load into Web3 to query the data.

npm i Web3 @charged-particles/protocol-subgraph dotenv

The most common ways to connect to an ethereum node are:

  1. IPC (uses local filesystem: fastest and most secure)

  2. Websockets (works remotely, faster than HTTP)

  3. HTTP (more nodes support it)

For now you will need a .env.development file with one variable, containing a RPC url so Web3 can connect to the ethereum blockchain. (grab one from AlchemyAPI it will look like https://eth-{network)/v2/sOmeThing--HeRe )

RPC_URL=YOUR_RPC_URL_FOR_KOVAN_HERE_WE_RECOMMEND_ALCHEMY_API
index.js
require('dotenv').config({
  path: `.env.${process.env.NODE_ENV || 'development'}`,
});

const Web3 = require('web3');

const kovanAddresses = require('@charged-particles/protocol-subgraph/networks/kovan');

const RPC_URL = process.env.RPC_URL;

const readOnlyProvider =  new Web3.providers.HttpProvider(RPC_URL);
const readOnlyWeb3 = new Web3(readOnlyProvider);

// These 2 are required to be able to craft a ProtonNFT web3 read only
const protonAbi = require('@charged-particles/protocol-subgraph/abis/Proton');
const protonAddress = kovanAddresses.proton.address;

const readOnlyProtonContract = new readOnlyWeb3.eth.Contract(protonAbi, protonAddress);

const getTotalProtons = async() => {

  const methodToCall = 'totalSupply';
  const lastId = await readOnlyProtonContract.methods[methodToCall]().call();

  console.log({protonAddress, lastId});
  return lastId;
}

getTotalProtons()
  .catch(err => console.error(err));

  // These 2 are required to be able to craft a ChargedParticles contract web3 read only
  const chargedParticlesAbi = require('@charged-particles/protocol-subgraph/abis/ChargedParticles');
  const chargedParticlesAddress = kovanAddresses.chargedParticles.address;
  const readOnlychargedParticlesContract = new readOnlyWeb3.eth.Contract(chargedParticlesAbi, chargedParticlesAddress);

  // Signature: currentParticleCharge(address,uint256,string,address)(uint256) 
  const getCurrentCharge = async({tokenId, managerId, underlyingAssetAddress}) => {
    
    // Params: https://docs.charged.fi/charged-particles-protocol/smart-contracts-documentation/contracts/smart-contracts-documentation#currentparticlecharge
    const methodToCall = 'currentParticleCharge'; 

    const args = [
        protonAddress,          // address contractAddress,
        tokenId,                // uint256 tokenId,
        managerId,              // string managerId,
        underlyingAssetAddress, // address assetToken
    ];

    const currentCharge = await readOnlychargedParticlesContract.methods[methodToCall](...args).call();

    console.log({currentCharge});
    return currentCharge;
  }

 // Now let's look at the charge generated on
 // https://staging.charged.fi/go/energize/0xD4F7389297d9cea850777EA6ccBD7Db5817a12b2/26
 // tokenId: 26

  // Getting the charge of the WETH for the Proton #26
  getCurrentCharge({
    tokenId: 26,       // https://staging.charged.fi/go/energize/0xD4F7389297d9cea850777EA6ccBD7Db5817a12b2/26
    managerId: 'aave', // or 'generic' if looking for an ERC20 balance, not supported by AAVE lending
    underlyingAssetAddress:  '0xd0a1e359811322d97991e03f863a0c30c2cf029c' // Kovan WETH
  })

Last updated