Skip to content
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

refactor: rename getBytecode to getCode #2411

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/three-dogs-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Deprecated `getBytecode` (use `getCode` instead).
12 changes: 6 additions & 6 deletions site/pages/docs/contract/getBytecode.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
description: Retrieves the bytecode at an address.
---

# getBytecode
# getCcode

Retrieves the bytecode at an address.

Expand All @@ -13,7 +13,7 @@ Retrieves the bytecode at an address.
```ts [example.ts]
import { publicClient } from './client'

const bytecode = await publicClient.getBytecode({
const bytecode = await publicClient.getCcode({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
})
```
Expand Down Expand Up @@ -45,7 +45,7 @@ The contract's bytecode.
The contract address.

```ts
const bytecode = await publicClient.getBytecode({
const bytecode = await publicClient.getCcode({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', // [!code focus]
})
```
Expand All @@ -57,7 +57,7 @@ const bytecode = await publicClient.getBytecode({
The block number to perform the bytecode read against.

```ts
const bytecode = await publicClient.getBytecode({
const bytecode = await publicClient.getCcode({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
blockNumber: 15121123n, // [!code focus]
})
Expand All @@ -71,12 +71,12 @@ const bytecode = await publicClient.getBytecode({
The block tag to perform the bytecode read against.

```ts
const bytecode = await publicClient.getBytecode({
const bytecode = await publicClient.getCcode({
address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
blockTag: 'safe', // [!code focus]
})
```

## JSON-RPC Method

[`eth_getCode`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getcode)
[`eth_getCode`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getcode)
4 changes: 2 additions & 2 deletions site/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ export const sidebar = {
link: '/docs/contract/estimateContractGas',
},
{
text: 'getBytecode',
link: '/docs/contract/getBytecode',
text: 'getCode',
link: '/docs/contract/getCode',
},
{
text: 'getContractEvents',
Expand Down
1 change: 1 addition & 0 deletions src/actions/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test('exports actions', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEnsAddress": [Function],
"getEnsAvatar": [Function],
Expand Down
20 changes: 14 additions & 6 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,25 @@ export {
type GetBlockTransactionCountReturnType,
getBlockTransactionCount,
} from './public/getBlockTransactionCount.js'
export {
type GetBytecodeErrorType,
type GetBytecodeParameters,
type GetBytecodeReturnType,
getBytecode,
} from './public/getBytecode.js'
export {
type GetChainIdErrorType,
type GetChainIdReturnType,
getChainId,
} from './public/getChainId.js'
export {
/** @deprecated Use `GetCodeErrorType` instead */
type GetCodeErrorType as GetBytecodeErrorType,
/** @deprecated Use `GetCodeParameters` instead */
type GetCodeParameters as GetBytecodeParameters,
/** @deprecated Use `GetCodeReturnType` instead */
type GetCodeReturnType as GetBytecodeReturnType,
/** @deprecated Use `getCode` instead */
getCode as getBytecode,
type GetCodeErrorType,
type GetCodeParameters,
type GetCodeReturnType,
getCode,
} from './public/getCode.js'
export {
type GetContractEventsErrorType,
type GetContractEventsParameters,
Expand Down

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions src/actions/public/getBytecode.ts → src/actions/public/getCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
numberToHex,
} from '../../utils/encoding/toHex.js'

export type GetBytecodeParameters = {
export type GetCodeParameters = {
address: Address
} & (
| {
Expand All @@ -25,40 +25,40 @@ export type GetBytecodeParameters = {
}
)

export type GetBytecodeReturnType = Hex | undefined
export type GetCodeReturnType = Hex | undefined

export type GetBytecodeErrorType =
export type GetCodeErrorType =
| NumberToHexErrorType
| RequestErrorType
| ErrorType

/**
* Retrieves the bytecode at an address.
*
* - Docs: https://viem.sh/docs/contract/getBytecode
* - Docs: https://viem.sh/docs/contract/getCode
* - JSON-RPC Methods: [`eth_getCode`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getcode)
*
* @param client - Client to use
* @param parameters - {@link GetBytecodeParameters}
* @returns The contract's bytecode. {@link GetBytecodeReturnType}
* @param parameters - {@link GetCodeParameters}
* @returns The contract's bytecode. {@link GetCodeReturnType}
*
* @example
* import { createPublicClient, http } from 'viem'
* import { mainnet } from 'viem/chains'
* import { getBytecode } from 'viem/contract'
* import { getCode } from 'viem/contract'
*
* const client = createPublicClient({
* chain: mainnet,
* transport: http(),
* })
* const code = await getBytecode(client, {
* const code = await getCode(client, {
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
* })
*/
export async function getBytecode<TChain extends Chain | undefined>(
export async function getCode<TChain extends Chain | undefined>(
client: Client<Transport, TChain>,
{ address, blockNumber, blockTag = 'latest' }: GetBytecodeParameters,
): Promise<GetBytecodeReturnType> {
{ address, blockNumber, blockTag = 'latest' }: GetCodeParameters,
): Promise<GetCodeReturnType> {
const blockNumberHex =
blockNumber !== undefined ? numberToHex(blockNumber) : undefined
const hex = await client.request({
Expand Down
1 change: 1 addition & 0 deletions src/clients/createClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ describe('extends', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEnsAddress": [Function],
"getEnsAvatar": [Function],
Expand Down
5 changes: 5 additions & 0 deletions src/clients/createPublicClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ test('creates', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEnsAddress": [Function],
"getEnsAvatar": [Function],
Expand Down Expand Up @@ -187,6 +188,7 @@ describe('transports', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEnsAddress": [Function],
"getEnsAvatar": [Function],
Expand Down Expand Up @@ -288,6 +290,7 @@ describe('transports', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEnsAddress": [Function],
"getEnsAvatar": [Function],
Expand Down Expand Up @@ -371,6 +374,7 @@ describe('transports', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEnsAddress": [Function],
"getEnsAvatar": [Function],
Expand Down Expand Up @@ -478,6 +482,7 @@ test('extend', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEnsAddress": [Function],
"getEnsAvatar": [Function],
Expand Down
1 change: 1 addition & 0 deletions src/clients/createTestClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ test('extend', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEnsAddress": [Function],
"getEnsAvatar": [Function],
Expand Down
1 change: 1 addition & 0 deletions src/clients/createWalletClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ test('extend', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEnsAddress": [Function],
"getEnsAvatar": [Function],
Expand Down
5 changes: 3 additions & 2 deletions src/clients/decorators/public.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ test('default', async () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEnsAddress": [Function],
"getEnsAvatar": [Function],
Expand Down Expand Up @@ -161,9 +162,9 @@ describe('smoke test', () => {
expect(await client.getBlockTransactionCount()).toBeDefined()
})

test('getBytecode', async () => {
test('getCode', async () => {
expect(
await client.getBytecode({ address: wagmiContractConfig.address }),
await client.getCode({ address: wagmiContractConfig.address }),
).toBeDefined()
})

Expand Down
47 changes: 25 additions & 22 deletions src/clients/decorators/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ import {
type GetBlockTransactionCountReturnType,
getBlockTransactionCount,
} from '../../actions/public/getBlockTransactionCount.js'
import {
type GetBytecodeParameters,
type GetBytecodeReturnType,
getBytecode,
} from '../../actions/public/getBytecode.js'
import {
type GetChainIdReturnType,
getChainId,
} from '../../actions/public/getChainId.js'
import {
type GetCodeParameters,
type GetCodeReturnType,
getCode,
} from '../../actions/public/getCode.js'
import {
type GetContractEventsParameters,
type GetContractEventsReturnType,
Expand Down Expand Up @@ -619,14 +619,15 @@ export type PublicActions<
getBlockTransactionCount: (
args?: GetBlockTransactionCountParameters | undefined,
) => Promise<GetBlockTransactionCountReturnType>
/** @deprecated Use `getCode` instead. */
getBytecode: (args: GetCodeParameters) => Promise<GetCodeReturnType>
/**
* Retrieves the bytecode at an address.
* Returns the chain ID associated with the current network.
*
* - Docs: https://viem.sh/docs/contract/getBytecode
* - JSON-RPC Methods: [`eth_getCode`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getcode)
* - Docs: https://viem.sh/docs/actions/public/getChainId
* - JSON-RPC Methods: [`eth_chainId`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_chainid)
*
* @param args - {@link GetBytecodeParameters}
* @returns The contract's bytecode. {@link GetBytecodeReturnType}
* @returns The current chain ID. {@link GetChainIdReturnType}
*
* @example
* import { createPublicClient, http } from 'viem'
Expand All @@ -636,18 +637,18 @@ export type PublicActions<
* chain: mainnet,
* transport: http(),
* })
* const code = await client.getBytecode({
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
* })
* const chainId = await client.getChainId()
* // 1
*/
getBytecode: (args: GetBytecodeParameters) => Promise<GetBytecodeReturnType>
getChainId: () => Promise<GetChainIdReturnType>
/**
* Returns the chain ID associated with the current network.
* Retrieves the bytecode at an address.
*
* - Docs: https://viem.sh/docs/actions/public/getChainId
* - JSON-RPC Methods: [`eth_chainId`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_chainid)
* - Docs: https://viem.sh/docs/contract/getCode
* - JSON-RPC Methods: [`eth_getCode`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getcode)
*
* @returns The current chain ID. {@link GetChainIdReturnType}
* @param args - {@link GetBytecodeParameters}
* @returns The contract's bytecode. {@link GetBytecodeReturnType}
*
* @example
* import { createPublicClient, http } from 'viem'
Expand All @@ -657,10 +658,11 @@ export type PublicActions<
* chain: mainnet,
* transport: http(),
* })
* const chainId = await client.getChainId()
* // 1
* const code = await client.getCode({
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
* })
*/
getChainId: () => Promise<GetChainIdReturnType>
getCode: (args: GetCodeParameters) => Promise<GetCodeReturnType>
/**
* Returns a list of event logs emitted by a contract.
*
Expand Down Expand Up @@ -1813,8 +1815,9 @@ export function publicActions<
getBlock: (args) => getBlock(client, args),
getBlockNumber: (args) => getBlockNumber(client, args),
getBlockTransactionCount: (args) => getBlockTransactionCount(client, args),
getBytecode: (args) => getBytecode(client, args),
getBytecode: (args) => getCode(client, args),
getChainId: () => getChainId(client),
getCode: (args) => getCode(client, args),
getContractEvents: (args) => getContractEvents(client, args),
getEnsAddress: (args) => getEnsAddress(client, args),
getEnsAvatar: (args) => getEnsAvatar(client, args),
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,16 @@ export type {
GetBlockTransactionCountReturnType,
} from './actions/public/getBlockTransactionCount.js'
export type {
GetBytecodeErrorType,
GetBytecodeParameters,
GetBytecodeReturnType,
} from './actions/public/getBytecode.js'
/** @deprecated Use `GetCodeErrorType` instead */
GetCodeErrorType as GetBytecodeErrorType,
/** @deprecated Use `GetCodeParameters` instead */
GetCodeParameters as GetBytecodeParameters,
/** @deprecated Use `GetCodeReturnType` instead */
GetCodeReturnType as GetBytecodeReturnType,
GetCodeErrorType,
GetCodeParameters,
GetCodeReturnType,
} from './actions/public/getCode.js'
export type {
GetChainIdErrorType,
GetChainIdReturnType,
Expand Down