Make HTTP Request Using Your Solidity Smart Contract

0
236

Have you ever tried to integrate a micro-service with your smart contract? Try to make an API request? Is that even possible on solidity? The short answer is NO. Etherum Blockchain protocols are totally different from the API protocols. The Etherum is deterministic it can’t change by time and all node should approve it but HTTPS requests change by time find out more on this link

Why it’s not easy like Nodejs and Laravel

The Problem is that a Smart contract, as an object on the blockchain, can’t just send an HTTP request. It would contradict a fundamental principle of all blockchain protocols — their deterministic nature ‘The result of any transaction must always be the same for nodes to verify it no matter where, how and when we call it’. So we can’t make API calls using solidity. A trick to solve the problem is ‘Orcalize’.What is Orcalize?

Use Oracle as intermediate between the blockchain and the API. The Oracle keep listing(observing the smart contract) whenever a transaction occurs. The Oracle request API data and inject the response to the smart contract. Find a lot of examples and explanation on the documentation and this repository

Unfortunately, The majority of all code examples are just GET calls. I don’t know why? I even supposed that Oraclize provide solution for GET calls only. Or honestly, I found some examples but they were a bit complicated for newbies like me. So I wrote this article to explain it in more easy way.

Code Implementation

1- We need to import the Orcalize library its name ‘provable’ clone the GitHub repository then import the contract

import “github.com/oraclize/ethereum-api/provableAPI.sol”;

2-It’s best practice to emit events when you make API calls if they fail or succeed.

3- Check if our contract have enough balance to make the transaction

if (provable_getPrice(“computation”) > address(this).balance) 

4-Then making HTTP calls using ‘provable_query’ function used to make all API calls, for example, to make a Post Call:(example from documentation)

// The URL datasource also supports a supplement argument, useful for creating HTTP POST requests.
// If that argument is a valid JSON string, it will be automatically sent as JSON.
provable_query(“URL”, “json(https://shapeshift.io/sendamount).success.deposit",
‘{“pair”:”eth_btc”,”amount”:”1",”withdrawal”:”1AAcCo21EUc1jbocjssSQDzLna9Vem2UN5"}’)

Full code should be something like that

if (provable_getPrice(“computation”) > address(this).balance) {
emit LogNewProvableQuery(“Provable query was NOT sent, please add some ETH to cover for the query fee”);
} else {
emit LogNewProvableQuery(“Provable query was sent, standing by for the answer…”);
provable_query(“URL”, “json(https://shapeshift.io/sendamount).success.deposit",
‘{“pair”:”eth_btc”,”amount”:”1",”withdrawal”:”1AAcCo21EUc1jbocjssSQDzLna9Vem2UN5"}’)
}
}

To make your code more dynamic and easy to call any type of methods like normal back-end. you could parse all provable_query function parameter from external request function like that:

function request(
string memory _query,
string memory _method,
string memory _url,
string memory _kwargs
)
public
payable
{
if (provable_getPrice(“computation”) > address(this).balance) {
emit LogNewProvableQuery(“Provable query was NOT sent, please add some ETH to cover for the query fee”);
} else {
emit LogNewProvableQuery(“Provable query was sent, standing by for the answer…”);
provable_query(“computation”,
[_query,
_method,
_url,
_kwargs]
);
}
}

That’s it, You can find a lot of truffle examples on this link. Thanks!


Make HTTP Request Using Your Solidity Smart Contract was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.