I am trying to create a contract for "Land Registry".

A property can be sold by the user who owns it. I've created a function transferProperty(address _from, address _to, uint _propertyId) which transfers the ownership of the property from '_from' to '_to'.

I want to create a function getHistory(uint _propertyId) that returns me the address of it's past owners.

eg, Let user1, user2, user3, user4 be the four users and 1 be the property ID. transferProperty(user1, user2, 1) // property 1 transferred from user1 to user2. transferProperty(user2, user3, 1) // property 1 transferred from user2 to user3. transferProperty(user3, user4, 1) // property 1 transferred from user3 to user4.

I want getHistory(1) to return user1->user2->user3->user4.


If your contract needs to read the history data, you will need to explicitly put each item of the history in contract storage. For example, you could keep a mapping for each property, holding a list of addresses:

mapping (uint256 => address[]) public property_ownerships;

However, if you are using a public chain for this then it will be fairly expensive, as every transfer needs to add an entry to the property_ownerships list.

If you want to be able to consult the list of transfers but you don't have any contract logic that depends on the historical data, it is more practical and efficient to use events, also known as logs. This is data that can be consulted by an external program (such as your JavaScript application) but doesn't need to be read by the contract.

See the this tutorial for an example of how events are used.

  • How can I listen to the triggered event? Where do I have to put the code and if at some point of time I wish to get the history of all of the previous owners of a particular property (using the property id), how can I do that? Aug 9 '17 at 12:59
  • See this for a JavaScript example: ethereumbuilders.gitbooks.io/guide/content/en/… - you can set a filter parameter of your property ID, then have it return all the transfers for that ID. Aug 9 '17 at 13:54
  • I am a beginner in solidity and blockchain. Can you suggest me some book or course which I can refer to make the concepts more clear and learn more about the topics. Aug 10 '17 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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