clawchain/viem/site/pages/docs/utilities/getContractAddress.md

126 lines
2.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
description: Retrieves a contract address.
---
# getContractAddress
Retrieves the contract address generated by the [`CREATE`](https://ethereum.stackexchange.com/a/68945) or [`CREATE2`](https://eips.ethereum.org/EIPS/eip-1014) opcode – invoked after deploying a contract to the network.
## Import
```ts
import { getContractAddress } from 'viem'
```
## Usage
```ts
import { getContractAddress } from 'viem'
getContractAddress({ // [!code focus:99]
from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b',
nonce: 69420n
})
// '0xDf2e056f7062790dF95A472f691670717Ae7b1B6'
```
## Returns
[`Address`](/docs/glossary/types#address)
The contract address.
## Parameters
### from (optional)
- **Type:** [`Address`](/docs/glossary/types#address)
The address the contract was deployed from.
```ts
getContractAddress({
from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b', // [!code focus:1]
nonce: 69420n
})
```
### nonce (optional)
- **Type:** [`Address`](/docs/glossary/types#address)
The nonce of the transaction which deployed the contract.
```ts
getContractAddress({
from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b',
nonce: 69420n // [!code focus:1]
})
```
### opcode (optional)
- **Type:** `"CREATE" | "CREATE2"`
- **Default:** `"CREATE"`
The opcode to invoke the contract deployment. Defaults to `"CREATE"`.
[Learn more about `CREATE2`](https://eips.ethereum.org/EIPS/eip-1014).
```ts
getContractAddress({
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b',
opcode: 'CREATE2', // [!code focus:1]
salt: toBytes('wagmi'),
})
```
### bytecode (optional)
- **Type:** `ByteArray` | [`Hex`](/docs/glossary/types#hex)
- **Only applicable for `opcode: 'CREATE2'` deployments**
The to-be-deployed contract’s bytecode
```ts
getContractAddress({
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...', // [!code focus:1]
from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b',
opcode: 'CREATE2',
salt: toBytes('wagmi'),
})
```
### bytecodeHash (optional)
- **Type:** `ByteArray` | [`Hex`](/docs/glossary/types#hex)
- **Only applicable for `opcode: 'CREATE2'` deployments**
A hash of the to-be-deployed contract’s bytecode
```ts
getContractAddress({
bytecodeHash: '0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54', // [!code focus:1]
from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b',
opcode: 'CREATE2',
salt: toBytes('wagmi'),
})
```
### salt (optional)
- **Type:** `ByteArray` | [`Hex`](/docs/glossary/types#hex)
- **Only applicable for `opcode: 'CREATE2'` deployments**
An arbitrary value provided by the sender.
```ts
getContractAddress({
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
from: '0xc961145a54C96E3aE9bAA048c4F4D6b04C13916b',
opcode: 'CREATE2',
salt: toBytes('wagmi'), // [!code focus:1]
})
```