Erc-20 tokens and ethplorer api. Reading an address in php, creating a table of tokens owned, color coding values, linking to exchange by firstamendment

View this thread on steempeak.com
· @firstamendment · (edited)
$1.59
Erc-20 tokens and ethplorer api. Reading an address in php, creating a table of tokens owned, color coding values, linking to exchange
ERC-20s are mostly a bunch of crap tokens given out freely in air drops that utilize ethereum.  Most of the ones I have received are worthless, and those that aren't probably will be in time-and in my opinion it is best for legal reasons not to sell stuff that is worthless and unmarketable-and just wait for them to prove themselves instead. Of course taking my  advice could cost you substantial profit.   I am guessing I am sitting on about $600-800 from just october, November has mostly been crap aside from the overall increase in value of crypto in general.

  usually to participate in an airdrop you have to get a myetherwallet (MEW) ethereum address-an address at an exchange is worthless, and try to hunt down an adrop on bitcointalk.  The drawback to my etherwallet is that it doesn't show up everything you recieved, and you manually have to enter in the contract address, symbol, and decimals.  needless to say i stopped using the MEW site.  There are other sites that will list your tokens and do a good job like ethplorer and etherscan.  However, they often don't list a price or have a link to an exchange.  And sometimes when they do, it is wrong.  For example, ethplorer thinks 5m token that trades at about .00000001 ETH each is worth about 50k.  I wish, good bye student loans if true.

  So what I did was I used the api from ethplorer, which basically dumps the information for a given address.

https://api.ethplorer.io/getAddressInfo/0xcf085317456133e93d72ab5fc56025d8d3802c38?apiKey=freekey

  And with such information, I was able to build my own custom table with the tokens names in alphabetical order, and link them to an exchange, color code the quantities, and do percent ownership.  At the end, I decided to add colors to help  improve readability; I mean when you are dealing with numbers maybe in the trillions or more and say 18 decimal places it gets hard to read.

  As far as quotes, the API for etherdelta wants sockets.  I am unsure if my host provider allows sockets in php, but letting this script pull the quotes would likely exceed what ethdelta would allow anyways.  If quotes were to be added, the script would have to pull them from a local data store (file or database), with another seperate server script pulling the quotes to feed the data stores on regular intervals as to not flood their servers with needless requests.    If I wanted to take it further, I could try to hunt down a list of websites, twitters, telegrams, facebook, discords, etc that the companies use.  But Really i didn't make this as a public tool.  I made it for myself.  If someone wants to take a look at what I did and build onto it, then it is fine by me.  

  Anyways for a look at a random public eth address (not mine) that has token so you may try it out.  Please note, my server does not support https so nothing is encrypted.  NEVER PASS A PRIVATE KEY.  

http://neophytesoftware.com/steemitfirstamendment/crypto.php?address=0xf33bc464fb8a61db1d8cce0c22dad4f833fabb2a

So basically same method from when I blogged about bots.  This time I get the API.  The api is pretty simple, it is just a url including the address, as well as an API key.

https://api.ethplorer.io/getAddressInfo/0xcf085317456133e93d72ab5fc56025d8d3802c38?apiKey=freekey

  And it is kind of messy.  It is partially organized with delimited of comma and colons, but then it breaks down.  Sometimes a url is included, so essentially a fake delimit can appear.

So anyways, here is the code crypto.php.

//This first part is just a form.  Pretty simple, an address is entered, and when clicked, it puts stores that address into memory the next when submit is pressed.  And w'll give it a nice lightblue background.  Obviously, I am not a fan of css or its many derivatives.  The best of html was netscape 4.0...long long time ago.  I know the body element should be after the html element, I don't care.
<i>
&#60;html&#62;
&#60;form action="crypto.php" method="get"&#62;
address:&#60;input type=text name=address&#62;
&#60;input type=submit value="Submit"&#62;
&#60;/form&#62;
&#60;body bgcolor="lightblue"&#62;
</i>

