How to do it...

  1. Create a state variable to store Boolean values in the contract. Use the bool keyword and initialize it using either true or false. The Boolean type supports basic operators such as - !, &&, !!, ==, and !=:

bool isAvailable = false;
  1. To create a variable to store integer values using either int or uint keyword, use int to store unsigned integers and uint to store signed integers of various memory sizes. You can store integers ranging from uint8/int8 to uint256/int256 in steps of 8:
// uint/int are aliases for uint256/int256
uint256 a = 9607111;
int32 b = 102;

Use the following supported operators to perform comparisons or arithmetic operations:

  • Comparisons: <=, <, ==, !=, >=, >
  • Bit operators: &, |, ^ (bitwise exclusive or), ~ (bitwise negation)
  • Arithmetic operators: +, -, unary -, unary +, *, /, % (remainder), ** (exponentiation), << (left shift), >> (right shift)

Integers in solidity are susceptible to overflow/underflow. It is strongly recommended to include such conditions in your contract. You can also use well-tested third-party libraries such as openzeppelin's SafeMath (https://github.com/OpenZeppelin/zeppelin-solidity) for performing arithmetic operations safely.

  1. Now, we can create a fixed-point variable using fixed/ufixed. Since they are not completely supported by Solidity, you can only declare them, but cannot be assigned to or from.
  2. Create an address variable with the address keyword. The address type is unique to solidity and holds an Ethereum address of 20 bytes. The address type also supports the <=, <, ==, !=, >=, and > operators:
address owner = 0x05ee546c1a62f90d7acbffd6d846c9c54c7cf94c;

The following members are part of each address:

  • <address>.balance: Can be used to query the balance of the address. Returns value in Wei.
  • <address>.transfer(<amount>): Sends Ether (in Wei) to an address. If the execution fails, the contract will stop with an exception:
owner.transfer(1 ether); // Throws an exception if failed
  • <address>.send(<amount>): Similar to transfer, but returns false on failure and will not stop the execution:
owner.send(1 ether); // Returns true or false
  • <address>.call(): Issues a low-level call. It is generally used to interact with a contract that does not specify an ABI. Call accepts an arbitrary number of arguments of any type and returns false on failure:
address productStore = 0x4db26171199535b2e4bae6615c9b8ffe33c41d74;
productStore.call("getProduct", "product_id_001");

Adjust the supplied gas and value for call():

productStore.call.gas(2000000).value(1 ether).("buyProduct", "product_id_001");

The ABI, or Application Binary Interface, is the standard for encoding or decoding data to or from machine code. Data in Ethereum is encoded according to its type, and this encoding is not self-describing. It requires a schema in order to decode, which is obtained from the ABI.

  • <address>.delegatecall(): delegatecall uses the code of the target address, taking all other aspects (storage, balance, and so on) from the calling contract. The purpose of delegatecall is to use library code that is stored in another contract. For delegatecall, the user has to ensure that the layout of storage in both contracts is suitable to be used. You can adjust the supplied gas and value like so:
contract A {
uint value;
address public sender;
// address of contract B
address a = 0xef55bfac4228981e850936aaf042951f7b146e41;

function makeDelegateCall(uint _value) public {
// Value of A is modified
a.delegatecall(bytes4(keccak256("setValue(uint)")), _value);
}
}

contract B {
uint value;
address public sender;

function setValue(uint _value) public {
value = _value;
// msg.sender is preserved in delegatecall.
// It was not available in callcode.
sender = msg.sender;
}
}

msg.sender is a global variable in solidity that has the address of the message/transaction sender. You can also make use of msg.value, msg.data, msg.gas, and msg.sig.

  • <address>.callcode(): Prior to homestead, only a limited variant called callcode was available, which did not provide access to the original msg.sender and msg.value values. It is not recommended to use callcode.
  1. Create an array with a static or dynamic length. Solidity also supports two-dimensional arrays and, unlike other programming languages, the notation is reversed. For example, an array of three dynamic arrays of int is int[][3]. To read the third int in the second dynamic array, you can use a[1][2]:
uint[] dynamicSizeArray; 
uint[7] fixedSizeArray;

Solidity will generate a getter method for arrays if you mark them as public. The index will become the input parameter for that method. If you want to return the whole array at once, create an explicit getter method that returns the required array.

  1. Create a fixed byte array in solidity using the bytes keyword. It ranges from bytes1 to bytes32 in steps of 1. These arrays are compatible with Comparisons (<=, <, ==, !=, >=, >) and Bit operators (&, |, ^ (bitwise exclusive or), ~ (bitwise negation), << (left shift), >> (right shift)).
  2.  Now, we can create an arbitrary length raw byte data type using bytes and for arbitrary length UTF-8 data, use string. These are used as dynamic byte arrays in solidity. String literals are written with either single or double quotes. They are implicitly convertible to bytes if they fit.
  3. If we try creating array literals that are written as expressions and are not assigned to a variable right away, the type of an array literal is memory of a fixed size. For example, the type of [2, 4, 8, 5] is uint8[4] memory.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.221.133