-1
pragma solidity ^0.4.19;

contract User {

  mapping(address => uint) private addressToIndex;
  mapping(bytes16 => uint) private usernameToIndex;
  mapping (address => uint) Balance;
  address public owner;


  address[] private addresses;
  bytes16[] private usernames;
  bytes[] private ipfsHashes;
  uint public price;

  function pricing() public {
    owner = msg.sender;
    price = 2 ether;
  }


  function User() public {

    // mappings are virtually initialized to zero values so we need to "waste" the first element of the arrays
    // instead of wasting it we use it to create a user for the contract itself
    addresses.push(msg.sender);
    usernames.push('self');
    ipfsHashes.push('not-available');

  }

  function hasUser(address userAddress) public view returns(bool hasIndeed)
  {
    return (addressToIndex[userAddress] > 0 || userAddress == addresses[0]);
  }

  function usernameTaken(bytes16 username) public view returns(bool takenIndeed)
  {
    return (usernameToIndex[username] > 0 || username == 'Creator');
  }

  function createUser(bytes16 username, bytes ipfsHash) payable public returns(bool success)
  {
    require(!hasUser(msg.sender));
    require(!usernameTaken(username));
    require(msg.value == price);


    addresses.push(msg.sender);
    usernames.push(username);
    ipfsHashes.push(ipfsHash);

    addressToIndex[msg.sender] = addresses.length - 1;
    usernameToIndex[username] = addresses.length - 1;
    Balance[msg.sender] += msg.value;

    return true;
  }

  function updateUser(bytes ipfsHash) public returns(bool success)
  {
    require(hasUser(msg.sender));

    ipfsHashes[addressToIndex[msg.sender]] = ipfsHash;
    return true;
  }

  function getUserCount() public view returns(uint count)
  {
    return addresses.length;
  }

  // get by index
  function getUserByIndex(uint index) public view returns(address userAddress, bytes16 username, bytes ipfsHash) {
    require(index < addresses.length);

    return(addresses[index], usernames[index], ipfsHashes[index]);
  }

  function getAddressByIndex(uint index) public view returns(address userAddress)
  {
    require(index < addresses.length);

    return addresses[index];
  }

  function getUsernameByIndex(uint index) public view returns(bytes16 username)
  {
    require(index < addresses.length);

    return usernames[index];
  }

  function getIpfsHashByIndex(uint index) public view returns(bytes ipfsHash)
  {
    require(index < addresses.length);

    return ipfsHashes[index];
  }

  // get by address
  function getUserByAddress(address userAddress) public view returns(uint index, bytes16 username, bytes ipfsHash) {
    require(index < addresses.length);

    return(addressToIndex[userAddress], usernames[addressToIndex[userAddress]], ipfsHashes[addressToIndex[userAddress]]);
  }

  function getIndexByAddress(address userAddress) public view returns(uint index)
  {
    require(hasUser(userAddress));

    return addressToIndex[userAddress];
  }

  function getUsernameByAddress(address userAddress) public view returns(bytes16 username)
  {
    require(hasUser(userAddress));

    return usernames[addressToIndex[userAddress]];
  }

  function getIpfsHashByAddress(address userAddress) public view returns(bytes ipfsHash)
  {
    require(hasUser(userAddress));

    return ipfsHashes[addressToIndex[userAddress]];
  }

  // get by username
  function getUserByUsername(bytes16 username) public view returns(uint index, address userAddress, bytes ipfsHash) {
    require(index < addresses.length);

    return(usernameToIndex[username], addresses[usernameToIndex[username]], ipfsHashes[usernameToIndex[username]]);
  }

  function getIndexByUsername(bytes16 username) public view returns(uint index)
  {
    require(usernameTaken(username));

    return usernameToIndex[username];
  }

  function getAddressByUsername(bytes16 username) public view returns(address userAddress)
  {
    require(usernameTaken(username));

    return addresses[usernameToIndex[username]];
  }

  function getIpfsHashByUsername(bytes16 username) public view returns(bytes ipfsHash)
  {
    require(usernameTaken(username));

    return ipfsHashes[usernameToIndex[username]];
  }

}

but i got error

  • Hi and welcome to Ethereum StackExchange! Please provide a bit more information. What is the error? What are you trying to do? Which function are you calling? How are you calling the function? – Lauri Peltonen Jan 11 at 19:08
  • @Lauri Peltonen it is not strictly speaking an error. it's just that no ether is sent – Josue Guessennd Jan 11 at 19:25
  • If you mean no Ether is sent when you call the function createUser then it simply means that you are not sending Ether with the transaction. You are probably issuing the transaction wrongly in some way. – Lauri Peltonen Jan 11 at 19:31
  • @Lauri Peltonen . Ohhh ok and how can I do it – Josue Guessennd Jan 11 at 19:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.