diff --git a/hardhat/contracts/PiggyBankMaster.sol b/hardhat/contracts/PiggyBankMaster.sol index e467dbe..ae4b591 100644 --- a/hardhat/contracts/PiggyBankMaster.sol +++ b/hardhat/contracts/PiggyBankMaster.sol @@ -9,22 +9,23 @@ contract PiggyBankMaster is IPiggyBankObserver { string piggyBankType; } - mapping(address => bool) private factories; - mapping(string => bool) private piggyBankTypes; + mapping(address => bool) private registeredFactories; + mapping(string => bool) private registeredPiggyBankTypes; mapping(address => string) private factoryTypes; mapping(address => PiggyBankDetails[]) piggyBanksByOwner; + mapping(address => string) piggyBankTypes; function registerPiggyBankFactory( string memory _piggyBankType, address _factory ) external { // TODO: Add authorizatoin check. - require(!factories[_factory], "Factory is already registered!"); - require(!piggyBankTypes[_piggyBankType], "Piggy bank type is already in use!"); + require(!registeredFactories[_factory], "Factory is already registered!"); + require(!registeredPiggyBankTypes[_piggyBankType], "Piggy bank type is already in use!"); - factories[_factory] = true; - piggyBankTypes[_piggyBankType] = true; + registeredFactories[_factory] = true; + registeredPiggyBankTypes[_piggyBankType] = true; factoryTypes[_factory] = _piggyBankType; } @@ -32,12 +33,16 @@ contract PiggyBankMaster is IPiggyBankObserver { address _owner, address _newPiggyBank ) override external { - require(factories[msg.sender], "Not a known factory!"); + require(registeredFactories[msg.sender], "Not a known factory!"); + + string memory piggyBankType = factoryTypes[msg.sender]; piggyBanksByOwner[_owner].push(PiggyBankDetails({ piggyBankAddress: _newPiggyBank, - piggyBankType: factoryTypes[msg.sender] + piggyBankType: piggyBankType })); + + piggyBankTypes[_newPiggyBank] = piggyBankType; } function getPiggyBanksByOwner( @@ -45,4 +50,10 @@ contract PiggyBankMaster is IPiggyBankObserver { ) external view returns (PiggyBankDetails[] memory) { return piggyBanksByOwner[_owner]; } + + function getPiggyBankType( + address _piggyBank + ) external view returns (string memory) { + return piggyBankTypes[_piggyBank]; + } } \ No newline at end of file diff --git a/hardhat/test/PiggyBankMaster.js b/hardhat/test/PiggyBankMaster.js index 560f676..ac389f6 100644 --- a/hardhat/test/PiggyBankMaster.js +++ b/hardhat/test/PiggyBankMaster.js @@ -118,4 +118,39 @@ describe("PiggyBankMaster", function () { expect(await piggyBankMaster.getPiggyBanksByOwner(owner.address)).to.be.empty; }); }); + + describe("Resolving Piggy Bank Type", function () { + it("Should resolve Piggy Bank Type by Piggy Bank address", async function () { + const { piggyBankMaster, owner } = await loadFixture(deployPiggyBankMasterFixture); + + const AmountPiggyBankFactory = await ethers.getContractFactory("AmountPiggyBankFactory"); + const amountPiggyBankFactory = await AmountPiggyBankFactory.deploy(piggyBankMaster.address); + + const expectedPiggyBankType = "Approve"; + + const registrationTx = await piggyBankMaster.registerPiggyBankFactory(expectedPiggyBankType, amountPiggyBankFactory.address); + await registrationTx.wait(); + + const description = "Amount Piggy Bank (1)"; + const targetAmount = 42; + + const amountPiggyBankAddress = await amountPiggyBankFactory.callStatic.createAmountPiggyBank( + owner.address, + description, + targetAmount); + + const creationTx = await amountPiggyBankFactory.createAmountPiggyBank( + owner.address, + description, + targetAmount); + + await creationTx.wait(); + + const actualPiggyBankType = await piggyBankMaster.callStatic.getPiggyBankType( + amountPiggyBankAddress + ); + + expect(actualPiggyBankType).to.be.eq(expectedPiggyBankType); + }); + }); });