如何检查Tron地址是否是TRC20中的合同



我正试图让这段代码与Tron网络一起工作,下面是我在网上看到的的示例

function isContract(address account) internal view returns (bool) {
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
// for accounts without code, i.e. `keccak256('')`
bytes32 codehash;
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
// solhint-disable-next-line no-inline-assembly
assembly { codehash := extcodehash(account) }
return (codehash != accountHash && codehash != 0x0);
}

这是我在Tron 中的实现

function isContract(address account) internal view returns (bool) {
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
// for accounts without code, i.e. `keccak256('')`
bytes32 codehash;
bytes32 accountHash = convertFromTronInt(THgQd3Z3nZuvVNBL9HcAyiutrpsigVKPT1);
//THgQd3Z3nZuvVNBL9HcAyiutrpsigVKPT1;
// solhint-disable-next-line no-inline-assembly
assembly { codehash := extcodehash(account) }
return true;
}
function convertFromTronInt(uint256 tronAddress) public view returns(address){
return address(tronAddress);

}

有没有更好的方法从Tron代币中进行同样的检查,或者我的解决方案可行吗?

您可以使用strongrid API来获取合同详细信息(如果您的用例负担得起的话(。不能将任何数据视为地址不是合同。

curl --request POST 
--url https://api.trongrid.io/wallet/getcontractinfo 
--header 'accept: application/json' 
--header 'content-type: application/json' 
--data '
{
"value": "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
"visible": true
}
'

最新更新