Receiving Contract Calls

  • checks that calldata size is at least 36 bytes - 4 for function selector, and 32 for the argument that’s being passed in

  • get the argument by calling calldataload and skipping the first 4 bytes

  • calldataload is only capable of loading 32 bytes

assembly {
	let cd := calldataload(0) // always loads 32 bytes
  // d2178b0800000000000000000000000000000000000000000000000000000000
  let selector := shr(0xe0, cd) // shift right 224 bits to get last 4 bytes
  // 00000000000000000000000000000000000000000000000000000000d2178b08	

	switch selector
        case 0xba88df04 /* get99(uint256) */ {
	            returnUint(getNotSoSecretValue())
         }

	function getNotSoSecretValue() -> r {
                if lt(calldatasize(), 36) { 
                    revert(0, 0)
                }

                let arg1 := calldataload(4)
                if eq(arg1, 8) {
                    r := 88
                    leave // end the function without returning the execution flow back to the calling contract 
                }
                r := 99
            }
}

Last updated