Skip to content

Instantly share code, notes, and snippets.

@ajb413
ajb413 / comp_earned.md
Last active July 27, 2023 15:19
How do I retrieve the "COMP earned" value from the Compound protocol? Get the amount of accrued COMP token for an address using the Ethereum blockchain.

How do I retrieve the "COMP earned" value?

With a Smart Contract or JSON RPC

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).

  1. Use the getCompBalanceMetadataExt method in the Lens contract https://etherscan.io/address/0xdCbDb7306c6Ff46f77B349188dC18cEd9DF30299#code
  2. Lens address is posted here https://github.com/compound-finance/compound-protocol/blob/master/networks/mainnet.json#L31
  3. Here is the definition https://github.com/compound-finance/compound-protocol/blob/master/contracts/Lens/CompoundLens.sol#L453
pragma solidity ^0.5.16;
pragma experimental ABIEncoderV2;
interface Comp {
function delegateBySig(
address delegatee,
uint nonce,
uint expiry,
uint8 v,
bytes32 r,
const myWalletAddress = web3.eth.accounts.wallet[0].address;
var batch = new web3.BatchRequest();
signatures.forEach((signature) => {
const { delegatee, nonce, expiry, v, r, s } = signature;
batch.add(comp.methods.delegateBySig(delegatee, nonce, expiry, v, r, s).send.request(
{
from: myWalletAddress,
gasLimit: web3.utils.toHex(1000000),
gasPrice: web3.utils.toHex(25000000000),
},
const createVoteBySigMessage = (govAddress, proposalId, support, chainId = 1) => {
const types = {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Ballot: [
{ name: 'proposalId', type: 'uint256' },
{ name: 'support', type: 'uint8' }
const sig = signature.value;
const r = '0x' + sig.substring(2).substring(0, 64);
const s = '0x' + sig.substring(2).substring(64, 128);
const v = '0x' + sig.substring(2).substring(128, 130);
const createDelegateBySigMessage = (compAddress, delegatee, expiry = 10e9, chainId = 1, nonce = 0) => {
const types = {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Delegation: [
{ name: 'delegatee', type: 'address' },
{ name: 'nonce', type: 'uint256' },
sign.onclick = async () => {
const _delegatee = delegateTo.value;
const _nonce = await comp.methods.nonces(myAccount).call();
const _expiry = 10e9; // expiration of signature, in seconds since unix epoch
const _chainId = window.ethereum.chainId.toString();
const msgParams = createDelegateBySigMessage(compAddress, _delegatee, _expiry, _chainId, _nonce);
window.ethereum.sendAsync({
method: 'eth_signTypedData_v4',
params: [ myAccount, msgParams ],
const Web3 = require('web3');
const web3 = new Web3(`https://mainnet.infura.io/v3/${process.env.infuraApiKey}`);
const cUsdtAddress = '0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9';
const cUsdtAddressAbi = [{"inputs":[{"internalType":"address","name":"underlying_","type":"address"},{"internalType":"contract ComptrollerInterface","name":"comptroller_","type":"address"},{"internalType":"contract InterestRateModel","name":"interestRateModel_","type":"address"},{"internalType":"uint256","name":"initialExchangeRateMantissa_","type":"uint256"},{"internalType":"string","name":"name_","type":"string"},{"internalType":"string","name":"symbol_","type":"string"},{"internalType":"uint8","name":"decimals_","type":"uint8"},{"internalType":"address payable","name":"admin_","type":"address"},{"internalType":"address","name":"implementation_","type":"address"},{"internalType":"bytes","name":"becomeImplementationData","type":"bytes"}],"payable":false,"stateMutability":"nonpayable","type":"constructor","signature":"constructor"}
@ajb413
ajb413 / cast_vote.js
Last active August 17, 2021 22:22
Example for casting a vote in Compound community governance. https://github.com/compound-developers/compound-governance-examples
governanceAbi = window.governanceAbi;
gov = new web3.eth.Contract(governanceAbi, governanceAddress);
// Add a list of active proposals to the UI
getProposals();
// Get my account's vote weight
getMyVoteWeight(myAccount);
submit.onclick = async () => {
@ajb413
ajb413 / delegate.js
Last active August 17, 2021 22:20
Example to delegate your Compound Governance voting rights on the Ropsten network using web3. https://github.com/compound-developers/compound-governance-examples
compAbi = window.compAbi;
comp = new web3.eth.Contract(compAbi, compAddress);
let currentDelegate;
try {
currentDelegate = await comp.methods.delegates(myAccount).call();
} catch (e) {
currentDelegate = 0;
}