forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.eth.pubsub.pas
150 lines (126 loc) · 4.05 KB
/
web3.eth.pubsub.pas
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2020 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under Creative Commons NonCommercial (aka CC BY-NC) license. }
{ }
{ https://geth.ethereum.org/docs/rpc/pubsub }
{ }
{******************************************************************************}
unit web3.eth.pubsub;
{$I web3.inc}
interface
uses
// Delphi
System.JSON,
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3,
web3.eth.types;
type
TSubscription = (
logs,
newHeads,
newPendingTransactions,
syncing
);
procedure subscribe(
client : TWeb3;
subscription: TSubscription;
callback : TAsyncString; // one-time callback (subscribed, or a JSON-RPC error)
notification: TAsyncJsonObject; // continuous notifications (or a JSON-RPC error)
onError : TAsyncError; // non-JSON-RPC-error handler (probably a socket error)
onDisconnect: TProc); // connection closed
procedure unsubscribe(
client : TWeb3;
const subscription: string; // as returned by the eth_subscribe callback
callback : TAsyncBoolean); // true if successful, otherwise false
function blockNumber(notification: TJsonObject): BigInteger;
implementation
uses
// Delphi
System.TypInfo,
// web3
web3.json;
{---------------------------- TSubscriptionHelper -----------------------------}
type
TSubscriptionHelper = record helper for TSubscription
public
function ToString: string;
end;
function TSubscriptionHelper.ToString: string;
begin
Result := GetEnumName(TypeInfo(TSubscription), Ord(Self));
end;
{---------------------------------- globals -----------------------------------}
procedure subscribe(
client : TWeb3;
subscription: TSubscription;
callback : TAsyncString;
notification: TAsyncJsonObject;
onError : TAsyncError;
onDisconnect: TProc);
var
pubSub: IPubSub;
result: string;
begin
pubSub := client.PubSub;
if not Assigned(pubSub) then
begin
callback('', TError.Create('not a WebSocket'));
EXIT;
end;
pubSub.OnError := onError;
pubSub.OnDisconnect := onDisconnect;
client.JsonRpc.Send(client.URL, client.Security, 'eth_subscribe', [subscription.ToString], procedure(resp: TJsonObject; err: IError)
begin
if Assigned(err) then
begin
callback('', err);
EXIT;
end;
result := web3.json.getPropAsStr(resp, 'result');
callback(result, nil);
pubSub.Subscribe(result, notification);
end);
end;
procedure unsubscribe(
client : TWeb3;
const subscription: string;
callback : TAsyncBoolean);
var
result: Boolean;
begin
client.JsonRpc.Send(client.URL, client.Security, 'eth_unsubscribe', [subscription], procedure(resp: TJsonObject; err: IError)
begin
if Assigned(err) then
begin
callback(False, err);
EXIT;
end;
result := web3.json.getPropAsStr(resp, 'result').Equals('true');
callback(result, nil);
if result then
client.PubSub.Unsubscribe(subscription);
end);
end;
function blockNumber(notification: TJsonObject): BigInteger;
var
params : TJsonObject;
_result: TJsonObject;
begin
Result := 0;
params := web3.json.getPropAsObj(notification, 'params');
if Assigned(params) then
begin
_result := web3.json.getPropAsObj(params, 'result');
if Assigned(_result) then
Result := web3.json.getPropAsStr(_result, 'number');
end;
end;
end.