Gelato Functions 推出 Callbacks 功能,通过提供交易生命周期的更多反馈来增强开发者体验。Callbacks 允许开发者高效管理交易结果,实现错误处理和自定义逻辑,例如发送警报、数据库更新或触发额外交易。开发者可以通过 onSuccess 和 onFail 回调来处理交易成功或失败的情况,并使用本地测试环境来模拟执行。
博客 - 更新
通过 Gelato Functions 回调,将你的 DevX 提升 1000 倍!
有了新的 Gelato Functions 更新 - 回调,通过更多关于交易生命周期的反馈来增强你的开发者体验。
Gelato 的 Functions 是无服务器且事件驱动的工具,专为使用链下和链上数据自动化区块链交易而设计。
在 Gelato Functions 中引入回调现在允许开发者有效地管理交易结果。此功能提供了一个强大的框架来处理自动化任务中的各种场景,最终增强去中心化应用程序 (dApp) 的功能。
在我们的 GitHub 存储库 中查看回调如何在实际场景中实施
这些回调在链上执行成功后触发。此回调非常适合跟踪交易和后处理。
Web3Function.onSuccess(async (context: Web3FunctionSuccessContext) => {
const { transactionHash } = context;
console.log("onSuccess: txHash: ", transactionHash);
// Additional onSuccess logic...
});
当执行遇到问题(例如资金不足、模拟失败或执行回滚)时,将调用此回调。这对于错误处理和实施回退策略至关重要。
Web3Function.onFail(async (context: Web3FunctionFailContext) => {
const { reason, transactionHash, callData } = context;
// Handle various failure scenarios...
});
考虑这样一种情况:你有一个预言机智能合约,需要定期从外部 API 更新加密货币价格。下面是如何使用回调:
onRun 函数:
onSuccess 回调:
onFail 回调:
//Web3 Function onSuccess callback
Web3Function.onSuccess(async (context: Web3FunctionSuccessContext) => {
const { userArgs, transactionHash, storage } = context;
console.log("userArgs: ", userArgs.canExec);
console.log("onSuccess: txHash: ", transactionHash);
const currency = (userArgs.currency as string) ?? "ethereum";
let price = 0;
try {
const price = await getCurrentPrice(currency);
console.log(`Current Price: ${price}`);
await storage.set("lastPrice", price.toString());
} catch (err) {
console.error("Failed to update price:", err);
}
// Update storage with the current price
await storage.set("lastPrice", price.toString());
});
在此 onSuccess 回调中,我们有几个关键要素:
//Web3 Function onFail callback
Web3Function.onFail(async (context: Web3FunctionFailContext) => {
const { userArgs, reason } = context;
let alertMessage = `Web3 Function Failed. Reason: ${reason}`;
console.log("userArgs: ", userArgs.canExec);
if (reason === "ExecutionReverted") {
alertMessage += ` TxHash: ${context.transactionHash}`;
console.log(`onFail: ${reason} txHash: ${context.transactionHash}`);
} else if (reason === "SimulationFailed") {
alertMessage += ` callData: ${JSON.stringify(context.callData)}`;
console.log(
`onFail: ${reason} callData: ${JSON.stringify(context.callData)}`
);
} else {
console.log(`onFail: ${reason}`);
}
// Send Slack alert if specified
await sendSlackAlert(alertMessage);
});
现在让我们探索 onFail 回调:
这些示例展示了 onSuccess 和 onFail 回调如何允许你对交易的成功或失败做出不同的响应、实施错误处理以及根据结果采取特定操作。它们提供了一种强大的机制来定制去中心化应用程序的行为,以满足你的确切要求。
对于那些渴望深入了解 Gelato Functions 的世界并亲身体验回调功能的人员 - 请前往我们的 Github 存储库并访问此处提到的代码示例。
为了确保回调的可靠性,Gelato Functions 提供了一个本地测试环境。这允许你模拟成功和失败的执行,从而帮助你在实时部署之前微调回调逻辑。你可以为此目的使用 –onFail 或 –onSuccess 标志。
## Test onFail Callback
yarn test src/web3-functions/callbacks/index.ts --logs --onFail
## Test onSuccess Callback
yarn test src/web3-functions/callbacks/index.ts --logs --onSuccess
总之,Gelato Functions 的回调提供了一种动态且强大的方式来控制你的去中心化应用程序 (dApp)。凭借响应成功和失败的能力,回调为你提供了无与伦比的灵活性。你可以微调 dApp 的行为、轻松处理错误,甚至引入自定义逻辑来满足你的特定需求。
Gelato 是一个 Web3 云平台,使开发者能够创建自动化的、无 Gas 的和链下感知的 Layer 2 链和智能合约。400 多个 web3 项目多年来一直依赖 Gelato 来促进 DeFi、NFT 和游戏中的数百万笔交易。
Gelato RaaS: 一键部署你自己的量身定制的 ZK 或 OP L2 链,其中包含原生账户抽象和所有内置的 Gelato 中间件。
VRF:Gelato VRF 为区块链应用程序提供快速、链上可验证的随机性。
Functions:无服务器、事件驱动的函数,用于自动化区块链交易。
Relay:通过易于使用的 API,让你的用户可以访问可靠、强大且可扩展的无 Gas 交易。
账户抽象 SDK:Gelato 已与 Safe 合作,构建一个功能完善的账户抽象 SDK,将 Gelato 业界最佳的无 Gas 交易功能与业界最安全的智能合约钱包相结合。
订阅我们的新闻通讯并打开你的 Twitter 通知,以获取有关 Gelato 生态系统的最新更新! 如果你有兴趣加入 Gelato 团队并构建互联网的未来,请浏览空缺职位并在此处申请 here。
- 原文链接: gelato.cloud/blog/Develo...
- 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!