我该如何从流量区块链上的智能合约中听取活动?

发布于 2025-02-11 17:57:03 字数 65 浏览 0 评论 0 原文

我使用了一些服务来聆听我在以太坊上的智能合约中的活动。 但是我找不到与流量区块链一起工作时的任何文档。 我该怎么做?

I used some services for listening to an event from my smart contract on Ethereum.
But I can't find any documents similar to that when working with Flow Blockchain.
How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

极度宠爱 2025-02-18 17:57:03

对于一项共同任务来说,这是一个非常好的问题!
@onflow/fllfly/fcl 软件包为您提供有用的方法 events ,可用于“订阅”特定事件。
您可以在Flow Docs网站上查看事件描述。或者,您可以复制/粘贴代码bellow并随身携带:

import * as fcl from "@onflow/fcl";

// We need to point FCL to some access node.
// We will use Mainnet REST endpoint for this, as the contract
// we want to listen to is deployed there

fcl.config({
  "accessNode.api": "https://rest-mainnet.onflow.org",
  // we will set the poll rate for events to 3 seconds
  "fcl.eventPollRate": 3000
});

// FlowFees is the most active contract, since every transaction will
// trigger "FeesDeducted" event, so it will be easier to see that our code
// is working correctly
const contractAddress = "f919ee77447b7497";
const contractName = "FlowFees";
const eventName = "FeesDeducted";

// Event name consist of 2 or 4 parts
// 2 part event name have only system events
// For deployed contract, event should be constructed from 4 parts
// - "A" prefix, stands for "account"
// - address where contract, holding definition of event is deployed
// - contract name
// - event name
const event = `A.${contractAddress}.${contractName}.${eventName}`;

console.log(
  `Listening for event "${eventName}" from "${contractName}" deployed on account 0x${contractAddress}`
);
fcl.events(event).subscribe((eventData) => {
  console.log(eventData);
});

您也可以尝试使用工作 codesandbox示例

This is a really nice question for a common task!
@onflow/fcl package provides you with a helpful method events, which can be used to "subscribe" to a specific event.
You can check events description on Flow Docs Site. Or you can copy/paste code bellow and play around with it:

import * as fcl from "@onflow/fcl";

// We need to point FCL to some access node.
// We will use Mainnet REST endpoint for this, as the contract
// we want to listen to is deployed there

fcl.config({
  "accessNode.api": "https://rest-mainnet.onflow.org",
  // we will set the poll rate for events to 3 seconds
  "fcl.eventPollRate": 3000
});

// FlowFees is the most active contract, since every transaction will
// trigger "FeesDeducted" event, so it will be easier to see that our code
// is working correctly
const contractAddress = "f919ee77447b7497";
const contractName = "FlowFees";
const eventName = "FeesDeducted";

// Event name consist of 2 or 4 parts
// 2 part event name have only system events
// For deployed contract, event should be constructed from 4 parts
// - "A" prefix, stands for "account"
// - address where contract, holding definition of event is deployed
// - contract name
// - event name
const event = `A.${contractAddress}.${contractName}.${eventName}`;

console.log(
  `Listening for event "${eventName}" from "${contractName}" deployed on account 0x${contractAddress}`
);
fcl.events(event).subscribe((eventData) => {
  console.log(eventData);
});

You can also try and play around with working Codesandbox Example

一场春暖 2025-02-18 17:57:03

有多种方法可以做到这一点。我认为最简单的是为此使用服务。目前,我们在.find上使用的是 https://graffle.io.io

您也可以使用其中一个SDK自己制作。 Kitty-items有一个 https://github.com/onflow/kitty/kitty-iTems/ << /a>在JavaScript中。

如果您喜欢Golang,我将有一些事件提取代码 https:// https:// github.com/bjartek/overflow/blob/main/overflow/event.go 。这是有关如何使用的示例:

There are multiple ways of doing this. The easiest I think is to use a service for this. The one we use at .find at the moment is https://graffle.io.

You could also make your own using one of the SDKs. Kitty-items has an example of this https://github.com/onflow/kitty-items/ in javascript.

If you prefer golang I have some event fetching code in overflow https://github.com/bjartek/overflow/blob/main/overflow/event.go. Here is an example on how it can be used: https://github.com/bjartek/overflow/blob/main/overflow/event_integration_test.go#L13

爱她像谁 2025-02-18 17:57:03

上面的更新:铺设不再提供此功能。

Flow最近发布了事件流API访问,可从访问节点获得。这为消费事件提供了基于推动的访问范式,而无需进行接触调查。

flip: https> https:https:https: //forum.flow.com/t/flip-73-proposal-to-add-event-streaming-to-access-api/4509

docs: https://developers.flower.flow.com/architector.com/architector/node-ops/nodecture/node-ops/nodops/ Access-api#tag/nodeversionInfo/paths/〜1node_version_info/get

Update on above: Graffle no longer provides this capability.

Flow recently released Event Streaming API access which are available from Access Nodes. This offers a push based access paradigm for consuming events without the need for contact polling.

FLIP: https://forum.flow.com/t/flip-73-proposal-to-add-event-streaming-to-access-api/4509

Docs: https://developers.flow.com/architecture/node-ops/nodes/access-api#tag/NodeVersionInfo/paths/~1node_version_info/get

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文