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.
address contractAddress = address(new UserMessage(message));
– Chericheria