0
pragma solidity ^0.8.6;

contract Contract1 {
    
    uint index;
    address owner;
    uint256[] values;
    
    mapping(uint => address payable) addresses;
    
    constructor() {
       owner = msg.sender;
    }
    
    function makeAPay() public payable {
        uint tempValue = msg.value/(10**6);
        addresses[index] = payable(msg.sender);
        values[index] = tempValue;
        index++;
    }
    
    
    function getAdr() public view returns(address) {
        if(index == 0){
            return owner;
        }
        return addresses[0];
    }
    
    function getValue() public view returns(uint) {
        if(index == 0){
            return 0;
        }
            return values[0];
    }
    
     function getIndex() public view returns(uint) {
            return index;
    }

    function getOwner() public view returns(address) {
            return owner;
    }
}

Why I always get root:"REVERT opcode executed. Message: 2" When I makeAPay() with any payable value

 status false Transaction mined but execution failed

when i commit, problem was gone, but i dont get value)

        uint tempValue = msg.value/(10**6);
        values[index] = tempValue;
0
1

As values is an array, you can check the value by passing an uint id in your getValue function and simply return values[id]. Also, I don't see the use for the index value with the values array. For the function makeAPay() you're not pushing the item in the array, hence you're getting 0. Instead of values[index] = tempValue it should be values.push(tempValue). That way you're actually occupying the position in the array with the tempValue.If you want to check if there are any values in the array you can get all the values in the array by returning uint[] instead of simple uint.

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.