Skip to content

Instantly share code, notes, and snippets.

@marceljay
Created June 15, 2021 17:00
Show Gist options
  • Save marceljay/1c3e86024c4c33eee4844e6dbf6a3970 to your computer and use it in GitHub Desktop.
Save marceljay/1c3e86024c4c33eee4844e6dbf6a3970 to your computer and use it in GitHub Desktop.
factory
// SPDX-License-Identifier: MIT
// Solidity files have to start with this pragma.
// It will be used by the Solidity compiler to validate its version.
pragma solidity ^0.8.0;
import "hardhat/console.sol"; // dev & testing
import "./ProjectToken.sol";
contract PTokenFactory {
event TokenCreated(address tokenAddress);
address[] private deployedContracts;
mapping (string => address) public pContractRegistry;
// struct pToken {
// string name;
// string symbol;
// string vintage;
// string standard;
// string country;
// }
function deployNewToken(
string memory _name,
string memory _symbol,
string memory _vintage,
string memory _standard,
string memory _country)
public {
console.log("DEBUG: deploying new pERC20");
string memory _pTokenIdentifier = append(_name, _symbol, _vintage, _standard, _country);
console.log(_pTokenIdentifier);
require(!checkExistence(_pTokenIdentifier), "Matching pERC20 already exists");
ProjectToken t = new ProjectToken(_name, _symbol, _vintage, _standard, _country);
deployedContracts.push(address(t));
console.log("Deployed new pERC20 at ", address(t));
pContractRegistry[_pTokenIdentifier] = address(t);
emit TokenCreated(address(t));
}
// Helper function to create unique pERC20 identifying string
function append(string memory a, string memory b, string memory c, string memory d, string memory e)
internal pure returns (string memory) {
return string(abi.encodePacked(a, b, c, d, e));
}
// Checks if same pToken has already been deployed
function checkExistence(string memory _pTokenIdentifier) internal view returns (bool) {
if (pContractRegistry[_pTokenIdentifier] == address(0)) {
console.log("DEBUG: checkExistence: false");
return false;
}
else {
console.log("DEBUG: checkExistence: true");
return true;
}
}
// Lists addresses of deployed contracts
function getContracts() public view returns (address[] memory) {
for (uint256 i = 0; i < deployedContracts.length; i++) {
console.log("sol: logging contract",i, deployedContracts[i]);
}
// console.log(deployedContracts);
return deployedContracts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment