@functionland/react-native-fula 1.41.0 → 1.43.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 (92) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +226 -226
  3. package/android/build.gradle +1 -1
  4. package/android/gradle.properties +5 -5
  5. package/android/src/main/AndroidManifest.xml +3 -3
  6. package/android/src/main/AndroidManifestNew.xml +2 -2
  7. package/android/src/main/java/land/fx/fula/ConfigRef.java +7 -7
  8. package/android/src/main/java/land/fx/fula/Cryptography.java +62 -62
  9. package/android/src/main/java/land/fx/fula/FulaModule.java +15 -0
  10. package/android/src/main/java/land/fx/fula/FulaPackage.java +32 -32
  11. package/android/src/main/java/land/fx/fula/SharedPreferenceHelper.java +65 -65
  12. package/android/src/main/java/land/fx/fula/StaticHelper.java +13 -13
  13. package/android/src/main/java/land/fx/fula/ThreadUtils.java +42 -42
  14. package/ios/Cryptography.swift +49 -49
  15. package/ios/Fula-Bridging-Header.h +3 -3
  16. package/ios/Fula.mm +170 -215
  17. package/ios/Fula.swift +1303 -1334
  18. package/ios/UserDataHelper.swift +143 -143
  19. package/lib/commonjs/index.js.map +1 -1
  20. package/lib/commonjs/interfaces/augment-api-errors.js.map +1 -1
  21. package/lib/commonjs/interfaces/augment-api-rpc.js.map +1 -1
  22. package/lib/commonjs/interfaces/augment-api-runtime.js.map +1 -1
  23. package/lib/commonjs/interfaces/augment-api.js.map +1 -1
  24. package/lib/commonjs/interfaces/augment-types.js.map +1 -1
  25. package/lib/commonjs/interfaces/fulaNativeModule.js.map +1 -1
  26. package/lib/commonjs/interfaces/index.js.map +1 -1
  27. package/lib/commonjs/interfaces/lookup.js +278 -278
  28. package/lib/commonjs/interfaces/lookup.js.map +1 -1
  29. package/lib/commonjs/interfaces/registry.js.map +1 -1
  30. package/lib/commonjs/interfaces/types-lookup.js.map +1 -1
  31. package/lib/commonjs/interfaces/types.js.map +1 -1
  32. package/lib/commonjs/protocols/blockchain.js +25 -2
  33. package/lib/commonjs/protocols/blockchain.js.map +1 -1
  34. package/lib/commonjs/protocols/chain-api.js +34 -13
  35. package/lib/commonjs/protocols/chain-api.js.map +1 -1
  36. package/lib/commonjs/protocols/fula.js +106 -106
  37. package/lib/commonjs/protocols/fula.js.map +1 -1
  38. package/lib/commonjs/protocols/fxblox.js +3 -3
  39. package/lib/commonjs/protocols/fxblox.js.map +1 -1
  40. package/lib/commonjs/types/blockchain.js.map +1 -1
  41. package/lib/commonjs/types/fxblox.js.map +1 -1
  42. package/lib/module/index.js.map +1 -1
  43. package/lib/module/interfaces/augment-api-errors.js.map +1 -1
  44. package/lib/module/interfaces/augment-api-rpc.js.map +1 -1
  45. package/lib/module/interfaces/augment-api-runtime.js.map +1 -1
  46. package/lib/module/interfaces/augment-api.js.map +1 -1
  47. package/lib/module/interfaces/augment-types.js.map +1 -1
  48. package/lib/module/interfaces/fulaNativeModule.js.map +1 -1
  49. package/lib/module/interfaces/index.js.map +1 -1
  50. package/lib/module/interfaces/lookup.js +278 -278
  51. package/lib/module/interfaces/lookup.js.map +1 -1
  52. package/lib/module/interfaces/registry.js.map +1 -1
  53. package/lib/module/interfaces/types-lookup.js.map +1 -1
  54. package/lib/module/interfaces/types.js.map +1 -1
  55. package/lib/module/protocols/blockchain.js +22 -0
  56. package/lib/module/protocols/blockchain.js.map +1 -1
  57. package/lib/module/protocols/chain-api.js +30 -11
  58. package/lib/module/protocols/chain-api.js.map +1 -1
  59. package/lib/module/protocols/fula.js +106 -106
  60. package/lib/module/protocols/fula.js.map +1 -1
  61. package/lib/module/protocols/fxblox.js +3 -3
  62. package/lib/module/protocols/fxblox.js.map +1 -1
  63. package/lib/module/types/blockchain.js.map +1 -1
  64. package/lib/module/types/fxblox.js.map +1 -1
  65. package/lib/typescript/interfaces/fulaNativeModule.d.ts +1 -0
  66. package/lib/typescript/interfaces/fulaNativeModule.d.ts.map +1 -1
  67. package/lib/typescript/protocols/blockchain.d.ts +1 -0
  68. package/lib/typescript/protocols/blockchain.d.ts.map +1 -1
  69. package/lib/typescript/protocols/chain-api.d.ts +4 -0
  70. package/lib/typescript/protocols/chain-api.d.ts.map +1 -1
  71. package/lib/typescript/types/blockchain.d.ts +5 -0
  72. package/lib/typescript/types/blockchain.d.ts.map +1 -1
  73. package/package.json +3 -2
  74. package/react-native-fula.podspec +47 -47
  75. package/src/index.tsx +4 -4
  76. package/src/interfaces/augment-api-errors.ts +449 -449
  77. package/src/interfaces/augment-api-rpc.ts +617 -617
  78. package/src/interfaces/augment-api-runtime.ts +224 -224
  79. package/src/interfaces/augment-api.ts +10 -10
  80. package/src/interfaces/augment-types.ts +1230 -1230
  81. package/src/interfaces/fulaNativeModule.ts +1 -0
  82. package/src/interfaces/index.ts +4 -4
  83. package/src/interfaces/lookup.ts +1889 -1889
  84. package/src/interfaces/registry.ts +152 -152
  85. package/src/interfaces/types-lookup.ts +2043 -2043
  86. package/src/interfaces/types.ts +3 -3
  87. package/src/protocols/blockchain.ts +26 -0
  88. package/src/protocols/chain-api.ts +308 -290
  89. package/src/protocols/fula.ts +427 -427
  90. package/src/protocols/fxblox.ts +70 -70
  91. package/src/types/blockchain.ts +6 -0
  92. package/src/types/fxblox.ts +13 -13
