<?php
/**
 * Lite-mode blockchain explorer requests (free public APIs).
 *
 * Used only when cryptoniq_is_pro() is false. PRO mode receives transaction
 * notifications via webhook from api.cryptoniq.io and never calls these.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit();
}

/**
 * Hit a public block explorer for a wallet's recent activity.
 *
 * @param string $coin   'BTC' or 'ETH'
 * @param string $wallet Wallet address to look up
 * @return array|WP_Error wp_remote_get() response
 */
function cryptoniq_remote_get( $coin = 'BTC', $wallet = '' ) {
    // Power-user override for the Etherscan key. If absent, fall back to a
    // shared key (low rate-limit but works out of the box for new installs).
    $api_change = cryptoniq_get_option( 'apikey_eth' );
    $api_def    = 'Z3RSPYD5E7Y84C7REJEA4HPVGFWW85CXTD';
    $key        = $api_change ? $api_change : $api_def;

    $url = '';
    if ( $coin === 'BTC' ) {
        $url = 'https://blockchain.info/unspent?active=' . rawurlencode( $wallet );
    } elseif ( $coin === 'ETH' ) {
        $url = 'https://api.etherscan.io/v2/api?chainid=1&module=account&action=txlist&sort=desc&page=1&offset=100&apikey='
             . rawurlencode( $key ) . '&address=' . rawurlencode( $wallet );
    }

    return wp_remote_get( $url, array(
        'timeout'    => 30,
        'sslverify'  => true,
        'user-agent' => get_bloginfo( 'url' ),
    ) );
}
