UNPKG

kamiweb3-sdk

Version:

TypeScript SDK for KAMI721-C, KAMI721-AC, and KAMI1155-C smart contracts

153 lines 8.41 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ERC721ACFactory = void 0; const ethers_1 = require("ethers"); const ERC721ACWrapper_1 = require("../contracts/ERC721ACWrapper"); const KAMI721AC_json_1 = __importDefault(require("../abis/KAMI721AC.json")); const KAMI721ACUpgradeable_json_1 = __importDefault(require("../abis/KAMI721ACUpgradeable.json")); // Import Upgradeable Artifact // Import OpenZeppelin Artifacts const ProxyAdmin_json_1 = __importDefault(require("../abis/openzeppelin/ProxyAdmin.json")); const TransparentUpgradeableProxy_json_1 = __importDefault(require("../abis/openzeppelin/TransparentUpgradeableProxy.json")); class ERC721ACFactory { /** * Attaches to an existing standard KAMI721AC contract. */ static attach(address, signerOrProvider) { // Use default ABI (standard) in wrapper constructor return new ERC721ACWrapper_1.ERC721ACWrapper(address, signerOrProvider); } /** * Attaches to an existing upgradeable ERC721AC contract (proxy). * Uses the KAMI721ACUpgradeable ABI. */ static attachUpgradeable(proxyAddress, signerOrProvider) { const upgradeableAbi = KAMI721ACUpgradeable_json_1.default.abi; if (!upgradeableAbi) throw new Error('KAMI721ACUpgradeable ABI not found'); // Pass the upgradeable ABI explicitly return new ERC721ACWrapper_1.ERC721ACWrapper(proxyAddress, signerOrProvider, upgradeableAbi); } /** * Deploys a new standard KAMI721AC contract. */ static async deploy(args, signer) { if (!signer) { throw new Error('Signer is required for deployment.'); } const abi = KAMI721AC_json_1.default.abi; const bytecode = KAMI721AC_json_1.default.bytecode; if (!abi || abi.length === 0) { throw new Error('ABI not found in KAMI721AC artifact.'); } if (!bytecode || bytecode === '0x' || bytecode === '') { throw new Error('Bytecode not found or is invalid in KAMI721AC artifact.'); } const factory = new ethers_1.ContractFactory(abi, bytecode, signer); console.log('Deploying KAMI721AC (standard) with arguments:', { usdcAddress: args.usdcAddress, name: args.name, symbol: args.symbol, baseURI: args.baseURI, initialMintPrice: args.initialMintPrice.toString(), platformAddress: args.platformAddress, platformCommissionPercentage: args.platformCommissionPercentage.toString(), }); try { const contract = await factory.deploy(args.usdcAddress, args.name, args.symbol, args.baseURI, args.initialMintPrice, args.platformAddress, args.platformCommissionPercentage); await contract.waitForDeployment(); const deployedAddress = await contract.getAddress(); console.log(`KAMI721AC (standard) deployed to: ${deployedAddress}`); // Use the standard attach method return ERC721ACFactory.attach(deployedAddress, signer); } catch (error) { console.error('KAMI721AC (standard) deployment failed:', error); if (error instanceof Error) { throw new Error(`KAMI721AC (standard) deployment failed: ${error.message}`); } throw new Error('KAMI721AC (standard) deployment failed with an unknown error.'); } } /** * Deploys a new upgradeable ERC721AC contract using the Transparent Proxy pattern. * @param initArgs The arguments for the initializer function. * @param signer The signer to use for deployment. * @param proxyAdminOwner (Optional) The address that will own the ProxyAdmin. Defaults to the signer. * @returns A Promise resolving to an ERC721ACWrapper instance attached to the proxy address. */ static async deployUpgradeable(initArgs, signer, proxyAdminOwner) { if (!signer) { throw new Error('Signer is required for deployment.'); } const signerAddress = await signer.getAddress(); const adminOwner = proxyAdminOwner ? proxyAdminOwner.toString() : signerAddress; // 1. Deploy Implementation const implFactory = new ethers_1.ContractFactory(KAMI721ACUpgradeable_json_1.default.abi, KAMI721ACUpgradeable_json_1.default.bytecode, signer); console.log('Deploying KAMI721ACUpgradeable implementation...'); const implementation = await implFactory.deploy(); await implementation.waitForDeployment(); const implementationAddress = await implementation.getAddress(); console.log(`Implementation deployed to: ${implementationAddress}`); // 2. Deploy ProxyAdmin const proxyAdminFactory = new ethers_1.ContractFactory(ProxyAdmin_json_1.default.abi, ProxyAdmin_json_1.default.bytecode, signer); console.log(`Deploying ProxyAdmin (owner: ${adminOwner})...`); const proxyAdminContract = await proxyAdminFactory.deploy(); await proxyAdminContract.waitForDeployment(); const proxyAdminAddress = await proxyAdminContract.getAddress(); const proxyAdmin = new ethers_1.Contract(proxyAdminAddress, ProxyAdmin_json_1.default.abi, signer); if (adminOwner.toLowerCase() !== signerAddress.toLowerCase()) { console.log(`Transferring ProxyAdmin ownership to ${adminOwner}...`); const tx = await proxyAdmin.transferOwnership(adminOwner); await tx.wait(); console.log('ProxyAdmin ownership transferred.'); } console.log(`ProxyAdmin deployed to: ${proxyAdminAddress}`); // 3. Encode Initializer Data const implementationInterface = new ethers_1.Interface(KAMI721ACUpgradeable_json_1.default.abi); // Use the same 7 args as KAMI721C's initializer const initializeData = implementationInterface.encodeFunctionData('initialize', [ initArgs.usdcAddress, initArgs.name, initArgs.symbol, initArgs.baseURI, initArgs.initialMintPrice, initArgs.platformAddress, initArgs.platformCommissionPercentage, ]); console.log('Encoded initialize data:', initializeData); // 4. Deploy TransparentUpgradeableProxy const proxyFactory = new ethers_1.ContractFactory(TransparentUpgradeableProxy_json_1.default.abi, TransparentUpgradeableProxy_json_1.default.bytecode, signer); console.log('Deploying TransparentUpgradeableProxy...'); const proxy = await proxyFactory.deploy(implementationAddress, proxyAdminAddress, initializeData); await proxy.waitForDeployment(); const proxyAddress = await proxy.getAddress(); console.log(`TransparentUpgradeableProxy deployed to: ${proxyAddress}`); // 5. Return Wrapper attached to Proxy using Implementation ABI console.log('Attaching wrapper to proxy...'); return ERC721ACFactory.attachUpgradeable(proxyAddress, signer); } /** * Initiates an upgrade of a KAMI721AC transparent proxy. * Deploys the new KAMI721ACUpgradeable implementation. * @param signer The signer for deployment. * @returns The address of the newly deployed implementation contract. */ static async deployNewImplementation(signer) { if (!signer) { throw new Error('Signer is required for deployment.'); } const implFactory = new ethers_1.ContractFactory(KAMI721ACUpgradeable_json_1.default.abi, KAMI721ACUpgradeable_json_1.default.bytecode, signer); console.log('Deploying new KAMI721ACUpgradeable implementation...'); const newImplementation = await implFactory.deploy(); await newImplementation.waitForDeployment(); const newImplementationAddress = await newImplementation.getAddress(); console.log(`New implementation deployed to: ${newImplementationAddress}`); console.warn(`IMPORTANT: New implementation deployed. ProxyAdmin owner must call 'upgrade(proxyAddress, newImplementationAddress)' on ProxyAdmin.`); return newImplementationAddress; } } exports.ERC721ACFactory = ERC721ACFactory; //# sourceMappingURL=ERC721ACFactory.js.map