SOLIDITY TUTORIAL: SIMPLE PROPERTY TRANSFER CONTRACT

Swaroop R
2 min readJul 17, 2021

Lets create a simple property transfer contract in solidity. Our property transfer contract will have a simple interface that allows a user to upload and transfer the property. And only the owner of the property can transfer the property. This is the interface that follows should offer just enough expressiveness to deal with this feature.

contract property {function uploadproperty() public returns(bool) {}function transfer() public returns (bool) {}function propertydetail() public view returns() {}}

Let’s begin writing smart contract,
The owner has to upload the property before transferring it so we will start by taking details like current owner address, property id, previous owner addresses. We will create a struct for creating property details and map it with uint.

struct propertydetails {uint _prodID;
address CurrentOwner;
string _PropertyAddr;
address[] PrevOwner;
}mapping(uint => propertydetails) public details;

Before proceeding further we can also initialize address of the contract owner this is an optional.

address ContractOwner;constructor() public {

ContractOwner = msg.sender;
}

After creating the property detail structure we have provide a property upload function for the above details.

function uploadproperty(uint _prodID, address _CurrentOwner, string memory _PropertyAddr) public returns(bool) {    address[] memory array;
details[_prodID] = propertydetails(_prodID, _CurrentOwner, _PropertyAddr, array);
return true;
}

After uploading property, we have to create a transferring function to transfer the property but this function should only be called by the present owner of the property.

function transfer(uint _prodID, address _NewOwner) public returns (bool) {    require(details[_prodID].CurrentOwner == msg.sender, "YOU ARE NOT THE OWNER OF THIS PROPERTY");
require(details[_prodID].CurrentOwner != _NewOwner);
details[_prodID].PrevOwner.push(details[_prodID].CurrentOwner);
details[_prodID].CurrentOwner = _NewOwner;
return true;
}

We will add a function to get the details of property by providing property id and also get all the past and present owners of property.

function propertydetail(uint _prodID) public view returns(string memory PROPERTY_ADDRESS, address CURRENT_OWNER, address[] memory PREVIOUS_OWNER){    return(details[_prodID]._PropertyAddr, details[_prodID].CurrentOwner, details[_prodID].PrevOwner);}

The final code is given below

pragma solidity ^0.5.0;contract property {address ContractOwner;struct propertydetails {   uint _prodID;   address CurrentOwner;   string _PropertyAddr;   address[] PrevOwner;}mapping(uint => propertydetails) public details;constructor() public {   ContractOwner = msg.sender;}function uploadproperty(uint _prodID, address _CurrentOwner, string memory _PropertyAddr) public returns(bool) {   address[] memory array;   details[_prodID] = propertydetails(_prodID, _CurrentOwner, _PropertyAddr, array);   return true;}function transfer(uint _prodID, address _NewOwner) public returns (bool) {   require(details[_prodID].CurrentOwner == msg.sender, "YOU ARE NOT THE OWNER OF THIS PROPERTY");   require(details[_prodID].CurrentOwner != _NewOwner);   details[_prodID].PrevOwner.push(details[_prodID].CurrentOwner);   details[_prodID].CurrentOwner = _NewOwner;   return true;}function propertydetail(uint _prodID) public view returns(string memory PROPERTY_ADDRESS, address CURRENT_OWNER, address[] memory PREVIOUS_OWNER){   return(details[_prodID]._PropertyAddr, details[_prodID].CurrentOwner, details[_prodID].PrevOwner);}}

Conclusion

This is the decentralize simple property transfer for understanding the solidity and to build strong basic in solidity.

--

--