Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #89 from ionicprotocol/create-separate-scripts-for…
Browse files Browse the repository at this point in the history
…-flywheel

Create separate scripts for flywheel
  • Loading branch information
rhlsthrm authored Aug 19, 2024
2 parents 748987f + 19312c1 commit c1f6164
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 82 deletions.
21 changes: 21 additions & 0 deletions tasks/chain-specific/base/removeFlywheel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { task } from "hardhat/config";
import { Address, formatEther, parseEther } from "viem";
import { COMPTROLLER } from ".";

task("market:base:remove-flywheel", "Deploys flywheel and adds rewards").setAction(
async (_, { viem, run, deployments, getNamedAccounts }) => {
const { deployer } = await getNamedAccounts();
const publicClient = await viem.getPublicClient();

const flywheel = await viem.getContractAt(
"IonicFlywheel",
(await deployments.get("IonicFlywheel_ION_v2")).address as Address
);

const comptroller = await viem.getContractAt("IonicComptroller", COMPTROLLER);
const addTx = await comptroller.write._removeFlywheel([flywheel.address]);
await publicClient.waitForTransactionReceipt({ hash: addTx });
console.log({ addTx });
console.log(`Remove IonicFlywheel_ION_v2 ${flywheel.address} from comptroller`);
}
);
150 changes: 68 additions & 82 deletions tasks/chain-specific/base/rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,14 @@ task("market:base:add-rewards-to-existing-flywheel", "Adds rewards to existing f
.addParam("market", "market address", undefined, types.string)
.addParam("rewardAmount", "the amount of tokens streamed to first epoch", undefined, types.string)
.addParam("reward", "token address of reward token", undefined, types.string)
.setAction(async ({ market, rewardAmount, reward }, { viem, run, deployments, getNamedAccounts }) => {
.addParam("name", "name of deployment", undefined, types.string)
.setAction(async ({ market, rewardAmount, reward, name }, { viem, run, deployments, getNamedAccounts }) => {
const { deployer } = await getNamedAccounts();
const publicClient = await viem.getPublicClient();
/*
// Upgrade markets to the new implementation
console.log(`Upgrading market: ${ionhyUSD} to CErc20RewardsDelegate`);
await run("market:upgrade", {
comptroller,
underlying: hyUSD,
implementationAddress: (await deployments.get("CErc20RewardsDelegate")).address,
signer: deployer
});

const publicClient = await viem.getPublicClient();
const { implementationAddress, comptroller: comptrollerAddress, underlying, signer: namedSigner } = taskArgs;
const comptroller = await viem.getContractAt("IonicComptroller", comptrollerAddress as Address);
// Upgrade markets to the new implementation
console.log(`Upgrading market: ${market} to CErc20RewardsDelegate`);
const comptroller = await viem.getContractAt("IonicComptroller", COMPTROLLER);

const allMarkets = await comptroller.read.getAllMarkets();

Expand All @@ -35,12 +26,12 @@ task("market:base:add-rewards-to-existing-flywheel", "Adds rewards to existing f
let cTokenInstance;
for (let index = 0; index < cTokenInstances.length; index++) {
const thisUnderlying = await cTokenInstances[index].read.underlying();
if (!cTokenInstance && thisUnderlying.toLowerCase() === hyUSD.toLowerCase()) {
if (!cTokenInstance && thisUnderlying.toLowerCase() === market.toLowerCase()) {
cTokenInstance = cTokenInstances[index];
}
}
if (!cTokenInstance) {
throw Error(`No market corresponds to this underlying: ${hyUSD}`);
throw Error(`No market corresponds to this underlying: ${market}`);
}

const implementationData = "0x";
Expand All @@ -60,19 +51,25 @@ task("market:base:add-rewards-to-existing-flywheel", "Adds rewards to existing f
console.log(
`Implementation successfully set to ${implementationAddress}: ${setImplementationTx}`
);
*/
/*

// Sending tokens
const ionToken = await viem.getContractAt("EIP20Interface", reward);
const balance = await ionToken.read.balanceOf([market]);
if (balance < parseEther(rewardAmount)) {
await ionToken.write.transfer([market, parseEther(rewardAmount)]);
await ionToken.write.transfer([market, parseEther(rewardAmount) - balance]);
}
*/

let contractName;
if (name.includes("Borrow")) {
contractName = "IonicFlywheelBorrow";
} else {
contractName = "IonicFlywheel";
}

// Approving token sepening for fwRewards contract
const flywheel = await viem.getContractAt(
"IonicFlywheel",
(await deployments.get("IonicFlywheel_ION_v3")).address as Address
`${contractName}`,
(await deployments.get(`${contractName}_${name}`)).address as Address
);

const _market = await viem.getContractAt("CErc20RewardsDelegate", market);
Expand All @@ -91,7 +88,7 @@ task("market:base:add-rewards-to-existing-flywheel", "Adds rewards to existing f
await publicClient.waitForTransactionReceipt({ hash: addTx });
console.log(`Added strategy (${market}) to flywheel (${flywheel.address})`);
} else console.log(`Strategy (${market}) was already added to flywheel (${flywheel.address})`);
});
});

task("market:base:deploy-flywheel-and-add-rewards", "Sets caps on a market")
.addParam("market", "market address", undefined, types.string)
Expand All @@ -104,66 +101,54 @@ task("market:base:deploy-flywheel-and-add-rewards", "Sets caps on a market")
const { deployer } = await getNamedAccounts();
const publicClient = await viem.getPublicClient();

/*
// Upgrade markets to the new implementation
console.log(`Upgrading market: ${ionhyUSD} to CErc20RewardsDelegate`);
await run("market:upgrade", {
comptroller,
underlying: hyUSD,
implementationAddress: (await deployments.get("CErc20RewardsDelegate")).address,
signer: deployer
});
const comptroller = await viem.getContractAt("IonicComptroller", COMPTROLLER);

const publicClient = await viem.getPublicClient();
const { implementationAddress, comptroller: comptrollerAddress, underlying, signer: namedSigner } = taskArgs;
// Upgrade markets to the new implementation
console.log(`Upgrading market: ${market} to CErc20RewardsDelegate`);
const allMarkets = await comptroller.read.getAllMarkets();

const comptroller = await viem.getContractAt("IonicComptroller", comptrollerAddress as Address);
const cTokenInstances = await Promise.all(
allMarkets.map(async (marketAddress) => {
return await viem.getContractAt("ICErc20PluginRewards", marketAddress);
})
);

const allMarkets = await comptroller.read.getAllMarkets();
let cTokenInstance;
for (let index = 0; index < cTokenInstances.length; index++) {
const thisUnderlying = await cTokenInstances[index].read.underlying();
if (!cTokenInstance && thisUnderlying.toLowerCase() === market.toLowerCase()) {
cTokenInstance = cTokenInstances[index];
}
}
if (!cTokenInstance) {
throw Error(`No market corresponds to this underlying: ${market}`);
}

const cTokenInstances = await Promise.all(
allMarkets.map(async (marketAddress) => {
return await viem.getContractAt("ICErc20PluginRewards", marketAddress);
})
);
const implementationData = "0x";
const implementationAddress = (await deployments.get("CErc20RewardsDelegate")).address;
console.log(`Setting implementation to ${implementationAddress}`);
const setImplementationTx = await cTokenInstance.write._setImplementationSafe([
implementationAddress,
implementationData
]);

let cTokenInstance;
for (let index = 0; index < cTokenInstances.length; index++) {
const thisUnderlying = await cTokenInstances[index].read.underlying();
if (!cTokenInstance && thisUnderlying.toLowerCase() === hyUSD.toLowerCase()) {
cTokenInstance = cTokenInstances[index];
const receipt = await publicClient.waitForTransactionReceipt({
hash: setImplementationTx
});
if (receipt.status !== "success") {
throw `Failed set implementation to ${implementationAddress}`;
}
}
if (!cTokenInstance) {
throw Error(`No market corresponds to this underlying: ${hyUSD}`);
}
const implementationData = "0x";
const implementationAddress = (await deployments.get("CErc20RewardsDelegate")).address;
console.log(`Setting implementation to ${implementationAddress}`);
const setImplementationTx = await cTokenInstance.write._setImplementationSafe([
implementationAddress,
implementationData
]);
console.log(
`Implementation successfully set to ${implementationAddress}: ${setImplementationTx}`
);

const receipt = await publicClient.waitForTransactionReceipt({
hash: setImplementationTx
});
if (receipt.status !== "success") {
throw `Failed set implementation to ${implementationAddress}`;
}
console.log(
`Implementation successfully set to ${implementationAddress}: ${setImplementationTx}`
);
*/
/*
// Sending tokens
const ionToken = await viem.getContractAt("EIP20Interface", reward);
const balance = await ionToken.read.balanceOf([market]);
if (balance < parseEther(rewardAmount)) {
await ionToken.write.transfer([market, parseEther(rewardAmount)]);
await ionToken.write.transfer([market, parseEther(rewardAmount) - balance]);
}
*/

// Deploying flywheel
let booster = "";
let flywheelBoosterAddress;
Expand All @@ -179,9 +164,9 @@ task("market:base:deploy-flywheel-and-add-rewards", "Sets caps on a market")
flywheelBoosterAddress = (await deployments.get(booster)).address as Address;
} else flywheelBoosterAddress = zeroAddress;

