UNPKG

kamiweb3-sdk

Version:

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

150 lines 8.31 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ERC1155CFactory = void 0; const ethers_1 = require("ethers"); const ERC1155CWrapper_1 = require("../contracts/ERC1155CWrapper"); const KAMI1155C_json_1 = __importDefault(require("../abis/KAMI1155C.json")); const KAMI1155CUpgradeable_json_1 = __importDefault(require("../abis/KAMI1155CUpgradeable.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 ERC1155CFactory { /** * Attaches to an existing standard KAMI1155C contract. */ static attach(address, signerOrProvider) { // Use default ABI (standard) in wrapper constructor return new ERC1155CWrapper_1.ERC1155CWrapper(address, signerOrProvider); } /** * Attaches to an existing upgradeable ERC1155C contract (proxy). * Uses the KAMI1155CUpgradeable ABI. */ static attachUpgradeable(proxyAddress, signerOrProvider) { const upgradeableAbi = KAMI1155CUpgradeable_json_1.default.abi; if (!upgradeableAbi) throw new Error('KAMI1155CUpgradeable ABI not found'); // Pass the upgradeable ABI explicitly return new ERC1155CWrapper_1.ERC1155CWrapper(proxyAddress, signerOrProvider, upgradeableAbi); } /** * Deploys a new standard KAMI1155C contract. */ static async deploy(args, signer) { if (!signer) { throw new Error('Signer is required for deployment.'); } const abi = KAMI1155C_json_1.default.abi; const bytecode = KAMI1155C_json_1.default.bytecode; if (!abi || abi.length === 0) { throw new Error('ABI not found in KAMI1155C artifact.'); } if (!bytecode || bytecode === '0x' || bytecode === '') { throw new Error('Bytecode not found or is invalid in KAMI1155C artifact.'); } const factory = new ethers_1.ContractFactory(abi, bytecode, signer); console.log('Deploying KAMI1155C (standard) with arguments:', { usdcAddress: args.usdcAddress, baseURI: args.baseURI, name: args.name, symbol: args.symbol, initialMintPrice: args.initialMintPrice.toString(), platformAddress: args.platformAddress, platformCommissionPercentage: args.platformCommissionPercentage.toString(), }); try { const contract = await factory.deploy(args.usdcAddress, args.baseURI, args.name, args.symbol, args.initialMintPrice, args.platformAddress, args.platformCommissionPercentage); await contract.waitForDeployment(); const deployedAddress = await contract.getAddress(); console.log(`KAMI1155C (standard) deployed to: ${deployedAddress}`); return ERC1155CFactory.attach(deployedAddress, signer); } catch (error) { console.error('KAMI1155C (standard) deployment failed:', error); if (error instanceof Error) { throw new Error(`KAMI1155C (standard) deployment failed: ${error.message}`); } throw new Error('KAMI1155C (standard) deployment failed with an unknown error.'); } } /** * Deploys a new upgradeable ERC1155C 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 ERC1155CWrapper 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(KAMI1155CUpgradeable_json_1.default.abi, KAMI1155CUpgradeable_json_1.default.bytecode, signer); console.log('Deploying KAMI1155CUpgradeable 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(KAMI1155CUpgradeable_json_1.default.abi); // Use the 5 arguments for KAMI1155C's initializer const initializeData = implementationInterface.encodeFunctionData('initialize', [ initArgs.usdcAddress, 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 ERC1155CFactory.attachUpgradeable(proxyAddress, signer); } /** * Initiates an upgrade of a KAMI1155C transparent proxy. * Deploys the new KAMI1155CUpgradeable 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(KAMI1155CUpgradeable_json_1.default.abi, KAMI1155CUpgradeable_json_1.default.bytecode, signer); console.log('Deploying new KAMI1155CUpgradeable 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.ERC1155CFactory = ERC1155CFactory; //# sourceMappingURL=ERC1155CFactory.js.map