usd -> {strtolower($currency_symbol)}; return $token_usd_price*$currency_rate; } /** * Get USD token rate by token address * * @param WC_8Pay_Token $token * @param string $chain * * @return float */ public static function get_usd_token_price($chain , $token){ //gecko-terminal chain mapping $chain = $chain == 'ethereum' ? 'eth':$chain; $chain = $chain == 'polygon' ? 'polygon_pos':$chain; //Fetch token_price by address $url = 'https://api.geckoterminal.com/api/v2/simple/networks/' . $chain . '/token_price/' . $token['address']; $response = wp_remote_get( $url ); $body = wp_remote_retrieve_body( $response ); $body_decoded = json_decode($body); //gecko-terminal is not EIP-55 compliant. $token_address = strtolower( $token['address']); $main_pool_address = array_key_exists('main_pool_address', $token) ? strtolower( $token['main_pool_address']) : ''; if ( isset($body_decoded->errors)) { throw new Exception( 'Error fetching rate: ' . $body_decoded->errors[0]->title ); } if (isset($body_decoded->data->attributes->token_prices->{$token_address})) { $token_usd_price = $body_decoded->data->attributes->token_prices->{$token_address}; } else { // Fallback to dex-pools price query $url = "https://api.geckoterminal.com/api/v2/networks/$chain/pools/$main_pool_address"; $response = wp_remote_get( $url ); $body = wp_remote_retrieve_body( $response ); $body_decoded = json_decode($body); if (isset($body_decoded->data->attributes->base_token_price_usd)) { $token_usd_price = $body_decoded->data->attributes->base_token_price_usd; }else{ throw new Exception( 'Error fetching rate.' ); } } return $token_usd_price; } } new WC_8Pay_Rates();