//So here is the beginning of the php code.  Pretty standard we check for a get Request, and if found store it into address.  Of just give the address a dummy value.  Part of using the ethplorer api means giving credit to the ethplorer ppl.
<i>
&#60;&#63;php
$s=$_GET[address] or $s="0x";
echo "This is a test site.  Powered in part by EthPlorer&#60;br&#62;";
</i>
//this is how we load the eth delta api over https.  The domain is api.ethplorer.io.  the path is "/GetAdrressInfo/[address]?apikey=freekey .  And then we do the connection of port 443.  Load the page and store it into gets. hahaha, in looking at the code, you can see an obvious error I made (drudgereport) and their server didn't care and processed it anyways. 
<i>
$url="ssl://api.ethplorer.io";
$path="/getAddressInfo/$s?apiKey=freekey";
$fp = fsockopen($url, 443, $errno, $errstr, 30); fputs($fp, "GET " . $path . " HTTP/1.1\r\nHost: drudgereport.com\r\nConnection:Close\r\n\n"); while(!feof($fp)) {$pagetext=$pagetext . fgets($fp);} fclose($fp);
$data;
$gets = $pagetext;
</i>
//We are going to remove some potential false delimiters  Here we remove http, we should have removed https as well.
<i>
$gets= str_replace('http:', '', $gets);
</i>
//we now skip over some uneeded data.
<i>
$gets=substr($gets, strpos($gets, '{"'));
</i>
//and we now create a list from a delimited that separateseach row of data, which is basically {"t.  We also set a counter
<i>
$list=explode('{"t', $gets);
$counter =0;
</i>
//for each element in our list, we are going to extract the data we want.  We skip over counter value 0, since it is ethereum itself and is irregular-and I decided not to do a special case. 
<i>

