Reputation: 1574
If a smart contract has the compiled bytecode for a contract can the first deploy a subsequent contract?
According to Ethereum.org: "To deploy a smart contract, you merely send an Ethereum transaction containing the compiled code of the smart contract without specifying any recipient."
I looked for how to send a transaction via smart contracts and the closest I could find were examples for transferring ETH...
_to.transfer(msg.value);
bool sent = _to.send(msg.value);
(bool sent, bytes memory data) = _to.call{value: msg.value}("");
Upvotes: 5
Views: 6588
Reputation: 23
Yes, you can always deploy a smart contract from another smart contracts. You may familiar with the term Smart Contract Factory, Factory contracts are used to deploy new smart contracts. Following code is a simple Factory contract to create ERC20 Tokens that you can refer. But if you want to master this I will add some links at the end of the answer.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Import the ERC-20 interface
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
// Factory contract to create ERC-20 token contracts
contract TokenFactory {
// Event emitted when a new token is created
event TokenCreated(address indexed creator, address indexed tokenAddress);
// Function to create a new ERC-20 token
function createToken(string memory _name, string memory _symbol, uint256 _initialSupply) external {
// Deploy a new ERC-20 token contract
ERC20Token newToken = new ERC20Token(_name, _symbol, _initialSupply, msg.sender);
// Emit an event with the creator's address and the new token's address
emit TokenCreated(msg.sender, address(newToken));
}
}
// ERC-20 token contract
contract ERC20Token is ERC20 {
// Address of the creator
address public creator;
// Constructor to initialize the token
constructor(
string memory _name,
string memory _symbol,
uint256 _initialSupply,
address _creator
) ERC20(_name, _symbol) {
// Mint initial supply to the creator
_mint(_creator, _initialSupply);
// Set the creator's address
creator = _creator;
}
}
Refer the article at https://www.quicknode.com/guides/ethereum-development/smart-contracts/how-to-create-a-smart-contract-factory-in-solidity-using-hardhat to get a broader knowledge on Contract Factories.
Upvotes: 0
Reputation: 19
To deploy smart contract in smart contract. you have to import smart contract code and call construct by using new keyword.
import "A.sol";
A a = new A(argumnet) // construct arguments
address(a) // deployed smart contract address
Upvotes: 0
Reputation: 49661
Another way is to call the parent's constructor if you are inheriting. For example, let's say you have "Name" contract
contract Name {
string public name;
constructor(string memory _name) {
name = _name;
}
}
Now my base contract should inherit from "Name" contract
// I deployed a new Name contract
contract Base is Name("Ronaldo"){}
Upvotes: 0
Reputation: 83
Yes It can, and it's called Factory Pattern contracts
contract Child {
string public name;
string public gender;
constructor(string memory _name, string memory _gender) {
name = _name;
gender = _gender;
}
}
contract Parent {
Child public childContract;
function createChild(string memory _name, string memory _gender) public returns(Child) {
childContract = new Child(_name, _gender); // creating new contract inside another parent contract
return childContract;
}
}
New Keyword is used to create contract in another contract function.
contract Child {
string public name;
string public gender;
constructor(string memory _name, string memory _gender) payable {
name = _name;
gender = _gender;
}
}
contract Parent{
Child public childContract;
function createChild(string memory _name, string memory _gender) public payable returns(Child) {
require(msg.value == 0.005 ether)
childContract = new Child{value: msg.value}(_name, _gender); // creating new contract inside another parent contract
return childContract;
}
}
Upvotes: 4
Reputation: 858
to make a smart contract deploy another smart contract you can do it with the use of the new keyword
contract UserMessage {
string message;
constructor(string memory message){
message = _message;
}
}
contract DeployUserMessage {
mapping(address => address) userToContract;
function Deploy(string memory message) public {
address contractAddress = new UserMessage(message);
userToContract[msg.sender] = contractAddress;
}
}
I think this example make it clear, but if you want, check the docs https://docs.soliditylang.org/en/v0.8.9/contracts.html#creating-contracts
Upvotes: 8