@lit-protocol/vincent-scaffold-sdk
Version:
Shared build configuration and utilities for Vincent abilities and policies
37 lines (36 loc) • 1.84 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupAccount = setupAccount;
const chalk_1 = __importDefault(require("chalk"));
const ethers_1 = require("ethers");
async function setupAccount(accountType, accountName, envPrivateKey, stateManager, funderWallet, provider, fundAmount = ethers_1.ethers.utils.parseEther("0.01")) {
// Get or generate account
const account = stateManager.getOrGenerateAccount(accountType, envPrivateKey);
console.log(`✅ ${accountName}:`, account.address, account.isNew ? chalk_1.default.yellow("(newly generated)") : "");
// Check balance
const balance = await provider.getBalance(account.address);
console.log(" ↳ Balance:", ethers_1.ethers.utils.formatEther(balance));
// Fund if new account with zero balance OR if balance is insufficient
const needsFunding = (account.isNew && balance.isZero()) || balance.lt(fundAmount);
if (needsFunding) {
const reason = account.isNew && balance.isZero() ? "new account" : "insufficient balance";
console.log(chalk_1.default.yellow(` ↳ Funding ${reason}...`));
const tx = await funderWallet.sendTransaction({
to: account.address,
value: fundAmount,
});
await tx.wait();
console.log(chalk_1.default.green(` ↳ Funded with ${ethers_1.ethers.utils.formatEther(fundAmount)} ETH`));
}
// Create ethers wallet for this account
const wallet = new ethers_1.ethers.Wallet(account.privateKey, provider);
return {
account,
address: account.address,
privateKey: account.privateKey,
wallet, // Return the ethers wallet for convenience
};
}