UNPKG

industry-tools

Version:

Industry Tools is a TypeScript library providing essential tools for the Industry AI Agent Platform.

42 lines (41 loc) 1.72 kB
import { ethers } from "ethers"; import { ERC20_CONTRACT_ABI, ERC20_CONTRACT_BYTECODE } from "../../utils/contracts/erc20"; export async function deployEVMERC20Contract(input) { const { userId, characterId, tokenName, tokenSymbol, totalSupply, network, storage } = input; try { const [wallet, rpc] = await Promise.all([ storage.getItem(`USER#${userId}`, `WALLET#${characterId}`), storage.getItem("RPC", `CHAIN#${network.toUpperCase()}`) ]); if (!wallet?.privateKey) { return { error: 'WalletNotFound', message: 'No wallet found for this character' }; } if (!rpc?.httpEndpoint) { return { error: 'RPCNotFound', message: 'No RPC found for this network' }; } const provider = new ethers.JsonRpcProvider(rpc?.httpEndpoint); const signer = new ethers.Wallet(wallet.privateKey, provider); const factory = new ethers.ContractFactory(ERC20_CONTRACT_ABI, ERC20_CONTRACT_BYTECODE, signer); const contract = await factory.deploy(tokenName, tokenSymbol, totalSupply); await contract.waitForDeployment(); const deployedAddress = await contract.getAddress(); return { contractAddress: deployedAddress, network: network, message: "Contract deployed successfully" }; } catch (error) { console.log(error); return { error: error instanceof Error ? error.name : 'DeploymentError', message: `Failed to deploy contract: ${error instanceof Error ? error.message : String(error)}` }; } }