Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve type of Piggy Bank #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions hardhat/contracts/PiggyBankMaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,51 @@ 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;
}

function handlePiggyBankCreated(
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(
address _owner
) external view returns (PiggyBankDetails[] memory) {
return piggyBanksByOwner[_owner];
}

function getPiggyBankType(
address _piggyBank
) external view returns (string memory) {
return piggyBankTypes[_piggyBank];
}
}
35 changes: 35 additions & 0 deletions hardhat/test/PiggyBankMaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});