Skip to content

Commit 93071dd

Browse files
committed
update custom depth w/ new helpers
1 parent ce7fa89 commit 93071dd

File tree

3 files changed

+35
-63
lines changed

3 files changed

+35
-63
lines changed

reports/llms-report.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{
2-
"startedAt": "2025-12-09T18:45:48.237Z",
2+
"startedAt": "2025-12-10T15:44:42.040Z",
33
"siteBase": "https://docs.chain.link",
44
"sections": [
55
{
66
"section": "cre-go",
77
"pagesProcessed": 84,
88
"outputPath": "src/content/cre/llms-full-go.txt",
9-
"bytes": 656030,
10-
"prevBytes": 656030,
9+
"bytes": 667067,
10+
"prevBytes": 667067,
1111
"deltaBytes": 0
1212
},
1313
{
1414
"section": "cre-ts",
1515
"pagesProcessed": 79,
1616
"outputPath": "src/content/cre/llms-full-ts.txt",
17-
"bytes": 612048,
18-
"prevBytes": 611515,
19-
"deltaBytes": 533
17+
"bytes": 623539,
18+
"prevBytes": 624187,
19+
"deltaBytes": -648
2020
},
2121
{
2222
"section": "vrf",
@@ -123,5 +123,5 @@
123123
"deltaBytes": 0
124124
}
125125
],
126-
"finishedAt": "2025-12-09T18:45:52.560Z"
126+
"finishedAt": "2025-12-10T15:44:46.121Z"
127127
}

