-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.ts
105 lines (87 loc) · 3.47 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// This file have to be here, because otherwise ProtoGrpcType imports point to a wrong location
import Long from "long";
import * as path from 'path';
import * as g from '@grpc/grpc-js';
import * as p from '@grpc/proto-loader';
import * as utils from './src/utils';
// NOTE: Run `npm run build` first to have these files:
import { ProtoGrpcType as AccountsApiRoot } from "./types/accounts_api";
import { ProtoGrpcType as AssetsApiRoot } from "./types/assets_api";
import { ProtoGrpcType as BlockchainApiRoot } from "./types/blockchain_api";
import { ProtoGrpcType as BlockchainUpdatesApiRoot } from "./types/blockchain_updates";
import { ProtoGrpcType as BlocksApiRoot } from "./types/blocks_api";
import { ProtoGrpcType as TransactionsApiRoot } from "./types/transactions_api";
// Useful FS functionality
export function protoFilesInSubDir(dir: string) {
return utils.filter(utils.walkSync(dir), (x: string) => x.endsWith('.proto'));
}
const protobufModuleDir = path.dirname(require.resolve('@waves/protobuf-serialization/package.json'));
export function wavesProtoFiles() {
return [...protoFilesInSubDir(path.resolve(protobufModuleDir, 'proto'))];
}
// API definitions
export type Api =
AccountsApiRoot
& AssetsApiRoot
& BlockchainApiRoot
& BlockchainUpdatesApiRoot
& BlocksApiRoot
& TransactionsApiRoot
export const allPackageDefinitions = p.loadSync(
wavesProtoFiles(),
{
longs: Long,
keepCase: false,
enums: String,
defaults: true,
oneofs: true,
includeDirs: [`${protobufModuleDir}/proto`]
}
);
export const grpcRootObject = (g.loadPackageDefinition(allPackageDefinitions) as any) as Api;
export const apiDefs = grpcRootObject.waves;
// API
export const grpc = {
defaultChannelCredentials: g.credentials.createInsecure(),
mkDefaultChannel: (target: string, options: g.ChannelOptions = {}) => {
return new g.Channel(
target,
g.credentials.createInsecure(),
// https://github.com/grpc/grpc-node/tree/master/packages/grpc-js#supported-channel-options
{
'grpc.enable_channelz': 0, // https://github.com/grpc/grpc-node/issues/1941
'grpc.max_receive_message_length': 8 * 1024 * 1024, // Some blocks on MainNet and TestNet are huge
...options
}
);
}
};
export function mkApi<ConcreteT extends g.Client>(TypeCreator: new (address: string, credentials: g.ChannelCredentials, options: g.ClientOptions) => ConcreteT,
channel: g.Channel): ConcreteT {
return new TypeCreator('', grpc.defaultChannelCredentials, { channelOverride: channel });
}
// TypeScript doesn't support curried functions, so we can't write blockchainUpdatesApi: mkApifun(grpc.BlockchainUpdatesApi)
export namespace api.waves {
export namespace events.grpc {
export function mkBlockchainUpdatesApi(channel: g.Channel) {
return mkApi(apiDefs.events.grpc.BlockchainUpdatesApi, channel)
}
}
export namespace node.grpc {
export function mkAccountsApi(channel: g.Channel) {
return mkApi(apiDefs.node.grpc.AccountsApi, channel)
}
export function mkAssetsApi(channel: g.Channel) {
return mkApi(apiDefs.node.grpc.AssetsApi, channel)
}
export function mkBlockchainApi(channel: g.Channel) {
return mkApi(apiDefs.node.grpc.BlockchainApi, channel)
}
export function mkBlocksApi(channel: g.Channel) {
return mkApi(apiDefs.node.grpc.BlocksApi, channel)
}
export function mkTransactionsApi(channel: g.Channel) {
return mkApi(apiDefs.node.grpc.TransactionsApi, channel)
}
}
}