UNPKG

@skynetxbt/venice-ai-plugin

Version:

Venice AI Plugin for Skynet Framework - Staking Contract Integration

104 lines (103 loc) 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VeniceToken = void 0; const ethers_1 = require("ethers"); const events_1 = require("events"); const constants_1 = require("./constants"); class VeniceToken extends events_1.EventEmitter { constructor(tokenAddress) { super(); this.contract = null; this.provider = null; this.signer = null; this.address = tokenAddress || constants_1.VENICE_TOKEN_ADDRESS; } /** * Initialize the token contract with provider and signer */ async initialize(provider, signer) { this.provider = provider; if (signer) { this.signer = signer; this.contract = new ethers_1.ethers.Contract(this.address, constants_1.VENICE_TOKEN_ABI, this.signer); } else { this.contract = new ethers_1.ethers.Contract(this.address, constants_1.VENICE_TOKEN_ABI, this.provider); } } /** * Cleanup resources */ async cleanup() { this.contract = null; this.provider = null; this.signer = null; } /** * Get token balance for an address */ async getBalance(address) { try { if (!this.contract) { throw new Error("Contract not initialized"); } const balance = await this.contract.balanceOf(address); return { success: true, data: balance }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } /** * Check allowance for spender */ async getAllowance(owner, spender) { try { if (!this.contract) { throw new Error("Contract not initialized"); } const allowance = await this.contract.allowance(owner, spender); return { success: true, data: allowance }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } /** * Approve spender to use tokens */ async approve(spender, amount) { try { if (!this.contract || !this.signer) { throw new Error("Contract not initialized with signer"); } const tx = await this.contract.approve(spender, amount, { gasLimit: 100000 }); const receipt = await tx.wait(); return { success: true, transactionHash: receipt.hash }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } } exports.VeniceToken = VeniceToken;