Solidity Error Handling Assignment

At the start of the function “require” is used to check if it makes sense to run the function at all. If the owner already has a dog, the function will do nothing. Using “require” has the advantage of not wasting any gas for the execution of this function.

At the end of the function “assert” is used in order to ensure, that the dog has its correct age assigned. If the age is not as it is supposed to be, something has gone wrong! So a rollback will be performed using up all the gas.

pragma solidity 0.5.1;

contract DogContract{

    struct Dog {
        string name;
        uint age;
    }

    mapping(address => Dog) ownerToDog;

    function addDog(string memory _name, uint _age) public {
        /* Make sure that the owner doesn't have a dog already! */
        require(ownerToDog[msg.sender].age == 0, "The owner already has a dog!");
        ownerToDog[msg.sender]  = Dog(_name, _age);
        /* Check if dog age is correct and do a rollback otherwise! */
        assert(ownerToDog[msg.sender].age == _age);
    }

    function getDog() public view returns (string memory) {
        address owner = msg.sender;
        return ownerToDog[owner].name;
    }

}

function addDog(string memory _name, uint _age) public{
require(ownerToDog[msg.sender].age == 0);
Dog memory currenDog = Dog(_name, _age);
ownerToDog[msg.sender] = currenDog;
}

hello…I believe the best solution to error handling would be using require.

pragma solidity 0.5.1;

contract DogContract{
struct Dog{
string name;
uint age;

}

mapping(address => Dog) ownerToDog;

function addDog(string memory _name, uint _age) public {
    require(ownerToDog[msg.sender].age == 0 && ownerToDog[msg.sender].age = _age);
    
        Dog memory currentDog = Dog(_name, _age);
        ownerToDog[msg.sender] = currentDog;
    
    
}

function getDog() public view returns (string memory){
    address owner = msg.sender;
    return ownerToDog[owner].name;
}

}

pragma solidity 0.5.1;

contract DogContract{

struct Dog {
    string name;
    uint age;
}

mapping(address => Dog) ownerToDog;

function addDog(string memory _name, uint _age) public {
    require(ownerToDog[msg.sender].age == 0) {

        Dog memory currentDog = Dog(_name, _age);
        ownerToDog[msg.sender] = currentDog;
    }
}

function getDog() public view returns (string memory) {
    address owner = msg.sender;
    return ownerToDog[owner].name;
}

}

This contract initializes the ownerToDog mapping for the contract owner in the constructor function, and the addDog function can be called without any parameters. The addDog function also hardcodes the dog’s name and age to “Fido” and 3, respectively.

pragma solidity 0.6.11;

contract DogContract {
struct Dog {
string name;
uint age;
}

mapping(address => Dog) private ownerToDog;

constructor() public {
    ownerToDog[msg.sender] = Dog("", 0);
}

function addDog() public {
    require(ownerToDog[msg.sender].age == 0, "There is already a dog registered to this address");
    ownerToDog[msg.sender] = Dog("Fido", 3);
}

function getDog() public view returns (string memory) {
    return ownerToDog[msg.sender].name;
}

}

1 Like