deploy static method
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,
);
}