deploy static method

Future<DeployResult> deploy({
  1. required String bytecode,
  2. required WalletClient walletClient,
  3. String? abi,
  4. List constructorArgs = const [],
  5. BigInt? value,
  6. BigInt? gasLimit,
})

Deploys a new contract.

Implementation

static Future<DeployResult> deploy({
  required String bytecode,
  required WalletClient walletClient,
  String? abi,
  List<dynamic> constructorArgs = const [],
  BigInt? value,
  BigInt? gasLimit,
}) async {
  // For now, we'll keep deployment simple without constructor args
  if (constructorArgs.isNotEmpty) {
    throw UnimplementedError('Constructor arguments not yet supported');
  }

  final deployData = HexUtils.decode(bytecode);

  // Create deployment transaction
  final request = TransactionRequest(
    data: deployData,
    value: value,
    gasLimit: gasLimit,
  );

  // Send deployment transaction
  final txHash = await walletClient.sendTransactionRequest(request);

  // Wait for transaction receipt to get contract address
  TransactionReceipt? receipt;
  var attempts = 0;
  const maxAttempts = 60; // Wait up to 60 seconds

  while (receipt == null && attempts < maxAttempts) {
    await Future<void>.delayed(const Duration(seconds: 1));
    receipt = await walletClient.getTransactionReceipt(txHash);
    attempts++;
  }

  if (receipt == null) {
    throw Exception('Deployment transaction not mined within timeout');
  }

  if (!receipt.success) {
    throw Exception('Contract deployment failed');
  }

  if (receipt.contractAddress == null) {
    throw Exception('No contract address in deployment receipt');
  }

  // Create contract instance
  final contract = Contract(
    address: receipt.contractAddress!,
    abi: abi ?? '[]',
    publicClient: walletClient,
    walletClient: walletClient,
  );

  return DeployResult(
    contract: contract,
    transactionHash: txHash,
    receipt: receipt,
  );
}