Atomic Transactions

Is there any way to ensure metaTransaction "atomicity".

eg:

``````contract first{

function one(uint256 a, uint256 b) public pure returns(uint){
uint256 c = a + b;
return c;
}

function two(uint c, uint d)public pure returns(uint){
uint256 e = c + d;
return e;
}

}
``````

.

``````import "./first.sol";

contract second{

first firstContract =

function combine(uint256 a, uint256 b, uint256 d) public view returns(uint256){
uint256 c = firstContract.one(a, b);
uint256 e = firstContract.two(c, d);
return e;
}

}
``````

Is there anyway to ensure that either everything in combine runs or nothing does?

In the case that you present, there is no way that these function will fail. I assume your question is if in general there is a way to control the atomicity.

I assume your question is that you require that function `one` and `two` should be executed correctly. Notice that you can achieve this by checking the output of each function, for instance in the addition you need to check that no overflow has occurred (I changed the types to uint8 so that you can test this with small numbers, if the sum of the numbers is bigger than 255 the function will throw):

``````contract first{

function one(uint8 a, uint8 b) public pure returns(uint8){
uint8 c = a + b;
require(c>a);
return c;
}

function two(uint8 c, uint8 d)public pure returns(uint8){
uint8 e = c + d;
require(e>c);
return e;
}

}
``````

Note that if you do `c = a + b`, `c` is always bigger than `a` and `b` and if `c` is bigger than `a` then is also bigger than `b` unless overflow occurrs. This is what the line require(c>a) is checking.

notice that if the execution of `one` or `two` throws, the whole transaction fails, which guaranty "atomicity"

Hope this helps.

Yes.

You can use `require` statement to ensure a condition is true, otherwise the whole transaction gets reverted. There are other "types" of error handling, read more here.

An example would be :

``````    function combine(uint256 a, uint256 b, uint256 d) public view returns(uint256){
uint256 c = firstContract.one(a, b);
require(c > 10, "a+b must be superior to 10");
uint256 e = firstContract.two(c, d);
require(e > 30, "a+b+d must be superior to 30");
return e;
}
``````