Skip to content

Commit

Permalink
refactor: rename getBytecode to getCode (#2411)
Browse files Browse the repository at this point in the history
* refactor: rename getBytecode to getCode

* chore: changeset
  • Loading branch information
tmm authored Jun 14, 2024
1 parent b7e3c2e commit 2fc14e5
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 59 deletions.
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 @@ -531,8 +531,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],
"getEip712Domain": [Function],
"getEnsAddress": [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],
"getEip712Domain": [Function],
"getEnsAddress": [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],
"getEip712Domain": [Function],
"getEnsAddress": [Function],
Expand Down Expand Up @@ -188,6 +189,7 @@ describe('transports', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEip712Domain": [Function],
"getEnsAddress": [Function],
Expand Down Expand Up @@ -290,6 +292,7 @@ describe('transports', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEip712Domain": [Function],
"getEnsAddress": [Function],
Expand Down Expand Up @@ -374,6 +377,7 @@ describe('transports', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEip712Domain": [Function],
"getEnsAddress": [Function],
Expand Down Expand Up @@ -482,6 +486,7 @@ test('extend', () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEip712Domain": [Function],
"getEnsAddress": [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],
"getEip712Domain": [Function],
"getEnsAddress": [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],
"getEip712Domain": [Function],
"getEnsAddress": [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 @@ -51,6 +51,7 @@ test('default', async () => {
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getCode": [Function],
"getContractEvents": [Function],
"getEip712Domain": [Function],
"getEnsAddress": [Function],
Expand Down Expand Up @@ -167,9 +168,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 @@ -624,14 +624,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 @@ -641,18 +642,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 @@ -662,10 +663,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 @@ -1853,8 +1855,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),
getEip712Domain: (args) => getEip712Domain(client, args),
getEnsAddress: (args) => getEnsAddress(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 @@ -146,10 +146,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

0 comments on commit 2fc14e5

Please sign in to comment.