How to determine contract address in solidity

bahador gh
Coinmonks
3 min readJul 22, 2022

--

Image credit: dashnews.org

Hello guys.

Welcome to this article about programming in solidity language(for development of smart contracts on Ethereum blockchain-if you don't know solidity programming language-)

First of all, let me tell you a bit about addresses in Ethereum:

Ethereum addresses are divided into 2 different(and exactly similar) types:

1- EOA (Externally owned account)

2- Smart contract account

On one hand, EOAs are accounts that have a person behind them and have a private key to sign transactions to being sent into the blockchain.

On the other hand, smart contract accounts, are accounts that don't have any real person behind them and also don't have a private key.

Now look at these two addresses:

0x5B38Da6a701c568545dCfcB03FcB875f56beddC4

0xd9145CCE52D386f254917e481eB44e9943F39138

Can you guess(by just watching them),

which one is an EOA and which one is a smart contract address?

definitely not!

Although both of them have 20bytes length(as you know, we ignore 0x, cause it is just for clearing that this value is in hexadecimal)

So how we can determine if an address is EOA or a smart contract address??

Is there any special variable/option/function/etc. in solidity for such this thing?

I'm afraid, not. there is no such function for this.

But, we can use assembly programming in such a case.

So let’s get it done and write a function to check the address:

For simplicity, we go through remix IDE:

1- Create a new file into the contracts folder, and name it whatever you want.

2- Then write your contract codes as below:

and now after compiling and deploying this contract on the remix, we check those addresses previously mentioned.

The result is like the below:

1'st address: 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4

2'nd address: 0xd9145CCE52D386f254917e481eB44e9943F39138

So by using assembly in solidity, we got able to distinguish EOA address from smart contract address.

Also for example, we can check if msg.sender is a real person or a smart contract(and just letting access of a specified function to humans)

Hope you enjoy this article, and clap if you liked it.

Any question or your prior experiences is warmly welcomed in the comments :-)

New to trading? Try crypto trading bots or copy trading

--

--

bahador gh
Coinmonks

Technology geek! blockchain(solidity) developer with marketing stuff in mind!