From 165b09e45cb8b2124170c1b32daa0f1578ed7a73 Mon Sep 17 00:00:00 2001 From: q1q0 Date: Wed, 24 May 2023 09:13:56 -0400 Subject: [PATCH] add compound v3 reward --- .../connectors/compound/v3-rewards/events.sol | 20 + .../compound/v3-rewards/helpers.sol | 13 + .../compound/v3-rewards/interface.sol | 37 ++ .../connectors/compound/v3-rewards/main.sol | 68 +++ .../compound/compound.iii.rewards.test.ts | 444 ++++++++++++++++++ 5 files changed, 582 insertions(+) create mode 100644 contracts/arbitrum/connectors/compound/v3-rewards/events.sol create mode 100644 contracts/arbitrum/connectors/compound/v3-rewards/helpers.sol create mode 100644 contracts/arbitrum/connectors/compound/v3-rewards/interface.sol create mode 100644 contracts/arbitrum/connectors/compound/v3-rewards/main.sol create mode 100644 test/arbitrum/compound/compound.iii.rewards.test.ts diff --git a/contracts/arbitrum/connectors/compound/v3-rewards/events.sol b/contracts/arbitrum/connectors/compound/v3-rewards/events.sol new file mode 100644 index 00000000..81754e7c --- /dev/null +++ b/contracts/arbitrum/connectors/compound/v3-rewards/events.sol @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.7.6; +pragma experimental ABIEncoderV2; + +contract Events { + event LogRewardsClaimed( + address indexed market, + address indexed account, + uint256 indexed rewardsClaimed, + uint256 setId + ); + + event LogRewardsClaimedOnBehalf( + address indexed market, + address indexed owner, + address to, + uint256 indexed rewardsClaimed, + uint256 setId + ); +} diff --git a/contracts/arbitrum/connectors/compound/v3-rewards/helpers.sol b/contracts/arbitrum/connectors/compound/v3-rewards/helpers.sol new file mode 100644 index 00000000..cf03f9dd --- /dev/null +++ b/contracts/arbitrum/connectors/compound/v3-rewards/helpers.sol @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma abicoder v2; + +import { TokenInterface } from "../../../common/interfaces.sol"; +import { DSMath } from "../../../common/math.sol"; +import { Basic } from "../../../common/basic.sol"; +import { CometRewards } from "./interface.sol"; + +abstract contract Helpers is DSMath, Basic { + CometRewards internal constant cometRewards = + CometRewards(0x88730d254A2f7e6AC8388c3198aFd694bA9f7fae); +} diff --git a/contracts/arbitrum/connectors/compound/v3-rewards/interface.sol b/contracts/arbitrum/connectors/compound/v3-rewards/interface.sol new file mode 100644 index 00000000..b2157255 --- /dev/null +++ b/contracts/arbitrum/connectors/compound/v3-rewards/interface.sol @@ -0,0 +1,37 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma abicoder v2; + +struct UserCollateral { + uint128 balance; + uint128 _reserved; +} + +struct RewardOwed { + address token; + uint256 owed; +} + +interface CometRewards { + function claim( + address comet, + address src, + bool shouldAccrue + ) external; + + function claimTo( + address comet, + address src, + address to, + bool shouldAccrue + ) external; + + function getRewardOwed(address comet, address account) + external + returns (RewardOwed memory); + + function rewardsClaimed(address cometProxy, address account) + external + view + returns (uint256); +} diff --git a/contracts/arbitrum/connectors/compound/v3-rewards/main.sol b/contracts/arbitrum/connectors/compound/v3-rewards/main.sol new file mode 100644 index 00000000..53d0118b --- /dev/null +++ b/contracts/arbitrum/connectors/compound/v3-rewards/main.sol @@ -0,0 +1,68 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +/** + * @title Compound. + * @dev Rewards. + */ + +import { TokenInterface } from "../../../common/interfaces.sol"; +import { Stores } from "../../../common/stores.sol"; +import { Helpers } from "./helpers.sol"; +import { Events } from "./events.sol"; + +abstract contract CompoundV3RewardsResolver is Events, Helpers { + /** + * @dev Claim rewards and interests accrued in supplied/borrowed base asset. + * @notice Claim rewards and interests accrued. + * @param market The address of the market. + * @param setId ID stores the amount of rewards claimed. + */ + function claimRewards( + address market, + uint256 setId + ) public returns (string memory eventName_, bytes memory eventParam_) { + uint256 rewardsOwed = cometRewards.getRewardOwed(market, address(this)).owed; + cometRewards.claim(market, address(this), true); + + setUint(setId, rewardsOwed); + + eventName_ = "LogRewardsClaimed(address,address,uint256,uint256)"; + eventParam_ = abi.encode(market, address(this), rewardsOwed, setId); + } + + /** + * @dev Claim rewards and interests accrued in supplied/borrowed base asset. + * @notice Claim rewards and interests accrued and transfer to dest address. + * @param market The address of the market. + * @param owner The account of which the rewards are to be claimed. + * @param to The account where to transfer the claimed rewards. + * @param setId ID stores the amount of rewards claimed. + */ + function claimRewardsOnBehalfOf( + address market, + address owner, + address to, + uint256 setId + ) public returns (string memory eventName_, bytes memory eventParam_) { + //in reward token decimals + uint256 rewardsOwed = cometRewards.getRewardOwed(market, owner).owed; + cometRewards.claimTo(market, owner, to, true); + + setUint(setId, rewardsOwed); + + eventName_ = "LogRewardsClaimedOnBehalf(address,address,address,uint256,uint256)"; + eventParam_ = abi.encode( + market, + owner, + to, + rewardsOwed, + setId + ); + } +} + +contract ConnectV2CompoundV3ArbitrumRewards is CompoundV3RewardsResolver { + string public name = "CompoundV3Rewards-v1.0"; +} diff --git a/test/arbitrum/compound/compound.iii.rewards.test.ts b/test/arbitrum/compound/compound.iii.rewards.test.ts new file mode 100644 index 00000000..80a56c5d --- /dev/null +++ b/test/arbitrum/compound/compound.iii.rewards.test.ts @@ -0,0 +1,444 @@ +import { expect } from "chai"; +import hre from "hardhat"; +const { waffle, ethers } = hre; +const { provider, deployContract } = waffle; + +import { Signer, Contract } from "ethers"; +import { BigNumber } from "bignumber.js"; + +import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector"; +import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; +import { encodeSpells } from "../../../scripts/tests/encodeSpells"; +import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; +import { addresses } from "../../../scripts/tests/arbitrum/addresses"; +import { tokens, tokenMapping } from "../../../scripts/tests/arbitrum/tokens"; +import { abis } from "../../../scripts/constant/abis"; +import { ConnectV2CompoundV3ArbitrumRewards__factory, ConnectV2CompoundV3Arbitrum__factory } from "../../../typechain"; + +describe("Compound III Rewards", function () { + let connectorName = "COMPOUND-V3-REWARDS-TEST-A"; + const market = "0xA5EDBDD9646f8dFF606d7448e414884C7d905dCA"; + const rewards = "0x88730d254A2f7e6AC8388c3198aFd694bA9f7fae"; + const base = "0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8"; + const account = "0x62383739d68dd0f844103db8dfb05a7eded5bbe6"; + const wethWhale = "0xe50fA9b3c56FfB159cB0FCA61F5c9D750e8128c8"; + const baseWhale = "0x62383739d68dd0f844103db8dfb05a7eded5bbe6"; + + const ABI = [ + "function balanceOf(address account) public view returns (uint256)", + "function approve(address spender, uint256 amount) external returns(bool)", + "function transfer(address recipient, uint256 amount) external returns (bool)" + ]; + const wethContract = new ethers.Contract(tokens.weth.address, ABI); + const baseContract = new ethers.Contract(base, ABI); + + const cometABI = [ + { + inputs: [ + { internalType: "address", name: "comet", type: "address" }, + { internalType: "address", name: "src", type: "address" }, + { internalType: "bool", name: "shouldAccrue", type: "bool" } + ], + name: "claim", + outputs: [], + stateMutability: "nonpayable", + type: "function" + }, + { + inputs: [ + { internalType: "address", name: "comet", type: "address" }, + { internalType: "address", name: "src", type: "address" }, + { internalType: "address", name: "to", type: "address" }, + { internalType: "bool", name: "shouldAccrue", type: "bool" } + ], + name: "claimTo", + outputs: [], + stateMutability: "nonpayable", + type: "function" + }, + { + inputs: [ + { internalType: "address", name: "comet", type: "address" }, + { internalType: "address", name: "account", type: "address" } + ], + name: "getRewardOwed", + outputs: [ + { + components: [ + { internalType: "address", name: "token", type: "address" }, + { internalType: "uint256", name: "owed", type: "uint256" } + ], + internalType: "struct CometRewards.RewardOwed", + name: "", + type: "tuple" + } + ], + stateMutability: "nonpayable", + type: "function" + }, + { + inputs: [{ internalType: "address", name: "", type: "address" }], + name: "rewardConfig", + outputs: [ + { internalType: "address", name: "token", type: "address" }, + { internalType: "uint64", name: "rescaleFactor", type: "uint64" }, + { internalType: "bool", name: "shouldUpscale", type: "bool" } + ], + stateMutability: "view", + type: "function" + }, + { + inputs: [ + { internalType: "address", name: "", type: "address" }, + { internalType: "address", name: "", type: "address" } + ], + name: "rewardsClaimed", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + } + ]; + + const marketABI = [ + { + inputs: [{ internalType: "address", name: "account", type: "address" }], + name: "balanceOf", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [{ internalType: "address", name: "account", type: "address" }], + name: "borrowBalanceOf", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [], + name: "baseBorrowMin", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [], + name: "baseMinForRewards", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [], + name: "baseToken", + outputs: [{ internalType: "address", name: "", type: "address" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [], + name: "decimals", + outputs: [{ internalType: "uint8", name: "", type: "uint8" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [{ internalType: "address", name: "priceFeed", type: "address" }], + name: "getPrice", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [ + { internalType: "address", name: "owner", type: "address" }, + { internalType: "address", name: "manager", type: "address" } + ], + name: "hasPermission", + outputs: [{ internalType: "bool", name: "", type: "bool" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [], + name: "numAssets", + outputs: [{ internalType: "uint8", name: "", type: "uint8" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [ + { internalType: "address", name: "asset", type: "address" }, + { internalType: "uint256", name: "baseAmount", type: "uint256" } + ], + name: "quoteCollateral", + outputs: [{ internalType: "uint256", name: "", type: "uint256" }], + stateMutability: "view", + type: "function" + }, + { + inputs: [{ internalType: "address", name: "", type: "address" }], + name: "userBasic", + outputs: [ + { internalType: "int104", name: "principal", type: "int104" }, + { internalType: "uint64", name: "baseTrackingIndex", type: "uint64" }, + { internalType: "uint64", name: "baseTrackingAccrued", type: "uint64" }, + { internalType: "uint16", name: "assetsIn", type: "uint16" }, + { internalType: "uint8", name: "_reserved", type: "uint8" } + ], + stateMutability: "view", + type: "function" + }, + { + inputs: [ + { internalType: "address", name: "", type: "address" }, + { internalType: "address", name: "", type: "address" } + ], + name: "userCollateral", + outputs: [ + { internalType: "uint128", name: "balance", type: "uint128" }, + { internalType: "uint128", name: "_reserved", type: "uint128" } + ], + stateMutability: "view", + type: "function" + } + ]; + + let dsaWallet0: any; + let dsaWallet1: any; + let wallet: any; + let dsa0Signer: any; + let masterSigner: Signer; + let instaConnectorsV2: Contract; + let connector: any; + let connectorMain: any; + let signer: any; + let wethSigner: any; + let usdcSigner: any; + + const cometReward = new ethers.Contract(rewards, cometABI); + const comet = new ethers.Contract(market, marketABI); + + const wallets = provider.getWallets(); + const [wallet0, wallet1, wallet2, wallet3] = wallets; + + before(async () => { + await hre.network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + //@ts-ignore + jsonRpcUrl: hre.config.networks.hardhat.forking.url, + // blockNumber: 15444500 + } + } + ] + }); + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2CompoundV3ArbitrumRewards__factory, + signer: masterSigner, + connectors: instaConnectorsV2 + }); + console.log("Connector address", connector.address); + + await hre.network.provider.send("hardhat_setBalance", [account, ethers.utils.parseEther("10").toHexString()]); + + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [account] + }); + + signer = await ethers.getSigner(account); + + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [wethWhale] + }); + wethSigner = await ethers.getSigner(wethWhale); + + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [baseWhale] + }); + usdcSigner = await ethers.getSigner(baseWhale); + await hre.network.provider.send("hardhat_setBalance", [ + usdcSigner.address, + ethers.utils.parseEther("10").toHexString() + ]); + }); + + it("Should have contracts deployed.", async function () { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!(await masterSigner.getAddress())).to.be.true; + }); + + describe("DSA wallet setup", function () { + it("Should build DSA v2", async function () { + dsaWallet0 = await buildDSAv2(wallet0.address); + expect(!!dsaWallet0.address).to.be.true; + dsaWallet1 = await buildDSAv2(wallet0.address); + expect(!!dsaWallet1.address).to.be.true; + wallet = await ethers.getSigner(dsaWallet0.address); + expect(!!dsaWallet1.address).to.be.true; + }); + + it("Deposit ETH into DSA wallet", async function () { + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [wallet.address] + }); + + dsa0Signer = await ethers.getSigner(wallet.address); + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("10") + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); + await wallet0.sendTransaction({ + to: dsaWallet1.address, + value: ethers.utils.parseEther("10") + }); + expect(await ethers.provider.getBalance(dsaWallet1.address)).to.be.gte(ethers.utils.parseEther("10")); + }); + + it("should deposit USDC in dsa wallet", async function () { + await baseContract.connect(usdcSigner).transfer(dsaWallet0.address, ethers.utils.parseUnits("500", 6)); + + expect(await baseContract.connect(usdcSigner).balanceOf(dsaWallet0.address)).to.be.gte( + ethers.utils.parseUnits("500", 6) + ); + }); + }); + + describe("Main", function () { + //deposit asset + it("Should supply USDC in Compound V3", async function () { + connectorName = "COMPOUND-V3-TEST-A"; + connectorMain = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2CompoundV3Arbitrum__factory, + signer: masterSigner, + connectors: instaConnectorsV2 + }); + const amount = ethers.utils.parseUnits("400", 6); + const spells = [ + { + connector: "COMPOUND-V3-TEST-A", + method: "deposit", + args: [market, base, amount, 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(new BigNumber(await baseContract.connect(signer).balanceOf(dsaWallet0.address)).toFixed(0)).to.be.lte( + ethers.utils.parseUnits("100", 6) + ); + expect(new BigNumber(await comet.connect(signer).balanceOf(dsaWallet0.address)).toFixed(0)).to.be.gte( + ethers.utils.parseUnits("399", 6) + ); + }); + + let connector_ = "COMPOUND-V3-REWARDS-TEST-A"; + it("Should claim rewards", async function () { + let reward = (await cometReward.connect(signer).rewardConfig(market)).token; + let rewardInterface = new ethers.Contract(reward, ABI); + let owed_ = await cometReward.connect(signer).callStatic.getRewardOwed(market, dsaWallet0.address); + let amt: number = owed_.owed; + console.log(new BigNumber(amt).toFixed(0)); + const spells = [ + { + connector: connector_, + method: "claimRewards", + args: [market, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(new BigNumber(await rewardInterface.connect(signer).balanceOf(dsaWallet0.address)).toFixed(0)).to.be.gte( + amt + ); + }); + + it("Should supply USDC in Compound V3 through dsaWallet0", async function () { + const amount = ethers.utils.parseUnits("100", 6); // 1 ETH + const spells = [ + { + connector: "COMPOUND-V3-TEST-A", + method: "deposit", + args: [market, base, amount, 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(new BigNumber(await baseContract.connect(signer).balanceOf(dsaWallet0.address)).toFixed(0)).to.be.lte( + ethers.utils.parseUnits("0", 6) + ); + expect(new BigNumber(await comet.connect(signer).balanceOf(dsaWallet0.address)).toFixed(0)).to.be.gte( + ethers.utils.parseUnits("499", 6) + ); + }); + + it("Should claim rewards to dsa1", async function () { + let reward = (await cometReward.connect(signer).rewardConfig(market)).token; + let rewardInterface = new ethers.Contract(reward, ABI); + let owed_ = await cometReward.connect(signer).callStatic.getRewardOwed(market, dsaWallet0.address); + let amt: number = owed_.owed; + + const spells = [ + { + connector: connector_, + method: "claimRewardsOnBehalfOf", + args: [market, dsaWallet0.address, dsaWallet1.address, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(new BigNumber(await rewardInterface.connect(signer).balanceOf(dsaWallet1.address)).toFixed(0)).to.be.gte( + amt + ); + }); + + it("should allow manager for dsaWallet0's collateral and base", async function () { + const spells = [ + { + connector: connectorName, + method: "toggleAccountManager", + args: [market, dsaWallet1.address, true] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + }); + + it("Should claim rewards to dsa1 using manager", async function () { + let reward = (await cometReward.connect(signer).rewardConfig(market)).token; + let rewardInterface = new ethers.Contract(reward, ABI); + let owed_ = await cometReward.connect(signer).callStatic.getRewardOwed(market, dsaWallet0.address); + let amt: number = owed_.owed; + + const spells = [ + { + connector: connector_, + method: "claimRewardsOnBehalfOf", + args: [market, dsaWallet0.address, dsaWallet1.address, 0] + } + ]; + + const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(new BigNumber(await rewardInterface.connect(signer).balanceOf(dsaWallet1.address)).toFixed(0)).to.be.gte( + amt + ); + }); + }); +});