0

i had developed a external adapter that make a http get request to cryptocompare api and get latest eth-usd price for me.

from bridge import Bridge

class Adapter: base_url = 'https://min-api.cryptocompare.com/data/price' from_params = ['base', 'from', 'coin'] to_params = ['quote', 'to', 'market']

def __init__(self, input):
    self.id = input.get('id', '1')
    self.request_data = input.get('data')
    if self.validate_request_data():
        self.bridge = Bridge()
        self.set_params()
        self.create_request()
    else:
        self.result_error('No data provided')

def validate_request_data(self):
    if self.request_data is None:
        return False
    if self.request_data == {}:
        return False
    return True

def set_params(self):
    for param in self.from_params:
        self.from_param = self.request_data.get(param)
        if self.from_param is not None:
            break
    for param in self.to_params:
        self.to_param = self.request_data.get(param)
        if self.to_param is not None:
            break

def create_request(self):
    try:
        params = {
            'fsym': self.from_param,
            'tsyms': self.to_param,
        }
        response = self.bridge.request(self.base_url, params)
        data = response.json()
        self.result = data[self.to_param]
        data['result'] = self.result
        self.result_success(data)
    except Exception as e:
        self.result_error(e)
    finally:
        self.bridge.close()

def result_success(self, data):
    self.result = {
        'jobRunID': self.id,
        'data': data,
        'result': self.result,
        'statusCode': 200,
    }

def result_error(self, error):
    self.result = {
        'jobRunID': self.id,
        'status': 'errored',
        'error': f'There was an error: {error}',
        'statusCode': 500,
    }

i was successful in getting eth-usd price. Here is the question that can i use smtp protocol in smart contract?

smtp mail sending code

# the first step is always the same: import all necessary components:
import smtplib
from socket import gaierror

now you can play with your code. Let’s define the SMTP server separately here:

port = 2525 smtp_server = "smtp.mailtrap.io" login = "1a2b3c4d5e6f7g" # paste your login generated by Mailtrap password = "1a2b3c4d5e6f7g" # paste your password generated by Mailtrap

specify the sender’s and receiver’s email addresses

sender = "from@example.com" receiver = "mailtrap@example.com"

type your message: use two newlines (\n) to separate the subject from the message body, and use 'f' to automatically insert variables in the text

message = f"""
Subject: Hi Mailtrap To: {receiver} From: {sender}

This is my first message with Python."""

try: #send your message with credentials specified above with smtplib.SMTP(smtp_server, port) as server: server.login(login, password) server.sendmail(sender, receiver, message)

# tell the script to report if your message was sent or which errors need to be fixed 
print('Sent')

except (gaierror, ConnectionRefusedError): print('Failed to connect to the server. Bad connection settings?') except smtplib.SMTPServerDisconnected: print('Failed to connect to the server. Wrong user/password?') except smtplib.SMTPException as e: print('SMTP error occurred: ' + str(e))

Hassan ALi
  • 141
  • 2

1 Answers1

0

It is not possible to use external APIs on a blockchain.

More information in this answer: https://ethereum.stackexchange.com/a/47778/620

Mikko Ohtamaa
  • 22,269
  • 6
  • 62
  • 127