How to check if an EOS account has a smart contract

Figuring out if an EOSIO account is a smart contract is an easy task if you are operating off-chain. One simply hits the get_code endpoint of an EOSIO node with the account name and checks whether the code_hash response is not 0.

But what if you want to check if there’s a smart contract deployed on an account from within your smart contract?

As of EOSIO 2.0 and EOSIO.CDT 1.7.x there’s no get_code_hash WASM intrinsic and it's not possible. However, if you trust NewDex there's still a quick and convenient way to check it.

NewDex

What NewDex does is scan the EOS mainnet for any eosio.setcode actions and then immediately call the savecontract action blacklisting your contract.

But this means that newdexpublic must keep an on-chain "contractlist" table consisting of all smart contracts and if they are allowed to trade. We can make use of this fact and just access Newdex' table from our own smart contract to figure out if an account is a smart contract:

TABLE contractlist {
eosio::name contract;
bool is_white = false;

uint64_t primary_key() const { return contract.value; }
}

typedef eosio::multi_index<"contractlist"_n, contractlist> contractlist_t;

static bool is_contract( const name& account)
{
contractlist_t contractlist_table("newdexpublic"_n, "newdexpublic"_n.value);
const auto& itr = contractlist_table.find( account.value );
return itr != contractlist_table.end();
}

It’s not a perfect solution because you need to trust Newdex to:

  1. keep their oracle running
  2. not update their smart contract to change or hide this table
  3. be honest about what smart contracts they add / remove from the table

Nevertheless, this solution might work for you if you ‘re looking for a quick and easy solution and:

  1. Don’t want to run your own monitoring & oracle
  2. Don’t want to pay for keeping a list of all smart contracts in RAM

Addendum

If you are like me and wondered how many and which accounts are actually whitelisted to trade on NewDex, here’s the list:

  1. sovconverter
  2. gy2dgmztgqge

Only these two contracts out of 5752 are whitelisted. Both of them don’t seem to do any automated trading.

Learn EOS Development Signup
Learn EOS Development Signup

Written by

Full Stack Software Engineer #javascript #EOS. Into Recreational Math / CS 🤯 Just message me about anything, my mind is open.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store