Skip to content

Commit 2e610bf

Browse files
committed
Add examples for added grpc client methods
1 parent 68f4ac7 commit 2e610bf

File tree

3 files changed

+131
-8
lines changed

3 files changed

+131
-8
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { BlockHash } from '@concordium/web-sdk';
2+
import { ConcordiumGRPCNodeClient } from '@concordium/web-sdk/nodejs';
3+
import { TokenId, TokenInfo } from '@concordium/web-sdk/plt';
4+
import { credentials } from '@grpc/grpc-js';
5+
import meow from 'meow';
6+
7+
import { parseEndpoint } from '../shared/util.js';
8+
9+
const cli = meow(
10+
`
11+
Usage
12+
$ yarn run-example <path-to-this-file> [options]
13+
14+
Required
15+
--token, -t The token to query information about
16+
17+
Options
18+
--help, Displays this message
19+
--block, -b A block to query from, defaults to last final block
20+
--endpoint, -e Specify endpoint of the form "address:port", defaults to localhost:20000
21+
`,
22+
{
23+
importMeta: import.meta,
24+
flags: {
25+
token: {
26+
type: 'string',
27+
alias: 't',
28+
isRequired: true,
29+
},
30+
block: {
31+
type: 'string',
32+
alias: 'b',
33+
},
34+
endpoint: {
35+
type: 'string',
36+
alias: 'e',
37+
default: 'localhost:20000',
38+
},
39+
},
40+
}
41+
);
42+
43+
const [address, port] = parseEndpoint(cli.flags.endpoint);
44+
45+
const client = new ConcordiumGRPCNodeClient(address, Number(port), credentials.createInsecure());
46+
47+
/**
48+
* Retrieves information about an protocol level token (PLT). The function must be provided a
49+
* token id.
50+
*/
51+
(async () => {
52+
// #region documentation-snippet
53+
const tokenId = TokenId.fromString(cli.flags.token);
54+
const blockHash = cli.flags.block === undefined ? undefined : BlockHash.fromHexString(cli.flags.block);
55+
const tokenInfo: TokenInfo = await client.getTokenInfo(tokenId, blockHash);
56+
57+
console.log('Total token supply:', tokenInfo.state.totalSupply);
58+
console.log('Token issuer:', tokenInfo.state.issuer);
59+
// #endregion documentation-snippet
60+
})();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { BlockHash } from '@concordium/web-sdk';
2+
import { ConcordiumGRPCNodeClient } from '@concordium/web-sdk/nodejs';
3+
import { TokenId } from '@concordium/web-sdk/plt';
4+
import { credentials } from '@grpc/grpc-js';
5+
import meow from 'meow';
6+
7+
import { parseEndpoint } from '../shared/util.js';
8+
9+
const cli = meow(
10+
`
11+
Usage
12+
$ yarn run-example <path-to-this-file> [options]
13+
14+
Options
15+
--help, Displays this message
16+
--block, -b A block to query from, defaults to last final block
17+
--endpoint, -e Specify endpoint of the form "address:port", defaults to localhost:20000
18+
`,
19+
{
20+
importMeta: import.meta,
21+
flags: {
22+
block: {
23+
type: 'string',
24+
alias: 'b',
25+
},
26+
endpoint: {
27+
type: 'string',
28+
alias: 'e',
29+
default: 'localhost:20000',
30+
},
31+
},
32+
}
33+
);
34+
35+
const [address, port] = parseEndpoint(cli.flags.endpoint);
36+
37+
const client = new ConcordiumGRPCNodeClient(address, Number(port), credentials.createInsecure());
38+
39+
/**
40+
* Retrieves the protocol level tokens that exists at the end of a given block as an async
41+
* iterable. If a blockhash is not supplied it will pick the latest finalized
42+
* block. An optional abortSignal can also be provided that closes the stream.
43+
44+
* Note: A stream can be collected to a list with the streamToList function.
45+
*/
46+
47+
(async () => {
48+
// #region documentation-snippet
49+
const blockHash = cli.flags.block === undefined ? undefined : BlockHash.fromHexString(cli.flags.block);
50+
const tokens: AsyncIterable<TokenId.Type> = client.getTokenList(blockHash);
51+
// #endregion documentation-snippet
52+
53+
console.log('Protocol level tokens (PLTs) that exists at the end of the given block:');
54+
for await (const token of tokens) {
55+
console.log(token.toString());
56+
}
57+
})();

