1

I am fairly new to blockchain development. I want to call a function in my contract when a condition in the API is true. Should I make this API call in my contract, or in my frontend?

2
  • Can you give more details? Smart contracts do not have direct access to resources outside of the blockchain, you need to use an oracle for such things.
    – Ismael
    Commented Dec 26, 2017 at 4:38
  • @Ismael I want to complete a transaction when a certain parameter in the RESTful API i want to access is true. Is it better to use an orcale or just use normal methods in javascript? Commented Dec 26, 2017 at 12:31

2 Answers 2

1

If your condition occurs within your smart contract, e.g. you've reached a certain number of crowdfunding subscribers, you can track the activities of your smart contracts using EVENTS and an external monitor (e.g. using a node.js daemon). When the event is triggered within your contract, it will be detected by the daemon which will in turn trigger the execution of your pre-programmed code/script. Ethers.io has a great smart contract library which you can use to build your daemon.

If you need to call a method of your smart contract when an external event occurs (e.g. a user signs in to your website) its as simple as using the Web3.js library's contract methods.


Here is a snippet of how to call a method on an Ethereum contract (from a JS-based API server):

const Web3 = require('web3')
const web3 = new Web3('https://mainnet.infura.io')

function myFunction(myParam) {
    var contractAddress = '<ENTER-YOUR-CONTRACT-ADDRESS-HERE>'
    var contractABI = [<ENTER-YOUR-ABI-HERE>]
    var contract = new web3.eth.Contract(contractABI, contractAddress)
    var callData = contract.methods.<CONTRACT-FUNCTION>(myParam).call()
    callData.then(function(result) {
        console.log(result)
    })
}

myFunction(<MY-PARAM>)
4
  • See, I want to complete a transaction when a certain parameter in the RESTful API i want to access is true. Should I be using an oracle to access this data from within my smart contract, or just use normal methods in javascript? Commented Dec 26, 2017 at 12:30
  • added an example to my answer. once your api condition becomes true, then you can then call that snippet to execute your contract.
    – Shomari
    Commented Dec 26, 2017 at 22:11
  • So, what your saying is, I can just keep track using events and such, and do my API calls in the frontend? Rather than using an oracle. Commented Dec 29, 2017 at 11:49
  • Others may disagree, but I find the oracle approach to be overly complicated atm. I've used the approach described above and it works quite excellent. Regardless, I do plan to keep exploring the oracle method of real-world interactions.
    – Shomari
    Commented Jan 1, 2018 at 1:23
0

Just call a method on contract to check the condition ( this does not cost gas ) if it's true call the API else don't . If you want to countinuously check the value of variable you can use web sockets .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.