跳至主要内容

部署合约

使用 deployContract() 将新的智能合约部署到任何支持的链上。Engine 支持基于模板的部署(ERC-20、ERC-721、ERC-1155、marketplace、staking、governance)和自定义字节码部署。

方法签名

client.deployContract(params: ContractDeployParams): Promise<ApiResponse<ContractDeployResult>>

ContractDeployParams

import type { ContractDeployParams, ContractType } from '@one_deploy/sdk';

interface ContractDeployParams {
type: ContractType; // 模板类型或 'custom'
chainId: number; // 目标链
name: string; // 人类可读的合约名称
constructorArgs?: Record<string, unknown>; // 命名的构造函数参数
bytecode?: string; // type 为 'custom' 时必填
abi?: Record<string, unknown>[]; // type 为 'custom' 时必填
metadata?: Record<string, unknown>; // Engine 存储的可选元数据
}

ContractType

ContractType 枚举决定 Engine 使用哪个模板:

type ContractType =
| 'erc20' // 同质化代币 (ERC-20)
| 'erc721' // 非同质化代币 (ERC-721)
| 'erc1155' // 多代币标准 (ERC-1155)
| 'custom' // 自定义字节码 -- 自行提供
| 'marketplace' // NFT 市场合约
| 'staking' // 代币质押 / 奖励分发
| 'governance'; // 链上治理(投票 + 提案)

ContractDeployResult

interface ContractDeployResult {
transactionHash: string;
contractAddress: string; // 预测地址(可能等待确认中)
chainId: number;
status: 'pending' | 'submitted';
}
备注

contractAddress 是确定性计算的,会立即返回。在部署交易确认之前,合约不可使用。请轮询 getTransactionStatus() 来确认。

示例

部署 ERC-20 代币

import { useOneEngine } from '@one_deploy/sdk';

function DeployToken() {
const { client } = useOneEngine();

async function handleDeploy() {
const res = await client.deployContract({
type: 'erc20',
chainId: 137, // Polygon
name: 'Acme Token',
constructorArgs: {
name: 'Acme Token', // 代币名称(链上)
symbol: 'ACME', // 代币符号
initialSupply: '1000000', // 1,000,000 个代币(整数单位)
decimals: 18,
owner: '0xYourWallet...', // 初始所有者 / 铸造者
},
});

if (!res.success) {
console.error('Deploy failed:', res.error?.message);
return;
}

console.log('Deploy tx:', res.data.transactionHash);
console.log('Predicted address:', res.data.contractAddress);

// 等待确认
let confirmed = false;
while (!confirmed) {
const tx = await client.getTransactionStatus(
res.data.transactionHash,
137,
);
if (tx.data.status === 'confirmed') {
confirmed = true;
console.log('Contract live at:', res.data.contractAddress);
} else if (tx.data.status === 'failed') {
throw new Error(`Deploy failed: ${tx.data.reason}`);
}
await new Promise((r) => setTimeout(r, 3000));
}
}

return <button onClick={handleDeploy}>Deploy ACME Token</button>;
}

部署 ERC-721 NFT 集合

const res = await client.deployContract({
type: 'erc721',
chainId: 1, // Ethereum 主网
name: 'CryptoCreatures',
constructorArgs: {
name: 'CryptoCreatures', // 集合名称
symbol: 'CRTR', // 集合符号
baseURI: 'ipfs://QmXyz.../metadata/', // 代币元数据的基础 URI
maxSupply: 10000, // 最大可铸造数量
owner: '0xYourWallet...', // 合约所有者
royaltyRecipient: '0xYourWallet...', // EIP-2981 版税接收者
royaltyBps: 500, // 5% 版税(基点)
},
metadata: {
description: 'A 10k generative NFT collection.',
website: 'https://cryptocreatures.xyz',
},
});

if (res.success) {
console.log('NFT collection deploying:', res.data.contractAddress);
}

部署 ERC-1155 多代币

const res = await client.deployContract({
type: 'erc1155',
chainId: 42161, // Arbitrum
name: 'GameItems',
constructorArgs: {
name: 'GameItems',
uri: 'https://api.mygame.com/items/{id}.json', // ERC-1155 URI 模板
owner: '0xYourWallet...',
},
});

部署自定义合约

提供你自己编译的字节码和 ABI:

import MyContractABI from './abis/MyContract.json';
import { BYTECODE } from './bytecodes/MyContract';

const res = await client.deployContract({
type: 'custom',
chainId: 137,
name: 'MyCustomVault',
bytecode: BYTECODE, // '0x6080604052...'
abi: MyContractABI,
constructorArgs: {
_token: '0xA0b8...3E7a', // address
_lockDuration: 2592000, // uint256(30 天,以秒计)
_feeRecipient: '0xFeeWallet...', // address
},
});

if (res.success) {
console.log('Custom contract deploying:', res.data.contractAddress);
}

构造函数参数参考

每种模板类型接受不同的构造函数参数。下表列出了必填和可选字段。

ERC-20

参数类型必填描述
namestring代币名称
symbolstring代币符号
initialSupplystring初始供应量(整数单位)
decimalsnumber代币小数位(默认 18)
ownerstring初始所有者地址

ERC-721

参数类型必填描述
namestring集合名称
symbolstring集合符号
baseURIstring基础元数据 URI
maxSupplynumber最大可铸造供应量(0 = 无限制)
ownerstring合约所有者
royaltyRecipientstringEIP-2981 版税地址
royaltyBpsnumber版税基点(0-10000)

ERC-1155

参数类型必填描述
namestring集合名称
uristring带有 {id} 占位符的 URI 模板
ownerstring合约所有者

Node.js / Edge 运行时

import { OneEngineClient } from '@one_deploy/sdk';

const client = new OneEngineClient({
apiKey: process.env.ONE_API_KEY!,
projectId: process.env.ONE_PROJECT_ID!,
});

const res = await client.deployContract({
type: 'erc20',
chainId: 137,
name: 'ServerToken',
constructorArgs: {
name: 'ServerToken',
symbol: 'SRVR',
initialSupply: '500000',
owner: '0xDeployerWallet...',
},
});

console.log('Deployed:', res.data.contractAddress);

错误处理

const res = await client.deployContract({ /* ... */ });

if (!res.success) {
switch (res.error?.code) {
case 'INSUFFICIENT_FUNDS':
console.error('Deployer wallet does not have enough native token for gas.');
break;
case 'INVALID_CONSTRUCTOR_ARGS':
console.error('Constructor arguments do not match the expected types.');
break;
case 'UNSUPPORTED_CHAIN':
console.error('The specified chainId is not supported for deployment.');
break;
case 'BYTECODE_REQUIRED':
console.error('Custom contract type requires the bytecode field.');
break;
default:
console.error('Deploy error:', res.error?.message);
}
}

下一步