let _flywheel = await deployments.getOrNull(`${contractName}_${name}_v3`);
let _flywheel = await deployments.getOrNull(`${contractName}_${name}`);
if (!_flywheel) {
_flywheel = await deployments.deploy(`${contractName}_${name}_v3`, {
_flywheel = await deployments.deploy(`${contractName}_${name}`, {
contract: contractName,
from: deployer,
log: true,
Expand All @@ -205,14 +190,14 @@ task("market:base:deploy-flywheel-and-add-rewards", "Sets caps on a market")
// Deploying flywheel rewards
const flywheel = await viem.getContractAt(
`${contractName}`,
(await deployments.get(`${contractName}_${name}_v3`)).address as Address
(await deployments.get(`${contractName}_${name}`)).address as Address
);

let flywheelRewards = await deployments.getOrNull(`IonicFlywheelDynamicRewards_${name}_v3`);
let flywheelRewards = await deployments.getOrNull(`IonicFlywheelDynamicRewards_${name}`);
if (flywheelRewards) {
console.log(`Flywheel rewards ${name} already deployed at ${flywheelRewards.address}`);
} else {
flywheelRewards = await deployments.deploy(`IonicFlywheelDynamicRewards_${name}_v3`, {
flywheelRewards = await deployments.deploy(`IonicFlywheelDynamicRewards_${name}`, {
contract: "IonicFlywheelDynamicRewards",
from: deployer,
log: true,
Expand All @@ -239,7 +224,6 @@ task("market:base:deploy-flywheel-and-add-rewards", "Sets caps on a market")
} else console.log(`Strategy (${market}) was already added to flywheel (${flywheel.address})`);

// Adding flywheel to comptroller
const comptroller = await viem.getContractAt("IonicComptroller", COMPTROLLER);
const rewardsDistributors = (await comptroller.read.getRewardsDistributors()) as Address[];
if (!rewardsDistributors.map((s) => s.toLowerCase()).includes(flywheel.address.toLowerCase())) {
const addTx = await comptroller.write._addRewardsDistributor([flywheel.address]);
Expand All @@ -258,26 +242,28 @@ task("market:base:deploy-flywheel-and-add-rewards", "Sets caps on a market")
console.log(`mining tx ${tx}`);
await publicClient.waitForTransactionReceipt({ hash: tx });
console.log(`approved flywheel ${flywheel.address} to pull reward tokens from market ${market}`);
}
);
}
);

task("market:base:add-flywheel-ION-rewards-to-ionbsdETH", "Adds rewards to existing flywheel").setAction(
async (_, { viem, run, deployments, getNamedAccounts }) => {
const market = "0x3d9669de9e3e98db41a1cbf6dc23446109945e3c"; // ionbsdETH
const rewardAmount = "23334"; // epoch will start 2 days so 25000 / 30 * 2
const rewardAmount = "23334"; // epoch will last for 28 days so 25000 / 30 * 28
const ion = "0x3eE5e23eEE121094f1cFc0Ccc79d6C809Ebd22e5";
const name = "ION"; // For borrow flywheel use Borrow_ION for supply flywheel just ION
await run("market:base:add-rewards-to-existing-flywheel", {
market,
rewardAmount,
reward: ion
reward: ion,
name: name
});
}
);

task("market:base:deploy-flywheel-and-add-ION-rewards-to-ionhyUSD", "Deploys flywheel and adds rewards").setAction(
async (_, { viem, run, deployments, getNamedAccounts }) => {
const market = "0x751911bDa88eFcF412326ABE649B7A3b28c4dEDe"; // ionhyUSD
const rewardAmount = "14000"; // epoch will start 2 days so 15000 / 30 * 2
const rewardAmount = "14000"; // epoch will last for 28 days so 15000 / 30 * 28
const ion = "0x3eE5e23eEE121094f1cFc0Ccc79d6C809Ebd22e5";
const name = "ION"; // For borrow flywheel use Borrow_ION for supply flywheel just ION
// NOTE: Make sure that epoch duration for supply and borrow are not the same
Expand Down

0 comments on commit c1f6164

Please sign in to comment.