How blockchain works
Blockchain is a distributed database, which consists of two records:
Integration on Blockchain API with Rails:Blockchain is a distributed database, which consists of two records:
- Individual transactions
- Blocks
To integrate blockchain api to exchange bitcoin from one wallet to another wallet.
API KEY:
To get the API key we have to first signup on the link https://api.blockchain.info/customer/signup
then we will get the API key, for e.g. :
api_code: XXXXXXXX-XXX-XXXX-XXXX-XXXXXXXXXXXX
Create API:
First need to create a wallet:
I have created the wallet in the method:
http://localhost:3030/api/v2/create_wallet
Description: This api is use to create blockchain wallet
# create_wallet: http://XXX.XXX.X.XX:3030/api/v2/create_wallet
# create_wallet: https://blockchain.info/api/v2/create_wallet
Write the API method to check the wallet balance.
wallet_address: “Address of the wallet” Confirmation: “Password”address_balance: https://blockchain.info/q/addressbalance/%{wallet_address}?confirmations=%{confirmation}
Write API to send or receive bitcoin using guid.
Guid: Uniq id of the user main_password: blockchain password second_password: blockchain second password to: Receiving address from: Sending addressmake_payment_by_guid: http://localhost:3030/merchant/%{guid}/payment?password=%{main_password}&second_password=%{second_password}&to=%{address}&amount=%{amount}&from=%{from}
current_rate: https://blockchain.info/ticker
Description: “this api will use for getting bitcoin value as per the local currency.”
for ex., 1btc = ? (USD)
The following method is used to call the bitcoin exchange API using guid
# def self.transaction_by_guid(receiver_address, amount_in_btc, guid, main_pass, second_pass)
def self.receive_btc_by_guid(guid, sender_main_password, sender_second_password, to_wallet_address, amt_in_btc, from_wallet_address)
# convert btc to satoshi and add transaction fee
amount = (btc_to_satoshi(amt_in_btc))
# Prepare params
request_url = Bitcoinexchange::Application.blockchain[“api”][“make_payment_by_guid”] % {guid: guid || ”, main_password: sender_main_password || ”, second_password: sender_second_password || ”, address: to_wallet_address, amount: amount, from: from_wallet_address}
# If in response failure occurred using third party API or http client it generate error and return nil into the response
response = begin
RestClient.get(request_url, {“Content-Type”: “application/x-www-form-urlencoded”})
rescue RestClient::ExceptionWithResponse => e
e.response
end
# If response is success(code == 200)
response_body = JSON.parse(response.body)
if response.code == 200
response_body
elsif response_body[“error”]!=””
response_body[“error”]
# it gives the the converted value in BTC
# response_body = “#{response_b[“message”]}” +” to “+”#{response_b[“to”]}”+”successfully.”
else
# To raise the error if error occured in executing blockchain api
raise Exceptions::TransactionError::ParamsError, response.body
end
end
Source: http://www.cryptextechnologies.com/blogs/blockchain-api-integration
Comments
Post a Comment