From 5612340e093269508b5c5c178bf41c1d0d4a682a Mon Sep 17 00:00:00 2001 From: JesusRGil <108557695+JRGIL12@users.noreply.github.com> Date: Sat, 22 Mar 2025 05:00:42 -0400 Subject: [PATCH 1/4] initial commit --- .env | 1 + .gitignore | 1 + README.md | 19 +++++++++++++++++++ contracts/factory.sol | 19 +++++++++++++++++++ scripts/deploy.js | 23 +++++++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 .env create mode 100644 .gitignore create mode 100644 README.md create mode 100644 contracts/factory.sol create mode 100644 scripts/deploy.js diff --git a/.env b/.env new file mode 100644 index 0000000..82789f2 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +# This file is intentionally left blank. \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..381e9c6 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Factory + +## Testing Branch for Factory Contract + +This branch is dedicated to creating a factory contract in Solidity. A factory contract is a smart contract that is capable of deploying other contracts. This pattern is useful for scenarios where you need to create multiple instances of a contract with the same code but different states. + +### Key Features + +- **Automated Deployment**: The factory contract can deploy new contracts automatically. +- **Centralized Management**: Manage all deployed contracts from a single factory contract. +- **Efficiency**: Reduce gas costs by reusing the same contract code. + +### Example + +Here is a simple example of a factory contract in Solidity: + +### Conclusion + +Using a factory contract can simplify the deployment and management of multiple contracts, making your Solidity projects more efficient and maintainable. diff --git a/contracts/factory.sol b/contracts/factory.sol new file mode 100644 index 0000000..f8044d2 --- /dev/null +++ b/contracts/factory.sol @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract SimpleContract { + address public owner; + + constructor(address _owner) { + owner = _owner; + } +} + +contract Factory { + SimpleContract[] public contracts; + + function createContract() public { + SimpleContract newContract = new SimpleContract(msg.sender); + contracts.push(newContract); + } +} \ No newline at end of file diff --git a/scripts/deploy.js b/scripts/deploy.js new file mode 100644 index 0000000..9786a0c --- /dev/null +++ b/scripts/deploy.js @@ -0,0 +1,23 @@ +const hre = require("hardhat"); + +async function main() { + await hre.run("compile"); + + const [deployer] = await hre.ethers.getSigners(); + + console.log("Deploying contracts with the account:", deployer.address); + + const ProjectFactory = await hre.ethers.getContractFactory("ProjectFactory"); + const projectFactory = await ProjectFactory.deploy(); + + await projectFactory.deployed(); + + console.log("ProjectFactory deployed to:", projectFactory.address); +} + +main() + .then(() => process.exit(0)) + .catch((error) => { + console.error(error); + process.exit(1); + }); From e3c6db470afaf689e22e498d63c665eef3048bdd Mon Sep 17 00:00:00 2001 From: JesusRGil <108557695+JRGIL12@users.noreply.github.com> Date: Sat, 22 Mar 2025 05:20:30 -0400 Subject: [PATCH 2/4] update createContract function to return the address of the newly created SimpleContract --- contracts/factory.sol | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/contracts/factory.sol b/contracts/factory.sol index f8044d2..468b8b0 100644 --- a/contracts/factory.sol +++ b/contracts/factory.sol @@ -12,8 +12,9 @@ contract SimpleContract { contract Factory { SimpleContract[] public contracts; - function createContract() public { + function createContract() public returns (address) { SimpleContract newContract = new SimpleContract(msg.sender); contracts.push(newContract); + return address(newContract); } } \ No newline at end of file From 55e372f7b4ebd3d852e78483d5fb7b02a3b16213 Mon Sep 17 00:00:00 2001 From: JesusRGil <108557695+JRGIL12@users.noreply.github.com> Date: Sat, 22 Mar 2025 05:26:15 -0400 Subject: [PATCH 3/4] update SimpleContract to include a phrase and modify createContract to accept it as a parameter --- contracts/factory.sol | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/contracts/factory.sol b/contracts/factory.sol index 468b8b0..29d3abd 100644 --- a/contracts/factory.sol +++ b/contracts/factory.sol @@ -3,17 +3,23 @@ pragma solidity ^0.8.0; contract SimpleContract { address public owner; + string public phrase; - constructor(address _owner) { + constructor(address _owner, string memory _phrase) { owner = _owner; + phrase = _phrase; + } + + function getPhrase() public view returns (string memory) { + return phrase; } } contract Factory { SimpleContract[] public contracts; - function createContract() public returns (address) { - SimpleContract newContract = new SimpleContract(msg.sender); + function createContract(string memory _phrase) public returns (address) { + SimpleContract newContract = new SimpleContract(msg.sender, _phrase); contracts.push(newContract); return address(newContract); } From 21147d725ef60208c1114dc6f9ce1195aed9a70a Mon Sep 17 00:00:00 2001 From: JesusRGil <108557695+JRGIL12@users.noreply.github.com> Date: Sat, 22 Mar 2025 05:27:21 -0400 Subject: [PATCH 4/4] add documentation for SimpleContract and Factory contracts --- docs/factory.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 docs/factory.md diff --git a/docs/factory.md b/docs/factory.md new file mode 100644 index 0000000..63b9735 --- /dev/null +++ b/docs/factory.md @@ -0,0 +1,33 @@ +# Solidity API + +## SimpleContract + +### Contract +SimpleContract : contracts/factory.sol + + --- +### Functions: +### constructor + +```solidity +constructor(address _owner, string _phrase) public +``` + +### getPhrase + +```solidity +function getPhrase() public view returns (string) +``` + +## Factory + +### Contract +Factory : contracts/factory.sol + + --- +### Functions: +### createContract + +```solidity +function createContract(string _phrase) public returns (address) +``` \ No newline at end of file