0

我正在尝试学习链环外部适配器,并且对这一切都很陌生。

是否有可能获得以下帮助?我的适配器代码不起作用,并告诉我我使用了无效参数。

我试图在 coinpaprika 上使用以下 API:

https://api.coinpaprika.com/v1/price-converter?base_currency_id=link-chainlink"e_currency_id=usdt-tether&amount=1

这是我在 nodejs 中的适配器代码


const customError = (data) => {
  if (data.Response === 'Error') return true
  return false
}

const customParams = {
  base: ['base', 'from', 'coin'],
  quote: ['quote', 'to', 'market'],
  amount: ['amount', 'qty', 'num'],
  endpoint: false
}

const createRequest = (input, callback) => {
  const validator = new Validator(callback, input, customParams)
  const jobRunID = validator.validated.id
  const endpoint = validator.validated.data.endpoint || 'price-converter'
  const url = `https://api.coinpaprika.com/v1/${endpoint}`
  const base_currency_id = validator.validated.data.base.toUpperCase()
  const quote_currency_id = validator.validated.data.quote.toUpperCase()
  const amount = validator.validated.data.amount.toUpperCase()


  const params = {
    base_currency_id,
    quote_currency_id,
    amount
  }

  const config = {
    url,
    params
  }

  Requester.request(config, customError)
    .then(response => {
      response.data.result = Requester.validateResultNumber(response.data, [amount])
      callback(response.status, Requester.success(jobRunID, response))
    })
    .catch(error => {
      callback(500, Requester.errored(jobRunID, error))
    })
}

这是我的 CURL 调用:

curl -X POST -H "content-type:application/json" "http://localhost:8080/" --data '{ "id": 0, "data": { "base": "chainlink", "quote": "usdt-tether","amount": "1" } }'

这是我得到的服务器错误输出

  id: 0,
  data: { base: 'chainlink', quote: 'usdt-tether', amount: '1' }
}
{"message":"Caught error. Retrying: \"Request failed with status code 400\"","level":"warn","timestamp":"2021-07-25T09:06:44.560Z"}
{"message":"Caught error. Retrying: \"Request failed with status code 400\"","level":"warn","timestamp":"2021-07-25T09:06:45.696Z"}
{"message":"Could not reach endpoint: \"Request failed with status code 400\"","level":"error","timestamp":"2021-07-25T09:06:45.800Z"}
Result:  {
  jobRunID: 0,
  status: 'errored',
  error: AdapterError: AdapterError: Request failed with status code 400
      at Function.errored (/Users/xxxx/CL-EA-NodeJS-Template/node_modules/@chainlink/external-adapter/src/requester.js:90:14)
      at /Users/pocondui/CL-EA-NodeJS-Template/index.js:42:31
      at processTicksAndRejections (internal/process/task_queues.js:95:5),
  statusCode: 500

更新

看起来我使用了错误的 CURL 参数,应该是link-chainlink而不是 chainlink

POST Data:  {
  id: 0,
  data: { from: 'link-chainlink', to: 'usdt-tether', qty: '1' }
}
{"message":"Received response: {\"base_currency_id\":\"link-chainlink\",\"base_currency_name\":\"Chainlink\",\"base_price_last_updated\":\"2021-07-25T10:40:07Z\",\"quote_currency_id\":\"usdt-tether\",\"quote_currency_name\":\"Tether\",\"quote_price_last_updated\":\"2021-07-25T10:40:07Z\",\"amount\":1,\"price\":16.917664107956217}","level":"info","timestamp":"2021-07-25T10:41:07.797Z"}
{"message":"Result could not be found in path","level":"error","timestamp":"2021-07-25T10:41:07.797Z"}
Result:  {
  jobRunID: 0,
  status: 'errored',
  error: AdapterError: AdapterError: Result could not be found in path
      at Function.errored (/Users/pocondui/CL-EA-NodeJS-Template/node_modules/@chainlink/external-adapter/src/requester.js:90:14)
      at /Users/pocondui/CL-EA-NodeJS-Template/index.js:42:31
      at processTicksAndRejections (internal/process/task_queues.js:95:5),
  statusCode: 500

现在我需要弄清楚如何正确传递结果

4

1 回答 1

1

我修好了它。2个问题是传递错误的参数,结果是错误的

将其更改为:

      response.data.result = Requester.validateResultNumber(response.data, ['price'])
于 2021-07-25T10:50:12.423 回答