@@ -1,3 +1,3 @@
1
- // Auto-generated via `yarn polkadot-types-from-defs`, do not edit
2
- /* eslint-disable */
3
-
1
+ // Auto-generated via `yarn polkadot-types-from-defs`, do not edit
2
+ /* eslint-disable */
3
+
@@ -53,6 +53,32 @@ export const checkAccountExists = (
53
53
  return res1;
54
54
  };
55
55
 
56
+ /*
57
+ accountFund: This function takes an account argument, and returns a promise of an object that contains the account and a boolean exists flag. If exists is true, it means the account exists, otherwise, the account does not exist
58
+ */
59
+ export const accountFund = (
60
+ account: string
61
+ ): Promise<BType.AccountFundResponse> => {
62
+ console.log('accountFund in react-native started', account);
63
+ let res1 = Fula.accountFund(account)
64
+ .then((res) => {
65
+ try {
66
+ let jsonRes: BType.AccountFundResponse = JSON.parse(res);
67
+ return jsonRes;
68
+ } catch (e) {
69
+ try {
70
+ return JSON.parse(res);
71
+ } catch (e2) {
72
+ return res;
73
+ }
74
+ }
75
+ })
76
+ .catch((err) => {
77
+ return err;
78
+ });
79
+ return res1;
80
+ };
81
+
56
82
  /*
57
83
  createPool: This function takes two arguments: seed and poolName. The seed is used to identify the account that is creating the pool, and the poolName is the name of the pool being created. The function returns a promise of an object that contains the owner of the pool and the poolID of the created pool.
58
84
  */
