0

This question already has an answer here:

I have an application in which I have to put each user information on a different block to get the block unique address, do I have to deploy contract every time I will add new user information?

marked as duplicate by Achala Dissanayake, Thomas Jay Rush, Eli Drion, flygoing, eth May 4 '18 at 7:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • The answer suggested by you doesn't fit in my case. I have to create a unique block for every user. Can you please suggest me a good approach to achieve that? – Anubhav Gupta Apr 25 '18 at 12:58
  • 3
    What do you mean by 'block'? – Henk Apr 25 '18 at 12:59
  • I mean unique address for every user like when we deploy a smart contract it returns a unique address of the contract. So, It's a block for me. – Anubhav Gupta Apr 30 '18 at 4:32
  • Ok so by block you mean a new smart contract – Henk Apr 30 '18 at 6:13
1

Ok, so you want a factory contract. You could do it this way, but I still do suggest you use a mapping.

First, deploy the UserFactorycontract. Each time you want to store new user information, call createUser. It emits the event UserCreated, which contains the index at which the user was stored.

pragma solidity ^0.4.22;

contract User {
    uint256 public id;
    bytes32 public name;
    uint8 public age;

    constructor( uint256 _id, bytes32 _name, uint8 _age) public {
        name = _name;
        id = _id;
        age = _age;
    }
}

contract UserFactory {
    User[] public users;
    function createUser(uint256 _id, bytes32 _name, uint8 _age) external {
        User user = new User(_id, _name, _age);

        emit UserCreated(user, users.length);

        users.push(user);
    }

    event UserCreated(User user, uint256 index);
}
  • Sorry, Henk! I am a fresher to ethereum, I didn't know exactly what I was asking. I just figure out when I deploy a contract and then call its method using send() I am getting a blockhash, blockNumber and transaction ID in response. Is there a block is created for every transaction I do and parameter I pass to the function get stored on the new block without overriding previous user data? And how to view the data stored on that blocks? What's the difference between using this process and the one you suggested in your answer? – Anubhav Gupta Apr 30 '18 at 9:13
  • Maybe you should read the Solidity documentation before you start to create anything with it. Also, please read up on how blockchain technology actually works, as it seems you are not very familiar with the internals of Ethereum and how smart contracts are executed. You might want to check out this video about blockchain and this video about smart contracts – Henk Apr 30 '18 at 9:17
  • Okay. Thank you. I'll definitely go through this. But at this moment it's kinda urgent for me. Can you please help me? – Anubhav Gupta Apr 30 '18 at 9:26
  • I am now adding a transaction for each set of new data and getting transaction id in return. Now, How can I see my saved data using that transaction Id? – Anubhav Gupta Apr 30 '18 at 10:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.