'Chainlink Hackathon Creating External Bridges
I have try to copy what Harry done in the youtube tutorial. Checked for several hours but couldn't find any solution. Here I am sharing all the code below:
index.js
const { Requester, Validator } = require('@chainlink/external-adapter')
require('dotenv').config()
// Define custom error scenarios for the API.
// Return true for the adapter to retry.
const customError = (data) => {
if (data.Response === 'Error') return true
return false
}
// Define custom parameters to be used by the adapter.
// Extra parameters can be stated in the extra object,
// with a Boolean value indicating whether or not they
// should be required.
const customParams = {
playerId: ['playerId'],
endpoint: false
}
const createRequest = (input, callback) => {
// The Validator helps you validate the Chainlink request data
const validator = new Validator(callback, input, customParams)
const jobRunID = validator.validated.id
const apiKey = process.env.API_KEY
const pId = validator.validated.data.playerId
const url = `https://api.sportsdata.io/v3/soccer/stats/json/PlayerSeasonStatsByPlayer/1/${pId}?key=${apiKey}`
const params = {
pId
}
// This is where you would add method and headers
// you can add method like GET or POST and add it to the config
// The default is GET requests
// method = 'get'
// headers = 'headers.....'
const config = {
url,
params
}
// The Requester allows API calls be retry in case of timeout
// or connection failure
Requester.request(config, customError)
.then(response => {
// It's common practice to store the desired value at the top-level
// result key. This allows different adapters to be compatible with
// one another.
//response.data.result = Requester.validateResultNumber(response.data, [tsyms])
callback(response.status, Requester.success(jobRunID, response))
})
.catch(error => {
callback(500, Requester.errored(jobRunID, error))
})
}
// This is a wrapper to allow the function to work with
// GCP Functions
exports.gcpservice = (req, res) => {
createRequest(req.body, (statusCode, data) => {
res.status(statusCode).send(data)
})
}
// This is a wrapper to allow the function to work with
// AWS Lambda
exports.handler = (event, context, callback) => {
createRequest(event, (statusCode, data) => {
callback(null, data)
})
}
// This is a wrapper to allow the function to work with
// newer AWS Lambda implementations
exports.handlerv2 = (event, context, callback) => {
createRequest(JSON.parse(event.body), (statusCode, data) => {
callback(null, {
statusCode: statusCode,
body: JSON.stringify(data),
isBase64Encoded: false
})
})
}
// This allows the function to be exported for testing
// or for running in express
module.exports.createRequest = createRequest
.env
API_KEY='I placed my API KEY here'
MINIMUM_CONTRACT_PAYMENT_LINK_JUELS=100000000000000000
My chainlink kovan node is active:
APIConsumer code in remix:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
/**
* Request testnet LINK and ETH here: https://faucets.chain.link/
* Find information on LINK Token Contracts and get the latest ETH and LINK faucets here: https://docs.chain.link/docs/link-token-contracts/
*/
/**
* THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
* PLEASE DO NOT USE THIS CODE IN PRODUCTION.
*/
contract APIConsumer is ChainlinkClient {
using Chainlink for Chainlink.Request;
uint256 public games;
address private oracle;
bytes32 private jobId;
uint256 private fee;
/**
* Network: Kovan
* Oracle: 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8 (Chainlink Devrel
* Node)
* Job ID: d5270d1c311941d0b08bead21fea7747
* Fee: 0.1 LINK
*/
constructor() {
setPublicChainlinkToken();
oracle = 0x75095239a719cE30A91c1bc77713420F7950BF70;
jobId = "c41b2ccd05e841e28c2026bb5e2e6e05";
fee = 0.1 * 10**18;
}
/**
* Create a Chainlink request to retrieve API response, find the target
* data, then multiply by 1000000000000000000 (to remove decimal places from data).
*/
function requestGamesData() public returns (bytes32 requestId)
{
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
// Set the URL to perform the GET request on
// Set the path to find the desired data in the API response, where the response format is:
// {"RAW":
// {"ETH":
// {"USD":
// {
// "VOLUME24HOUR": xxx.xxx,
// }
// }
// }
// }
request.add("playerId", "90026231");
// Sends the request
return sendChainlinkRequestTo(oracle, request, fee);
}
/**
* Receive the response in the form of uint256
*/
function fulfill(bytes32 _requestId, uint256 _games) public recordChainlinkFulfillment(_requestId)
{
games = _games;
}
// function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract
}
I can deploy contract, see transaction executed, but when I check localhost8080 or Runs I see nothing. Any help is appreciated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
