UNPKG

mind-hubs-sdk

Version:

Typescript SDK to interact with MIND Hub Framework

63 lines (62 loc) 2.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getVotingReward = getVotingReward; const ethers_1 = require("ethers"); const context_1 = require("../context"); const MemberPool_json_1 = __importDefault(require("../abi/MemberPool.json")); const logger_1 = __importDefault(require("../logger")); /** * A singleton instance of the MemberPool contract. * It will be initialized lazily upon the first access. */ let memberPoolContract = null; /** * Ensures that the MemberPool contract is initialized * This must be called after the CoreContextManager has been initialized * * @returns The initialized MemberPool contract instance */ function ensureMemberPoolContract() { if (!memberPoolContract) { const config = context_1.CoreContextManager.getConfiguration(); const context = context_1.CoreContextManager.getContext(); memberPoolContract = new ethers_1.Contract(config.memberPoolAddress, MemberPool_json_1.default, context.jsonRpcProvider); logger_1.default.info({ msg: 'MemberPool contract initialized', address: config.memberPoolAddress }); } return memberPoolContract; } /** * Fetches the voting reward for a given cold wallet address * * @param coldWalletAddress - The address of the cold wallet * @param hubId - The ID of the hub * @returns A promise resolving to the reward earned as a bigint * @throws {Error} If there is an issue fetching the voting reward */ async function getVotingReward(coldWalletAddress, hubId) { try { const contract = ensureMemberPoolContract(); const reward = await contract.voterRewardEarned(coldWalletAddress, hubId); logger_1.default.info({ msg: 'Fetched voting reward successfully', coldWalletAddress, hubId, reward }); return reward; } catch (error) { logger_1.default.error({ msg: 'Error fetching voting reward', coldWalletAddress, error }); throw new Error(`Failed to fetch voting reward for address ${coldWalletAddress}`); } }