diff --git a/contracts/erc4626/IERC4626.sol b/contracts/erc4626/IERC4626.sol index 131a71f..d325e23 100644 --- a/contracts/erc4626/IERC4626.sol +++ b/contracts/erc4626/IERC4626.sol @@ -11,6 +11,8 @@ abstract contract IERC4626 is ERC20 { event Deposit(address indexed from, address indexed to, uint256 amount, uint256 shares); event Withdraw(address indexed from, address indexed to, uint256 amount, uint256 shares); + error ZeroShares(uint256 numberOfShares); + /*/////////////////////////////////////////////////////////////// Mutable Functions //////////////////////////////////////////////////////////////*/ diff --git a/contracts/erc4626/Vault.sol b/contracts/erc4626/Vault.sol index f319826..7f9c596 100644 --- a/contracts/erc4626/Vault.sol +++ b/contracts/erc4626/Vault.sol @@ -26,8 +26,7 @@ contract HederaVault is IERC4626 { constructor( ERC20 _underlying, string memory _name, - string memory _symbol, - address[] memory rewardTokens + string memory _symbol ) payable ERC20(_name, _symbol, _underlying.decimals()) { owner = msg.sender; @@ -61,8 +60,6 @@ contract HederaVault is IERC4626 { newTokenAddress = SafeHTS.safeCreateFungibleToken(newToken, 0, _underlying.decimals()); emit createdToken(newTokenAddress); asset = _underlying; - - tokenAddress = rewardTokens; } struct UserInfo { @@ -84,7 +81,7 @@ contract HederaVault is IERC4626 { //////////////////////////////////////////////////////////////*/ function deposit(uint256 amount, address to) public override returns (uint256 shares) { - require((shares = previewDeposit(amount)) != 0, "ZERO_SHARES"); + if ((shares = previewDeposit(amount)) == 0) revert ZeroShares(amount); asset.safeTransferFrom(msg.sender, address(this), amount); diff --git a/hardhat.config.ts b/hardhat.config.ts index 9bead94..91af1d4 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -1,6 +1,7 @@ import { HardhatUserConfig } from "hardhat/config"; import "@nomicfoundation/hardhat-toolbox"; import "@openzeppelin/hardhat-upgrades"; +import "@nomicfoundation/hardhat-chai-matchers"; import * as dotenv from "dotenv"; diff --git a/scripts/deployVault.ts b/scripts/deployVault.ts index c817d91..b00c9d0 100644 --- a/scripts/deployVault.ts +++ b/scripts/deployVault.ts @@ -30,24 +30,13 @@ async function main() { operatorPrKey ); - const rewardToken = await createFungibleToken( - "Reward Token 1", - "RT1", - process.env.ACCOUNT_ID, - operatorPrKey.publicKey, - client, - operatorPrKey - ); - const stakingTokenAddress = "0x" + stakingToken!.toSolidityAddress(); - const rewardTokenAddress = "0x" + rewardToken!.toSolidityAddress(); const HederaVault = await ethers.getContractFactory("HederaVault"); const hederaVault = await HederaVault.deploy( stakingTokenAddress, "TST", "TST", - [rewardTokenAddress], { from: deployer.address, gasLimit: 3000000, value: ethers.parseUnits("10", 18) } ); console.log("Hash ", hederaVault.deploymentTransaction()?.hash); diff --git a/test/erc4626/vault.test.ts b/test/erc4626/vault.test.ts index f2c2775..29a2a42 100644 --- a/test/erc4626/vault.test.ts +++ b/test/erc4626/vault.test.ts @@ -2,19 +2,23 @@ import { anyValue, ethers, expect } from "../setup"; import { TokenTransfer, createFungibleToken, TokenBalance, createAccount, addToken, mintToken } from "../../scripts/utils"; import { PrivateKey, Client, AccountId, TokenAssociateTransaction, AccountBalanceQuery } from "@hashgraph/sdk"; import hre from "hardhat"; +import { should } from "chai"; // constants const stakingTokenId = "0.0.3757626"; const sharesTokenAddress = "0x0000000000000000000000000000000000395640"; -const vaultAddress = "0xb3C24B140BA2a69099276e55dE1885e93517C6C6"; +const revertCasesVaultAddress = "0xb3C24B140BA2a69099276e55dE1885e93517C6C6"; const vaultId = "0.0.3757631"; const newStakingTokenId = "0.0.4178090"; const newVaultAddress = "0x26767C096B669b0A5Df59efeF0d6CbA3840E47F6" const newRewardTokenId = "0.0.4178093"; -const rewardTokenAddress = ""; +const rewardTokenAddress = "0x00000000000000000000000000000000003fc0ad"; const newSharesTokenAddress = "0x00000000000000000000000000000000003fc0b0"; +const newSharesTokenId = "0.0.4178096"; const newVaultId = "0.0.4178095"; + +const vaultEr = "0xB2B9f864fE12B9a9129d27Bec92b0c64DB5C9a69"; // Tests describe("Vault", function () { async function deployFixture() { @@ -26,6 +30,7 @@ describe("Vault", function () { const operatorPrKey = PrivateKey.fromStringECDSA(process.env.PRIVATE_KEY || ''); const operatorAccountId = AccountId.fromString(process.env.ACCOUNT_ID || ''); + const stAccountId = AccountId.fromString("0.0.2673429"); client.setOperator( operatorAccountId, @@ -44,19 +49,29 @@ describe("Vault", function () { // ); // const tokenAssociate = await new TokenAssociateTransaction() + // .setAccountId(stAccountId) + // .setTokenIds([newSharesTokenId]) + // .execute(client); + + // const tokenAssociateV = await new TokenAssociateTransaction() // .setAccountId(newVaultId) // .setTokenIds([newRewardTokenId]) // .execute(client); const hederaVaultRevertCases = await ethers.getContractAt( "HederaVault", - vaultAddress + revertCasesVaultAddress ); const hederaVault = await ethers.getContractAt( "HederaVault", newVaultAddress ); + const rewardToken = await ethers.getContractAt( + erc20.abi, + rewardTokenAddress + ); + const stakingToken = await ethers.getContractAt( erc20.abi, await hederaVault.asset() @@ -64,15 +79,19 @@ describe("Vault", function () { const sharesToken = await ethers.getContractAt( erc20.abi, - sharesTokenAddress + newSharesTokenId ); - // await TokenTransfer(stakingTokenId, operatorAccountId, vaultId, 10, client); + // await TokenTransfer(newStakingTokenId, operatorAccountId, stAccountId, 10, client); + + // const stakingTokenOperatorBalance = await ( + // await TokenBalance(stAccountId, client) + // ).tokens!.get(stakingTokenId); + // console.log("Staking token balance: ", stakingTokenOperatorBalance.toString()); - const stakingTokenOperatorBalance = await ( - await TokenBalance(operatorAccountId, client) - ).tokens!.get(stakingTokenId); - console.log("Staking token balance: ", stakingTokenOperatorBalance.toString()); + // const tx = await rewardToken.approve(hederaVault.target, 100); + + // const rewTx = await hederaVault.addReward(rewardTokenAddress, 100, { gasLimit: 3000000 }); return { hederaVault, @@ -91,18 +110,18 @@ describe("Vault", function () { console.log(await hederaVault.previewDeposit(amountToDeposit)); - await stakingToken.approve(hederaVault.target, amountToDeposit); + // await stakingToken.approve(hederaVault.target, amountToDeposit); - const tx = await hederaVault.connect(owner).deposit( - amountToDeposit, - owner.address, - { gasLimit: 3000000 } - ); + // const tx = await hederaVault.connect(owner).deposit( + // amountToDeposit, + // owner.address, + // { gasLimit: 3000000 } + // ); - await expect( - tx - ).to.emit(hederaVault, "Deposit") - .withArgs(owner.address, owner.address, amountToDeposit, anyValue); + // await expect( + // tx + // ).to.emit(hederaVault, "Deposit") + // .withArgs(owner.address, owner.address, amountToDeposit, anyValue); }); it("Should revert if zero shares", async function () { @@ -112,32 +131,28 @@ describe("Vault", function () { console.log(await hederaVaultRevertCases.previewDeposit(amountToDeposit)); await expect( - hederaVaultRevertCases.connect(owner).deposit( - amountToDeposit, - owner.address, - { gasLimit: 3000000 } - ) - ).to.be.revertedWith("ZERO_SHARES"); + hederaVaultRevertCases.connect(owner).deposit(amountToDeposit, owner.address) + ).to.be.reverted; }); }); describe("withdraw", function () { - it("Should withdraw tokens", async function () { - const { hederaVault, owner } = await deployFixture(); - const amountToWithdraw = 1; + // it("Should withdraw tokens", async function () { + // const { hederaVault, owner } = await deployFixture(); + // const amountToWithdraw = 1; - const tx = await hederaVault.connect(owner).withdraw( - amountToWithdraw, - owner.address, - owner.address, - { gasLimit: 3000000 } - ); + // const tx = await hederaVault.connect(owner).withdraw( + // amountToWithdraw, + // owner.address, + // owner.address, + // { gasLimit: 3000000 } + // ); - await expect( - tx - ).to.emit(hederaVault, "Withdraw") - .withArgs(owner.address, owner.address, amountToWithdraw, anyValue); - }); + // await expect( + // tx + // ).to.emit(hederaVault, "Withdraw") + // .withArgs(owner.address, owner.address, amountToWithdraw, anyValue); + // }); }); describe("mint", function () { @@ -163,45 +178,47 @@ describe("Vault", function () { }); describe("redeem", function () { - it("Should redeem tokens", async function () { - const { hederaVault, owner, stakingToken, sharesToken, client } = await deployFixture(); - const amountOfShares = 1; + // it("Should redeem tokens", async function () { + // const { hederaVault, owner, stakingToken, sharesToken, client } = await deployFixture(); + // const amountOfShares = 1; - const tokensAmount = await hederaVault.previewRedeem(amountOfShares); - console.log("Preview redeem ", tokensAmount); + // const tokensAmount = await hederaVault.previewRedeem(amountOfShares); + // console.log("Preview redeem ", tokensAmount); - console.log(await sharesToken.balanceOf(owner.address)); - console.log("TOTAL SUPPLY", await hederaVault.totalSupply()); - console.log("TOTAL ASSETS", await hederaVault.totalAssets()); - console.log("TOTAL TOKENS", await hederaVault.totalTokens()); - - await stakingToken.approve(hederaVault.target, amountOfShares); - - const tx = await hederaVault.connect(owner).redeem( - amountOfShares, - owner.address, - owner.address, - { gasLimit: 3000000 } - ); + // console.log(await sharesToken.balanceOf(owner.address)); + // console.log("TOTAL SUPPLY", await hederaVault.totalSupply()); + // console.log("TOTAL ASSETS", await hederaVault.totalAssets()); + // console.log("TOTAL TOKENS", await hederaVault.totalTokens()); - await expect( - tx - ).to.emit(hederaVault, "Withdraw") - .withArgs(owner.address, owner.address, tokensAmount, amountOfShares); - }); + // await stakingToken.approve(hederaVault.target, amountOfShares); - // it("Should revert if zero assets", async function () { - // const { hederaVault, owner } = await deployFixture(); - // const amountToReedem = 0; + // const tx = await hederaVault.connect(owner).redeem( + // amountOfShares, + // owner.address, + // owner.address, + // { gasLimit: 3000000 } + // ); // await expect( - // hederaVault.connect(owner).redeem( - // amountToReedem, - // owner.address, - // owner.address, - // { gasLimit: 3000000 } - // ) - // ).to.be.revertedWith("ZERO_ASSETS"); + // tx + // ).to.emit(hederaVault, "Withdraw") + // .withArgs(owner.address, owner.address, tokensAmount, amountOfShares); // }); + + it("Should revert if zero assets", async function () { + const { hederaVaultRevertCases, owner } = await deployFixture(); + const amountToReedem = 0; + + console.log(await hederaVaultRevertCases.previewRedeem(amountToReedem)); + + await expect( + hederaVaultRevertCases.connect(owner).redeem( + amountToReedem, + owner.address, + owner.address, + { gasLimit: 3000000 } + ) + ).to.be.reverted; + }); }); }); diff --git a/yarn.lock b/yarn.lock index c5708fd..dead1ec 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1205,7 +1205,12 @@ dependencies: "@types/chai" "*" -"@types/chai@*", "@types/chai@^4.2.0": +"@types/chai@*": + version "4.3.14" + resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.3.14.tgz#ae3055ea2be43c91c9fd700a36d67820026d96e6" + integrity sha512-Wj71sXE4Q4AkGdG9Tvq1u/fquNz9EdG4LIJMwVVII7ashjD/8cf8fyIfJAjRr6YcsXnSE8cOGQPq1gqeR8z+3w== + +"@types/chai@^4.2.0": version "4.3.12" resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.3.12.tgz#b192fe1c553b54f45d20543adc2ab88455a07d5e" integrity sha512-zNKDHG/1yxm8Il6uCCVsm+dRdEsJlFoDu73X17y09bId6UwoYww+vFBsAcRzl8knM1sab3Dp1VRikFQwDOtDDw==