Skip to content

Commit

Permalink
add new Vault for positive tests cases, refactor vault contract
Browse files Browse the repository at this point in the history
  • Loading branch information
maksimKrukovich committed Apr 11, 2024
1 parent 539b05c commit 4e471a5
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 89 deletions.
2 changes: 2 additions & 0 deletions contracts/erc4626/IERC4626.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
//////////////////////////////////////////////////////////////*/
Expand Down
7 changes: 2 additions & 5 deletions contracts/erc4626/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -61,8 +60,6 @@ contract HederaVault is IERC4626 {
newTokenAddress = SafeHTS.safeCreateFungibleToken(newToken, 0, _underlying.decimals());
emit createdToken(newTokenAddress);
asset = _underlying;

tokenAddress = rewardTokens;
}

struct UserInfo {
Expand All @@ -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);

Expand Down
1 change: 1 addition & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
11 changes: 0 additions & 11 deletions scripts/deployVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
161 changes: 89 additions & 72 deletions test/erc4626/vault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
Expand All @@ -44,35 +49,49 @@ 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()
);

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,
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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;
});
});
});
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 4e471a5

Please sign in to comment.