DEV Community

Cover image for Address Member Functions in Solidity
Shlok Kumar
Shlok Kumar

Posted on

Address Member Functions in Solidity

balance and transfer

Balance:

The property balance may be used to check an address's balance and the transfer function to send Ether (in wei) to a payable address:

address payable x = payable(0x123);
address myAddress = address(this);

if (x.balance 10 && myAddress.balance >= 10) 
{
x.transfer(10);
}
Enter fullscreen mode Exit fullscreen mode

The transfer function fails if the current contract's balance is insufficient or the recipient account rejects the Ether transfer. If the transfer fails, it reverts.

Important:
It will be run together with the transfer call if x is a contract address (more precisely, its Receive Ether Function or its Fallback Function) (this is a feature of the EVM and cannot be prevented). That execution will fail if it runs out of gas or else fails, and the Ether transfer will be reversed.

Send:

Send is the opposite of transfer. If the execution fails, the current contract persists, but send returns false.

Warning: Using send might be risky. It fails if the recipient runs out of gas. Always verify the return value of a send, utilize a transfer or even better: set up a pattern in which the receiver withdraws their money.

call, delegatecall and staticcall

If you need to work with contracts that don't follow the ABI, or if you want to get more control over how they're written, the functions call, delegatecall, and staticcall are there. It's the same thing with each one. They all take one byte of memory and return the success condition (a bool) and the data that was returned (bytes memory). Abi's encode, encodePacked, encodeWithSelector, and encodeWithSignature functions can be used to encode data that is organized in a way that can be read.

It is possible to adjust the supplied gas with the gas modifier:

address(address_name).call
{gas:1000000}
(abi.encodeWithSignature("register(string)", "MyName"));
Enter fullscreen mode Exit fullscreen mode

Similarly, the supplied Ether value can be controlled too:

address(address_name).call
{value: 1 ether}
(abi.encodeWithSignature("register(string)", "MyName"));
Enter fullscreen mode Exit fullscreen mode

Lastly, these modifiers can be combined. Their order does not matter:

address(address_name).call{gas: 1000000,
 value: 1 ether}
(abi.encodeWithSignature("register(string)", "MyName"));
Enter fullscreen mode Exit fullscreen mode

 Using the function delegatecall is similar; the difference is that just the code of the provided address is utilized, and all other characteristics (storage, balance, and so on) are obtained from the current contract, rather than the previous contract. To make use of library code that is contained in another contract, the delegatecall function must be called. The user must check that the arrangement of storage in both contracts is acceptable for the usage of delegatecall before proceeding with the implementation.

Warning:

All of these functions are low-level functions, and they should only be used with extreme caution when necessary. To be more specific, any unknown contract may be malicious, and if you call it, you pass over power to that contract, which may in turn call back into your contract, so be prepared for changes to your state variables when the call is completed. Contact with other contracts is often accomplished by calling a function on the contract object (e.g., f() on contract)

code and codehash

You may look for any smart contract in the code that has been deployed. Use the .code command to get the EVM bytecode as a bytes memory, which may or may not include any data. Codehash is a command that returns the Keccak-256 hash of a code (as a bytes32). It should be noted that using addr.codehash is less expensive than using keccak256 (addr.code).

 Address Literals:

Hexadecimal literals that pass the address checksum test, for example “0xdCad3a6d3569DF655070DEd06cb7A1b2Ccd1D3AF” are of address type. Hexadecimal literals that are between 39 and 41 digits long and do not pass the checksum test produce an error. You can prepend (for integer types) or append (for bytesNN types) zeros to remove the error.

For more content, follow me on - https://linktr.ee/shlokkumar2303

Top comments (0)