Skip to content

Commit

Permalink
Hardhat tasks working with Sonic (#2357)
Browse files Browse the repository at this point in the history
* Made depositWETH work with Sonic

* HH tasks allocate, capital, rebase, mint, requestWithdrawal, claimWithdrawal and  and snapVault working with Sonic
  • Loading branch information
naddison36 authored Jan 17, 2025
1 parent 570f5af commit 6859cb8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 47 deletions.
33 changes: 17 additions & 16 deletions contracts/tasks/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ subtask("depositWETH", "Deposit ETH into WETH")
const signer = await getSigner();

const { chainId } = await ethers.provider.getNetwork();
const wethAddress = addresses[networkMap[chainId]].WETH;
const symbol = chainId == 146 ? "wS" : "WETH";
const wethAddress = addresses[networkMap[chainId]][symbol];
const weth = await ethers.getContractAt("IWETH9", wethAddress);

await depositWETH({ ...taskArgs, weth, signer });
Expand Down Expand Up @@ -291,8 +292,8 @@ task("queueLiquidity").setAction(async (_, __, runSuper) => {
task("allocate", "Call allocate() on the Vault")
.addOptionalParam(
"symbol",
"Symbol of the OToken. eg OETH or OUSD",
"OETH",
"Symbol of the OToken. eg OETH, OUSD or OS",
undefined,
types.string
)
.setAction(allocate);
Expand All @@ -303,14 +304,14 @@ task("allocate").setAction(async (_, __, runSuper) => {
task("capital", "Set the Vault's pauseCapital flag")
.addOptionalParam(
"symbol",
"Symbol of the OToken. eg OETH or OUSD",
"OETH",
"Symbol of the OToken. eg OETH, OUSD or OS",
undefined,
types.string
)
.addParam(
"pause",
"Whether to pause or unpause the capital allocation",
"true",
true,
types.boolean
)
.setAction(capital);
Expand All @@ -321,8 +322,8 @@ task("capital").setAction(async (_, __, runSuper) => {
task("rebase", "Call rebase() on the Vault")
.addOptionalParam(
"symbol",
"Symbol of the OToken. eg OETH or OUSD",
"OETH",
"Symbol of the OToken. eg OETH, OUSD or OS",
undefined,
types.string
)
.setAction(rebase);
Expand All @@ -333,9 +334,9 @@ task("rebase").setAction(async (_, __, runSuper) => {
task("yield", "Artificially generate yield on the OUSD Vault", yieldTask);

subtask("mint", "Mint OTokens from the Vault using collateral assets")
.addParam(
.addOptionalParam(
"asset",
"Symbol of the collateral asset to deposit. eg WETH, frxETH, USDT, DAI",
"Symbol of the collateral asset to deposit. eg WETH, wS, USDT, DAI or USDC",
undefined,
types.string
)
Expand All @@ -347,8 +348,8 @@ subtask("mint", "Mint OTokens from the Vault using collateral assets")
)
.addOptionalParam(
"symbol",
"Symbol of the OToken. eg OETH or OUSD",
"OETH",
"Symbol of the OToken. eg OETH, OUSD or OS",
undefined,
types.string
)
.addOptionalParam("min", "Minimum amount of OTokens to mint", 0, types.float)
Expand Down Expand Up @@ -505,8 +506,8 @@ subtask("requestWithdrawal", "Request a withdrawal from a vault")
)
.addOptionalParam(
"symbol",
"Symbol of the OToken. eg OETH or OUSD",
"OETH",
"Symbol of the OToken. eg OETH, OUSD or OS",
undefined,
types.string
)
.setAction(requestWithdrawal);
Expand All @@ -526,8 +527,8 @@ subtask(
)
.addOptionalParam(
"symbol",
"Symbol of the OToken. eg OETH or OUSD",
"OETH",
"Symbol of the OToken. eg OETH, OUSD or OS",
undefined,
types.string
)
.setAction(claimWithdrawal);
Expand Down
84 changes: 53 additions & 31 deletions contracts/tasks/vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,57 @@ const { networkMap } = require("../utils/hardhat-helpers");

const log = require("../utils/logger")("task:vault");

async function getContract(hre, symbol) {
async function getContracts(hre, symbol, assetSymbol) {
const { chainId } = await hre.ethers.provider.getNetwork();

// If no symbol is provided, set to OSonic if Sonic network, else default to OETH
symbol = symbol
? symbol
: networkMap[chainId] === "sonic"
? "OSonic"
: "OETH";
// Convert OS to OSonic
symbol = symbol === "OS" ? "OSonic" : symbol;
const contractPrefix = symbol === "OUSD" ? "" : symbol;

const { chainId } = await hre.ethers.provider.getNetwork();
const networkPrefix = networkMap[chainId] === "base" ? "Base" : "";
const network = networkMap[chainId];
const networkPrefix = network === "base" ? "Base" : "";

// Resolve the vault
const vaultProxy = await hre.ethers.getContract(
`${contractPrefix}${networkPrefix}VaultProxy`
);
const vault = await hre.ethers.getContractAt("IVault", vaultProxy.address);
log(`Resolved ${networkPrefix} ${symbol} Vault to address ${vault.address}`);
log(`Resolved ${network} ${symbol} Vault to address ${vault.address}`);

// Resolve the OToken. eg OUSD, OETH or OSonic
const oTokenProxy = await ethers.getContract(
`${symbol}${networkPrefix}Proxy`
);
const oToken = await ethers.getContractAt(symbol, oTokenProxy.address);
log(`Resolved ${network} ${symbol} OToken to address ${oToken.address}`);

// Resolve the Asset. eg WETH or wS
// This won't work for OUSD if the assetSymbol has not been set as it has three assets
assetSymbol = assetSymbol || network === "sonic" ? "wS" : "WETH";
const asset = await resolveAsset(assetSymbol);
log(
`Resolved ${networkPrefix} ${symbol} OToken to address ${oToken.address}`
`Resolved ${network} ${symbol} Vault asset to ${assetSymbol} with address ${asset.address}`
);

return {
vault,
oToken,
asset,
};
}

async function snapVault({ block }, hre) {
const blockTag = getBlock(block);

const { vault, oToken } = await getContract(hre, "OETH");

const { chainId } = await hre.ethers.provider.getNetwork();
const network = networkMap[chainId];
const wethAddress = addresses[network].WETH;
const weth = await ethers.getContractAt("IERC20", wethAddress);
const { vault, oToken, asset } = await getContracts(hre);

const wethBalance = await weth.balanceOf(vault.address, {
const assetBalance = await asset.balanceOf(vault.address, {
blockTag,
});

Expand All @@ -59,7 +73,7 @@ async function snapVault({ block }, hre) {
});
const shortfall = queue.queued.sub(queue.claimable);
const unclaimed = queue.queued.sub(queue.claimed);
const available = wethBalance.add(queue.claimed).sub(queue.queued);
const available = assetBalance.add(queue.claimed).sub(queue.queued);
const availablePercentage = available.mul(10000).div(totalSupply);

const totalAssets = await vault.totalValue({
Expand All @@ -74,7 +88,7 @@ async function snapVault({ block }, hre) {
.div(parseUnits("1"));

console.log(
`Vault WETH : ${formatUnits(wethBalance)}, ${wethBalance} wei`
`Vault assets : ${formatUnits(assetBalance)}, ${assetBalance} wei`
);

console.log(
Expand Down Expand Up @@ -115,7 +129,7 @@ async function snapVault({ block }, hre) {
async function addWithdrawalQueueLiquidity(_, hre) {
const signer = await getSigner();

const { vault } = await getContract(hre, "OETH");
const { vault } = await getContracts(hre);

log(
`About to call addWithdrawalQueueLiquidity() on the vault with address ${vault.address}`
Expand All @@ -127,7 +141,7 @@ async function addWithdrawalQueueLiquidity(_, hre) {
async function allocate({ symbol }, hre) {
const signer = await getSigner();

const { vault } = await getContract(hre, symbol);
const { vault } = await getContracts(hre, symbol);

log(
`About to send a transaction to call allocate() on the ${symbol} vault with address ${vault.address}`
Expand All @@ -139,13 +153,13 @@ async function allocate({ symbol }, hre) {
async function rebase({ symbol }, hre) {
const signer = await getSigner();

const { vault } = await getContract(hre, symbol);
const { vault } = await getContracts(hre, symbol);

log(
`About to send a transaction to call rebase() on the ${symbol} vault with address ${vault.address}`
);
const tx = await vault.connect(signer).rebase();
await logTxDetails(tx, "harvest");
await logTxDetails(tx, "rebase");
}

/**
Expand Down Expand Up @@ -178,7 +192,7 @@ async function yieldTask(_, hre) {
usdt = await hre.ethers.getContract("MockUSDT");
}

const { vault, oToken } = await getContract(hre, "OUSD");
const { vault, oToken } = await getContracts(hre, "OUSD");

log("Sending yield to vault");
let usdtBalance = await usdt.balanceOf(vault.address);
Expand Down Expand Up @@ -210,7 +224,7 @@ async function capital({ symbol, pause }, hre) {

const sGovernor = await getSigner();

const { vault } = await getContract(hre, symbol);
const { vault } = await getContracts(hre, symbol);

if (isMainnet) {
const signature = pause ? "pauseCapital()" : "unpauseCapital()";
Expand All @@ -234,20 +248,28 @@ async function capital({ symbol, pause }, hre) {
async function mint({ amount, asset, symbol, min, approve }, hre) {
const signer = await getSigner();

const { vault } = await getContract(hre, symbol);
const {
vault,
oToken,
asset: cAsset,
} = await getContracts(hre, symbol, asset);

const cAsset = await resolveAsset(asset);
const assetUnits = parseUnits(amount.toString(), await cAsset.decimals());
const minUnits = parseUnits(min.toString());

if (approve) {
log(
`About to approve the ${await oToken.symbol()} Vault to spend ${amount} ${await cAsset.symbol()}`
);
const approveTx = await cAsset
.connect(signer)
.approve(vault.address, assetUnits);
await logTxDetails(approveTx, "approve");
}

log(`About to mint ${symbol} using ${amount} ${asset}`);
log(
`About to mint ${await oToken.symbol()} using ${amount} ${await cAsset.symbol()}`
);
const tx = await vault
.connect(signer)
.mint(cAsset.address, assetUnits, minUnits);
Expand All @@ -257,7 +279,7 @@ async function mint({ amount, asset, symbol, min, approve }, hre) {
async function redeem({ amount, min, symbol }, hre) {
const signer = await getSigner();

const { vault } = await getContract(hre, symbol);
const { vault } = await getContracts(hre, symbol);

const oTokenUnits = parseUnits(amount.toString());
const minUnits = parseUnits(min.toString());
Expand All @@ -270,7 +292,7 @@ async function redeem({ amount, min, symbol }, hre) {
async function redeemAll({ min, symbol }, hre) {
const signer = await getSigner();

const { vault } = await getContract(hre, symbol);
const { vault } = await getContracts(hre, symbol);

const minUnits = parseUnits(min.toString());

Expand Down Expand Up @@ -330,7 +352,7 @@ async function resolveAmounts(amounts, assetContracts) {
async function depositToStrategy({ amounts, assets, symbol, strategy }, hre) {
const signer = await getSigner();

const { vault } = await getContract(hre, symbol);
const { vault } = await getContracts(hre, symbol);

const strategyAddr = await resolveStrategyAddress(strategy, hre);

Expand All @@ -353,7 +375,7 @@ async function withdrawFromStrategy(
) {
const signer = await getSigner();

const { vault } = await getContract(hre, symbol);
const { vault } = await getContracts(hre, symbol);

const strategyAddr = await resolveStrategyAddress(strategy, hre);

Expand All @@ -373,7 +395,7 @@ async function withdrawFromStrategy(
async function withdrawAllFromStrategy({ symbol, strategy }, hre) {
const signer = await getSigner();

const { vault } = await getContract(hre, symbol);
const { vault } = await getContracts(hre, symbol);

const strategyAddr = await resolveStrategyAddress(strategy, hre);

Expand All @@ -385,7 +407,7 @@ async function withdrawAllFromStrategy({ symbol, strategy }, hre) {
async function withdrawAllFromStrategies({ symbol }, hre) {
const signer = await getSigner();

const { vault } = await getContract(hre, symbol);
const { vault } = await getContracts(hre, symbol);

log(`About to withdraw all from all strategies`);
const tx = await vault.connect(signer).withdrawAllFromStrategies();
Expand All @@ -397,7 +419,7 @@ async function requestWithdrawal({ amount, symbol }, hre) {

const oTokenUnits = parseUnits(amount.toString());

const { vault } = await getContract(hre, symbol);
const { vault } = await getContracts(hre, symbol);

// Get the withdrawal request ID by statically calling requestWithdrawal
const { requestId } = await vault
Expand All @@ -414,7 +436,7 @@ async function requestWithdrawal({ amount, symbol }, hre) {
async function claimWithdrawal({ requestId, symbol }, hre) {
const signer = await getSigner();

const { vault } = await getContract(hre, symbol);
const { vault } = await getContracts(hre, symbol);

log(
`About to claim withdrawal from the ${symbol} vault for request ${requestId}`
Expand Down
4 changes: 4 additions & 0 deletions contracts/utils/addresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ addresses.sonic.admin = "0xAdDEA7933Db7d83855786EB43a238111C69B00b6";
addresses.sonic.guardian = "0x63cdd3072F25664eeC6FAEFf6dAeB668Ea4de94a";
addresses.sonic.timelock = "0x31a91336414d3B955E494E7d485a6B06b55FC8fB";

addresses.sonic.OSonicProxy = "0xb1e25689D55734FD3ffFc939c4C3Eb52DFf8A794";
addresses.sonic.WOSonicProxy = "0x9F0dF7799f6FDAd409300080cfF680f5A23df4b1";
addresses.sonic.OSonicVaultProxy = "0xa3c0eCA00D2B76b4d1F170b0AB3FdeA16C180186";

// Holesky
addresses.holesky.WETH = "0x94373a4919B3240D86eA41593D5eBa789FEF3848";

Expand Down

0 comments on commit 6859cb8

Please sign in to comment.