Get the value of COMP earned for an address that uses the Compound protocol. This can be done using the Lens contract with JSON RPC or another Smart Contract. If you do an eth_call
with JSON RPC, it is free (no gas costs).
- Use the
getCompBalanceMetadataExt
method in the Lens contract https://etherscan.io/address/0xdCbDb7306c6Ff46f77B349188dC18cEd9DF30299#code - Lens address is posted here https://github.com/compound-finance/compound-protocol/blob/master/networks/mainnet.json#L17
- Here is the definition https://github.com/compound-finance/compound-protocol/blob/master/contracts/Lens/CompoundLens.sol#L278
- pass: COMP contract address, Comptroller contract address, and the address you want to know the amount of COMP earned
There is an API endpoint hosted by Compound Labs. This data is slightly behind the blockchain information with respect to time. Use URL parameters to provide the address
that you wish to retrieve the COMP accrued value.
HTTP GET https://api.compound.finance/api/v2/governance/comp/account?address=0x123123123123abcabcabcabcabcab
Use the returned comp_allocated
value in the JSON object response.
Alternatively there is a cool tool from KZen networks that does the same thing. https://github.com/KZen-networks/compound-playground#comp
KZen also created the COMP checker page: https://kzen-networks.github.io/comp-checker/
Follow script below to get comp accrued using compound lens: //check unclaimed comp and claimed comp in ropsten network
const hre = require("hardhat"); async function main() { const CompAddress = "0xf76D4a441E4ba86A923ce32B89AFF89dBccAA075"; const CompAbi = require("../abi/Comp.json"); const Comp = new hre.ethers.Contract( CompAddress, CompAbi, ethers.provider.getSigner() ); const CompoundLensAddress = "0xA9ddAb1032F224B510f7Bf002E0E75BA49E78755"; const CompoundLensAbi = require("../abi/CompoundLens.json"); const CompoundLens = new hre.ethers.Contract( CompoundLensAddress, CompoundLensAbi, ethers.provider.getSigner() ); console.log("Comp balance",Comp.address); console.log("Comp balance of the address is ",await Comp.balanceOf("address_to_check")); console.log("Comp balance from lens",await CompoundLens.connect(ethers.provider.getSigner()).callStatic.getCompBalanceMetadataExt("0xf76D4a441E4ba86A923ce32B89AFF89dBccAA075","0xcfa7b0e37f5AC60f3ae25226F5e39ec59AD26152",address_to_check)) } // We recommend this pattern to be able to use async/await everywhere // and properly handle errors. main().catch((error) => { console.error(error); process.exitCode = 1; });
//check unclaimed comp and claimed comp in ropsten network
const hre = require("hardhat");
async function main() {
const CompAddress = "0xf76D4a441E4ba86A923ce32B89AFF89dBccAA075";
const CompAbi = require("../abi/Comp.json");
const Comp = new hre.ethers.Contract(
CompAddress,
CompAbi,
ethers.provider.getSigner()
);
const CompoundLensAddress = "0xA9ddAb1032F224B510f7Bf002E0E75BA49E78755";
const CompoundLensAbi = require("../abi/CompoundLens.json");
const CompoundLens = new hre.ethers.Contract(
CompoundLensAddress,
CompoundLensAbi,
ethers.provider.getSigner()
);
console.log("Comp balance",Comp.address);
console.log("Comp balance of the address is ",await Comp.balanceOf("address_to_check"));
console.log("Comp balance from lens",await CompoundLens.connect(ethers.provider.getSigner()).callStatic.getCompBalanceMetadataExt("0xf76D4a441E4ba86A923ce32B89AFF89dBccAA075","0xcfa7b0e37f5AC60f3ae25226F5e39ec59AD26152",address_to_check))
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});