0

I'm trying to make a smart contract in where anybody can donate to the specific center/ account. And after that they will get some points. Like points will be add according to their sending time. If someone send 2 times this person will get 20 points. But I'm unable to do that. Because whenever I tried to send from different address the points does not changed. In fact, it's added to the previous points which was achieved by someone else. something like , person 'A' donate 3 times and 'A' got 30 points. Then person 'B' donate 1 time . 'B' should get 10 points. But 'B' got 40 points.That means 30+10 = 40.

Here is my smart contract

pragma solidity ^0.5.1;

contract MyContract{
    
    struct Center{
        uint id;
        uint balance;
    }
    mapping(uint => Center) public center;
    uint count;
    
    address[100] public sender;
    uint countDown;
    
    uint public bonus;
    uint checkAddress;
    uint public a;
    
    constructor() public{
        addCenter(1,0);
        addCenter(2,0);
    }
    
    function addCenter(uint _id,uint amount) private{
        count++;
        center[count] = Center(_id, amount);
    }
    
    function sendMoney(uint centerId, uint _amount) payable public{
        require(centerId >0 && centerId <= count);
        
        countDown++;
        bonus+=10;
        
        sender[countDown] = msg.sender;
        
        if(centerId == 1){
            center[centerId].balance += _amount;
        }
        if(centerId == 2){
            center[centerId].balance += _amount;
        }
        

    }
    
    
}

1 Answer 1

1

In your code, bonus is a global variable which is incremented by 10 each time the sendMoney method is called, regardless of the function caller.

If you want to track the bonus points from specific accounts you should use a mapping structure :

  • Replace uint public bonus; by mapping(address => uint256) public senderToBonus;.
  • In sendMoney replace bonus+=10; by senderToBonus[msg.sender]+=10;.
1
  • Oh great. Now it's working perfectly..
    – tithidey
    Commented Oct 15, 2020 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.