Hello developers π
This is Day 2 of my 30 Days of Solidity Challenge where I share my learning journey while building small but useful smart contracts.
π Task for Today
Yesterday, we built a simple contract that stored just a userβs name and bio.
Today, I enhanced that contract to make it feel more like a real user profile system.
We added extra fields like:
- Username
- Bio
- Age
- Location
- Profile Picture (can be an IPFS hash or URL)
- JoinedOn (timestamp when profile was created/updated)
π Smart Contract β Enhanced Profile
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract EnhancedProfile {
struct Profile {
string username;
string bio;
uint256 age;
string location;
string profilePic; // could store IPFS hash or URL
uint256 joinedOn;
}
mapping(address => Profile) private profiles;
event ProfileUpdated(
address indexed user,
string username,
string bio,
uint256 age,
string location,
string profilePic
);
function setProfile(
string memory _username,
string memory _bio,
uint256 _age,
string memory _location,
string memory _profilePic
) public {
profiles[msg.sender] = Profile({
username: _username,
bio: _bio,
age: _age,
location: _location,
profilePic: _profilePic,
joinedOn: block.timestamp
});
emit ProfileUpdated(msg.sender, _username, _bio, _age, _location, _profilePic);
}
function getProfile(address _user)
public
view
returns (
string memory username,
string memory bio,
uint256 age,
string memory location,
string memory profilePic,
uint256 joinedOn
)
{
Profile memory profile = profiles[_user];
return (
profile.username,
profile.bio,
profile.age,
profile.location,
profile.profilePic,
profile.joinedOn
);
}
}
π How It Works
- Struct Profile β Stores all user details.
- Mapping (address => Profile) β Each wallet address is linked to its profile.
- setProfile() β Users can create or update their profile.
- getProfile() β Anyone can fetch a profile by providing the wallet address.
- Event ProfileUpdated β Logs every update, useful for dApp frontends.
β‘ Example Usage
- A user saves their profile:
setProfile("Alice", "I build dApps", 25, "Bangalore, India", "ipfs://QmHashOfPic");
- Anyone can retrieve Aliceβs profile:
getProfile(aliceAddress);
β‘οΈ Returns all details including the timestamp.
π― Learning Outcome
With this task, I learned how to:
β
Store multiple user details on-chain.
β
Use structs + mappings effectively.
β
Add events for tracking profile changes.
β
Design a basic identity system for future dApps.
This is a small but important step towards understanding data storage & retrieval in Solidity. Tomorrow, Iβll move to the next challenge and keep building!
π You can also check out Day 1 blog here: Day 1 of Solidity Challenge β Click Counter
Stay tuned for Day 3 π
β¨ Thanks for reading! If youβre also learning Solidity, letβs connect and build together.
Top comments (0)