Skip to content

Commit c48f86f

Browse files
authored
fix: retrieve decimals from asset (#3)
* fix: retrieve decimals from asset * chore: bump oz version
1 parent 11e9e99 commit c48f86f

File tree

6 files changed

+61
-14
lines changed

6 files changed

+61
-14
lines changed

contracts/TimeLockVault.sol

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ abstract contract TimeLockVault is ERC20Upgradeable, ITimeLockVault, TimeLockVau
9999
return _asset.balanceOf(address(this));
100100
}
101101

102+
function decimals()
103+
public
104+
view
105+
virtual
106+
override(ERC20Upgradeable, IERC20MetadataUpgradeable)
107+
returns (uint8)
108+
{
109+
if (address(_asset) == address(0)) {
110+
revert AssetUndefined();
111+
}
112+
return _asset.decimals();
113+
}
114+
102115
function _setAsset(address asset_) internal {
103116
_asset = IERC20MetadataUpgradeable(asset_);
104117
}

contracts/interfaces/TimeLockVaultErrors.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ interface TimeLockVaultErrors {
66

77
error InvalidActiveDeposit();
88

9+
error AssetUndefined();
10+
911
error DepositNotMatured();
1012

1113
error DepositAlreadyMatured();

contracts/mocks/MockTimeLockVault.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ contract MockTimeLockVault is SimpleTimeLockVault {
1818
) public returns (DepositInfo memory) {
1919
return _prematureWithdraw(from, to, depositId);
2020
}
21+
22+
function setAssetInternal(address asset_) public {
23+
_setAsset(asset_);
24+
}
2125
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
"typescript": "^4.9.5"
5656
},
5757
"dependencies": {
58-
"@openzeppelin/contracts": "^4.8.3",
59-
"@openzeppelin/contracts-upgradeable": "^4.8.3"
58+
"@openzeppelin/contracts": "^4.9.3",
59+
"@openzeppelin/contracts-upgradeable": "^4.9.3"
6060
},
6161
"files": [
6262
"/contracts"

test/TimeLockVault/time-lock-vault-setup.spec.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
22
import { expect } from "chai";
3+
import { constants } from "ethers";
34

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

1718
describe("Setup", () => {
18-
it("should return the correct asset contract address", async () => {
19-
const asset = await mockVaultContract.asset();
19+
describe("When asset is specified", () => {
20+
it("should return the correct asset contract address", async () => {
21+
const asset = await mockVaultContract.asset();
2022

21-
expect(asset).to.equal(fixtures.mockERC20Contract.address);
23+
expect(asset).to.equal(fixtures.mockERC20Contract.address);
24+
});
25+
26+
it("should have the same decimals as asset contract", async () => {
27+
const decimals = await mockVaultContract.decimals();
28+
const assetDecimals = await fixtures.mockERC20Contract.decimals();
29+
30+
expect(decimals).to.equal(assetDecimals);
31+
});
32+
});
33+
34+
describe("When asset is zero address", () => {
35+
beforeEach(async () => {
36+
await mockVaultContract
37+
.connect(fixtures.signers.deployer)
38+
.setAssetInternal(constants.AddressZero);
39+
40+
// Assert that asset is zero address
41+
const asset = await mockVaultContract.asset();
42+
expect(asset).to.equal(constants.AddressZero);
43+
});
44+
45+
it("should revert when returning decimals", async () => {
46+
const tx = mockVaultContract.decimals();
47+
48+
await expect(tx).to.be.revertedWithCustomError(mockVaultContract, "AssetUndefined");
49+
});
2250
});
2351
});
2452
});

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -901,15 +901,15 @@
901901
table "^6.8.0"
902902
undici "^5.14.0"
903903

904-
"@openzeppelin/contracts-upgradeable@^4.8.3":
905-
version "4.8.3"
906-
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts-upgradeable/-/contracts-upgradeable-4.8.3.tgz#6b076a7b751811b90fe3a172a7faeaa603e13a3f"
907-
integrity sha512-SXDRl7HKpl2WDoJpn7CK/M9U4Z8gNXDHHChAKh0Iz+Wew3wu6CmFYBeie3je8V0GSXZAIYYwUktSrnW/kwVPtg==
908-
909-
"@openzeppelin/contracts@^4.8.3":
910-
version "4.8.3"
911-
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.8.3.tgz#cbef3146bfc570849405f59cba18235da95a252a"
912-
integrity sha512-bQHV8R9Me8IaJoJ2vPG4rXcL7seB7YVuskr4f+f5RyOStSZetwzkWtoqDMl5erkBJy0lDRUnIR2WIkPiC0GJlg==
904+
"@openzeppelin/contracts-upgradeable@^4.9.3":
905+
version "4.9.3"
906+
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts-upgradeable/-/contracts-upgradeable-4.9.3.tgz#ff17a80fb945f5102571f8efecb5ce5915cc4811"
907+
integrity sha512-jjaHAVRMrE4UuZNfDwjlLGDxTHWIOwTJS2ldnc278a0gevfXfPr8hxKEVBGFBE96kl2G3VHDZhUimw/+G3TG2A==
908+
909+
"@openzeppelin/contracts@^4.9.3":
910+
version "4.9.3"
911+
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.9.3.tgz#00d7a8cf35a475b160b3f0293a6403c511099364"
912+
integrity sha512-He3LieZ1pP2TNt5JbkPA4PNT9WC3gOTOlDcFGJW4Le4QKqwmiNJCRt44APfxMxvq7OugU/cqYuPcSBzOw38DAg==
913913

914914
"@scure/base@~1.1.0":
915915
version "1.1.1"

0 commit comments

Comments
 (0)