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:
IPC (uses local filesystem: fastest and most secure)
Websockets (works remotely, faster than HTTP)
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 )
require('dotenv').config({ path:`.env.${process.env.NODE_ENV||'development'}`,});constWeb3=require('web3');constkovanAddresses=require('@charged-particles/protocol-subgraph/networks/kovan');constRPC_URL=process.env.RPC_URL;constreadOnlyProvider=newWeb3.providers.HttpProvider(RPC_URL);constreadOnlyWeb3=newWeb3(readOnlyProvider);// These 2 are required to be able to craft a ProtonNFT web3 read onlyconstprotonAbi=require('@charged-particles/protocol-subgraph/abis/Proton');constprotonAddress=kovanAddresses.proton.address;constreadOnlyProtonContract=newreadOnlyWeb3.eth.Contract(protonAbi, protonAddress);constgetTotalProtons=async() => {constmethodToCall='totalSupply';constlastId=awaitreadOnlyProtonContract.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 onlyconstchargedParticlesAbi=require('@charged-particles/protocol-subgraph/abis/ChargedParticles');constchargedParticlesAddress=kovanAddresses.chargedParticles.address;constreadOnlychargedParticlesContract=newreadOnlyWeb3.eth.Contract(chargedParticlesAbi, chargedParticlesAddress);// Signature: currentParticleCharge(address,uint256,string,address)(uint256) constgetCurrentCharge=async({tokenId, managerId, underlyingAssetAddress}) => {// Params: https://docs.charged.fi/charged-particles-protocol/smart-contracts-documentation/contracts/smart-contracts-documentation#currentparticlechargeconstmethodToCall='currentParticleCharge'; constargs= [ protonAddress,// address contractAddress, tokenId,// uint256 tokenId, managerId,// string managerId, underlyingAssetAddress,// address assetToken ];constcurrentCharge=awaitreadOnlychargedParticlesContract.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 #26getCurrentCharge({ 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 })