Skip to content

Commit

Permalink
fix: retrieve decimals from asset
Browse files Browse the repository at this point in the history
  • Loading branch information
superical committed Oct 1, 2023
1 parent 11e9e99 commit e6809c3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
13 changes: 13 additions & 0 deletions contracts/TimeLockVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ abstract contract TimeLockVault is ERC20Upgradeable, ITimeLockVault, TimeLockVau
return _asset.balanceOf(address(this));
}

function decimals()
public
view
virtual
override(ERC20Upgradeable, IERC20MetadataUpgradeable)
returns (uint8)
{
if (address(_asset) == address(0)) {
revert AssetUndefined();
}
return _asset.decimals();
}

function _setAsset(address asset_) internal {
_asset = IERC20MetadataUpgradeable(asset_);
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/interfaces/TimeLockVaultErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ interface TimeLockVaultErrors {

error InvalidActiveDeposit();

error AssetUndefined();

error DepositNotMatured();

error DepositAlreadyMatured();
Expand Down
4 changes: 4 additions & 0 deletions contracts/mocks/MockTimeLockVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ contract MockTimeLockVault is SimpleTimeLockVault {
) public returns (DepositInfo memory) {
return _prematureWithdraw(from, to, depositId);
}

function setAssetInternal(address asset_) public {
_setAsset(asset_);
}
}
34 changes: 31 additions & 3 deletions test/TimeLockVault/time-lock-vault-setup.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import { constants } from "ethers";

import { MockTimeLockVault } from "../../types";
import { deployMockTimeLockVaultFixture } from "./time-lock-vault.fixture";
Expand All @@ -15,10 +16,37 @@ describe("Time-Lock Vault", () => {
});

describe("Setup", () => {
it("should return the correct asset contract address", async () => {
const asset = await mockVaultContract.asset();
describe("When asset is specified", () => {
it("should return the correct asset contract address", async () => {
const asset = await mockVaultContract.asset();

expect(asset).to.equal(fixtures.mockERC20Contract.address);
expect(asset).to.equal(fixtures.mockERC20Contract.address);
});

it("should have the same decimals as asset contract", async () => {
const decimals = await mockVaultContract.decimals();
const assetDecimals = await fixtures.mockERC20Contract.decimals();

expect(decimals).to.equal(assetDecimals);
});
});

describe("When asset is zero address", () => {
beforeEach(async () => {
await mockVaultContract
.connect(fixtures.signers.deployer)
.setAssetInternal(constants.AddressZero);

// Assert that asset is zero address
const asset = await mockVaultContract.asset();
expect(asset).to.equal(constants.AddressZero);
});

it("should revert when returning decimals", async () => {
const tx = mockVaultContract.decimals();

await expect(tx).to.be.revertedWithCustomError(mockVaultContract, "AssetUndefined");
});
});
});
});

0 comments on commit e6809c3

Please sign in to comment.