For the complete documentation index, see llms.txt. This page is also available as Markdown.

GetTdByHash - Polygon gRPC

Example code for the GetTdByHash grpc method. Сomplete guide on how to use GetTdByHash grpc in GetBlock.io Web3 documentation.

Returns the total difficulty (TD) for a block identified by its hash. Total difficulty is the cumulative consensus difficulty from genesis to this block — Bor uses TD for fork-choice: the canonical chain is the one with the highest cumulative TD, with in-turn producer blocks scoring higher than out-of-turn blocks.

Request Parameters

Parameter
Type
Required
Description

hash

H256

Yes

Block hash to look up TD for

Request Example

grpcurl -H 'x-access-token: <ACCESS_TOKEN>' \
        -d '{"hash": {"Hi": {"Hi": "3272624667754571407", "Lo": "4672811091460113517"}, "Lo": {"Hi": "5134667498046831133", "Lo": "17102463161894732817"}}}' \
        shared.ap-southeast-1.getblock.io:443 \
        bor.BorApi/GetTdByHash
import * as grpc from '@grpc/grpc-js';
import protoLoader from '@grpc/proto-loader';

const GRPC_TARGET = 'shared.ap-southeast-1.getblock.io:443';
const ACCESS_TOKEN = '<ACCESS-TOKEN>';
const PROTO_PATH = './protos/bor/bor.proto';

const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true,
    includeDirs: ['./protos'],
});
const proto = grpc.loadPackageDefinition(packageDefinition);
const BorApiClient = proto.bor.BorApi;
const client = new BorApiClient(GRPC_TARGET, grpc.credentials.createSsl());

const metadata = new grpc.Metadata();
metadata.add('x-access-token', ACCESS_TOKEN);

const request = {"hash": {"Hi": {"Hi": "3272624667754571407", "Lo": "4672811091460113517"}, "Lo": {"Hi": "5134667498046831133", "Lo": "17102463161894732817"}}};

client.GetTdByHash(request, metadata, (err, response) => {
    if (err) {
        console.error('gRPC error:', err.code, err.message);
        return;
    }
    console.log(JSON.stringify(response, null, 2));
});

Response Example

{
  "totalDifficulty": "1506972941"
}

H160 and H256 encoding. In JSON representations of protobuf messages, common.H160 (20-byte addresses) and common.H256 (32-byte hashes) are shown here as hex strings for readability. The actual JSON wire format from grpcurl or MessageToJson reflects the message structure defined in common/common.proto — typically a nested object encoding. Applications using the typed generated clients receive H160/H256 as strongly-typed message objects, not strings.

Response Fields

Field
Type
Description

totalDifficulty

uint64

Cumulative consensus difficulty from genesis to this block

Use Cases

  • Fork detection — comparing TD of competing chain tips

  • Chain reorganization analysis

  • Verifying that a proposed chain segment has higher cumulative difficulty than the current canonical chain

Error Handling

gRPC Status (Code)
Message
Cause

UNAUTHENTICATED (16)

missing or invalid x-access-token

Access token missing from x-access-token gRPC metadata or invalid

PERMISSION_DENIED (7)

access denied

Access token doesn't have permission for this method or endpoint

INVALID_ARGUMENT (3)

invalid request

Request message is malformed or fails validation (e.g. invalid block number tag)

INTERNAL (13)

internal error

Server-side error while processing the request

UNAVAILABLE (14)

service unavailable

Bor node is temporarily unavailable (syncing, restart, or overload)

DEADLINE_EXCEEDED (4)

deadline exceeded

Request took longer than the client-configured timeout

RESOURCE_EXHAUSTED (8)

rate limit exceeded

Rate limit exceeded for your plan

NOT_FOUND (5)

block not found

Block hash doesn't correspond to a known block

SDK Integration

Last updated

Was this helpful?