UNPKG

@patchworkdev/pdk

Version:

Patchwork Development Kit

196 lines (195 loc) 9.02 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FeeService = void 0; const prompts_1 = require("@inquirer/prompts"); const types_1 = require("@patchworkdev/common/types"); const path_1 = __importDefault(require("path")); const viem_1 = require("viem"); const accounts_1 = require("viem/accounts"); const config_1 = require("../../../common/helpers/config"); const PatchworkProtocol_abi_1 = require("./abis/PatchworkProtocol.abi"); const helpers_1 = require("./helpers"); class FeeService { publicClient; walletClient; patchworkAddress; account; configPath; project; deployConfig; constructor(configPath, deployConfig) { this.configPath = configPath; this.deployConfig = deployConfig; this.account = (0, accounts_1.privateKeyToAccount)(deployConfig.privateKey); this.publicClient = (0, viem_1.createPublicClient)({ transport: (0, viem_1.http)(deployConfig.rpcUrl), }); this.walletClient = (0, viem_1.createWalletClient)({ account: this.account, chain: (0, helpers_1.getChainForNetwork)(deployConfig.network), transport: (0, viem_1.http)(deployConfig.rpcUrl), }); this.patchworkAddress = deployConfig.patchworkProtocol; this.project = { name: 'temp', scopes: [], contracts: {}, plugins: [], }; } async loadConfig() { const fullConfigPath = path_1.default.isAbsolute(this.configPath) ? this.configPath : path_1.default.resolve(process.cwd(), this.configPath); this.project = await (0, config_1.importPatchworkConfig)(fullConfigPath); } async checkMintConfig(contractAddress) { try { const result = await this.publicClient.readContract({ address: this.patchworkAddress, abi: PatchworkProtocol_abi_1.PatchworkProtocol, functionName: 'getMintConfiguration', args: [contractAddress], }); return result; } catch (error) { console.error('Error checking mint configuration:', error); return null; } } async checkAssignFee(contractAddress) { try { const result = await this.publicClient.readContract({ address: this.patchworkAddress, abi: PatchworkProtocol_abi_1.PatchworkProtocol, functionName: 'getAssignFee', args: [contractAddress], }); return result; } catch (error) { console.error('Error checking assign fee:', error); return null; } } async checkPatchFee(contractAddress) { try { const result = await this.publicClient.readContract({ address: this.patchworkAddress, abi: PatchworkProtocol_abi_1.PatchworkProtocol, functionName: 'getPatchFee', args: [contractAddress], }); return result; } catch (error) { console.error('Error checking patch fee:', error); return null; } } hasFeature(features, ...requiredFeatures) { return requiredFeatures.some((feature) => features.includes(feature)); } async configureFeesForDeployment(deployedContracts) { await this.loadConfig(); for (const [contractName, deployment] of Object.entries(deployedContracts)) { const contractConfig = this.project.contracts[contractName]; // Skip if it's a string (reference to another contract) or doesn't have fees configured if (typeof contractConfig === 'string' || !contractConfig?.fees) { continue; } await this.configureFees(deployment.deployedAddress, contractConfig.fees, contractConfig.features); } } async configureFees(contractAddress, fees, features) { const mintConfig = await this.checkMintConfig(contractAddress); const assignFee = await this.checkAssignFee(contractAddress); const patchFee = await this.checkPatchFee(contractAddress); const isLocal = this.deployConfig.network === 'local'; if (!isLocal) { const shouldConfigure = await (0, prompts_1.confirm)({ message: 'Fee configuration not found. Would you like to configure fees now?', default: true, }); if (!shouldConfigure) { console.info('Skipping fee configuration...'); return; } } // Configure mint fee only if MINTABLE feature is present if (fees.mintFee !== undefined && this.hasFeature(features, types_1.Feature.MINTABLE)) { let shouldSetMintConfig = isLocal; let shouldActivate = isLocal; if (!isLocal && (!mintConfig || mintConfig.flatFee === 0n)) { shouldSetMintConfig = await (0, prompts_1.confirm)({ message: 'Would you like to configure the mint fee?', default: true, }); } if (!isLocal && mintConfig && !mintConfig.active) { shouldActivate = await (0, prompts_1.confirm)({ message: 'Minting is currently not active for this contract. Would you like to enable minting?', default: true, }); } if (shouldSetMintConfig || shouldActivate) { const feeInWei = BigInt(Math.floor(fees.mintFee * 1e18)); const { request } = await this.publicClient.simulateContract({ account: this.account, address: this.patchworkAddress, abi: PatchworkProtocol_abi_1.PatchworkProtocol, functionName: 'setMintConfiguration', args: [ contractAddress, { flatFee: shouldSetMintConfig ? feeInWei : mintConfig?.flatFee || 0n, active: true, }, ], }); const hash = await this.walletClient.writeContract(request); console.info(`Set mint config for ${contractAddress} to ${fees.mintFee} ETH, minting active: true (tx: ${hash})`); } } else if (fees.mintFee !== undefined) { console.warn(`Skipping mint fee configuration for ${contractAddress} - MINTABLE feature not present`); } // Configure assign fee only if FRAGMENTMULTI or FRAGMENTSINGLE features are present if (fees.assignFee !== undefined && this.hasFeature(features, types_1.Feature.FRAGMENTMULTI, types_1.Feature.FRAGMENTSINGLE) && (!assignFee || assignFee === 0n)) { const feeInWei = BigInt(Math.floor(fees.assignFee * 1e18)); const { request } = await this.publicClient.simulateContract({ account: this.account, address: this.patchworkAddress, abi: PatchworkProtocol_abi_1.PatchworkProtocol, functionName: 'setAssignFee', args: [contractAddress, feeInWei], }); const hash = await this.walletClient.writeContract(request); console.info(`Set assign fee for ${contractAddress} to ${fees.assignFee} ETH (tx: ${hash})`); } else if (fees.assignFee !== undefined) { console.warn(`Skipping assign fee configuration for ${contractAddress} - FRAGMENTMULTI or FRAGMENTSINGLE features not present`); } // Configure patch fee only if PATCH, 1155PATCH, or ACCOUNTPATCH features are present if (fees.patchFee !== undefined && this.hasFeature(features, types_1.Feature.PATCH, types_1.Feature['1155PATCH'], types_1.Feature.ACCOUNTPATCH) && (!patchFee || patchFee === 0n)) { const feeInWei = BigInt(Math.floor(fees.patchFee * 1e18)); const { request } = await this.publicClient.simulateContract({ account: this.account, address: this.patchworkAddress, abi: PatchworkProtocol_abi_1.PatchworkProtocol, functionName: 'setPatchFee', args: [contractAddress, feeInWei], }); const hash = await this.walletClient.writeContract(request); console.info(`Set patch fee for ${contractAddress} to ${fees.patchFee} ETH (tx: ${hash})`); } else if (fees.patchFee !== undefined) { console.warn(`Skipping patch fee configuration for ${contractAddress} - PATCH, 1155PATCH, or ACCOUNTPATCH features not present`); } } } exports.FeeService = FeeService;