Skip to content

Commit

Permalink
feat: getBytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Feb 2, 2023
1 parent 5bfe080 commit 257c8f3
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/violet-vans-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Added `getBytecode`.
2 changes: 1 addition & 1 deletion site/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export const sidebar: DefaultTheme.Sidebar = {
link: '/docs/contract/deployContract',
},
{
text: 'getBytecode 🚧',
text: 'getBytecode',
link: '/docs/contract/getBytecode',
},
{
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 @@ -20,6 +20,7 @@ test('exports actions', () => {
"getBlockNumber": [Function],
"getBlockNumberCache": [Function],
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getFeeHistory": [Function],
"getFilterChanges": [Function],
Expand Down
3 changes: 3 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
getBlockNumber,
getBlockNumberCache,
getBlockTransactionCount,
getBytecode,
getChainId,
getFeeHistory,
getFilterChanges,
Expand Down Expand Up @@ -44,6 +45,8 @@ export type {
GetBlockResponse,
GetBlockTransactionCountArgs,
GetBlockTransactionCountResponse,
GetBytecodeArgs,
GetBytecodeResponse,
GetFeeHistoryArgs,
GetFeeHistoryResponse,
GetFilterChangesArgs,
Expand Down
27 changes: 27 additions & 0 deletions src/actions/public/getBytecode.test.ts

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions src/actions/public/getBytecode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { PublicClient } from '../../clients'
import { Address, BlockTag, Hex } from '../../types'
import { numberToHex } from '../../utils'

export type GetBytecodeArgs = {
address: Address
} & (
| {
blockNumber?: never
blockTag?: BlockTag
}
| {
blockNumber?: bigint
blockTag?: never
}
)

export type GetBytecodeResponse = Hex | undefined

export async function getBytecode(
client: PublicClient,
{ address, blockNumber, blockTag = 'latest' }: GetBytecodeArgs,
): Promise<GetBytecodeResponse> {
const blockNumberHex =
blockNumber !== undefined ? numberToHex(blockNumber) : undefined
const hex = await client.request({
method: 'eth_getCode',
params: [address, blockNumberHex || blockTag],
})
if (hex === '0x') return undefined
return hex
}
1 change: 1 addition & 0 deletions src/actions/public/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ test('exports actions', () => {
"getBlockNumber": [Function],
"getBlockNumberCache": [Function],
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getFeeHistory": [Function],
"getFilterChanges": [Function],
Expand Down
3 changes: 3 additions & 0 deletions src/actions/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export type {
GetBlockTransactionCountResponse,
} from './getBlockTransactionCount'

export { getBytecode } from './getBytecode'
export type { GetBytecodeArgs, GetBytecodeResponse } from './getBytecode'

export { getChainId } from './getChainId'

export { getFeeHistory } from './getFeeHistory'
Expand Down
1 change: 1 addition & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ test('exports actions', () => {
"getBlock": [Function],
"getBlockNumber": [Function],
"getBlockTransactionCount": [Function],
"getBytecode": [Function],
"getChainId": [Function],
"getContractAddress": [Function],
"getCreate2Address": [Function],
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type {
GetBlockResponse,
GetBlockTransactionCountArgs,
GetBlockTransactionCountResponse,
GetBytecodeArgs,
GetBytecodeResponse,
GetFeeHistoryArgs,
GetFeeHistoryResponse,
GetFilterChangesArgs,
Expand Down Expand Up @@ -90,6 +92,7 @@ export {
getBlock,
getBlockNumber,
getBlockTransactionCount,
getBytecode,
getChainId,
getFeeHistory,
getFilterChanges,
Expand Down

3 comments on commit 257c8f3

@vercel
Copy link

@vercel vercel bot commented on 257c8f3 Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viem-benchmark – ./playgrounds/benchmark

viem-benchmark-wagmi-dev.vercel.app
viem-benchmark-git-main-wagmi-dev.vercel.app
viem-benchmark.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 257c8f3 Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viem-site – ./site

viem-site-git-main-wagmi-dev.vercel.app
viem-site-wagmi-dev.vercel.app
viem-site.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 257c8f3 Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viem-playground – ./playgrounds/dev

viem-playground-wagmi-dev.vercel.app
viem-playground-git-main-wagmi-dev.vercel.app
viem-playground.vercel.app

Please sign in to comment.