Deploying contracts
Deploying a contract with some EVM bytecode is straightforward with EIP-1193 JavaScript provider objects, although it does require making a few RPC requests:
export async function deployContract(
createBytecode: Raw,
provider: EthereumProvider,
): Promise<{
transactionHash: Raw;
contractAddress: Raw;
}> {
// just use the first unlocked account
const [account] = await provider.request({
method: "eth_accounts",
params: [],
});
// issue a transaction that will be mined immediately
const transactionHash = Raw.fromHex(
await provider.request({
method: "eth_sendTransaction",
params: [
{
from: account,
gas: "0x989680",
data: Raw.toHex(createBytecode),
},
],
}),
);
// read the receipt and extract the deployed contract address
const contractAddress = Raw.fromHex(
(
await provider.request({
method: "eth_getTransactionReceipt",
params: [Raw.toHex(transactionHash)],
})
).contractAddress,
);
return {
transactionHash,
contractAddress,
};
}