Skip to main content

Contract Event

In this tutorial, we will learn how to subscribe to an ERC20 contract Transfer event using the Arkhia API.

info

We will be using the Events API to create a new event subscription for a contract.

To subscribe to a contract event, follow these steps:

  1. Create a new item using the Create Item API.
  2. Update the item ABI file using the Update Item API.
  3. Add item events using the Update Item API.
  4. Enable the item using the Enable Item API.

Copy the following code in your preferred language and replace the placeholders with your Arkhia API Key and Arkhia API Secret.

1. Create new event item
const axios = require('axios');const createApiUrl = `https://api.arkhia.io/events/hedera/create/contract/<YOUR_ARKHIA_API_KEY>`;const apiSecret = `<YOUR_ARKHIA_API_SECRET>`;const headers = { headers: { "x-api-secret": apiSecret } };const createPayload = {    item_id: '0.0.6071924',    network_id: 296}createItem = async () => {    try {        const response = await axios.post(createApiUrl, { scoutSettings: createPayload }, headers);        return response;    } catch (e) {        console.error(e);    }}createItem();
2. Update item ABI
const axios = require('axios');const updateAbiApiUrl = `https://api.arkhia.io/events/hedera/settings/update/contract/abi/<YOUR_ARKHIA_API_KEY>`;const apiSecret = `<YOUR_ARKHIA_API_SECRET>`;const headers = { headers: { "x-api-secret": apiSecret } };const updateAbiPayload = {    item_id: "0.0.6071924",    network_id: 296,    config_type: "abi",    config_object: [        {            anonymous: false,            inputs: [                { indexed: true, internalType: "address", name: "owner", type: "address" },                { indexed: true, internalType: "address", name: "spender", type: "address" },                { indexed: false, internalType: "uint256", name: "value", type: "uint256" }            ],            name: "Approval",            type: "event"        },        {            anonymous: false,            inputs: [                { indexed: true, internalType: "address", name: "from", type: "address" },                { indexed: true, internalType: "address", name: "to", type: "address" },                { indexed: false, internalType: "uint256", name: "value", type: "uint256" }            ],            name: "Transfer",            type: "event"        }    ]}uploadAbi = async () => {    try {        const response = await axios.post(updateAbiApiUrl, { scoutSettings: updateAbiPayload }, headers);        return response;    } catch (e) {        console.error(e);    }}uploadAbi();
3. Add item event
const axios = require('axios');const updateEventUrl = `https://api.arkhia.io/events/hedera/settings/update/contract/events/<YOUR_ARKHIA_API_KEY>`;const apiSecret = `<YOUR_ARKHIA_API_SECRET>`;const headers = { headers: { "x-api-secret": apiSecret } };const addEvent = {    item_id: "0.0.6071924",    network_id: 296,    config_type: "events",    config_object: [        {            eventId: "0.0.6071924_296_Transfer",            eventName: "Transfer",            eventRules: [                {                    ruleId: "Transfer_rule0",                    ruleName: "Transfer Rule0",                    enabled: true,                    email_notification: false,                    polling_interval: 1000,                    webhooks: [                        {                            tag: "My-Webhook",                            key: "my-webhook",                            value: "https://yet.another.webhook",                            type: "get",                            active: true                        }                    ],                    parameterCollection: [                        {                            enabled: false,                            parameterId: "Transfer_rule0_from",                            parameterName: "from",                            parameterType: "address",                            parameterRule: 1,                            parameterRuleValue: "",                            webhooks: []                        },                        {                            enabled: false,                            parameterId: "Transfer_rule0_to",                            parameterName: "to",                            parameterType: "address",                            parameterRule: 1,                            parameterRuleValue: "",                            webhooks: []                        },                        {                            enabled: true,                            parameterId: "Transfer_rule0_value",                            parameterName: "value",                            parameterType: "integer",                            parameterRule: 4,                            parameterRuleValue: "5000",                            webhooks: []                        }                    ]                }            ]        }    ]}updateEvent = async () => {    try {        const response = await axios.post(updateEventUrl, { scoutSettings: addEvent }, headers);        return response;    } catch (e) {        console.error(e);    }}updateEvent();
4. Enable item event
const axios = require('axios');const enableApiUrl = `https://api.arkhia.io/events/hedera/enable/contract/<YOUR_ARKHIA_API_KEY>`;const apiSecret = `<YOUR_ARKHIA_API_SECRET>`;const headers = { headers: { "x-api-secret": apiSecret } };const enablePayload = {    item_id: '0.0.6071924',    network_id: 296}enableItem = async () => {    try {        const response = await axios.post(enableApiUrl, { scoutSettings: enablePayload }, headers);        return response;    } catch (e) {        console.error(e);    }}enableItem();
Example webhook response payload
{  "params": {    "body": {      "blockNumber": 7768608,      "blockHash": "0x095a2b0fac3f934eadf06768cdd4cf5b05d54da705e1e8f4555e8746c33fb1ff",      "transactionIndex": 1,      "removed": false,      "address": "0xFC89B20D1DD88F981487eE0E4F26897C8685a1B8",      "data": "0x00000000000000000000000000000000000000000000000000000000000005dc",      "topics": [        "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",        "0x0000000000000000000000000000000000000000000000000000000000000000",        "0x0000000000000000000000000180bcd527702c21e2ce767626704f39e1976aac"      ],      "transactionHash": "0x8aca8ea31fad6c52fc034a0c537225a2d8a7e50b1a78d924cbc7b4fcbd41fd00",      "logIndex": 0,      "from": "0x0000000000000000000000000000000000000000000000000000000000000000",      "to": "0x0000000000000000000000000180bcd527702c21e2ce767626704f39e1976aac",      "value": { "type": "BigNumber", "hex": "0x05dc" }    },    "scoutType": 2  }}