Charged Particles
  • What is Charged Particles
  • GETTING STARTED
    • Quick Start
  • CHARGED PARTICLES PROTOCOL
    • Protocol Overview
      • How It Works
      • Current Status
      • Protocol Revenue
      • Protocol Features
    • Why Use Charged Particles?
      • NFT Platforms And How They Differ
      • Charged Particles NFTs - What Are They?
    • How to Use Charged Particles
      • Minting an NFT
      • Energizing an NFT
      • Manage an NFT
      • Using A Test Network
      • Migrating Particles from V1 to V2
    • Developer Docs
      • System Overview
      • Quickstart
        • Read and Write to Contracts
        • Read from the Subgraph
        • Get Kovan ETH + ERC20s
        • VSCode Node.js Typescript Debugging
      • Smart Contracts
        • V2
          • Charged Particles Contract (V2)
          • Charged Settings Contract (V2)
          • Charged State Contract (V2)
          • ProtonB Contract
        • V1
          • Charged Particles Contract
          • Charged Settings Contract
          • Charged State Contract
          • Proton Contract
        • Error Codes
      • Protocol Subgraph
    • IONX
      • IONX Token Addresses
      • IONX Rewards
    • Web3 Packs
    • Liquidity Mining
      • Base Reward Program
      • Leptons
    • Protocol Governance
      • Governance Process
      • Creating a Proposal
      • Voting
      • Delegating Votes
    • Projects + Use Cases
    • Allowlist Application
    • Team
    • Guilds
    • Roadmap
    • Hackathons
      • Hackathon Ideas
      • Previous Hackathons
    • Dune Analytics
  • SDK
    • Charged Particles SDK Overview
    • Quick Start
    • API
    • Advanced Use
    • Types
    • Common Issues / FAQ
    • Common Terminology
  • RESOURCES
    • Hiring!
      • Full-stack/Back-end Web3 Engineer
      • Create Your Own Position
    • Glossary of Terms
    • FAQs
      • Protocol and app.charged.fi
      • Governance
      • Other
      • How to report a bug
    • External Contracts Allowlisted
    • Audits
    • Legal
      • Beta End User License Agreement (EULA)
      • Privacy Policy
      • Policies & Terms of Service
    • Media Kit
  • Additional Resources
    • Discord
    • Telegram
    • Twitter
    • Medium
    • Github
    • LinkedIn
    • Instagram
    • TikTok
Powered by GitBook
On this page

Was this helpful?

  1. CHARGED PARTICLES PROTOCOL
  2. Developer Docs
  3. Quickstart

Read and Write to Contracts

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

PreviousQuickstartNextRead from the Subgraph

Last updated 4 years ago

Was this helpful?

Read and Write: Web3.js

To help your interact with the contracts our contains the that you will need to load into 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 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
  })
repository of the subgraph
ABI
Web3
AlchemyAPI