UNPKG

@skynetxbt/venice-ai-plugin

Version:

Venice AI Plugin for Skynet Framework - Staking Contract Integration

251 lines (250 loc) 8.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StakingContract = void 0; const ethers_1 = require("ethers"); const events_1 = require("events"); const constants_1 = require("./constants"); class StakingContract extends events_1.EventEmitter { constructor(config = {}) { super(); this.contract = null; this.provider = null; this.signer = null; this.address = config.contractAddress || constants_1.STAKING_CONTRACT_ADDRESS; } async initialize(provider, signer) { if (!provider) { if (!this.provider) { throw new Error("Provider is required for initialization"); } } else { this.provider = provider; } if (signer) { this.signer = signer; } else if (this.provider) { // If no signer provided but we have a provider, create a read-only contract this.contract = new ethers_1.ethers.Contract(this.address, constants_1.STAKING_CONTRACT_ABI, this.provider); return; } if (!this.signer) { throw new Error("Signer is required for write operations"); } this.contract = new ethers_1.ethers.Contract(this.address, constants_1.STAKING_CONTRACT_ABI, this.signer); } async cleanup() { this.contract = null; this.provider = null; this.signer = null; } async stake(recipient, amount) { try { if (!this.contract || !this.signer) { throw new Error("Contract not initialized with signer"); } console.log("Staking with params:", { recipient, amount: amount.toString(), contractAddress: this.address }); // Debug the exact data being sent const data = this.contract.interface.encodeFunctionData("stake", [recipient, amount]); console.log("Amount in BigInt:", amount); console.log("Encoded transaction data:", data); const tx = await this.contract.stake(recipient, amount, { gasLimit: 300000 // Reduced gas limit to a more reasonable value }); const receipt = await tx.wait(); return { success: true, transactionHash: receipt.hash }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } async initiateUnstake(amount) { try { if (!this.contract || !this.signer) { throw new Error("Contract not initialized with signer"); } const tx = await this.contract.initiateUnstake(amount); const receipt = await tx.wait(); return { success: true, transactionHash: receipt.hash }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } async finalizeUnstake() { try { if (!this.contract || !this.signer) { throw new Error("Contract not initialized with signer"); } const tx = await this.contract.finalizeUnstake(); const receipt = await tx.wait(); return { success: true, transactionHash: receipt.hash }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } async claim() { try { if (!this.contract || !this.signer) { throw new Error("Contract not initialized with signer"); } const tx = await this.contract.claim(); const receipt = await tx.wait(); return { success: true, transactionHash: receipt.hash }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } async getPendingRewards(userAddress) { try { if (!this.contract) { throw new Error("Contract not initialized"); } const pendingRewards = await this.contract.pendingRewards(userAddress); return { success: true, data: pendingRewards }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } async getVenicePercentage() { try { if (!this.contract) { throw new Error("Contract not initialized"); } const percentage = await this.contract.getVenicePercentage(); return { success: true, data: percentage }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } async getStakeInfo(userAddress) { try { if (!this.contract) { throw new Error("Contract not initialized"); } const stakeInfo = await this.contract.stakes(userAddress); return { success: true, data: { rewardDebt: stakeInfo[0], cooldownEnd: stakeInfo[1], cooldownAmount: stakeInfo[2] } }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } async getUserStakeInfo(userAddress) { try { if (!this.contract) { throw new Error("Contract not initialized"); } const [stakeInfoResult, pendingRewardsResult] = await Promise.all([ this.getStakeInfo(userAddress), this.getPendingRewards(userAddress) ]); if (!stakeInfoResult.success || !pendingRewardsResult.success) { throw new Error("Failed to fetch user stake info"); } // Get the user's staked balance const stakeAmount = await this.contract.balanceOf(userAddress); return { success: true, data: { stakeAmount, pendingRewards: pendingRewardsResult.data, stakeInfo: stakeInfoResult.data } }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } async getStakingStats() { try { if (!this.contract) { throw new Error("Contract not initialized"); } const [totalSupply, emissionRate, cooldownDuration, venicePercentage, accRewardPerShare, lastRewardTimestamp] = await Promise.all([ this.contract.totalSupply(), this.contract.emissionRatePerSecond(), this.contract.cooldownDuration(), this.contract.getVenicePercentage(), this.contract.accRewardPerShare(), this.contract.lastRewardTimestamp() ]); return { success: true, data: { totalStaked: totalSupply, emissionRate, cooldownDuration, venicePercentage, accRewardPerShare, lastRewardTimestamp } }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } } exports.StakingContract = StakingContract;