0
pragma solidity ^0.4.17;
//  I want to write a code for  loyalty points when user pays 
contract GiveRefreceId {

    struct  referenceids {
        uint256 rid ;
        address addressrid;
}
    mapping(uint256 => referenceids) referenceid;
    referenceids[] lenrefrenceid;

function giveRefrenceId() public returns (uint256)  { 
    for (uint256 i=0 ;i<=lenrefrenceid.length; i++){

        require( lenrefrenceid[i]  != msg.sender);
    } 

    uint256 lenvarble = lenrefrenceid.length;

    referenceids  storage temprid = referenceid[lenvarble];

    temprid.rid = lenvarble;
    temprid.addressrid = msg.sender;
    lenrefrenceid.push(temprid);
    return lenvarble;
}

function  getrefrnceId()  view returns(uint256) {

return ((lenrefrenceid.length)-1);

}
}

1)I have written a function to give reference id's to the user's

2)I want to restrict the user to call only once the giverefrenceid function so i taught of writing for loop and check the address is present or not in the Array

3)but i am getting error

Operator != not compatible with types struct GiveRefreceId.referenceids storage ref and address
        require( lenrefrenceid[i]  != msg.sender);

4) I know it is complaining that struct and require coudn't be used but i coudn't find any other option to check

5)The gas will be costing more if the numbers increase Any way to avoid for loop,and check the user is calling the function for the first time

6)I don't want to write two functions i want to get id by using one function is there any way ?

  • could you describe what this code should do? I mean the main functionality. This looks overcomplicated, you have a struct, a mapping of those structs, and also an array of the same structs. – Jaime Oct 31 '18 at 10:25
  • Hi jame first thanks for your response I am trying to give a refrence id to user and i want to use that refrence id to give some loyality points in the next part of code for time being i taught giving refrence id with the length of the array would be good so that i can get the address of the refreer person and give loyality points and i want to restrict the function to be called only once by any user – md venkatesh Oct 31 '18 at 10:33
  • See my answer and let me know. – Jaime Oct 31 '18 at 10:35
  • No it is not working plese see the following link remix.ethereum.org/… – md venkatesh Oct 31 '18 at 10:53
1

The code below should do.

The function giveReferenceId assigns an id to a user based on the address. The array clients save a list of users (just in case you need that list at some point).

To be sure that the same address only gets one id, the line: require(referenceid[msg.sender]==0); is used, as 0 is an invalid Id, meaning that the user is not registered.

The function getrefrnceId() return the id of the user that executes the function.

pragma solidity ^0.4.25;

contract GiveRefreceId {

    mapping(address => uint256) public referenceid;
    address[] public clients;
    uint256 idcounter;

    event createdId(address user, uint256 id);

    function giveRefrenceId() public returns (uint256)  { 
        require(referenceid[msg.sender]==0);
        idcounter += 1;
        clients.push(msg.sender);
        referenceid[msg.sender] = idcounter;
        emit createdId(masg.sender,idcounter);
        return idcounter;

    }


    function  getrefrnceId() public view returns(uint256) {
        return referenceid[msg.sender];
    }
}

Hope this helps

  • Sorry it is not working with same id i am able to create 2 refrence ids remix.ethereum.org/… – md venkatesh Oct 31 '18 at 10:49
  • you mean with the same address you can register two different Ids?, this is no possible. Also, I run this in remix, if you try to execute giveReferenceId twice with the same address the transactions revert – Jaime Oct 31 '18 at 10:54
  • No i want the user must call Give refrence id function() only once he shouldn't be able to call it twice – md venkatesh Oct 31 '18 at 10:58
  • You seem to be confused. You can not prevent the user from calling the function, what you can do is to reject the call by reverting if the subject already has called it, such that he/she doesn't create two different ids with the same address, and this is exactly what the contract does. – Jaime Oct 31 '18 at 11:00
  • Thanks john i want same functionality what you can do is to reject the call by reverting if the subject already has called it, such that he/she doesn't create two different ids with the same address, and this is exactly what the contract does But just see the transcations on etherscan with same address i called 3 times the function and generated 2 ids – md venkatesh Oct 31 '18 at 11:05

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.