pragma solidity ^0.7.1; pragma experimental ABIEncoderV2; contract Bio { string public name = "Bio"; address public owner; struct UsernameReservation{ string username; uint256 count; bool exists; } mapping (string => UsernameReservation) usernameCount; struct Username { string username; uint256 issue; } mapping (address => Username[]) walletUsernames; mapping (string => address) usernameOwnership; struct UserBio { string description; string twitterUsername; string location; } mapping (string => UserBio) bios; constructor() { owner = msg.sender; } /** START Helpers */ function uint2str(uint _i) internal pure returns (string memory _uintAsString) { if (_i == 0) { return "0"; } uint j = _i; uint len; while (j != 0) { len++; j /= 10; } bytes memory bstr = new bytes(len); uint k = len; while (_i != 0) { k = k-1; uint8 temp = (48 + uint8(_i - _i / 10 * 10)); bytes1 b1 = bytes1(temp); bstr[k] = b1; _i /= 10; } return string(bstr); } /** END Helpers */ function reserveUsername(string calldata username) external returns (Username memory) { // allocate the issue ID UsernameReservation memory u = usernameCount[username]; if (!u.exists) { u.count = 1; u.exists = true; } else { u.count += 1; } // update the mapping usernameCount[username] = u; // allocate the username object Username memory newUsername = Username({username: username, issue: u.count}); string memory id = string(abi.encodePacked(newUsername.username, "|", uint2str(u.count))); // update the wallet owners list of usernames walletUsernames[msg.sender].push(newUsername); usernameOwnership[id] = msg.sender; return newUsername; } function getUsernames() external view returns (Username[] memory) { return walletUsernames[msg.sender]; } function doesOwnBio(string calldata id, address adr) public view returns (bool) { return usernameOwnership[id] == adr; } function getBio(string calldata id) public view returns (UserBio memory) { return bios[id]; } function updateBio(string calldata id, string calldata description, string calldata twitterUsername, string calldata location) external { require(doesOwnBio(id, msg.sender)); UserBio memory bio = bios[id]; bio.description = description; bio.twitterUsername = twitterUsername; bio.location = location; bios[id] = bio; } }
0.7.1