0

Etherscan apikey returns an error

This is my code:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\AdminSettings;
use App\Models\User;
use Auth;

define('APIURL', 'http://104.248.243.148');
define('APIKEY','<REMOVED>');
define('ETH_API_URL', 'https://api.etherscan.io/api?module=account&action=balance&tag=latest&apikey='.APIKEY.'&address=');


class BaseController extends Controller
{
    public function get_balance()
    {
        $settings = AdminSettings::first();
        $user = User::find(Auth::User()->id);
        $url = "https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=$settings->contractaddress&address=$user->address&tag=latest&apikey=".APIKEY;

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);      
        curl_close($ch);
        $token_response = json_decode($output);
        // print_r($token_response); exit;
        if($output==false)
        {
            $TOKEN_balance=0;
        }
        else
        {
            $TOKEN_balance=$token_response->result/1000000000000000000;
        }

        // echo $TOKEN_balance; exit;
        // ETH balance      
        $url1 = ETH_API_URL.$user->address; 
        $ch = curl_init($url1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);      
        curl_close($ch);
        $eth_response = json_decode($output);
        if($output==false)
        {
            $ETH_balance=0;
        }
        else
        {
            $ETH_balance=$eth_response->result/1000000000000000000;
        }
        // End of ETH balance

        return array('Crypto_Bal'=>$TOKEN_balance,'ETH_Bal'=>$ETH_balance);
    }


    public function get_balance_with_address($address)
    {
        $settings = AdminSettings::first();
        $url = "https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=$settings->contractaddress&address=$address&tag=latest&apikey=".APIKEY;

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);      
        curl_close($ch);
        $token_response = json_decode($output);
        // print_r($token_response); exit;
        if($output==false)
        {
            $TOKEN_balance=0;
        }
        else
        {
            $TOKEN_balance=$token_response->result/1000000000000000000;
        }

        // echo $TOKEN_balance; exit;
        // ETH balance      
        $url1 = ETH_API_URL.$address; 
        $ch = curl_init($url1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);      
        curl_close($ch);
        $eth_response = json_decode($output);
        if($output==false)
        {
            $ETH_balance=0;
        }
        else
        {
            $ETH_balance=$eth_response->result/1000000000000000000;
        }
        // End of ETH balance

        return array('Crypto_Bal'=>$TOKEN_balance,'ETH_Bal'=>$ETH_balance);
    }


    public function send_token($fromAddr,$private_key,$toAddr, $token)
    {  
        $settings = AdminSettings::first();

        $fields = array(
        "jsonrpc" => '2.0',
        "method" => 'sendTokens',
        "params" => array($settings->contractaddress,$fromAddr,$private_key,$toAddr,$token),
        "id" => '1',
        );
        $fields = json_encode($fields);
        $headers = array(
        'Content-Type: application/json'
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, APIURL);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

        $result = curl_exec($ch);
        curl_close($ch);
        $response=json_decode($result);     
        if(isset($response->error->code))
        {
            return false;
        }
        else
        {
            return $response->result->hash;
        }
    }


    public function get_address()
    {
        $fields = array(
                "jsonrpc" => '2.0',
                "method" => 'createAddressForToken',
                "params" => array('password'),
                "id" => '1',
        );
        $fields = json_encode($fields);
        $headers = array(
            'Content-Type: application/json'
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, APIURL);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        $result = curl_exec($ch);
        curl_close($ch);
        $response=json_decode($result);       
        if(isset($response->error->code))
        {
            return $address=0;          
        }
        else
        {           
            return $address=$response->result;
        }
    }
}

Are my APIURL, APIKEY, ETH_API_URL right?

How do I know the API URL of my Laravel project?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.