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.
1
npm i Web3 @charged-particles/protocol-subgraph dotenv
Copied!
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 )
1
RPC_URL=YOUR_RPC_URL_FOR_KOVAN_HERE_WE_RECOMMEND_ALCHEMY_API
Copied!
index.js
1
require('dotenv').config({
2
path: `.env.${process.env.NODE_ENV || 'development'}`,
3
});
4
5
const Web3 = require('web3');
6
7
const kovanAddresses = require('@charged-particles/protocol-subgraph/networks/kovan');
8
9
const RPC_URL = process.env.RPC_URL;
10
11
const readOnlyProvider = new Web3.providers.HttpProvider(RPC_URL);
12
const readOnlyWeb3 = new Web3(readOnlyProvider);
13
14
// These 2 are required to be able to craft a ProtonNFT web3 read only
15
const protonAbi = require('@charged-particles/protocol-subgraph/abis/Proton');
16
const protonAddress = kovanAddresses.proton.address;
17
18
const readOnlyProtonContract = new readOnlyWeb3.eth.Contract(protonAbi, protonAddress);
19
20
const getTotalProtons = async() => {
21
22
const methodToCall = 'totalSupply';
23
const lastId = await readOnlyProtonContract.methods[methodToCall]().call();
24
25
console.log({protonAddress, lastId});
26
return lastId;
27
}
28
29
getTotalProtons()
30
.catch(err => console.error(err));
31
32
// These 2 are required to be able to craft a ChargedParticles contract web3 read only
33
const chargedParticlesAbi = require('@charged-particles/protocol-subgraph/abis/ChargedParticles');
34
const chargedParticlesAddress = kovanAddresses.chargedParticles.address;
35
const readOnlychargedParticlesContract = new readOnlyWeb3.eth.Contract(chargedParticlesAbi, chargedParticlesAddress);
36
37
// Signature: currentParticleCharge(address,uint256,string,address)(uint256)
38
const getCurrentCharge = async({tokenId, managerId, underlyingAssetAddress}) => {
39
40
// Params: https://docs.charged.fi/charged-particles-protocol/smart-contracts-documentation/contracts/smart-contracts-documentation#currentparticlecharge
41
const methodToCall = 'currentParticleCharge';
42
43
const args = [
44
protonAddress, // address contractAddress,
45
tokenId, // uint256 tokenId,
46
managerId, // string managerId,
47
underlyingAssetAddress, // address assetToken
48
];
49
50
const currentCharge = await readOnlychargedParticlesContract.methods[methodToCall](...args).call();
51
52
console.log({currentCharge});
53
return currentCharge;
54
}
55
56
// Now let's look at the charge generated on
57
// https://staging.charged.fi/go/energize/0xD4F7389297d9cea850777EA6ccBD7Db5817a12b2/26
58
// tokenId: 26
59
60
// Getting the charge of the WETH for the Proton #26
61
getCurrentCharge({
62
tokenId: 26, // https://staging.charged.fi/go/energize/0xD4F7389297d9cea850777EA6ccBD7Db5817a12b2/26
63
managerId: 'aave', // or 'generic' if looking for an ERC20 balance, not supported by AAVE lending
64
underlyingAssetAddress: '0xd0a1e359811322d97991e03f863a0c30c2cf029c' // Kovan WETH
65
})
Copied!
Last modified 6mo ago
Copy link