packages/sdk/src/grpc/GRPCClient.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import { getAccountTransactionHandler } from '../accountTransactions.js';
1212
import { DEFAULT_INVOKE_ENERGY } from '../constants.js';
1313
import { calculateEnergyCost } from '../energyCost.js';
1414
import { HealthClient } from '../grpc-api/v2/concordium/health.client.js';
15+
import * as GRPCKernel from '../grpc-api/v2/concordium/kernel.js';
1516
import { QueriesClient } from '../grpc-api/v2/concordium/service.client.js';
1617
import * as GRPC from '../grpc-api/v2/concordium/types.js';
17-
import * as GRPCKernel from '../grpc-api/v2/concordium/kernel.js';
18+
import * as PLT from '../plt/types.js';
1819
import { RawModuleSchema } from '../schemaTypes.js';
1920
import { serializeAccountTransactionPayload } from '../serialization.js';
2021
import * as SDK from '../types.js';
21-
import * as PLT from '../plt/types.js';
2222
import { HexString, isRpcError } from '../types.js';
2323
import * as AccountAddress from '../types/AccountAddress.js';
2424
import * as BlockHash from '../types/BlockHash.js';
@@ -226,7 +226,10 @@ export class ConcordiumGRPCClient {
226226
* @returns An object with information about the contract instance.
227227
* @throws An error of type `RpcError` if not found in the block.
228228
*/
229-
async getInstanceInfo(contractAddress: ContractAddress.Type, blockHash?: BlockHash.Type): Promise<SDK.InstanceInfo> {
229+
async getInstanceInfo(
230+
contractAddress: ContractAddress.Type,
231+
blockHash?: BlockHash.Type
232+
): Promise<SDK.InstanceInfo> {
230233
const instanceInfoRequest: GRPC.InstanceInfoRequest = {
231234
blockHash: getBlockHashInput(blockHash),
232235
address: ContractAddress.toProto(contractAddress),
@@ -1510,11 +1513,12 @@ export class ConcordiumGRPCClient {
15101513
}
15111514
}
15121515

1513-
// TODO: add example snippet
15141516
/**
15151517
* Get information about a protocol level token (PLT) at a certain block.
15161518
* This endpoint is only supported for protocol version 9 and onwards.
15171519
*
1520+
* {@codeblock ~~:nodejs/client/getTokenInfo.ts#documentation-snippet}
1521+
*
15181522
* @param tokenId the ID of the token to query information about
15191523
* @param blockHash an optional block hash to get the info from, otherwise retrieves from last finalized block.
15201524
* @returns {PLT.TokenInfo} information about the corresponding token.
@@ -1524,23 +1528,25 @@ export class ConcordiumGRPCClient {
15241528
const req: GRPC.TokenInfoRequest = {
15251529
tokenId: PLT.TokenId.toProto(tokenId),
15261530
blockHash: blockHashInput,
1527-
}
1531+
};
15281532
const res = await this.client.getTokenInfo(req);
15291533
return translate.trTokenInfo(res.response);
15301534
}
15311535

1532-
// TODO: add example snippet
15331536
/**
15341537
* Get all token IDs currently registered at a block.
15351538
* This endpoint is only supported for protocol version 9 and onwards.
15361539
*
1540+
* {@codeblock ~~:nodejs/client/getTokenList.ts#documentation-snippet}
1541+
*
15371542
* @param blockHash optional block hash, otherwise retrieves from last finalized block.
1543+
* @param abortSignal an optional AbortSignal to close the stream.
15381544
*
15391545
* @returns All token IDs registered at a block
15401546
*/
1541-
getTokenList(blockHash?: BlockHash.Type): AsyncIterable<PLT.TokenId.Type> {
1547+
getTokenList(blockHash?: BlockHash.Type, abortSignal?: AbortSignal): AsyncIterable<PLT.TokenId.Type> {
15421548
const blockHashInput = getBlockHashInput(blockHash);
1543-
const tokenIds = this.client.getTokenList(blockHashInput).responses;
1549+
const tokenIds = this.client.getTokenList(blockHashInput, { abort: abortSignal }).responses;
15441550
return mapStream(tokenIds, PLT.TokenId.fromProto);
15451551
}
15461552
}

0 commit comments

Comments
 (0)