0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【入門】Chainlink Automationを使ってみる(Custom-logic Trigger編)

Posted at

Chainlink AutomationでCustom Logic Triggerを登録する

この記事では、Chainlink AutomationのCustom Logic Triggerを使用して、定期的に処理を実行するスマートコントラクトを登録する方法を紹介します。

すでにTime-based Triggerを用いた記事Hardhatでのデプロイ方法について投稿していますので、適宜参照してください。


Custom Logic Triggerとは?

Custom Logic Triggerでは、独自の条件で処理を自動実行させることができます。この仕組みを利用するには、スマートコントラクトがAutomationCompatibleInterface継承し、以下の2つの関数を実装する必要があります。

  • checkUpkeep: performUpkeepを実行すべきかどうかを判定
  • performUpkeep: 実際の処理を実行

checkUpkeep はスマートコントラクト内で定義しますが、オンチェーンで実行されることはなく、オフチェーンでシミュレーションされます。trueが返された場合にのみperformUpkeepが呼び出されます。


サンプルコード

以下は、指定時間ごとにカウンターをインクリメントし、イベントを発行するシンプルなコントラクトです。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import {AutomationCompatibleInterface} from "@chainlink/contracts/src/v0.8/automation/AutomationCompatible.sol";

contract Counter is AutomationCompatibleInterface {
    uint256 public counter;
    uint256 public immutable interval;
    uint256 public lastTimeStamp;

    event CounterUpdated(uint256 counter);

    constructor(uint256 updateInterval) {
        interval = updateInterval;
        lastTimeStamp = block.timestamp;
        counter = 0;
    }

    function checkUpkeep(
        bytes calldata /* checkData */
    )
        external
        view
        override
        returns (bool upkeepNeeded, bytes memory /* performData */)
    {
        upkeepNeeded = (block.timestamp - lastTimeStamp) > interval;
    }

    function performUpkeep(bytes calldata /* performData */) external override {
        if ((block.timestamp - lastTimeStamp) > interval) {
            lastTimeStamp = block.timestamp;
            counter++;
            emit CounterUpdated(counter);
        }
    }
}

📝 このコントラクトでは、updateIntervalに指定した秒数が経過するごとにperformUpkeepが呼び出され、カウンターを1つ増やしてイベントを発行します。


Hardhatでのデプロイ方法

以下は、Hardhat Ignitionを使ってこのコントラクトをデプロイするモジュールです。

import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

const CounterModule = buildModule("CounterModule", (m) => {
  const counter = m.contract("Counter", [300]); // 300秒間隔

  return { counter };
});

export default CounterModule;

* デプロイ後に表示されるコントラクトアドレスを必ず控えておいてください。これを後ほどUpkeep登録時に使用します。

Upkeepの登録手順

コントラクトをデプロイしたら、以下の公式ドキュメントに従ってUpkeepを登録します:

📖 Chainlink Automation - Upkeep登録ガイド

登録に必要なもの:

  • コントラクトアドレス
  • Upkeep名と説明
  • checkUpkeepのロジック概要(簡潔でOK)
  • Gas Limit
  • 十分なLINKトークンの残高

Upkeepを登録・維持するにはLINKトークンが必要です。これは、Chainlinkノードが定期的にコントラクトをチェック・実行するための報酬として支払われます。

LINKの補充方法(テストネットの場合):

テストネット(例:Sepolia)を使っている場合、Chainlink Faucetでテスト用LINKを入手できます:

🔗 Chainlink Faucet

  1. ウォレットを接続
  2. 対象ネットワークを選択(例:Sepolia)
  3. ETHとLINKを請求

✅ LINK残高の確認と補充

Chainlink Automationの管理画面では、登録したUpkeepごとに残高や実行履歴を確認できます。残高が不足すると自動実行が停止されるため、定期的にLINKの残高チェックを行うことが重要です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?