⛓️

Chainlink Functions

2024/04/19に公開

Chainlink Functionsを試してみた際のメモ。

Call an API with HTTP Query Parameters

Call an API with HTTP Query Parametersに従う。

Prerequisites

Set up your environment

$ node -v
v20.12.1
$ npm -v
10.5.0

Configure your onchain resources

Deploy a Functions consumer contract on Sepolia

0xD4b85454621C3363f8C99a6E60701311a60A3B15

Create a subscription

各Stepでトランザクションが発生するのでガス代が必要。

Subscription created

0xa708485b0868f6aef374fc8a515d68738bfa02c85b4c482c5bc6e0e90a68aa5b

Add funds

0x8ce82b3f100a0bfb142ba56b2501afdb4897281b88ecef7fb5f5555556c116c6

Add consumer

0x19970ee9fcae162db838af58fcfde3de219e3c807acaed12a2f868b0e8f0ac82

After creation, you can fetch the details of your subscription:

Subscription 2366

etherscanでもContractから確認可能。

Tutorial

Replace the consumer contract address and the subscription ID with your own values.

const consumerAddress = "0xd4b85454621c3363f8c99a6e60701311a60a3b15";
const subscriptionId = 2366;

Make a request:

$ node examples/2-call-api/request.js
secp256k1 unavailable, reverting to browser version
Start simulation...
Simulation result {
  capturedTerminalOutput: 'HTTP GET Request to https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD\n' +
    'ETH price is: 2999.57 USD\n',
  responseBytesHexstring: '0x00000000000000000000000000000000000000000000000000000000000493b5'
}Decoded response to uint256:  299957n

Estimate request costs...
Fulfillment cost estimated to 1.097842819195555 LINK

Make request...Functions request sent! Transaction hash 0x2368e0c63329ebe4873b21601047417697ce4df7aabb5379f0d4aaa736494fff. Waiting for a response...
See your request in the explorer https://sepolia.etherscan.io/tx/0x2368e0c63329ebe4873b21601047417697ce4df7aabb5379f0d4aaa736494fffRequest 0x98ec5b605c70ed4a09050d27a92d0cd74deaeed1a999931a828c9b3a19b0ceb0 successfully fulfilled. Cost is 0.26784723648294764 LINK.Complete reponse:  {
  requestId: '0x98ec5b605c70ed4a09050d27a92d0cd74deaeed1a999931a828c9b3a19b0ceb0',
  subscriptionId: 2366,
  totalCostInJuels: 267847236482947640n,
  responseBytesHexstring: '0x00000000000000000000000000000000000000000000000000000000000493ec',
  errorString: '',
  returnDataBytesHexstring: '0x',
  fulfillmentCode: 0
}Decoded response to uint256:  300012n

Chainlinkサービスでもfullfillmentを確認できる。

Request ID

Consumerで直接確認することもできる。

0xD4b85454621C3363f8C99a6E60701311a60A3B15

Summary

  • 必要なコスト
    • functions.chain.linkへのLINKトークンのデポジット
    • フロントエンドなどからConsumerへトランザクションを発行する際のガス代

Discussion