diff --git a/contracts/FundMe.sol b/contracts/FundMe.sol index 5512711..df99b98 100644 --- a/contracts/FundMe.sol +++ b/contracts/FundMe.sol @@ -41,12 +41,12 @@ contract FundMe { /***** 6.1 Type Declarations *****/ using PriceConverter for uint256; uint256 public constant MINIMUM_USD = 50 * 1e18; - address[] public s_funders; - address public immutable i_owner; // immutable only can declare 1 time at contructor - AggregatorV3Interface public s_priceFeed; + address[] private s_funders; + address private immutable i_owner; // immutable only can declare 1 time at contructor + AggregatorV3Interface private s_priceFeed; /***** 6.2 State Variable *****/ - mapping(address => uint256) public s_addressToAmountFunded; + mapping(address => uint256) private s_addressToAmountFunded; /***** 6.3 Modifier *****/ modifier onlyOwner() { @@ -94,7 +94,7 @@ contract FundMe { require( msg.value.getConversionRate(s_priceFeed) >= MINIMUM_USD, "Didn't send enough" - ); + ); // Use error code is gas cheaper s_funders.push(msg.sender); s_addressToAmountFunded[msg.sender] = msg.value; } @@ -147,4 +147,23 @@ contract FundMe { (bool callSuccess, ) = i_owner.call{value: address(this).balance}(""); require(callSuccess, "Call failed"); } + + // private variable is gas cheaper + function getOwner() public view returns (address) { + return i_owner; + } + + function getFunders(uint256 index) public view returns (address) { + return s_funders[index]; + } + + function getAddressToAmountFunded( + address funder + ) public view returns (uint256) { + return s_addressToAmountFunded[funder]; + } + + function getPriceFeed() public view returns (AggregatorV3Interface) { + return s_priceFeed; + } } diff --git a/test/unit/FundMeTest.js b/test/unit/FundMeTest.js index 3458168..89db4c4 100644 --- a/test/unit/FundMeTest.js +++ b/test/unit/FundMeTest.js @@ -25,18 +25,18 @@ describe("FundMe", function () { eth1 = ethers.utils.parseEther("1"); // Same 1 ETH describe("constructor", async function () { it("Set the aggregator priceFeedAddress correctly", async function () { - const response = await fundMe.s_priceFeed(); // AggregatorV3Interface + const response = await fundMe.getPriceFeed(); // AggregatorV3Interface //console.log("AggregatorV3Interface" + response); assert.equal(response, mockV3Aggregator.address); }); it("Updated the amount funded data structure", async function () { await fundMe.fund({ value: eth1 }); - const response = await fundMe.s_addressToAmountFunded(deployer); + const response = await fundMe.getAddressToAmountFunded(deployer); assert.equal(response.toString(), eth1.toString()); // Send 1 ETH }); it("Adds funder to array of funders", async function () { await fundMe.fund({ value: eth1 }); - const funder = await fundMe.s_funders(0); + const funder = await fundMe.getFunders(0); assert.equal(funder, deployer); }); }); @@ -80,10 +80,10 @@ describe("FundMe", function () { }); // Check if addr1 is added to funders - expect(await fundMe.s_funders(0)).to.equal(addr1.address); + expect(await fundMe.getFunders(0)).to.equal(addr1.address); // Check if the amount funded is recorded expect( - await fundMe.s_addressToAmountFunded(addr1.address), + await fundMe.getAddressToAmountFunded(addr1.address), ).to.equal(ethers.utils.parseEther("1.0")); }); @@ -110,9 +110,9 @@ describe("FundMe", function () { }), ).not.to.be.reverted; - expect(await fundMe.s_funders(0)).to.equal(addr1.address); + expect(await fundMe.getFunders(0)).to.equal(addr1.address); expect( - await fundMe.s_addressToAmountFunded(addr1.address), + await fundMe.getAddressToAmountFunded(addr1.address), ).to.equal(minFunding); }); }); @@ -191,11 +191,11 @@ describe("FundMe", function () { endingDeployerBalance.add(withdrawGasCost).toString(), ); // Make a getter for storage variables - await expect(fundMe.s_funders(0)).to.be.reverted; + await expect(fundMe.getFunders(0)).to.be.reverted; for (i = 1; i < 6; i++) { assert.equal( - await fundMe.s_addressToAmountFunded(accounts[i].address), + await fundMe.getAddressToAmountFunded(accounts[i].address), 0, ); } @@ -286,11 +286,11 @@ describe("FundMe", function () { endingDeployerBalance.add(withdrawGasCost).toString(), ); // Make a getter for storage variables - await expect(fundMe.s_funders(0)).to.be.reverted; + await expect(fundMe.getFunders(0)).to.be.reverted; for (i = 1; i < 6; i++) { assert.equal( - await fundMe.s_addressToAmountFunded(accounts[i].address), + await fundMe.getAddressToAmountFunded(accounts[i].address), 0, ); }