How to check if an EOS account has a smart contract
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
NewDex is the biggest (?) decentralized exchange on EOS.
A not well-known fact is that they ban all smart contracts from trading.
Deploy a smart contract and try to send a trade action to newdexpublic
. You'll run into an assertion error saying that your contract is not whitelisted.
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:
- keep their oracle running
- not update their smart contract to change or hide this table
- 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:
- Don't want to run your own monitoring & oracle
- Don't want to pay for keeping a list of all smart contracts in RAM
Addendum
From my tests, their oracle blacklists new contracts within minutes from setting code on your account.
If you are like me and wondered how many and which accounts are actually whitelisted to trade on NewDex, here's the list:
Only these two contracts out of 5752 are whitelisted. Both of them don't seem to do any automated trading.
Originally published at https://cmichel.io/how-to-check-if-an-eos-account-has-a-smart-contract/