foreach ($list as $a)  {
if ($counter!=0) {
</i>
//we remove all quotes to ensure that our numbers are numbers.  We then create a new list which is separated by a colon.
<i>
$a= str_replace('"', '', $a);
$c=explode(':',$a);
</i>
//The third index of a line into the data recieved is the name of the token.  Sometimes names are not assigned to a token.  So here we are getting the name of the token, or setting a data store to "N/A".
<i>
$z=strlen(substr($c[3], 0, strpos($c[3],',')));
if ($z==0) {$data[$counter][0]="N/A";}
else {$data[$counter][0]=substr($c[3], 0, strpos($c[3],','));}
</i>
//The next thing I am extracting is the address, followed by the ticker symbol.
<i>
$data[$counter][1]=substr($c[2], 0, strpos($c[2],','));
</i>
//this is how many decimals are used. 
<i>
$data[$counter][2]=substr($c[4], 0, strpos($c[4],','));
</i>
//  Again, sometimes the ticker symbol is underfined.
<i>
$z=strlen(substr($c[5], 0, strpos($c[5],',')));
if ($z==0) {$data[$counter][3]="N/A";}
else {$data[$counter][3]=substr($c[5], 0, strpos($c[5],','));}
</i>
//now we get the total supple, and of course adjust it to our decimal places.
<i>
$data[$counter][4]=pow(10,-$data[$counter][2])&#42;substr($c[6], 0, strpos($c[6],','));
</i>
//the balance is irregular in the data recieved.  We chose a final value of 28 indexes to dig into, the actual number may be higher in some cases-oh well. We we look for an index that has balance, and extract the data and store it. 
<i>
for ($d=7;$d&#60;28;$d++) {
if (strpos($c[$d], "balance")&#62;1)
{$data[$counter][5]=substr($c[$d+1], 0, strpos($c[$d+1],','))&#42;pow(10,-$data[$counter][2]);
break;}}
</i>
//This is in preparation for a saort, we make a copy of the name of the token-only in uppercase.
<i>
$data[$counter][6]=strtoupper($data[$counter][0]);
}
$counter++;
}


</i>

//So sorting we wanted to ensure that the size of the names were of equal length.  We we searched for a max size of the name.
<i>
$stlen=0;
for($a=1;$a&#60;$counter;$a++) {
$st=strlen($data[$a][0]);
if ($st&#62;$stlen) {$stlen=$st;}}
</i>

//using the maximum size, we padding the copy of the namestore with tildes-antil they are all the same length.
<i>
for($a=1;$a&#60;$counter;$a++) {
while(strlen($data[$a][6])&#60;$stlen) {$data[$a][6]=$data[$a][6]."~";}
</i>
//some tokens have the same name.  What separates them is the contract address.  So we concatonate the contract address to the name
<i>
$data1[$a]=$data[$a][6].$data[$a][1];
</i>
//We couldn't get array multisort to work.  So we created an array where each element is a string of our values delimited by a tilde.
<i>
$data2[$a]=$data[$a][0]."~".$data[$a][1]."~".$data[$a][2]."~".$data[$a][3]."~".$data[$a][4]."~".$data[$a][5]."~";}
</i>
//and we now sort.
<i>
sort($data1);
</i>
//and our table begins
<i>
echo '&#60;center&#62;&#60;table bgcolor="white" border=1&#62&#60;tr&#62;&#60;TH&#62index&#60;/TH&#62;&#60;TH&#62;Name&#60;/th&#62;&#60;Th&#62;Ticker&#60;/TH&#62;&#60;/tr&#62;&#60;tr&#62;&#60;TH&#62;Total Supply&#60;/th&#62;&#60;Th&#62;owned&#60;/TH&#62;&#60;Th&#62;Percent Ownership&#60;/TH&#62;&#60;/tr&#62;';
</i>
//This is a yucky sort.  We are extracting the address from data 2, and comparing it to the already sorted data1.  We then store.  So we look at an index data1, and if an data2 address is inside data1 , then data2 is copying into data 3 at the index mapped bt data1.  So data3 is a sorted data 2 in the same order as data 1.
<i>
for($a=0;$a&#60;$counter;$a++) {
for($b=0;$b&#60;$counter;$b++) {
$n=explode("~",$data2[$b]);
if (strpos($data1[$a],$n[1])&#62;0) {
$data3[$a]=$data2[$b];
}}}
</i>
//now we fill our tables.
<i>
for($a=0;$a&#60;$counter-1;$a++) {
echo "&#60;tr&#62;";
</i>
//separating the data in data3 into lists
<i>
$n=explode("~",$data3[$a]);
echo "&#60;td&#62;";
</i>
//a basic index.
<i>
echo ($a+1);
echo "&#60;/td&#62;&#60;td&#62;";
</i>
//the name of the token
<i>
echo $n[0];
echo "&#60;/td&#62;&#60;td&#62;";
<i>
//link to the exchange
<i>
echo '&#60;a href="https://etherdelta.com/#';
echo $n[1];
echo '-ETH"&#62;';
echo $n[3];
echo "&#60;/a&#62;&#60;/td&#62;&#60;/tr&#62;&#60;tr&#62;&#60;td&#62;";
</i>
//getting rid of commas in the numbers.
<i>
$n[4]= str_replace(',', '', $n[4]);
</i>
//setting the colors to display
<i>
       if ($n[4]&#62;=1000000000000) {echo '&#60;font color="red"&#62;';}
else if ($n[4]&#62;=1000000000) {echo '&#60;font color="orange"&#62;';}
else if ($n[4]&#62;=1000000) {echo '&#60;font color="green"&#62;';}
else if ($n[4]&#62;=1000) {echo '&#60;font color="blue"&#62;';}
</i>
//If a number has a decimal point, we are going to have to trim off the zeros on theright hand side (rtrim).  f a number doesn't have a decimal point, it will lose significant digits.  So have have to create a special case.
<i>
if (strpos($n[4],".")) {
echo rtrim(number_format($n[4],$n[2],".",","),"0");}
else {echo number_format($n[4],0,".",",");}
echo "&#60;/font&#62;&#60;/td&#62;&#60;td&#62;";
</i>
//this is pretty much the same as $n[4] but for $n[5] and this is the owner balance
<i>
$n[5]= str_replace(',', '', $n[5]);
       if ($n[5]&#62;=1000000000000) {echo '&#60;font color="red"&#62;';}
else if ($n[5]&#62;=1000000000) {echo '&#60;font color="orange"&#62;';}
else if ($n[5]&#62;=1000000) {echo '&#60;font color="green"&#62;';}
else if ($n[5]&#62;=1000) {echo '&#60;font color="blue"&#62;';}
if (strpos($n[5],".")) {
echo rtrim(number_format($n[5],$n[2],".",","),"0");}
else {echo number_format($n[5],0,".",",");}
echo "&#60;/font&#62;&#60;/td&#62;&#60;td&#62;";
</i>
//and this is the percent ownership.
<i>
echo 100&#42;$n[5]/$n[4];

echo "%&#60;/TD&#62;&#60;/tr&#62;";
}

echo "&#60;/table&#62;";

&#63;&#62;
&#60;/body&#62;
&#60;/html&#62;
</i>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 8 others
properties (23)
post_id18,888,317
authorfirstamendment
permlinkerc-20-tokens-and-ethplorer-api-reading-an-address-in-php-creating-a-table-of-tokens-owned-color-coding-values-linking-to
categoryaltcoins
json_metadata"{"app": "steemit/0.1", "format": "markdown", "links": ["https://api.ethplorer.io/getAddressInfo/0xcf085317456133e93d72ab5fc56025d8d3802c38?apiKey=freekey", "http://neophytesoftware.com/steemitfirstamendment/crypto.php?address=0xf33bc464fb8a61db1d8cce0c22dad4f833fabb2a", "https://etherdelta.com/#';"], "tags": ["altcoins", "programming", "php", "erc", "crypto"]}"
created2017-11-28 02:21:39
last_update2017-11-28 03:02:45
depth0
children4
net_rshares598,516,230,136
last_payout2017-12-05 02:21:39
cashout_time1969-12-31 23:59:59
total_payout_value1.246 SBD
curator_payout_value0.343 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length11,490
author_reputation-4,872,790,926,189
root_title"Erc-20 tokens and ethplorer api. Reading an address in php, creating a table of tokens owned, color coding values, linking to exchange"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars0
author_curate_reward""
vote details (72)
@cryptofixer ·
Nice post, I am still learning to code at the moment
👍  
properties (23)
post_id18,889,982
authorcryptofixer
permlinkre-firstamendment-erc-20-tokens-and-ethplorer-api-reading-an-address-in-php-creating-a-table-of-tokens-owned-color-coding-values-linking-to-20171128t025315284z
categoryaltcoins
json_metadata"{"app": "steemit/0.1", "tags": ["altcoins"]}"
created2017-11-28 02:53:12
last_update2017-11-28 02:53:12
depth1
children0
net_rshares6,495,734,240
last_payout2017-12-05 02:53:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length52
author_reputation2,479,958,677,629
root_title"Erc-20 tokens and ethplorer api. Reading an address in php, creating a table of tokens owned, color coding values, linking to exchange"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@minnowsupport ·
<p>Congratulations!  This post has been upvoted from the communal account, @minnowsupport, by firstamendment from the Minnow Support Project.  It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso.  The goal is to help Steemit grow by supporting Minnows and creating a social network.  Please find us in the <a href="https://discord.gg/HYj4yvw">Peace, Abundance, and Liberty Network (PALnet) Discord Channel</a>.  It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.</p>
properties (22)
post_id18,891,179
authorminnowsupport
permlinkre-firstamendment-erc-20-tokens-and-ethplorer-api-reading-an-address-in-php-creating-a-table-of-tokens-owned-color-coding-values-linking-to-20171128t031458011z
categoryaltcoins
json_metadata"{"app": "cosgrove/0.0.2", "tags": ["altcoins"]}"
created2017-11-28 03:14:57
last_update2017-11-28 03:14:57
depth1
children0
net_rshares0
last_payout2017-12-05 03:14:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length615
author_reputation104,981,098,086,561
root_title"Erc-20 tokens and ethplorer api. Reading an address in php, creating a table of tokens owned, color coding values, linking to exchange"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@crypto-trail ·
Some educational stuff. Great job on the post.
👍  
properties (23)
post_id18,893,973
authorcrypto-trail
permlinkre-firstamendment-erc-20-tokens-and-ethplorer-api-reading-an-address-in-php-creating-a-table-of-tokens-owned-color-coding-values-linking-to-20171128t040647467z
categoryaltcoins
json_metadata"{"app": "steemit/0.1", "tags": ["altcoins"]}"
created2017-11-28 04:06:51
last_update2017-11-28 04:06:51
depth1
children0
net_rshares6,333,340,884
last_payout2017-12-05 04:06:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length46
author_reputation18,150,512,355,968
root_title"Erc-20 tokens and ethplorer api. Reading an address in php, creating a table of tokens owned, color coding values, linking to exchange"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@vidallia ·
$0.09
Really detailed post but, I would have liked to see the use of "Code Tags" so the html is easier on the eyes.
👍  ,
properties (23)
post_id19,231,834
authorvidallia
permlinkre-firstamendment-erc-20-tokens-and-ethplorer-api-reading-an-address-in-php-creating-a-table-of-tokens-owned-color-coding-values-linking-to-20171201t192108684z
categoryaltcoins
json_metadata"{"app": "steemit/0.1", "tags": ["altcoins"]}"
created2017-12-01 19:21:06
last_update2017-12-01 19:21:06
depth1
children0
net_rshares27,452,007,549
last_payout2017-12-08 19:21:06
cashout_time1969-12-31 23:59:59
total_payout_value0.088 SBD
curator_payout_value0.001 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length109
author_reputation252,477,233,160
root_title"Erc-20 tokens and ethplorer api. Reading an address in php, creating a table of tokens owned, color coding values, linking to exchange"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (2)