# Deploy a contract

Walkthrough of compiling a Solidity contract and deploying it to AEREDIUM. Covers Hardhat, Foundry, and a raw-bytecode path.

The example contract is a minimal counter:

```solidity
// src/Counter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract Counter {
    uint256 public count;

    event Incremented(uint256 newCount);

    function increment() external {
        unchecked { count += 1; }
        emit Incremented(count);
    }
}
```

## Hardhat

```bash
npx hardhat compile
```

`scripts/deploy.ts`:

```ts
import { ethers } from "hardhat";

async function main() {
  const Counter = await ethers.getContractFactory("Counter");
  const counter = await Counter.deploy();
  await counter.waitForDeployment();

  const addr = await counter.getAddress();
  console.log("Counter:", addr);

  // Send a tx to it
  const tx = await counter.increment();
  await tx.wait();
  console.log("count:", (await counter.count()).toString());
}

main().catch((err) => { console.error(err); process.exit(1); });
```

```bash
npx hardhat run scripts/deploy.ts --network aeredium
```

## Foundry

```bash
forge build

forge create src/Counter.sol:Counter \
  --rpc-url https://rpc.aeredium.io \
  --private-key $PRIVATE_KEY
```

Foundry prints the deployed address. Interact:

```bash
cast send <COUNTER_ADDR> "increment()" \
  --rpc-url https://rpc.aeredium.io \
  --private-key $PRIVATE_KEY

cast call <COUNTER_ADDR> "count()(uint256)" \
  --rpc-url https://rpc.aeredium.io
```

For multi-step deploys, use a `Script` (see [Foundry tooling page](/developer.aeredium.io/tooling/foundry.md)).

## viem

```ts
import { createWalletClient, createPublicClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { aeredium } from "./chain";

// ABI + bytecode from `forge build` (out/Counter.sol/Counter.json) or hardhat compile
import counterArtifact from "./Counter.json";

const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
const wallet = createWalletClient({ account, chain: aeredium, transport: http() });
const client = createPublicClient({ chain: aeredium, transport: http() });

const hash = await wallet.deployContract({
  abi: counterArtifact.abi,
  bytecode: counterArtifact.bytecode.object as `0x${string}`,
  args: [],
});

const { contractAddress } = await client.waitForTransactionReceipt({ hash });
console.log("Counter deployed at:", contractAddress);
```

## Raw bytecode (curl)

If you have a signed deployment transaction (`to` field empty, `data` = bytecode), broadcast it like any other transaction:

```bash
curl -X POST https://rpc.aeredium.io \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["0x02f9..."],"id":1}'
```

Then look up the receipt — the deployed address is in `contractAddress`:

```bash
curl -X POST https://rpc.aeredium.io \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0xTxHash"],"id":1}'
```

## After deploying

* **Verify deployment**: `eth_getCode(address, "latest")` should return non-empty bytecode.
* **Watch events**: subscribe to `logs` on the contract address ([WebSocket subscriptions](/developer.aeredium.io/json-rpc-api/websocket.md)).
* **Verify execution proof** *(optional)*: once the next attestation is generated, call `aer_getAttestationByBlock` on the deployment block to fetch a ZK-STARK proof of correct execution. See [AEREDIUM extensions](/developer.aeredium.io/json-rpc-api/aeredium.md).

## Gas tips

* Constructor gas usage is bundled with the deployment tx — set a generous `gasLimit` or rely on the client's estimator.
* Storage-heavy constructors (deploying with large initial state) can cost a lot; consider using a lazy-init pattern.
* AEREDIUM follows EIP-1559 mechanics; deployment txs work the same as any other tx of `type: 2`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aeredium.gitbook.io/developer.aeredium.io/examples/deploy-contract.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