@@ -1,290 +1,308 @@
1
- import { default as EventTypes } from '../interfaces/lookup';
2
-
3
- import { ApiPromise, WsProvider } from '@polkadot/api';
4
- import { Keyring } from '@polkadot/keyring';
5
- const { cryptoWaitReady } = require('@polkadot/util-crypto');
6
- import type * as BType from '../types/blockchain';
7
-
8
- const types = {
9
- FulaPoolPool: EventTypes.FulaPoolPool,
10
- };
11
-
12
- export const init = async (
13
- wsAddress: string = 'wss://node3.functionyard.fula.network'
14
- ): Promise<ApiPromise> => {
15
- const provider = new WsProvider(wsAddress);
16
- const api = await ApiPromise.create({ types, provider }).catch((err) => {
17
- console.log(err);
18
- return Promise.reject(err);
19
- });
20
- return api;
21
- };
22
-
23
- export const disconnectApi = async (api: ApiPromise): Promise<void> => {
24
- await api.disconnect();
25
- };
26
-
27
- /*function addDoubleSlashToSeed(seed: string): string {
28
- return seed.startsWith('//') ? seed : '//' + seed;
29
- }*/
30
-
31
- /*
32
- createManifest: This function batch uploads manifests
33
- */
34
- function serialize(obj: any): string {
35
- return JSON.stringify(obj);
36
- }
37
-
38
- function createManifest(
39
- cids: string[],
40
- poolId: number,
41
- replicationFactor: number = 4
42
- ): {
43
- manifest: string[]; // or string[]
44
- cids: string[]; // or string[]
45
- poolId: number[];
46
- replicationFactor: number[];
47
- } {
48
- const manifest_metadata = cids.map((cid) => ({
49
- job: {
50
- work: 'Storage',
51
- engine: 'IPFS',
52
- uri: cid,
53
- },
54
- }));
55
-
56
- // Serialize manifest_metadata to Uint8Array or string
57
- const serializedManifest = manifest_metadata.map((item) => serialize(item)); // Implement `serialize` accordingly
58
-
59
- // Serialize cids to Uint8Array or string
60
- const serializedCids = cids.map((cid) => serialize(cid)); // Implement `serialize` accordingly
61
-
62
- // Create arrays for `poolId` and `replicationFactor`
63
- const poolIds = new Array(cids.length).fill(poolId);
64
- const replicationFactors = new Array(cids.length).fill(replicationFactor);
65
-
66
- const batchUploadManifest = {
67
- manifest: serializedManifest,
68
- cids: serializedCids,
69
- poolId: poolIds,
70
- replicationFactor: replicationFactors,
71
- };
72
-
73
- return batchUploadManifest;
74
- }
75
-
76
- export const batchUploadManifest = async (
77
- api: ApiPromise | undefined,
78
- seed: string,
79
- cids_i: string[],
80
- poolId_i: number,
81
- replicationFactor_i: number = 4
82
- ): Promise<{ hash: string }> => {
83
- const { manifest, cids, poolId, replicationFactor } = createManifest(
84
- cids_i,
85
- poolId_i,
86
- replicationFactor_i
87
- );
88
-
89
- console.log('uploadManifest in react-native started');
90
- try {
91
- if (api === undefined) {
92
- api = await init();
93
- }
94
-
95
- // Simple transaction
96
- const keyring = new Keyring({ type: 'sr25519' });
97
- const userKey = keyring.addFromUri(seed, { name: 'account' }, 'sr25519');
98
- console.log(
99
- `${userKey.meta.name}: has address ${userKey.address} with publicKey [${userKey.publicKey}]`
100
- );
101
- if (api?.tx?.fula?.batchUploadManifest) {
102
- const submitExtrinsic = api.tx.fula.batchUploadManifest(
103
- manifest,
104
- cids,
105
- poolId,
106
- replicationFactor
107
- );
108
- let unsub: () => void; // Define a variable to hold the unsub function
109
-
110
- if (submitExtrinsic) {
111
- return new Promise<{ hash: string }>((resolve, reject) => {
112
- submitExtrinsic
113
- .signAndSend(userKey, { nonce: -1 }, ({ status }) => {
114
- if (status.isInBlock || status.isFinalized) {
115
- if (unsub) {
116
- unsub(); // Call unsub before resolving the promise
117
- }
118
- resolve({ hash: status.asInBlock.toString() });
119
- }
120
- })
121
- .then((unsubFn) => {
122
- unsub = unsubFn; // Store the unsub function once it becomes available
123
- })
124
- .catch((error) => {
125
- if (unsub) {
126
- unsub(); // Call unsub before rejecting the promise
127
- }
128
- console.log(':( transaction failed', error);
129
- reject(error);
130
- });
131
- });
132
- } else {
133
- return Promise.reject(new TypeError('submitExtrinsic not constructed'));
134
- }
135
- } else {
136
- return Promise.reject(new TypeError('api not initialized'));
137
- }
138
- } catch (err) {
139
- return Promise.reject(err);
140
- }
141
- };
142
-
143
- /*
144
- listPools: This function takes start index and length and returns a promise of an object that contains a list of pools. Each pool in the list contains the poolID, owner, poolName, parent, and participants of the pool
145
- */
146
- export const listPools = async (
147
- api: ApiPromise | undefined,
148
- start: number = 1,
149
- length: number = 10
150
- ): Promise<BType.PoolListResponse> => {
151
- console.log('listPools in react-native started');
152
- try {
153
- if (api === undefined) {
154
- api = await init();
155
- }
156
- // Type guard to assure TypeScript that api is not undefined
157
- if (!api?.query?.pool?.lastPoolId || !api?.query?.pool?.pools) {
158
- throw new Error('Failed to initialize api or api.query.pool');
159
- }
160
- const pools: BType.PoolListResponse = { pools: [] };
161
- const lastPoolId = await api.query.pool.lastPoolId();
162
- let finalReturnedId: number = Number(lastPoolId.toHuman());
163
- if (Number(lastPoolId.toHuman()) > start + length) {
164
- finalReturnedId = start + length;
165
- }
166
- for (let i = start; i <= finalReturnedId; i++) {
167
- const poolInfo = await api.query.pool.pools(i).catch((err) => {
168
- console.log(err);
169
- return Promise.reject(err);
170
- });
171
- if (poolInfo != null) {
172
- let formattedPoolInfo: BType.Pool = JSON.parse(
173
- JSON.stringify(poolInfo.toHuman())
174
- );
175
- formattedPoolInfo.poolID = i;
176
- pools.pools.push(formattedPoolInfo);
177
- }
178
- }
179
- return Promise.resolve(pools);
180
- } catch (err) {
181
- return Promise.reject(err);
182
- }
183
- };
184
-
185
- /*
186
- checkJoinRequest: This function takes poolId and AccontId and returns a promise of an object that contains request to the pools.
187
- */
188
- export const checkJoinRequest = async (
189
- api: ApiPromise | undefined,
190
- poolId: number,
191
- accountId: string
192
- ): Promise<BType.PoolRequest | null> => {
193
- console.log('checkJoinRequest in react-native started');
194
- try {
195
- if (api === undefined) {
196
- api = await init();
197
- }
198
- // Type guard to assure TypeScript that api is not undefined
199
- if (!api?.query?.pool?.poolRequests) {
200
- throw new Error('Failed to initialize api or api.query.pool');
201
- }
202
-
203
- const poolRequest = await api.query.pool.poolRequests(poolId, accountId);
204
-
205
- if (poolRequest != null) {
206
- let formattedPoolRequest: BType.PoolRequest = JSON.parse(
207
- JSON.stringify(poolRequest.toHuman())
208
- );
209
- return Promise.resolve(formattedPoolRequest);
210
- }
211
- return Promise.resolve(null);
212
- } catch (err) {
213
- return Promise.reject(err);
214
- }
215
- };
216
-
217
- export const getUserPool = async (
218
- api: ApiPromise | undefined,
219
- accountId: string
220
- ): Promise<BType.PoolUsers | null> => {
221
- console.log('GetUserPool in react-native started');
222
- try {
223
- if (api === undefined) {
224
- api = await init();
225
- }
226
- // Type guard to assure TypeScript that api is not undefined
227
- if (!api?.query?.pool?.users) {
228
- throw new Error(
229
- 'Failed to initialize api or api.query.pool or api.query.pool.users'
230
- );
231
- }
232
-
233
- const poolUsers = await api.query.pool.users(accountId);
234
-
235
- if (poolUsers != null) {
236
- let formattedPoolUsers: BType.PoolUsers = JSON.parse(
237
- JSON.stringify(poolUsers.toHuman())
238
- );
239
- return Promise.resolve(formattedPoolUsers);
240
- }
241
- return Promise.resolve(null);
242
- } catch (err) {
243
- return Promise.reject(err);
244
- }
245
- };
246
-
247
- function isAccountInfo(obj: any): obj is { data: { free: any } } {
248
- return 'data' in obj && 'free' in obj.data;
249
- }
250
- /*
251
- checkAccountExsists: This function takes accountId and checks if the account exists
252
- */
253
- export const checkAccountBalance = async (
254
- api: ApiPromise | undefined,
255
- accountId: string
256
- ): Promise<string> => {
257
- console.log('checkAcocuntExsists in react-native started');
258
- try {
259
- if (api === undefined) {
260
- api = await init();
261
- }
262
- // Type guard to assure TypeScript that api is not undefined
263
- if (!api?.query?.system?.account) {
264
- throw new Error('Failed to initialize api or api.query.account');
265
- }
266
- let balance: any;
267
- let accountInfo = await api.query.system.account(accountId);
268
- if (isAccountInfo(accountInfo)) {
269
- balance = accountInfo.data.free;
270
- }
271
-
272
- if (balance && balance !== '0' && balance > 0) {
273
- return Promise.resolve(balance.toHuman());
274
- }
275
- return Promise.resolve('0');
276
- } catch (err) {
277
- return Promise.reject(err);
278
- }
279
- };
280
-
281
- export const getAccountIdFromSeed = async (seed: string): Promise<string> => {
282
- try {
283
- await cryptoWaitReady();
284
- const keyring = new Keyring({ type: 'sr25519' });
285
- const account = keyring.addFromUri(seed, { name: 'account' }, 'sr25519');
286
- return Promise.resolve(account.address);
287
- } catch (err) {
288
- return Promise.reject(err);
289
- }
290
- };
1
+ import { default as EventTypes } from '../interfaces/lookup';
2
+
3
+ import { ApiPromise, WsProvider } from '@polkadot/api';
4
+ import { Keyring } from '@polkadot/keyring';
5
+ const { cryptoWaitReady, blake2AsHex } = require('@polkadot/util-crypto');
6
+ import type * as BType from '../types/blockchain';
7
+
8
+ const types = {
9
+ FulaPoolPool: EventTypes.FulaPoolPool,
10
+ };
11
+
12
+ export const init = async (
13
+ wsAddress: string = 'wss://node3.functionyard.fula.network'
14
+ ): Promise<ApiPromise> => {
15
+ const provider = new WsProvider(wsAddress);
16
+ const api = await ApiPromise.create({ types, provider }).catch((err) => {
17
+ console.log(err);
18
+ return Promise.reject(err);
19
+ });
20
+ return api;
21
+ };
22
+
23
+ export const disconnectApi = async (api: ApiPromise): Promise<void> => {
24
+ await api.disconnect();
25
+ };
26
+
27
+ /*function addDoubleSlashToSeed(seed: string): string {
28
+ return seed.startsWith('//') ? seed : '//' + seed;
29
+ }*/
30
+
31
+ /*
32
+ createManifest: This function batch uploads manifests
33
+ */
34
+ function serialize(obj: any): string {
35
+ return JSON.stringify(obj);
36
+ }
37
+
38
+ function createManifest(
39
+ cids: string[],
40
+ poolId: number,
41
+ replicationFactor: number = 4
42
+ ): {
43
+ manifest: string[]; // or string[]
44
+ cids: string[]; // or string[]
45
+ poolId: number[];
46
+ replicationFactor: number[];
47
+ } {
48
+ const manifest_metadata = cids.map((cid) => ({
49
+ job: {
50
+ work: 'Storage',
51
+ engine: 'IPFS',
52
+ uri: cid,
53
+ },
54
+ }));
55
+
56
+ // Serialize manifest_metadata to Uint8Array or string
57
+ const serializedManifest = manifest_metadata.map((item) => serialize(item)); // Implement `serialize` accordingly
58
+
59
+ // Serialize cids to Uint8Array or string
60
+ const serializedCids = cids.map((cid) => serialize(cid)); // Implement `serialize` accordingly
61
+
62
+ // Create arrays for `poolId` and `replicationFactor`
63
+ const poolIds = new Array(cids.length).fill(poolId);
64
+ const replicationFactors = new Array(cids.length).fill(replicationFactor);
65
+
66
+ const batchUploadManifest = {
67
+ manifest: serializedManifest,
68
+ cids: serializedCids,
69
+ poolId: poolIds,
70
+ replicationFactor: replicationFactors,
71
+ };
72
+
73
+ return batchUploadManifest;
74
+ }
75
+
76
+ export const batchUploadManifest = async (
77
+ api: ApiPromise | undefined,
78
+ seed: string,
79
+ cids_i: string[],
80
+ poolId_i: number,
81
+ replicationFactor_i: number = 4
82
+ ): Promise<{ hash: string }> => {
83
+ const { manifest, cids, poolId, replicationFactor } = createManifest(
84
+ cids_i,
85
+ poolId_i,
86
+ replicationFactor_i
87
+ );
88
+
89
+ console.log('uploadManifest in react-native started');
90
+ try {
91
+ if (api === undefined) {
92
+ api = await init();
93
+ }
94
+
95
+ // Simple transaction
96
+ const keyring = new Keyring({ type: 'sr25519' });
97
+ const userKey = keyring.addFromUri(seed, { name: 'account' }, 'sr25519');
98
+ console.log(
99
+ `${userKey.meta.name}: has address ${userKey.address} with publicKey [${userKey.publicKey}]`
100
+ );
101
+ if (api?.tx?.fula?.batchUploadManifest) {
102
+ const submitExtrinsic = api.tx.fula.batchUploadManifest(
103
+ manifest,
104
+ cids,
105
+ poolId,
106
+ replicationFactor
107
+ );
108
+ let unsub: () => void; // Define a variable to hold the unsub function
109
+
110
+ if (submitExtrinsic) {
111
+ return new Promise<{ hash: string }>((resolve, reject) => {
112
+ submitExtrinsic
113
+ .signAndSend(userKey, { nonce: -1 }, ({ status }) => {
114
+ if (status.isInBlock || status.isFinalized) {
115
+ if (unsub) {
116
+ unsub(); // Call unsub before resolving the promise
117
+ }
118
+ resolve({ hash: status.asInBlock.toString() });
119
+ }
120
+ })
121
+ .then((unsubFn) => {
122
+ unsub = unsubFn; // Store the unsub function once it becomes available
123
+ })
124
+ .catch((error) => {
125
+ if (unsub) {
126
+ unsub(); // Call unsub before rejecting the promise
127
+ }
128
+ console.log(':( transaction failed', error);
129
+ reject(error);
130
+ });
131
+ });
132
+ } else {
133
+ return Promise.reject(new TypeError('submitExtrinsic not constructed'));
134
+ }
135
+ } else {
136
+ return Promise.reject(new TypeError('api not initialized'));
137
+ }
138
+ } catch (err) {
139
+ return Promise.reject(err);
140
+ }
141
+ };
142
+
143
+ export const createHexSeedFromString = async (
144
+ seed: string
145
+ ): Promise<string> => {
146
+ await cryptoWaitReady(); // Ensure the crypto library is initialized
147
+ const seedHex = blake2AsHex(seed, 256); // Hash the input to a 32-byte hex string
148
+ return seedHex;
149
+ };
150
+
151
+ export const getLocalAccount = (seed: string): { account: string } => {
152
+ // Simple transaction
153
+ const keyring = new Keyring({ type: 'sr25519' });
154
+ const userKey = keyring.addFromUri(seed, { name: 'account' }, 'sr25519');
155
+ console.log(
156
+ `${userKey.meta.name}: has address ${userKey.address} with publicKey [${userKey.publicKey}]`
157
+ );
158
+ return { account: userKey.address };
159
+ };
160
+
161
+ /*
162
+ listPools: This function takes start index and length and returns a promise of an object that contains a list of pools. Each pool in the list contains the poolID, owner, poolName, parent, and participants of the pool
163
+ */
164
+ export const listPools = async (
165
+ api: ApiPromise | undefined,
166
+ start: number = 1,
167
+ length: number = 10
168
+ ): Promise<BType.PoolListResponse> => {
169
+ console.log('listPools in react-native started');
170
+ try {
171
+ if (api === undefined) {
172
+ api = await init();
173
+ }
174
+ // Type guard to assure TypeScript that api is not undefined
175
+ if (!api?.query?.pool?.lastPoolId || !api?.query?.pool?.pools) {
176
+ throw new Error('Failed to initialize api or api.query.pool');
177
+ }
178
+ const pools: BType.PoolListResponse = { pools: [] };
179
+ const lastPoolId = await api.query.pool.lastPoolId();
180
+ let finalReturnedId: number = Number(lastPoolId.toHuman());
181
+ if (Number(lastPoolId.toHuman()) > start + length) {
182
+ finalReturnedId = start + length;
183
+ }
184
+ for (let i = start; i <= finalReturnedId; i++) {
185
+ const poolInfo = await api.query.pool.pools(i).catch((err) => {
186
+ console.log(err);
187
+ return Promise.reject(err);
188
+ });
189
+ if (poolInfo != null) {
190
+ let formattedPoolInfo: BType.Pool = JSON.parse(
191
+ JSON.stringify(poolInfo.toHuman())
192
+ );
193
+ formattedPoolInfo.poolID = i;
194
+ pools.pools.push(formattedPoolInfo);
195
+ }
196
+ }
197
+ return Promise.resolve(pools);
198
+ } catch (err) {
199
+ return Promise.reject(err);
200
+ }
201
+ };
202
+
203
+ /*
204
+ checkJoinRequest: This function takes poolId and AccontId and returns a promise of an object that contains request to the pools.
205
+ */
206
+ export const checkJoinRequest = async (
207
+ api: ApiPromise | undefined,
208
+ poolId: number,
209
+ accountId: string
210
+ ): Promise<BType.PoolRequest | null> => {
211
+ console.log('checkJoinRequest in react-native started');
212
+ try {
213
+ if (api === undefined) {
214
+ api = await init();
215
+ }
216
+ // Type guard to assure TypeScript that api is not undefined
217
+ if (!api?.query?.pool?.poolRequests) {
218
+ throw new Error('Failed to initialize api or api.query.pool');
219
+ }
220
+
221
+ const poolRequest = await api.query.pool.poolRequests(poolId, accountId);
222
+
223
+ if (poolRequest != null) {
224
+ let formattedPoolRequest: BType.PoolRequest = JSON.parse(
225
+ JSON.stringify(poolRequest.toHuman())
226
+ );
227
+ return Promise.resolve(formattedPoolRequest);
228
+ }
229
+ return Promise.resolve(null);
230
+ } catch (err) {
231
+ return Promise.reject(err);
232
+ }
233
+ };
234
+
235
+ export const getUserPool = async (
236
+ api: ApiPromise | undefined,
237
+ accountId: string
238
+ ): Promise<BType.PoolUsers | null> => {
239
+ console.log('GetUserPool in react-native started');
240
+ try {
241
+ if (api === undefined) {
242
+ api = await init();
243
+ }
244
+ // Type guard to assure TypeScript that api is not undefined
245
+ if (!api?.query?.pool?.users) {
246
+ throw new Error(
247
+ 'Failed to initialize api or api.query.pool or api.query.pool.users'
248
+ );
249
+ }
250
+
251
+ const poolUsers = await api.query.pool.users(accountId);
252
+
253
+ if (poolUsers != null) {
254
+ let formattedPoolUsers: BType.PoolUsers = JSON.parse(
255
+ JSON.stringify(poolUsers.toHuman())
256
+ );
257
+ return Promise.resolve(formattedPoolUsers);
258
+ }
259
+ return Promise.resolve(null);
260
+ } catch (err) {
261
+ return Promise.reject(err);
262
+ }
263
+ };
264
+
265
+ function isAccountInfo(obj: any): obj is { data: { free: any } } {
266
+ return 'data' in obj && 'free' in obj.data;
267
+ }
268
+ /*
269
+ checkAccountExsists: This function takes accountId and checks if the account exists
270
+ */
271
+ export const checkAccountBalance = async (
272
+ api: ApiPromise | undefined,
273
+ accountId: string
274
+ ): Promise<string> => {
275
+ console.log('checkAcocuntExsists in react-native started');
276
+ try {
277
+ if (api === undefined) {
278
+ api = await init();
279
+ }
280
+ // Type guard to assure TypeScript that api is not undefined
281
+ if (!api?.query?.system?.account) {
282
+ throw new Error('Failed to initialize api or api.query.account');
283
+ }
284
+ let balance: any;
285
+ let accountInfo = await api.query.system.account(accountId);
286
+ if (isAccountInfo(accountInfo)) {
287
+ balance = accountInfo.data.free;
288
+ }
289
+
290
+ if (balance && balance !== '0' && balance > 0) {
291
+ return Promise.resolve(balance.toHuman());
292
+ }
293
+ return Promise.resolve('0');
294
+ } catch (err) {
295
+ return Promise.reject(err);
296
+ }
297
+ };
298
+
299
+ export const getAccountIdFromSeed = async (seed: string): Promise<string> => {
300
+ try {
301
+ await cryptoWaitReady();
302
+ const keyring = new Keyring({ type: 'sr25519' });
303
+ const account = keyring.addFromUri(seed, { name: 'account' }, 'sr25519');
304
+ return Promise.resolve(account.address);
305
+ } catch (err) {
306
+ return Promise.reject(err);
307
+ }
308
+ };