@aboutcircles/sdk-rpc 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/README.md +169 -0
  2. package/dist/client.d.ts +58 -0
  3. package/dist/client.d.ts.map +1 -0
  4. package/dist/client.js +194 -0
  5. package/dist/errors.d.ts +44 -0
  6. package/dist/errors.d.ts.map +1 -0
  7. package/dist/errors.js +63 -0
  8. package/dist/events/index.d.ts +8 -0
  9. package/dist/events/index.d.ts.map +1 -0
  10. package/dist/events/index.js +8 -0
  11. package/dist/events/observable.d.ts +23 -0
  12. package/dist/events/observable.d.ts.map +1 -0
  13. package/dist/events/observable.js +37 -0
  14. package/dist/events/parser.d.ts +10 -0
  15. package/dist/events/parser.d.ts.map +1 -0
  16. package/dist/events/parser.js +103 -0
  17. package/dist/events/types.d.ts +7 -0
  18. package/dist/events/types.d.ts.map +1 -0
  19. package/dist/events/types.js +11 -0
  20. package/dist/index.d.ts +12 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +2654 -0
  23. package/dist/methods/avatar.d.ts +51 -0
  24. package/dist/methods/avatar.d.ts.map +1 -0
  25. package/dist/methods/avatar.js +64 -0
  26. package/dist/methods/balance.d.ts +37 -0
  27. package/dist/methods/balance.d.ts.map +1 -0
  28. package/dist/methods/balance.js +50 -0
  29. package/dist/methods/group.d.ts +145 -0
  30. package/dist/methods/group.d.ts.map +1 -0
  31. package/dist/methods/group.js +380 -0
  32. package/dist/methods/index.d.ts +11 -0
  33. package/dist/methods/index.d.ts.map +1 -0
  34. package/dist/methods/index.js +10 -0
  35. package/dist/methods/invitation.d.ts +61 -0
  36. package/dist/methods/invitation.d.ts.map +1 -0
  37. package/dist/methods/invitation.js +230 -0
  38. package/dist/methods/pathfinder.d.ts +41 -0
  39. package/dist/methods/pathfinder.d.ts.map +1 -0
  40. package/dist/methods/pathfinder.js +53 -0
  41. package/dist/methods/profile.d.ts +109 -0
  42. package/dist/methods/profile.d.ts.map +1 -0
  43. package/dist/methods/profile.js +166 -0
  44. package/dist/methods/query.d.ts +79 -0
  45. package/dist/methods/query.d.ts.map +1 -0
  46. package/dist/methods/query.js +87 -0
  47. package/dist/methods/token.d.ts +61 -0
  48. package/dist/methods/token.d.ts.map +1 -0
  49. package/dist/methods/token.js +99 -0
  50. package/dist/methods/transaction.d.ts +41 -0
  51. package/dist/methods/transaction.d.ts.map +1 -0
  52. package/dist/methods/transaction.js +111 -0
  53. package/dist/methods/trust.d.ts +114 -0
  54. package/dist/methods/trust.d.ts.map +1 -0
  55. package/dist/methods/trust.js +245 -0
  56. package/dist/pagedQuery.d.ts +106 -0
  57. package/dist/pagedQuery.d.ts.map +1 -0
  58. package/dist/pagedQuery.js +254 -0
  59. package/dist/rpc.d.ts +61 -0
  60. package/dist/rpc.d.ts.map +1 -0
  61. package/dist/rpc.js +76 -0
  62. package/dist/types.d.ts +82 -0
  63. package/dist/types.d.ts.map +1 -0
  64. package/dist/types.js +1 -0
  65. package/dist/utils.d.ts +27 -0
  66. package/dist/utils.d.ts.map +1 -0
  67. package/dist/utils.js +111 -0
  68. package/package.json +32 -0
