0

For example, i have this piece of code here require(keys[_address].purpose != _purpose, "Key Already Exists"); in a Smart Contract. Upon executing a function and failing a requirement, require statement does not return the defined error message. Instead it returns UnhandledPromiseRejectionWarning: Error: Transaction has been reverted by the EVM. I am writing an application in Node JS and i know my code works correctly because i tested it with different type of inputs and it provide the correct outputs.

I am using Truffle for development with parity. I tested it with Remix and it provides me defined error message so issue is not with the code.

My actual question is why require statement does not return the provided error message?. Am i missing something?. Do i need to provide a certain flag to parity or setup something to catch the provided error message?. Any suggestion would be helpful

Smart Contract Code:

address public administrator;

constructor() public
    {
        administrator = msg.sender;
        addKey(msg.sender, 1, 1);
        emit KeyAdded(msg.sender, 1, 1);
    }
   
mapping(address => Key)keys;
mapping(uint256 => address[])keysByType;
mapping(uint256 => address[])keysByPurpose;

event KeyAdded(address indexed key, uint256 indexed purpose, uint256 indexed keyType);

struct Key {
        uint256 purpose;
        uint256 keyType;
        address key;
    }

modifier onlyAdmin
    {
        require(msg.sender == administrator, "Sender is not admin");
        _;
    }

function addKey(address _address, uint256 _purpose, uint256 _type) public onlyAdmin override returns(bool success)
    {
        require(keys[_address].purpose != _purpose, "Key Already Exists");
        keys[_address].key = _address;
        keys[_address].purpose = _purpose;
        keys[_address].keyType = _type;

        keysByType[_type].push(_address);
        keysByPurpose[_purpose].push(_address);

        emit KeyAdded(_address, _purpose, _type);
        return true;
    }

Node JS Sample Execution:

myContract.methods.addKey("00a329c0648769a73afac7f9381e08fb43dbea72", 3, 1).send({from:"00a329c0648769a73afac7f9381e08fb43dbea72", gas:4613288}).then(console.log)
  • Your transaction possibly fails for a different reason. Please share your code. – goodvibration Jun 21 at 6:51
  • Updated: The execution will fail if i run that node js method twice in a row – Taimoor Jun 21 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.