Track Pool Rewards
Learn how to track rewards accrued from Compound or Aave V2 through Morpho, onchain via a smart contract & offchain via ethers.js.
Anyone can, at anytime, query Morpho's Lens to get information about the rewards from Compound or Aave V2 accrued with a position on Morpho. Here are concrete examples 👇
Morpho Aave Optimizer Instances​
There are currently no rewards distributed by Aave V2, or Aave V3.
Morpho CompoundV2 Optimizer​
- Solidity
- ethers.js
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity ^0.8.16;
import {ILens} from "@morpho-org/morpho-optimizers/contracts/compound/interfaces/ILens.sol";
contract MorphoCompoundSupplier {
address public constant CWBTC = 0xC11b1268C1A384e55C48c2391d8d480264A3A7F4;
address public constant LENS = 0x930f1b46e1D081Ec1524efD95752bE3eCe51EF67;
function getUnclaimedComp() external view returns (uint256) {
address[] memory markets = new address[](1);
markets[0] = CDAI; // the DAI market, represented by the cDAI ERC20 token
return
ILens(LENS).getUserUnclaimedRewards(
markets, // the markets to query unclaimed COMP rewards on
address(this) // the address of the user you want to query unclaimed COMP rewards of
);
}
}
import ethers from "ethers";
const signer = new ethers.Wallet(
process.env.PRIVATE_KEY,
new ethers.providers.JsonRpcBatchProvider(process.env.RPC_URL)
);
const signerAddress = await signer.getAddress();
const cDaiAddress = "0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643";
const cCompAddress = "0x70e36f6BF80a52b3B46b3aF8e106CC0ed743E8e4";
const compDecimals = 18;
const lens = new ethers.Contract(
"0x930f1b46e1D081Ec1524efD95752bE3eCe51EF67",
[
"function getUserUnclaimedRewards(address[] calldata, address) external view returns (uint256)",
],
signer
);
const oracle = new ethers.Contract(
"0x65c816077C29b557BEE980ae3cC2dCE80204A0C5",
["function getUnderlyingPrice(address) external view returns (uint256)"],
signer
);
async function getUnclaimedComp() {
const compRewards = await lens.getUserUnclaimedRewards(
[cDaiAddress], // the markets to query unclaimed COMP rewards on
signerAddress // the address of the user you want to query unclaimed COMP rewards of
);
return Number(ethers.utils.formatUnits(compRewards, compDecimals));
}
async function getUnclaimedCompUSD() {
const compRewards = await getUnclaimedComp(
[cDaiAddress], // the markets to query unclaimed COMP rewards on
signerAddress // the address of the user you want to query unclaimed COMP rewards of
);
const oraclePrice = await oracle.getUnderlyingPrice(cCompAddress); // in (36 - nb decimals of WBTC = 28) decimals
return (
compRewards *
Number(ethers.utils.formatUnits(oraclePrice, 36 - compDecimals))
);
}
getUnclaimedComp().then((val) => console.log("unclaimed COMP rewards", val));
getUnclaimedCompUSD().then((val) =>
console.log("unclaimed COMP rewards USD", val)
);
ERC4626 Vaults​
Morpho's vaults are another way to interact with Morpho according to the ERC4626 standard.
- Solidity
- ethers.js
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity ^0.8.13;
import {ISupplyVault} from "@morpho-org/morpho-tokenized-vaults/src/compound/interfaces/ISupplyVault.sol";
contract MorphoCompoundVaultSupplier {
address public constant MC_DAI = 0xd99D793B8FDaE42C1867293C172b9CBBD3ea49FF;
/// @notice Returns the amount of rewards this contract accrued through the vault.
/// @return unclaimed The amount of COMP rewards this contract accrued through the vault.
function getUnclaimedRewards() public view returns (uint256 unclaimed) {
(, unclaimed) = ISupplyVault(MC_DAI).userRewards(address(this));
}
}
import ethers from "ethers";
const signer = new ethers.Wallet(
process.env.PRIVATE_KEY,
new ethers.providers.JsonRpcBatchProvider(process.env.RPC_URL)
);
const daiDecimals = 18;
const compDecimals = 18;
const mcDai = new ethers.Contract(
"0xd99D793B8FDaE42C1867293C172b9CBBD3ea49FF",
SupplyVaultAbi,
signer
);
async function getUnclaimedRewards(address) {
const rewardsData = await mcDai.userRewards(address);
return Number(ethers.utils.formatUnits(rewardsData.unclaimed, compDecimals));
}
getUnclaimedRewards(signer.address).then((val) =>
console.log("Total COMP rewards", val)
);