@@ -25,6 +25,8 @@ import {
25
25
DeepClient , DeepClientAuthResult , DeepClientGuestOptions , DeepClientInstance ,
26
26
DeepClientJWTOptions , Exp , GUEST , InsertObjects , JWT , ReadOptions , UpdateValue , WHOISME , WriteOptions ,
27
27
} from './client.js' ;
28
+ import { CyberClient } from '@cybercongress/cyber-js' ;
29
+ import _m0 from "protobufjs/minimal" ;
28
30
29
31
const log = debug . extend ( 'log' ) ;
30
32
const error = debug . extend ( 'error' ) ;
@@ -34,24 +36,182 @@ corePckg.data.filter(l => !!l.type).forEach((l, i) => {
34
36
corePckgIds [ l . id ] = i + 1 ;
35
37
} ) ;
36
38
37
- export async function generateCyberInDeepClient ( options : any ) : Promise < CyberDeepClient < Link < Id > > > {
38
- return new CyberDeepClient ( options ) ;
39
+ export interface Models {
40
+ [ model : string ] : {
41
+ out : { [ field : string ] : string } ;
42
+ in : { [ model : string ] : { [ field : string ] : true } } ;
43
+ } ;
44
+ } ;
45
+ export const model = (
46
+ name : string ,
47
+ fields : { [ field : string ] : string } ,
48
+ models : Models = { } ,
49
+ ) => {
50
+ const th = models [ name ] = {
51
+ out : fields ,
52
+ in : { } ,
53
+ } ;
54
+ for ( const f in fields ) {
55
+ if ( models [ fields [ f ] ] ) {
56
+ models [ fields [ f ] ] . in [ name ] = models [ fields [ f ] ] . in [ name ] || { } ;
57
+ models [ fields [ f ] ] . in [ name ] [ f ] = true ;
58
+ }
59
+ }
60
+ for ( const m in models ) {
61
+ for ( const f in models [ m ] . out ) {
62
+ if ( models [ m ] . out [ f ] === name ) {
63
+ th . in [ m ] = th . in [ m ] || { } ;
64
+ th . in [ m ] [ f ] = true ;
65
+ }
66
+ }
67
+ }
68
+ } ;
69
+
70
+ export interface Schemas {
71
+ byType ?: { [ type : string ] : {
72
+ from : string ;
73
+ to : string ;
74
+ get ?: ( deep , id ) => Promise < any > ;
75
+ } } ;
76
+ byFrom ?: { [ type : string ] : string [ ] } ;
77
+ byTo ?: { [ type : string ] : string [ ] } ;
78
+ }
79
+
80
+ export const schema = ( type , from = '' , to = '' , schemas : Schemas = { } ) => {
81
+ if ( ! schemas . byType ) schemas . byType = { } ;
82
+ if ( ! schemas . byFrom ) schemas . byFrom = { } ;
83
+ if ( ! schemas . byTo ) schemas . byTo = { } ;
84
+ schemas . byType [ type ] = { from, to } ;
85
+ if ( from ) {
86
+ schemas . byFrom [ from ] = schemas . byFrom [ from ] || [ ] ;
87
+ schemas . byFrom [ from ] . push ( type ) ;
88
+ }
89
+ if ( to ) {
90
+ schemas . byTo [ to ] = schemas . byTo [ to ] || [ ] ;
91
+ schemas . byTo [ to ] . push ( type ) ;
92
+ }
93
+ } ;
94
+
95
+ // model('account', {});
96
+ // model('tx', {
97
+ // 'coin_received.receiver': 'account',
98
+ // 'coin_spent.spender': 'account',
99
+ // 'message.sender': 'account',
100
+ // 'transfer.recipient': 'account',
101
+ // 'transfer.sender': 'account',
102
+ // });
103
+ // schema('tx', '', '');
104
+ // schema('txReceiver', 'tx', 'account');
105
+ // schema('txSender', 'tx', 'account');
106
+
107
+ export interface CONFIG {
108
+ "CYBER_CONGRESS_ADDRESS" : string ;
109
+ "DIVISOR_CYBER_G" : number ;
110
+ "HYDROGEN" : string ;
111
+ "CHAIN_ID" : string ;
112
+ "DENOM_CYBER" : string ;
113
+ "DENOM_LIQUID_TOKEN" : string ;
114
+ "DENOM_CYBER_G" : string ;
115
+ "CYBER_NODE_URL_API" : string ;
116
+ "CYBER_WEBSOCKET_URL" : string ;
117
+ "CYBER_NODE_URL_LCD" : string ;
118
+ "CYBER_INDEX_HTTPS" : string ;
119
+ "CYBER_INDEX_WEBSOCKET" : string ;
120
+ "BECH32_PREFIX_ACC_ADDR_CYBER" : string ;
121
+ "BECH32_PREFIX_ACC_ADDR_CYBERVALOPER" : string ;
122
+ "MEMO_KEPLR" : string ;
123
+ "CYBER_GATEWAY" : string ;
124
+ } ;
125
+
126
+ export async function generateCyberDeepClient ( options : {
127
+ config : CONFIG ;
128
+ } ) : Promise < CyberDeepClient < Link < string > > > {
129
+ const cyberClient = await CyberClient . connect ( options . config . CYBER_NODE_URL_API ) ;
130
+ const schemas = { } ;
131
+ const models = { } ;
132
+ model ( 'account' , { } , models ) ;
133
+ model ( 'tx' , {
134
+ 'coin_received.receiver' : 'account' ,
135
+ 'coin_spent.spender' : 'account' ,
136
+ 'message.sender' : 'account' ,
137
+ 'transfer.recipient' : 'account' ,
138
+ 'transfer.sender' : 'account' ,
139
+ } , models ) ;
140
+ schema ( 'tx' , '' , '' , schemas ) ;
141
+ schema ( 'txReceiver' , 'tx' , 'account' , schemas ) ;
142
+ schema ( 'txSender' , 'tx' , 'account' , schemas ) ;
143
+ return new CyberDeepClient ( {
144
+ cyberClient,
145
+ config : options . config ,
146
+ schemas, models,
147
+ } ) ;
39
148
}
40
149
41
150
export interface CyberDeepClientInstance < L extends Link < Id > = Link < Id > > extends DeepClientInstance < L > {
42
151
}
43
152
44
- export class CyberDeepClient < L extends Link < Id > = Link < Id > > extends DeepClient < L > implements CyberDeepClientInstance < L > {
153
+ export interface CyberDeepClientOptions < L extends Link < Id > > extends DeepClientOptions < L > {
154
+ cyberClient : CyberClient ;
155
+ config : CONFIG ;
156
+
157
+ schemas ?: Schemas ;
158
+ models ?: Models ;
159
+ }
160
+
161
+ export class CyberDeepClient < L extends Link < string > = Link < string > > extends DeepClient < L > implements CyberDeepClientInstance < L > {
45
162
static resolveDependency ?: ( path : string ) => Promise < any >
46
163
164
+ cyberClient : CyberClient ;
165
+ config : CONFIG ;
166
+
167
+ accountPrefix : string ;
168
+
169
+ _byId : { [ id : string ] : any } = { } ;
170
+
171
+ schemas : Schemas ;
172
+ models : Models ;
173
+
47
174
// @ts -ignore
48
- constructor ( options : DeepClientOptions < L > ) {
49
- super ( options ) ;
175
+ constructor ( options : CyberDeepClientOptions < L > ) {
176
+ super ( {
177
+ apolloClient : generateApolloClient ( {
178
+ path : options . config . CYBER_INDEX_HTTPS . slice ( 8 ) ,
179
+ ssl : true ,
180
+ token : ''
181
+ } ) ,
182
+ } ) ;
183
+ this . cyberClient = options . cyberClient ;
184
+ this . config = options . config ;
185
+
186
+ this . accountPrefix = this . config . BECH32_PREFIX_ACC_ADDR_CYBER ;
187
+ this . schemas = options . schemas ;
188
+ this . models = options . models ;
50
189
}
51
190
52
- async select < TTable extends 'links' | 'numbers' | 'strings' | 'objects' | 'can' | 'selectors' | 'tree' | 'handlers' , LL = L > ( exp : Exp < TTable > , options ?: ReadOptions < TTable > ) : Promise < DeepClientResult < LL [ ] > > {
53
- throw new Error ( 'not implemented' ) ;
54
- } ;
191
+ // async select<TTable extends 'links'|'numbers'|'strings'|'objects'|'can'|'selectors'|'tree'|'handlers', LL = L>(exp: Exp<TTable>, options?: ReadOptions<TTable>): Promise<DeepClientResult<LL[]>> {
192
+ // let q = {};
193
+ // if (typeof(exp) === 'string') {
194
+ // if (exp.slice(0, this.accountPrefix.length) === this.accountPrefix) {
195
+ // return {
196
+ // data: [{ id: exp, type_id: 'account' } as LL],
197
+ // loading: false,
198
+ // networkStatus: 7,
199
+ // };
200
+ // }
201
+ // } else if (typeof(exp) === 'number') {
202
+ // throw new Error('not implemented');
203
+ // } else q = exp;
204
+ // const level = async (prev, exp) => {
205
+ // if (!exp.type_id) {
206
+ // throw new Error('!type_id');
207
+ // }
208
+ // if (exp.to_id && exp.type_id === 'tx') {
209
+ // await this.cyberClient.getTx(exp.to_id);
210
+ // }
211
+ // };
212
+ // await level(undefined, exp);
213
+ // throw new Error('not implemented');
214
+ // };
55
215
56
216
/**
57
217
* deep.subscribe
0 commit comments