We wrote a small computing contract called Hello World. What if we could create a contract that allowed users to create their own counters?

Let’s create our contract, the CounterFactory, which will manage all the other Counters. It will contain a map that associates the owner with the address of its counter contract.

mapping(address => address) counters;
Copy the code

When a user wants to use our counter system to have his own counter, he will need to request that his counter be created.

function createCounter() public {
    if(counters[msg.sender] == 0) { counters[msg.sender] = new Counter(msg.sender); }}Copy the code

Notice that we pass the address of the constructor to the constructor, so we transfer the ownership of the caller. In the constructor of the new smart contract, msG. sender will reference the address of our contract factory. This is a very important point because it is common practice to use contracts to interact with other contracts. Therefore, you should take care of who is the sender in complicated situations.

Now for the delta function, we first check to see if the user has registered the smart contract and call the delta function from the contract. Since the map stores smart addresses, we need to convert the address to the Counter contract type. Storing the address of the contract rather than referencing it directly smart contracts allow us to check if the contract is initialized by using an empty address: 0 or 0x0..

functionincrement() public { require (counters[msg.sender] ! = 0); Counter(counters[msg.sender]).increment(msg.sender); }Copy the code

Finally, to read the value of the counter, we take the user’s address as an argument to get the value of the counter.

function getCount(address account) public constant returns (uint) {
   if(counters[account] ! = 0) {return(Counter(counters[account]).getCount()); }}Copy the code

In this example, we’ll keep it simple but you can imagine a few scenarios, such as needing to send Ether to the createCounter() function so that the original creator of the contract can get some income to do his job. We can also have the original creator remove counters or associate contracts with strings or numbers.

The Counter contract was briefly edited to accommodate the new address passed as a parameter.

Here’s the complete code:

Pragma solidity ^ 0.4.11; contract Counter { address owner; address factory; uint count = 0;function Counter(address _owner) {
        owner = _owner;
        factory = msg.sender
    }

    modifier isOwner(address _caller) {
        require(msg.sender == factory);
        require(_caller == owner);
        _;
    }
    
    function increment(address caller) public isOwner(caller) {
       count = count + 1;
    }

    function getCount() constant returns (uint) {
       return count;
    }

}

contract CounterFactory {
 
    mapping(address => address) counters;

    function createCounter() public {
        if(counters[msg.sender] == 0) { counters[msg.sender] = new Counter(msg.sender); }}functionincrement() public { require (counters[msg.sender] ! = 0); Counter(counters[msg.sender]).increment(msg.sender); }function getCount(address account) public constant returns (uint) {
        if(counters[account] ! = 0) {return(Counter(counters[account]).getCount()); }}}Copy the code

Be aware that our counter could fall victim to an overflow if called more than once. You should use the SafeMath library whenever possible to prevent this.

To deploy our smart contract, you need to provide the code for CounterFactory and Counter. When deploying, you need to select CounterFactory.

After executing the createCounter() function from one of your accounts and calling increment() in the reading section of the contract screen, you need to set the account address to read the counter value. You can now set up a counter for each account.

In the next tutorial, we’ll see how inheritance can be used to keep code clean and reuse existing and tested blocks.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Share some interactive online programming tutorials related to blockchain such as Ethereum, EOS and Bitcoin:

  • Java Ethereum development tutorial, mainly for Java and Android programmers for blockchain Ethereum development web3J details.
  • Python Ethereum is a detailed explanation of blockchain ethereum development by Python engineers using Web3.py.
  • PHP Ethereum, mainly introduces the use of PHP for smart contract development interaction, account creation, transactions, transfers, token development, filters and transactions and other content.
  • Ethereum introductory course, mainly introduces smart contract and DAPP application development, suitable for entry.
  • Ethereum development advanced tutorial, mainly introduces the use of Node.js, mongodb, blockchain, IPFS to achieve decentralized e-commerce DApp combat, suitable for advanced.
  • C# ethereum, mainly explains how to use C# based development. Net ethereum applications, including account management, status and transactions, smart contract development and interaction, filters and transactions, etc.
  • This course will help you get a quick start on the development of EOS blockchain decentralized applications. It covers core knowledge points such as EOS tool chain, account and wallet, issuing tokens, development and deployment of smart contracts, and interaction between codes and smart contracts. Finally, the development of a notepad DApp will be completed using all knowledge points comprehensively.
  • Java development tutorial COINS, this course for beginners, content covers the core concepts of COINS, such as block mechanism, key chain store, decentralized consensus and script, trading and UTXO etc, also explained how to integrate the currency support functions in Java code, such as creating address wallet, tectonic naked trading, management, Is a rare bitcoin development course for Java engineers.
  • PHP currency development tutorial, this course for beginners, content covers the core concepts of COINS, such as block mechanism, key chain store, decentralized consensus and script, trading and UTXO etc, also explained how to integrated the currency support functions in PHP code, such as creating address wallet, tectonic naked trading, management, This is a rare bitcoin development course for Php engineers.
  • Tendermint blockchain development in detail, this course is suitable for engineers who want to use Tendermint blockchain development, the course content includes the core concepts of tendermint application development model, such as ABCI interface, Merkle tree, multi-version state library, etc., also includes the rich practical code such as token issuance. It is the best choice for go language engineers to quickly start blockchain development.

Huizhi net original translation, reprint please indicate the source. Manage multiple Solidity smart contracts in factory mode