Skip to content

Feat: Add deployContract and useDeployContract #3816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gentle-dryers-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wagmi": minor
---

Added `useDeployContract` hook.
5 changes: 5 additions & 0 deletions .changeset/pretty-dogs-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/core": minor
---

Added `deployContract` action.
5 changes: 5 additions & 0 deletions docs/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ export function getSidebar() {
text: 'useConnectors',
link: '/react/api/hooks/useConnectors',
},
{
text: 'useDeployContract',
link: '/react/api/hooks/useDeployContract',
},
{ text: 'useDisconnect', link: '/react/api/hooks/useDisconnect' },
{ text: 'useEnsAddress', link: '/react/api/hooks/useEnsAddress' },
{ text: 'useEnsAvatar', link: '/react/api/hooks/useEnsAvatar' },
Expand Down Expand Up @@ -490,6 +494,7 @@ export function getSidebar() {
link: '/core/api/actions/call',
},
{ text: 'connect', link: '/core/api/actions/connect' },
{ text: 'deployContract', link: '/core/api/actions/deployContract' },
{ text: 'disconnect', link: '/core/api/actions/disconnect' },
{
text: 'estimateFeesPerGas',
Expand Down
264 changes: 264 additions & 0 deletions docs/core/api/actions/deployContract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
<script setup>
const packageName = '@wagmi/core'
const actionName = 'deployContract'
const typeName = 'DeployContract'
</script>

# deployContract <Badge text="viem@>=2.8.18" />

Action for deploying a contract to the network, given bytecode & constructor arguments.

## Import

```ts
import { deployContract } from '@wagmi/core'
```

## Usage

::: code-group
```ts [index.ts]
import { deployContract } from '@wagmi/core'
import { wagmiAbi } from './abi'
import { config } from './config'

const result = await deployContract(config, {
abi: wagmiAbi,
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
```
```ts [abi.ts]
export const wagmiAbi = [
...
{
inputs: [],
stateMutability: "nonpayable",
type: "constructor",
},
...
] as const
```
<<< @/snippets/core/config.ts[config.ts]
:::

## Deploying with Constructor Args

::: code-group
```ts [index.ts]
import { deployContract } from '@wagmi/core'
import { wagmiAbi } from './abi'
import { config } from './config'

const result = await deployContract(config, {
abi: wagmiAbi,
args: [69420],
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
```
```ts [abi.ts]
export const wagmiAbi = [
...
{
inputs: [{ name: "x", type: "uint32" }],
stateMutability: "nonpayable",
type: "constructor",
},
...
] as const;
```
<<< @/snippets/core/config.ts[config.ts]
:::


## Parameters

```ts
import { type DeployContractParameters } from '@wagmi/core'
```

### abi

`Abi`

The contract's ABI.

::: code-group
```ts [index.ts]
import { deployContract } from '@wagmi/core'
import { wagmiAbi } from './abi'
import { config } from './config'

const result = await deployContract(config, {
abi: wagmiAbi, // [!code focus]
args: [69420],
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
```
```ts [abi.ts]
export const wagmiAbi = [
...
{
inputs: [{ name: "x", type: "uint32" }],
stateMutability: "nonpayable",
type: "constructor",
},
...
] as const;
```
<<< @/snippets/core/config.ts[config.ts]
:::

### account

`Address | Account | undefined`

Account to use when deploying a contract. Throws if account is not found on [`connector`](#connector).

::: code-group
```ts [index.ts]
import { deployContract } from '@wagmi/core'
import { wagmiAbi } from './abi'
import { config } from './config'

const result = await deployContract(config, {
abi: wagmiAbi,
account: '0xd2135CfB216b74109775236E36d4b433F1DF507B', // [!code focus]
args: [69420],
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
```
```ts [abi.ts]
export const wagmiAbi = [
...
{
inputs: [{ name: "x", type: "uint32" }],
stateMutability: "nonpayable",
type: "constructor",
},
...
] as const;
```
<<< @/snippets/core/config.ts[config.ts]
:::

### args

`readonly unknown[] | undefined`

- Arguments to pass when deploying the contract.
- Inferred from [`abi`](#abi).

::: code-group
```ts [index.ts]
import { deployContract } from '@wagmi/core'
import { wagmiAbi } from './abi'
import { config } from './config'

const result = await deployContract(config, {
abi: wagmiAbi,
args: [69420], // [!code focus]
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
})
```
```ts [abi.ts]
export const wagmiAbi = [
...
{
inputs: [{ name: "x", type: "uint32" }],
stateMutability: "nonpayable",
type: "constructor",
},
...
] as const;
```
<<< @/snippets/core/config.ts[config.ts]
:::

### bytecode

`Hex`

The contract's bytecode.

::: code-group
```ts [index.ts]
import { deployContract } from '@wagmi/core'
import { wagmiAbi } from './abi'
import { config } from './config'

const result = await deployContract(config, {
abi: wagmiAbi,
args: [69420],
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...', // [!code focus]
})
```
```ts [abi.ts]
export const wagmiAbi = [
...
{
inputs: [{ name: "x", type: "uint32" }],
stateMutability: "nonpayable",
type: "constructor",
},
...
] as const;
```
<<< @/snippets/core/config.ts[config.ts]
:::

### connector

`Connector | undefined`

- Connector to use when deploying a contract.
- Defaults to current connector.

::: code-group
```ts [index.ts]
import { getAccount, deployContract } from '@wagmi/core'
import { wagmiAbi } from './abi'
import { config } from './config'

const { connector } = getAccount(config)
const result = await deployContract(config, {
abi: wagmiAbi,
args: [69420],
bytecode: '0x608060405260405161083e38038061083e833981016040819052610...',
connector, // [!code focus]
})
```
```ts [abi.ts]
export const wagmiAbi = [
...
{
inputs: [{ name: "x", type: "uint32" }],
stateMutability: "nonpayable",
type: "constructor",
},
...
] as const;
```
<<< @/snippets/core/config.ts[config.ts]
:::

## Return Type

```ts
import { type DeployContractReturnType } from '@wagmi/core'
```

[`Hash`](https://viem.sh/docs/glossary/types.html#hash)

Transaction hash.

## Error

```ts
import { type DeployContractErrorType } from '@wagmi/core'
```

<!--@include: @shared/mutation-imports.md-->

## Viem

- [`deployContract`](https://viem.sh/docs/contract/deployContract)
Loading
Loading