如何通过user_id在Flow中用TS/Python编程获取用户委托及奖励?
获取Flow网络用户委托与奖励信息的编程实现
Flow的质押、委托及奖励数据都存储在链上的官方质押合约中,JS SDK(@onflow/fcl)其实支持通过执行Cadence脚本调用合约视图方法来获取这些数据,以下是TypeScript和Python的具体实现方案:
TypeScript 实现(基于@onflow/fcl)
- 安装依赖:
npm install @onflow/fcl
- 核心代码:
import * as fcl from "@onflow/fcl"; // 配置网络(主网/测试网二选一) fcl.config() .put("accessNode.api", "https://rest-mainnet.onflow.org") // 主网 // .put("accessNode.api", "https://rest-testnet.onflow.org") // 测试网 .put("0xFlowIDStaking", "0x8624b52f9ddcd04a"); // 主网质押合约地址,测试网为0x7e60df042a9c0868 async function getUserStakingData(userId: string) { // 构造获取委托信息的Cadence脚本 const delegationScript = ` import FlowIDStaking from 0xFlowIDStaking pub fun main(user: Address): FlowIDStaking.DelegationInfo? { return FlowIDStaking.getDelegationInfo(user) } `; // 构造获取可用奖励的Cadence脚本 const rewardsScript = ` import FlowIDStaking from 0xFlowIDStaking pub fun main(user: Address): UFix64 { return FlowIDStaking.getRewardsAvailable(user) } `; // 执行脚本获取数据 const delegationInfo = await fcl.query({ cadence: delegationScript, args: (arg, t) => [arg(userId, t.Address)], }); const availableRewards = await fcl.query({ cadence: rewardsScript, args: (arg, t) => [arg(userId, t.Address)], }); return { delegation: delegationInfo, availableRewards: availableRewards.toString(), }; } // 调用示例 getUserStakingData("0x1234567890abcdef") .then(data => console.log("用户质押数据:", data)) .catch(err => console.error("获取失败:", err));
Python 实现(直接调用Flow RPC)
无需依赖SDK,直接通过HTTP请求调用Flow的REST API执行Cadence脚本:
import requests import json def get_user_staking_data(user_id: str, is_mainnet: bool = True): # 配置网络参数 if is_mainnet: rpc_url = "https://rest-mainnet.onflow.org/v1/scripts" staking_contract = "0x8624b52f9ddcd04a" else: rpc_url = "https://rest-testnet.onflow.org/v1/scripts" staking_contract = "0x7e60df042a9c0868" # 获取委托信息的Cadence脚本 delegation_script = f''' import FlowIDStaking from {staking_contract} pub fun main(user: Address): FlowIDStaking.DelegationInfo? {{ return FlowIDStaking.getDelegationInfo(user) }} ''' # 获取可用奖励的Cadence脚本 rewards_script = f''' import FlowIDStaking from {staking_contract} pub fun main(user: Address): UFix64 {{ return FlowIDStaking.getRewardsAvailable(user) }} ''' def execute_script(script: str): payload = { "script": bytes(script, "utf-8").hex(), "arguments": [ { "type": "Address", "value": user_id } ] } headers = {"Content-Type": "application/json"} response = requests.post(rpc_url, data=json.dumps(payload), headers=headers) return response.json() # 执行两个脚本 delegation_result = execute_script(delegation_script) rewards_result = execute_script(rewards_script) return { "delegation": delegation_result.get("value"), "available_rewards": rewards_result.get("value") } # 调用示例 if __name__ == "__main__": data = get_user_staking_data("0x1234567890abcdef") print("用户质押数据:", data)
关键说明
user_id需要传入标准的Flow地址(格式如0x1234567890abcdef)- 委托信息包含委托金额、节点ID、委托状态等字段,具体结构可参考FlowIDStaking合约的
DelegationInfo结构体定义 getRewardsAvailable获取的是用户当前可领取的奖励总额,已领取的奖励可通过调用getRewardsClaimed方法获取
内容的提问来源于stack exchange,提问作者Naman Thaker
相关产品推荐
相关产品推荐