src/content/cre/guides/workflow/using-evm-client/onchain-read-ts.mdx

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -193,58 +193,44 @@ For use cases requiring fixed confirmation thresholds (e.g., regulatory complian
193193
**Example 1 - Read from a specific historical block**:
194194

195195
```typescript
196+
import { blockNumber } from '@chainlink/cre-sdk'
197+
196198
const historicalBlock = 9767655n
197199
const contractCall = evmClient.callContract(runtime, {
198200
call: encodeCallMsg({...}),
199-
blockNumber: {
200-
absVal: Buffer.from(historicalBlock.toString(16).padStart(2, '0'), 'hex').toString('base64'),
201-
},
201+
blockNumber: blockNumber(historicalBlock),
202202
}).result()
203203
```
204204

205-
**Example 2 - Read from 500 blocks ago for latest block**:
205+
**Example 2 - Read from 500 blocks ago for custom finality**:
206206

207207
```typescript
208-
// Helper to convert protobuf BigInt to native bigint
209-
const protoBigIntToBigInt = (pb: { absVal: Uint8Array; sign: bigint }): bigint => {
210-
let result = 0n
211-
for (const byte of pb.absVal) {
212-
result = (result << 8n) + BigInt(byte)
213-
}
214-
return pb.sign < 0n ? -result : result
215-
}
208+
import { protoBigIntToBigint, blockNumber } from '@chainlink/cre-sdk'
216209

217-
// Example: Read from 500 blocks ago for custom finality
210+
// Get the latest block number
218211
const latestHeader = evmClient.headerByNumber(runtime, {}).result()
219212
if (!latestHeader.header?.blockNumber) {
220213
throw new Error("Failed to get latest block number")
221214
}
222215

223-
const latestBlockNum = protoBigIntToBigInt(latestHeader.header.blockNumber)
216+
// Convert protobuf BigInt to native bigint and calculate custom block
217+
const latestBlockNum = protoBigIntToBigint(latestHeader.header.blockNumber)
224218
const customBlock = latestBlockNum - 500n
225219

220+
// Call the contract at the custom block height
226221
const contractCall = evmClient.callContract(runtime, {
227222
call: encodeCallMsg({...}),
228-
blockNumber: {
229-
absVal: Buffer.from(customBlock.toString(16).padStart(2, '0'), 'hex').toString('base64'),
230-
},
223+
blockNumber: blockNumber(customBlock),
231224
}).result()
232225
```
233226

234-
**Understanding the conversions:**
227+
**Helper functions:**
235228

236-
1. **Protobuf `BigInt` to native `bigint`:** The `protoBigIntToBigInt` helper converts the SDK's protobuf `BigInt` type (which stores the value as `absVal: Uint8Array`) to JavaScript's native `bigint`. This allows you to perform arithmetic like subtracting 500 blocks.
229+
The SDK provides two helper functions for working with block numbers:
237230

238-
1. **Native `bigint` to `BigIntJson`:** To pass a native `bigint` back to the SDK:
239-
- `blockNum.toString(16)` converts to hexadecimal
240-
- `.padStart(2, '0')` ensures even-length hex string (required by `Buffer.from`)
241-
- `Buffer.from(..., 'hex')` creates a byte array from the hex
242-
- `.toString('base64')` converts to base64 format required by `BigIntJson.absVal`
231+
- **`protoBigIntToBigint(pb)`** — Converts a protobuf `BigInt` (returned by SDK methods like `headerByNumber`) to a native JavaScript `bigint`. Use this when you need to perform arithmetic on block numbers.
243232

244-
<Aside type="note" title="SDK enhancements planned">
245-
The SDK team is working on built-in helpers for both conversions (protobuf `BigInt` ↔ native `bigint` and `bigint`
246-
`BigIntJson`). Until then, use the patterns shown above.
247-
</Aside>
233+
- **`blockNumber(n)`** — Converts a native `bigint`, `number`, or `string` to the protobuf `BigInt` JSON format required by SDK methods. This is an alias for `bigintToProtoBigInt`.
248234

249235
See [Finality and Confidence Levels](/cre/concepts/finality-ts) for more details on when to use custom block depths.
250236

src/content/cre/llms-full-ts.txt

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9049,58 +9049,44 @@ For use cases requiring fixed confirmation thresholds (e.g., regulatory complian
90499049
**Example 1 - Read from a specific historical block**:
90509050

90519051
```typescript
9052+
import { blockNumber } from '@chainlink/cre-sdk'
9053+
90529054
const historicalBlock = 9767655n
90539055
const contractCall = evmClient.callContract(runtime, {
90549056
call: encodeCallMsg({...}),
9055-
blockNumber: {
9056-
absVal: Buffer.from(historicalBlock.toString(16).padStart(2, '0'), 'hex').toString('base64'),
9057-
},
9057+
blockNumber: blockNumber(historicalBlock),
90589058
}).result()
90599059
```
90609060

9061-
**Example 2 - Read from 500 blocks ago for latest block**:
9061+
**Example 2 - Read from 500 blocks ago for custom finality**:
90629062

90639063
```typescript
9064-
// Helper to convert protobuf BigInt to native bigint
9065-
const protoBigIntToBigInt = (pb: { absVal: Uint8Array; sign: bigint }): bigint => {
9066-
let result = 0n
9067-
for (const byte of pb.absVal) {
9068-
result = (result << 8n) + BigInt(byte)
9069-
}
9070-
return pb.sign < 0n ? -result : result
9071-
}
9064+
import { protoBigIntToBigint, blockNumber } from '@chainlink/cre-sdk'
90729065

9073-
// Example: Read from 500 blocks ago for custom finality
9066+
// Get the latest block number
90749067
const latestHeader = evmClient.headerByNumber(runtime, {}).result()
90759068
if (!latestHeader.header?.blockNumber) {
90769069
throw new Error("Failed to get latest block number")
90779070
}
90789071

9079-
const latestBlockNum = protoBigIntToBigInt(latestHeader.header.blockNumber)
9072+
// Convert protobuf BigInt to native bigint and calculate custom block
9073+
const latestBlockNum = protoBigIntToBigint(latestHeader.header.blockNumber)
90809074
const customBlock = latestBlockNum - 500n
90819075

9076+
// Call the contract at the custom block height
90829077
const contractCall = evmClient.callContract(runtime, {
90839078
call: encodeCallMsg({...}),
9084-
blockNumber: {
9085-
absVal: Buffer.from(customBlock.toString(16).padStart(2, '0'), 'hex').toString('base64'),
9086-
},
9079+
blockNumber: blockNumber(customBlock),
90879080
}).result()
90889081
```
90899082

9090-
**Understanding the conversions:**
9083+
**Helper functions:**
90919084

9092-
1. **Protobuf `BigInt` to native `bigint`:** The `protoBigIntToBigInt` helper converts the SDK's protobuf `BigInt` type (which stores the value as `absVal: Uint8Array`) to JavaScript's native `bigint`. This allows you to perform arithmetic like subtracting 500 blocks.
9085+
The SDK provides two helper functions for working with block numbers:
90939086

9094-
2. **Native `bigint` to `BigIntJson`:** To pass a native `bigint` back to the SDK:
9095-
- `blockNum.toString(16)` converts to hexadecimal
9096-
- `.padStart(2, '0')` ensures even-length hex string (required by `Buffer.from`)
9097-
- `Buffer.from(..., 'hex')` creates a byte array from the hex
9098-
- `.toString('base64')` converts to base64 format required by `BigIntJson.absVal`
9087+
- **`protoBigIntToBigint(pb)`** — Converts a protobuf `BigInt` (returned by SDK methods like `headerByNumber`) to a native JavaScript `bigint`. Use this when you need to perform arithmetic on block numbers.
90999088

9100-
<Aside type="note" title="SDK enhancements planned">
9101-
The SDK team is working on built-in helpers for both conversions (protobuf `BigInt` ↔ native `bigint` and `bigint` →
9102-
`BigIntJson`). Until then, use the patterns shown above.
9103-
</Aside>
9089+
- **`blockNumber(n)`** — Converts a native `bigint`, `number`, or `string` to the protobuf `BigInt` JSON format required by SDK methods. This is an alias for `bigintToProtoBigInt`.
91049090

91059091
See [Finality and Confidence Levels](/cre/concepts/finality-ts) for more details on when to use custom block depths.
91069092

0 commit comments

Comments
 (0)