@@ -0,0 +1,230 @@
1
+ import { normalizeAddress, checksumAddresses } from '../utils';
2
+ /**
3
+ * Invitation RPC methods
4
+ */
5
+ export class InvitationMethods {
6
+ client;
7
+ constructor(client) {
8
+ this.client = client;
9
+ }
10
+ transformQueryResponse(response) {
11
+ const { columns, rows } = response;
12
+ return rows.map((row) => {
13
+ const obj = {};
14
+ columns.forEach((col, index) => {
15
+ obj[col] = row[index];
16
+ });
17
+ return obj;
18
+ });
19
+ }
20
+ /**
21
+ * Get the avatar that invited a specific avatar
22
+ *
23
+ * @param address - The address of the invited avatar
24
+ * @returns The address of the inviting avatar or undefined if not found
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * const inviter = await rpc.invitation.getInvitedBy('0xde374ece6fa50e781e81aac78e811b33d16912c7');
29
+ * console.log(inviter); // '0x...'
30
+ * ```
31
+ */
32
+ async getInvitedBy(address) {
33
+ const normalized = normalizeAddress(address);
34
+ const results = await this.client.call('circles_query', [
35
+ {
36
+ Namespace: 'CrcV2',
37
+ Table: 'RegisterHuman',
38
+ Columns: ['inviter'],
39
+ Filter: [
40
+ {
41
+ Type: 'FilterPredicate',
42
+ FilterType: 'Equals',
43
+ Column: 'avatar',
44
+ Value: normalized,
45
+ },
46
+ ],
47
+ Order: [
48
+ {
49
+ Column: 'blockNumber',
50
+ SortOrder: 'DESC',
51
+ },
52
+ ],
53
+ Limit: 1,
54
+ },
55
+ ]);
56
+ if (results.length > 0) {
57
+ return checksumAddresses(results[0].inviter);
58
+ }
59
+ return undefined;
60
+ }
61
+ /**
62
+ * Get the list of avatars who have invited this avatar
63
+ * Checks v2 trust relations and validates that inviters have enough balance
64
+ *
65
+ * @param address - The address to check for invitations
66
+ * @returns Array of avatar info for valid inviters
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * const invitations = await rpc.invitation.getInvitations('0xde374ece6fa50e781e81aac78e811b33d16912c7');
71
+ * console.log(invitations); // Array of AvatarInfo
72
+ * ```
73
+ */
74
+ async getInvitations(address) {
75
+ const normalized = normalizeAddress(address);
76
+ const MIN_TOKENS_REQUIRED = 96;
77
+ // Check if the avatar is still on v1
78
+ const avatarInfoResults = await this.client.call('circles_getAvatarInfoBatch', [[normalized]]);
79
+ const avatarInfo = avatarInfoResults.length > 0 ? avatarInfoResults[0] : undefined;
80
+ if (avatarInfo?.version === 2) {
81
+ // Already on v2, no invitations needed
82
+ return [];
83
+ }
84
+ // Get trust relations where others trust this avatar
85
+ const response = await this.client.call('circles_query', [
86
+ {
87
+ Namespace: 'V_Crc',
88
+ Table: 'TrustRelations',
89
+ Columns: ['truster', 'trustee'],
90
+ Filter: [
91
+ {
92
+ Type: 'Conjunction',
93
+ ConjunctionType: 'And',
94
+ Predicates: [
95
+ {
96
+ Type: 'FilterPredicate',
97
+ FilterType: 'Equals',
98
+ Column: 'version',
99
+ Value: 2,
100
+ },
101
+ {
102
+ Type: 'FilterPredicate',
103
+ FilterType: 'Equals',
104
+ Column: 'trustee',
105
+ Value: normalized,
106
+ },
107
+ ],
108
+ },
109
+ ],
110
+ Order: [],
111
+ },
112
+ ]);
113
+ const trustRelations = this.transformQueryResponse(response);
114
+ const v2Trusters = trustRelations.map((r) => r.truster);
115
+ if (v2Trusters.length === 0) {
116
+ return [];
117
+ }
118
+ // Get avatar info for all trusters
119
+ const trusterInfos = await this.client.call('circles_getAvatarInfoBatch', [v2Trusters]);
120
+ const humanInviters = [];
121
+ for (const trusterInfo of trusterInfos) {
122
+ // Only humans can invite other humans
123
+ if (!trusterInfo?.isHuman) {
124
+ continue;
125
+ }
126
+ // Check if the inviter has enough tokens
127
+ const balances = await this.client.call('circles_getTokenBalances', [trusterInfo.avatar]);
128
+ const inviterOwnToken = balances.find((b) => normalizeAddress(b.tokenAddress) === normalizeAddress(trusterInfo.avatar));
129
+ if (inviterOwnToken && inviterOwnToken.circles >= MIN_TOKENS_REQUIRED) {
130
+ humanInviters.push(trusterInfo);
131
+ }
132
+ }
133
+ return checksumAddresses(humanInviters);
134
+ }
135
+ /**
136
+ * Get the list of accounts that were invited by a specific avatar
137
+ *
138
+ * @param address - The address of the inviter
139
+ * @param accepted - If true, returns accepted invitations; if false, returns pending invitations
140
+ * @returns Array of invited addresses
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * // Get accepted invitations
145
+ * const accepted = await rpc.invitation.getInvitationsFrom(
146
+ * '0xde374ece6fa50e781e81aac78e811b33d16912c7',
147
+ * true
148
+ * );
149
+ *
150
+ * // Get pending invitations
151
+ * const pending = await rpc.invitation.getInvitationsFrom(
152
+ * '0xde374ece6fa50e781e81aac78e811b33d16912c7',
153
+ * false
154
+ * );
155
+ * ```
156
+ */
157
+ async getInvitationsFrom(address, accepted = false) {
158
+ const normalized = normalizeAddress(address);
159
+ if (accepted) {
160
+ // Query for accounts that have registered using this avatar as inviter
161
+ const response = await this.client.call('circles_query', [
162
+ {
163
+ Namespace: 'CrcV2',
164
+ Table: 'RegisterHuman',
165
+ Columns: ['avatar'],
166
+ Filter: [
167
+ {
168
+ Type: 'FilterPredicate',
169
+ FilterType: 'Equals',
170
+ Column: 'inviter',
171
+ Value: normalized,
172
+ },
173
+ ],
174
+ Order: [
175
+ {
176
+ Column: 'blockNumber',
177
+ SortOrder: 'DESC',
178
+ },
179
+ ],
180
+ },
181
+ ]);
182
+ const results = this.transformQueryResponse(response);
183
+ const avatars = results.map((r) => r.avatar);
184
+ return checksumAddresses(avatars);
185
+ }
186
+ else {
187
+ // Find accounts that this avatar trusts without mutual trust
188
+ const response = await this.client.call('circles_query', [
189
+ {
190
+ Namespace: 'V_Crc',
191
+ Table: 'TrustRelations',
192
+ Columns: ['trustee', 'truster'],
193
+ Filter: [
194
+ {
195
+ Type: 'Conjunction',
196
+ ConjunctionType: 'And',
197
+ Predicates: [
198
+ {
199
+ Type: 'FilterPredicate',
200
+ FilterType: 'Equals',
201
+ Column: 'version',
202
+ Value: 2,
203
+ },
204
+ {
205
+ Type: 'FilterPredicate',
206
+ FilterType: 'Equals',
207
+ Column: 'truster',
208
+ Value: normalized,
209
+ },
210
+ ],
211
+ },
212
+ ],
213
+ Order: [],
214
+ },
215
+ ]);
216
+ const trustRelations = this.transformQueryResponse(response);
217
+ const v2Trusted = trustRelations.map((r) => r.trustee);
218
+ if (v2Trusted.length === 0) {
219
+ return [];
220
+ }
221
+ // Get avatar info for trusted accounts
222
+ const trustedAvatarsInfo = await this.client.call('circles_getAvatarInfoBatch', [v2Trusted]);
223
+ // Create a Set of registered avatars (filter out null values) - normalize for comparison
224
+ const registeredAvatarsSet = new Set(trustedAvatarsInfo.filter((a) => a !== null).map((a) => normalizeAddress(a.avatar)));
225
+ // Return only unregistered accounts (pending invitations)
226
+ const pending = v2Trusted.filter((addr) => !registeredAvatarsSet.has(normalizeAddress(addr)));
227
+ return checksumAddresses(pending);
228
+ }
229
+ }
230
+ }
@@ -0,0 +1,41 @@
1
+ import type { RpcClient } from '../client';
2
+ import type { FindPathParams, PathfindingResult } from '@aboutcircles/sdk-types';
3
+ /**
4
+ * Circles V1 and V2 balance and pathfinding methods
5
+ */
6
+ export declare class PathfinderMethods {
7
+ private client;
8
+ constructor(client: RpcClient);
9
+ /**
10
+ * Calculate a path between two addresses with a target flow
11
+ *
12
+ * @param params - Path finding parameters
13
+ * @returns The computed path with transfers (amounts as bigint)
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const path = await rpc.pathfinder.findPath({
18
+ * from: '0x749c930256b47049cb65adcd7c25e72d5de44b3b',
19
+ * to: '0xde374ece6fa50e781e81aac78e811b33d16912c7',
20
+ * targetFlow: 99999999999999999999999999999999999n
21
+ * });
22
+ * ```
23
+ */
24
+ findPath(params: FindPathParams): Promise<PathfindingResult>;
25
+ /**
26
+ * Find the maximum flow between two addresses
27
+ *
28
+ * @param params - Path finding parameters (without targetFlow)
29
+ * @returns The maximum flow as bigint
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * const maxFlow = await rpc.pathfinder.findMaxFlow({
34
+ * from: '0x749c930256b47049cb65adcd7c25e72d5de44b3b',
35
+ * to: '0xde374ece6fa50e781e81aac78e811b33d16912c7'
36
+ * });
37
+ * ```
38
+ */
39
+ findMaxFlow(params: Omit<FindPathParams, 'targetFlow'>): Promise<bigint>;
40
+ }
41
+ //# sourceMappingURL=pathfinder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pathfinder.d.ts","sourceRoot":"","sources":["../../src/methods/pathfinder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAGjF;;GAEG;AACH,qBAAa,iBAAiB;IAChB,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,SAAS;IAErC;;;;;;;;;;;;;;OAcG;IACG,QAAQ,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAYlE;;;;;;;;;;;;;OAaG;IACG,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;CAQ/E"}
@@ -0,0 +1,53 @@
1
+ import { normalizeFindPathParams, parseStringsToBigInt, checksumAddresses } from '../utils';
2
+ /**
3
+ * Circles V1 and V2 balance and pathfinding methods
4
+ */
5
+ export class PathfinderMethods {
6
+ client;
7
+ constructor(client) {
8
+ this.client = client;
9
+ }
10
+ /**
11
+ * Calculate a path between two addresses with a target flow
12
+ *
13
+ * @param params - Path finding parameters
14
+ * @returns The computed path with transfers (amounts as bigint)
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const path = await rpc.pathfinder.findPath({
19
+ * from: '0x749c930256b47049cb65adcd7c25e72d5de44b3b',
20
+ * to: '0xde374ece6fa50e781e81aac78e811b33d16912c7',
21
+ * targetFlow: 99999999999999999999999999999999999n
22
+ * });
23
+ * ```
24
+ */
25
+ async findPath(params) {
26
+ const normalizedParams = normalizeFindPathParams(params);
27
+ const result = await this.client.call('circlesV2_findPath', [normalizedParams]);
28
+ const parsed = parseStringsToBigInt(result);
29
+ return checksumAddresses(parsed);
30
+ }
31
+ /**
32
+ * Find the maximum flow between two addresses
33
+ *
34
+ * @param params - Path finding parameters (without targetFlow)
35
+ * @returns The maximum flow as bigint
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * const maxFlow = await rpc.pathfinder.findMaxFlow({
40
+ * from: '0x749c930256b47049cb65adcd7c25e72d5de44b3b',
41
+ * to: '0xde374ece6fa50e781e81aac78e811b33d16912c7'
42
+ * });
43
+ * ```
44
+ */
45
+ async findMaxFlow(params) {
46
+ const targetFlow = 9999999999999999999999999999999999999n;
47
+ const path = await this.findPath({
48
+ ...params,
49
+ targetFlow
50
+ });
51
+ return BigInt(path.maxFlow);
52
+ }
53
+ }
@@ -0,0 +1,109 @@
1
+ import type { RpcClient } from '../client';
2
+ import type { Address, Profile } from '@aboutcircles/sdk-types';
3
+ import type { SearchResultProfile } from '../types';
4
+ /**
5
+ * Profile RPC methods
6
+ */
7
+ export declare class ProfileMethods {
8
+ private client;
9
+ constructor(client: RpcClient);
10
+ /**
11
+ * Get a profile by its CID
12
+ *
13
+ * @param cid - The CID of the profile
14
+ * @returns Profile information or null if not found
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const profile = await rpc.profile.getProfileByCid('Qmb2s3hjxXXcFqWvDDSPCd1fXXa9gcFJd8bzdZNNAvkq9W');
19
+ * console.log(profile);
20
+ * ```
21
+ */
22
+ getProfileByCid(cid: string): Promise<Profile | null>;
23
+ /**
24
+ * Get many profiles by CID in batch
25
+ *
26
+ * @param cids - Array of CIDs (null values are allowed in the array)
27
+ * @returns Array of profiles (null for not found)
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const profiles = await rpc.profile.getProfileByCidBatch([
32
+ * 'Qmb2s3hjxXXcFqWvDDSPCd1fXXa9gcFJd8bzdZNNAvkq9W',
33
+ * null,
34
+ * 'QmZuR1Jkhs9RLXVY28eTTRSnqbxLTBSoggp18Yde858xCM'
35
+ * ]);
36
+ * ```
37
+ */
38
+ getProfileByCidBatch(cids: (string | null)[]): Promise<(Profile | null)[]>;
39
+ /**
40
+ * Query the profile for an avatar address
41
+ *
42
+ * @param address - The avatar address
43
+ * @returns Profile information or null if not found
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const profile = await rpc.profile.getProfileByAddress('0xc3a1428c04c426cdf513c6fc8e09f55ddaf50cd7');
48
+ * console.log(profile);
49
+ * ```
50
+ */
51
+ getProfileByAddress(address: Address): Promise<Profile | null>;
52
+ /**
53
+ * Query profiles by address in batch
54
+ *
55
+ * @param addresses - Array of addresses (null values are allowed in the array)
56
+ * @returns Array of profiles (null for not found)
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * const profiles = await rpc.profile.getProfileByAddressBatch([
61
+ * '0xc3a1428c04c426cdf513c6fc8e09f55ddaf50cd7',
62
+ * null,
63
+ * '0xf712d3b31de494b5c0ea51a6a407460ca66b12e8'
64
+ * ]);
65
+ * ```
66
+ */
67
+ getProfileByAddressBatch(addresses: (Address | null)[]): Promise<(Profile | null)[]>;
68
+ /**
69
+ * Search profiles by name, description or address
70
+ *
71
+ * @param query - Search query string
72
+ * @param limit - Maximum number of results (default: 10)
73
+ * @param offset - Offset for pagination (default: 0)
74
+ * @param avatarTypes - Optional array of avatar types to filter by (e.g., ['CrcV2_RegisterHuman', 'CrcV2_RegisterGroup'])
75
+ * @returns Array of matching profiles
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * const results = await rpc.profile.searchProfiles('alice', 10, 0);
80
+ * console.log(results);
81
+ *
82
+ * // Search only humans
83
+ * const humans = await rpc.profile.searchProfiles('alice', 10, 0, ['CrcV2_RegisterHuman']);
84
+ * ```
85
+ */
86
+ searchProfiles(query: string, limit?: number, offset?: number, avatarTypes?: string[]): Promise<SearchResultProfile[]>;
87
+ /**
88
+ * Search profiles by address or username
89
+ * If the query is a valid address, it will search by address first,
90
+ * otherwise it will search by name/description
91
+ *
92
+ * @param query - Search query (address or username)
93
+ * @param limit - Maximum number of results (default: 10)
94
+ * @param offset - Offset for pagination (default: 0)
95
+ * @param avatarTypes - Optional array of avatar types to filter by
96
+ * @returns Array of matching profiles, with exact address match (if valid) at the top
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * // Search by username
101
+ * const results = await rpc.profile.searchByAddressOrName('alice', 20);
102
+ *
103
+ * // Search by address
104
+ * const results = await rpc.profile.searchByAddressOrName('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb', 20);
105
+ * ```
106
+ */
107
+ searchByAddressOrName(query: string, limit?: number, offset?: number, avatarTypes?: string[]): Promise<SearchResultProfile[]>;
108
+ }
109
+ //# sourceMappingURL=profile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"profile.d.ts","sourceRoot":"","sources":["../../src/methods/profile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAGpD;;GAEG;AACH,qBAAa,cAAc;IACb,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,SAAS;IAErC;;;;;;;;;;;OAWG;IACG,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAI3D;;;;;;;;;;;;;;OAcG;IACG,oBAAoB,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,CAAC,OAAO,GAAG,IAAI,CAAC,EAAE,CAAC;IAOhF;;;;;;;;;;;OAWG;IACG,mBAAmB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;IAMpE;;;;;;;;;;;;;;OAcG;IACG,wBAAwB,CAAC,SAAS,EAAE,CAAC,OAAO,GAAG,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,CAAC,OAAO,GAAG,IAAI,CAAC,EAAE,CAAC;IAO1F;;;;;;;;;;;;;;;;;OAiBG;IACG,cAAc,CAClB,KAAK,EAAE,MAAM,EACb,KAAK,GAAE,MAAW,EAClB,MAAM,GAAE,MAAU,EAClB,WAAW,CAAC,EAAE,MAAM,EAAE,GACrB,OAAO,CAAC,mBAAmB,EAAE,CAAC;IAYjC;;;;;;;;;;;;;;;;;;;OAmBG;IACG,qBAAqB,CACzB,KAAK,EAAE,MAAM,EACb,KAAK,GAAE,MAAW,EAClB,MAAM,GAAE,MAAU,EAClB,WAAW,CAAC,EAAE,MAAM,EAAE,GACrB,OAAO,CAAC,mBAAmB,EAAE,CAAC;CA8ClC"}
@@ -0,0 +1,166 @@
1
+ import { normalizeAddress } from '../utils';
2
+ /**
3
+ * Profile RPC methods
4
+ */
5
+ export class ProfileMethods {
6
+ client;
7
+ constructor(client) {
8
+ this.client = client;
9
+ }
10
+ /**
11
+ * Get a profile by its CID
12
+ *
13
+ * @param cid - The CID of the profile
14
+ * @returns Profile information or null if not found
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const profile = await rpc.profile.getProfileByCid('Qmb2s3hjxXXcFqWvDDSPCd1fXXa9gcFJd8bzdZNNAvkq9W');
19
+ * console.log(profile);
20
+ * ```
21
+ */
22
+ async getProfileByCid(cid) {
23
+ return this.client.call('circles_getProfileByCid', [cid]);
24
+ }
25
+ /**
26
+ * Get many profiles by CID in batch
27
+ *
28
+ * @param cids - Array of CIDs (null values are allowed in the array)
29
+ * @returns Array of profiles (null for not found)
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * const profiles = await rpc.profile.getProfileByCidBatch([
34
+ * 'Qmb2s3hjxXXcFqWvDDSPCd1fXXa9gcFJd8bzdZNNAvkq9W',
35
+ * null,
36
+ * 'QmZuR1Jkhs9RLXVY28eTTRSnqbxLTBSoggp18Yde858xCM'
37
+ * ]);
38
+ * ```
39
+ */
40
+ async getProfileByCidBatch(cids) {
41
+ return this.client.call('circles_getProfileByCidBatch', [cids]);
42
+ }
43
+ /**
44
+ * Query the profile for an avatar address
45
+ *
46
+ * @param address - The avatar address
47
+ * @returns Profile information or null if not found
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * const profile = await rpc.profile.getProfileByAddress('0xc3a1428c04c426cdf513c6fc8e09f55ddaf50cd7');
52
+ * console.log(profile);
53
+ * ```
54
+ */
55
+ async getProfileByAddress(address) {
56
+ return this.client.call('circles_getProfileByAddress', [
57
+ normalizeAddress(address),
58
+ ]);
59
+ }
60
+ /**
61
+ * Query profiles by address in batch
62
+ *
63
+ * @param addresses - Array of addresses (null values are allowed in the array)
64
+ * @returns Array of profiles (null for not found)
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const profiles = await rpc.profile.getProfileByAddressBatch([
69
+ * '0xc3a1428c04c426cdf513c6fc8e09f55ddaf50cd7',
70
+ * null,
71
+ * '0xf712d3b31de494b5c0ea51a6a407460ca66b12e8'
72
+ * ]);
73
+ * ```
74
+ */
75
+ async getProfileByAddressBatch(addresses) {
76
+ return this.client.call('circles_getProfileByAddressBatch', [addresses.map((addr) => (addr === null ? null : normalizeAddress(addr)))]);
77
+ }
78
+ /**
79
+ * Search profiles by name, description or address
80
+ *
81
+ * @param query - Search query string
82
+ * @param limit - Maximum number of results (default: 10)
83
+ * @param offset - Offset for pagination (default: 0)
84
+ * @param avatarTypes - Optional array of avatar types to filter by (e.g., ['CrcV2_RegisterHuman', 'CrcV2_RegisterGroup'])
85
+ * @returns Array of matching profiles
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * const results = await rpc.profile.searchProfiles('alice', 10, 0);
90
+ * console.log(results);
91
+ *
92
+ * // Search only humans
93
+ * const humans = await rpc.profile.searchProfiles('alice', 10, 0, ['CrcV2_RegisterHuman']);
94
+ * ```
95
+ */
96
+ async searchProfiles(query, limit = 10, offset = 0, avatarTypes) {
97
+ return this.client.call('circles_searchProfiles', [
98
+ query.toLowerCase(),
99
+ limit,
100
+ offset,
101
+ avatarTypes
102
+ ]);
103
+ }
104
+ /**
105
+ * Search profiles by address or username
106
+ * If the query is a valid address, it will search by address first,
107
+ * otherwise it will search by name/description
108
+ *
109
+ * @param query - Search query (address or username)
110
+ * @param limit - Maximum number of results (default: 10)
111
+ * @param offset - Offset for pagination (default: 0)
112
+ * @param avatarTypes - Optional array of avatar types to filter by
113
+ * @returns Array of matching profiles, with exact address match (if valid) at the top
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * // Search by username
118
+ * const results = await rpc.profile.searchByAddressOrName('alice', 20);
119
+ *
120
+ * // Search by address
121
+ * const results = await rpc.profile.searchByAddressOrName('0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb', 20);
122
+ * ```
123
+ */
124
+ async searchByAddressOrName(query, limit = 10, offset = 0, avatarTypes) {
125
+ const results = [];
126
+ // Check if query is a valid address
127
+ const isAddress = /^0x[a-fA-F0-9]{40}$/.test(query);
128
+ if (isAddress) {
129
+ // Try to get profile by address first
130
+ try {
131
+ const profile = await this.getProfileByAddress(query);
132
+ if (profile) {
133
+ // Convert Profile to SearchResultProfile by adding address
134
+ const searchResult = {
135
+ ...profile,
136
+ address: query
137
+ };
138
+ // Check if profile matches avatar type filter
139
+ if (!avatarTypes || !searchResult.avatarType || avatarTypes.includes(searchResult.avatarType)) {
140
+ results.push(searchResult);
141
+ }
142
+ }
143
+ }
144
+ catch (error) {
145
+ console.warn('Failed to get profile by address:', error);
146
+ }
147
+ }
148
+ // Always search by text as well
149
+ try {
150
+ const textResults = await this.searchProfiles(query, limit, offset, avatarTypes);
151
+ // If we already added an address match, filter it out from text results to avoid duplicates
152
+ if (isAddress && results.length > 0) {
153
+ const addressLower = query.toLowerCase();
154
+ const filteredResults = textResults.filter(p => p.address?.toLowerCase() !== addressLower);
155
+ results.push(...filteredResults);
156
+ }
157
+ else {
158
+ results.push(...textResults);
159
+ }
160
+ }
161
+ catch (error) {
162
+ console.warn('Failed to search profiles by text:', error);
163
+ }
164
+ return results.slice(0, limit);
165
+ }
166
+ }
@@ -0,0 +1,79 @@
1
+ import type { RpcClient } from '../client';
2
+ import type { QueryParams, TableInfo, EventType } from '@aboutcircles/sdk-types';
3
+ /**
4
+ * Query and table RPC methods
5
+ */
6
+ export declare class QueryMethods {
7
+ private client;
8
+ constructor(client: RpcClient);
9
+ /**
10
+ * Query tables with filters
11
+ *
12
+ * @param params - Query parameters including namespace, table, columns, filters, and ordering
13
+ * @returns Array of query results
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const results = await rpc.query.query({
18
+ * Namespace: 'V_CrcV2',
19
+ * Table: 'TrustRelations',
20
+ * Columns: [],
21
+ * Filter: [{
22
+ * Type: 'Conjunction',
23
+ * ConjunctionType: 'Or',
24
+ * Predicates: [
25
+ * {
26
+ * Type: 'FilterPredicate',
27
+ * FilterType: 'Equals',
28
+ * Column: 'truster',
29
+ * Value: '0xae3a29a9ff24d0e936a5579bae5c4179c4dff565'
30
+ * },
31
+ * {
32
+ * Type: 'FilterPredicate',
33
+ * FilterType: 'Equals',
34
+ * Column: 'trustee',
35
+ * Value: '0xae3a29a9ff24d0e936a5579bae5c4179c4dff565'
36
+ * }
37
+ * ]
38
+ * }],
39
+ * Order: []
40
+ * });
41
+ * ```
42
+ */
43
+ query<T = unknown>(params: QueryParams): Promise<T[]>;
44
+ /**
45
+ * Return all available namespaces and tables which can be queried
46
+ *
47
+ * @returns Array of table information
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * const tables = await rpc.query.tables();
52
+ * console.log(tables);
53
+ * ```
54
+ */
55
+ tables(): Promise<TableInfo[]>;
56
+ /**
57
+ * Query events of specific types within a block range
58
+ *
59
+ * @param fromBlock - Starting block number (null for genesis)
60
+ * @param toBlock - Ending block number (null for latest)
61
+ * @param eventTypes - Array of event types to filter (null for all)
62
+ * @param address - Optional address filter
63
+ * @param includeTransactionData - Whether to include transaction data
64
+ * @returns Array of events
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const events = await rpc.query.events(
69
+ * 38000000,
70
+ * null,
71
+ * ['CrcV1_Trust'],
72
+ * null,
73
+ * false
74
+ * );
75
+ * ```
76
+ */
77
+ events<T = unknown>(fromBlock: number | null, toBlock: number | null, eventTypes?: EventType[] | null, address?: string | null, includeTransactionData?: boolean): Promise<T[]>;
78
+ }
79
+ //# sourceMappingURL=query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../../src/methods/query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGjF;;GAEG;AACH,qBAAa,YAAY;IACX,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,SAAS;IAErC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACG,KAAK,CAAC,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAK3D;;;;;;;;;;OAUG;IACG,MAAM,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;IAIpC;;;;;;;;;;;;;;;;;;;;OAoBG;IACG,MAAM,CAAC,CAAC,GAAG,OAAO,EACtB,SAAS,EAAE,MAAM,GAAG,IAAI,EACxB,OAAO,EAAE,MAAM,GAAG,IAAI,EACtB,UAAU,GAAE,SAAS,EAAE,GAAG,IAAW,EACrC,OAAO,GAAE,MAAM,GAAG,IAAW,EAC7B,sBAAsB,GAAE,OAAe,GACtC,OAAO,CAAC,CAAC,EAAE,CAAC;CAOhB"}