@functionland/react-native-fula 1.29.0 → 1.32.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.
@@ -94,7 +94,7 @@ dependencies {
94
94
  // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
95
95
  //noinspection GradleDynamicVersion
96
96
  implementation "com.facebook.react:react-native:+"
97
- implementation 'com.github.functionland:fula-build-aar:v1.29.0' // From jitpack.io
97
+ implementation 'com.github.functionland:fula-build-aar:v1.32.0' // From jitpack.io
98
98
  implementation 'com.github.functionland:wnfs-android:v1.8.1' // From jitpack.io
99
99
  implementation 'commons-io:commons-io:20030203.000550'
100
100
  implementation 'commons-codec:commons-codec:1.15'
@@ -835,7 +835,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
835
835
  @ReactMethod
836
836
  public void ls(String path, Promise promise) {
837
837
  ThreadUtils.runOnExecutor(() -> {
838
- Log.d("ReactNative", "ls: path = " + path);
838
+ Log.d("ReactNative", "ls: path = " + path + "rootCid= " + this.rootConfig.getCid());
839
839
  try {
840
840
  byte[] res = Fs.ls(this.client, this.rootConfig.getCid(), path);
841
841
 
@@ -843,7 +843,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
843
843
  Log.d("ReactNative", "ls: res = " + s);
844
844
  promise.resolve(s);
845
845
  } catch (Exception e) {
846
- Log.d("get", e.getMessage());
846
+ Log.d("ReactNative", e.getMessage());
847
847
  promise.reject(e);
848
848
  }
849
849
  });
@@ -869,7 +869,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
869
869
  promise.reject(new Exception("rm Error: config is null"));
870
870
  }
871
871
  } catch (Exception e) {
872
- Log.d("get", e.getMessage());
872
+ Log.d("ReactNative", e.getMessage());
873
873
  promise.reject(e);
874
874
  }
875
875
  });
@@ -893,7 +893,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
893
893
  promise.reject(new Exception("cp Error: config is null"));
894
894
  }
895
895
  } catch (Exception e) {
896
- Log.d("get", e.getMessage());
896
+ Log.d("ReactNative", e.getMessage());
897
897
  promise.reject(e);
898
898
  }
899
899
  });
@@ -917,7 +917,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
917
917
  promise.reject(new Exception("mv Error: config is null"));
918
918
  }
919
919
  } catch (Exception e) {
920
- Log.d("get", e.getMessage());
920
+ Log.d("ReactNative", e.getMessage());
921
921
  promise.reject(e);
922
922
  }
923
923
  });
@@ -939,7 +939,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
939
939
  String path = Fs.readFilestreamToPath(this.client, this.rootConfig.getCid(), fulaTargetFilename, localFilename);
940
940
  promise.resolve(path);
941
941
  } catch (Exception e) {
942
- Log.d("get", e.getMessage());
942
+ Log.d("ReactNative", e.getMessage());
943
943
  promise.reject(e);
944
944
  }
945
945
  });
@@ -954,7 +954,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
954
954
  String resString = toString(res);
955
955
  promise.resolve(resString);
956
956
  } catch (Exception e) {
957
- Log.d("get", e.getMessage());
957
+ Log.d("ReactNative", e.getMessage());
958
958
  promise.reject(e);
959
959
  }
960
960
  });
@@ -970,7 +970,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
970
970
  String valueString = toString(value);
971
971
  promise.resolve(valueString);
972
972
  } catch (Exception e) {
973
- Log.d("get", e.getMessage());
973
+ Log.d("ReactNative", e.getMessage());
974
974
  promise.reject(e);
975
975
  }
976
976
  });
@@ -1000,7 +1000,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1000
1000
  boolean result = this.hasInternal(key);
1001
1001
  promise.resolve(result);
1002
1002
  } catch (Exception e) {
1003
- Log.d("get", e.getMessage());
1003
+ Log.d("ReactNative", e.getMessage());
1004
1004
  promise.reject(e);
1005
1005
  }
1006
1006
  });
@@ -1011,7 +1011,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1011
1011
  boolean res = this.fula.has(key);
1012
1012
  return res;
1013
1013
  } catch (Exception e) {
1014
- Log.d("hasInternal", e.getMessage());
1014
+ Log.d("ReactNative", e.getMessage());
1015
1015
  throw (e);
1016
1016
  }
1017
1017
  }
@@ -1020,7 +1020,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1020
1020
  try {
1021
1021
  this.fula.pull(key);
1022
1022
  } catch (Exception e) {
1023
- Log.d("pullInternal", e.getMessage());
1023
+ Log.d("ReactNative", e.getMessage());
1024
1024
  throw (e);
1025
1025
  }
1026
1026
  }
@@ -1033,7 +1033,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1033
1033
  this.pushInternal(this.convertStringToByte(this.rootConfig.getCid()));
1034
1034
  promise.resolve(this.rootConfig.getCid());
1035
1035
  } catch (Exception e) {
1036
- Log.d("get", e.getMessage());
1036
+ Log.d("ReactNative", e.getMessage());
1037
1037
  promise.reject(e);
1038
1038
  }
1039
1039
  });
@@ -1113,7 +1113,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1113
1113
  }
1114
1114
  promise.resolve(false);
1115
1115
  } catch (Exception e) {
1116
- Log.d("get", e.getMessage());
1116
+ Log.d("ReactNative", e.getMessage());
1117
1117
  promise.reject(e);
1118
1118
  }
1119
1119
  });
@@ -1162,7 +1162,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1162
1162
  String resultString = toString(result);
1163
1163
  promise.resolve(resultString);
1164
1164
  } catch (Exception e) {
1165
- Log.d("get", e.getMessage());
1165
+ Log.d("ReactNative", e.getMessage());
1166
1166
  promise.reject(e);
1167
1167
  }
1168
1168
  });
@@ -1177,7 +1177,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1177
1177
  String resultString = toString(result);
1178
1178
  promise.resolve(resultString);
1179
1179
  } catch (Exception e) {
1180
- Log.d("get", e.getMessage());
1180
+ Log.d("ReactNative", e.getMessage());
1181
1181
  promise.reject(e);
1182
1182
  }
1183
1183
  });
@@ -1192,7 +1192,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1192
1192
  String resultString = toString(result);
1193
1193
  promise.resolve(resultString);
1194
1194
  } catch (Exception e) {
1195
- Log.d("get", e.getMessage());
1195
+ Log.d("ReactNative", e.getMessage());
1196
1196
  promise.reject(e);
1197
1197
  }
1198
1198
  });
@@ -1207,7 +1207,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1207
1207
  String resultString = toString(result);
1208
1208
  promise.resolve(resultString);
1209
1209
  } catch (Exception e) {
1210
- Log.d("get", e.getMessage());
1210
+ Log.d("ReactNative", e.getMessage());
1211
1211
  promise.reject(e);
1212
1212
  }
1213
1213
  });
@@ -1223,7 +1223,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1223
1223
  String resultString = toString(result);
1224
1224
  promise.resolve(resultString);
1225
1225
  } catch (Exception e) {
1226
- Log.d("get", e.getMessage());
1226
+ Log.d("ReactNative", e.getMessage());
1227
1227
  promise.reject(e);
1228
1228
  }
1229
1229
  });
@@ -1238,7 +1238,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1238
1238
  String resultString = toString(result);
1239
1239
  promise.resolve(resultString);
1240
1240
  } catch (Exception e) {
1241
- Log.d("get", e.getMessage());
1241
+ Log.d("ReactNative", e.getMessage());
1242
1242
  promise.reject(e);
1243
1243
  }
1244
1244
  });
@@ -1253,7 +1253,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1253
1253
  String resultString = toString(result);
1254
1254
  promise.resolve(resultString);
1255
1255
  } catch (Exception e) {
1256
- Log.d("get", e.getMessage());
1256
+ Log.d("ReactNative", e.getMessage());
1257
1257
  promise.reject(e);
1258
1258
  }
1259
1259
  });
@@ -1268,7 +1268,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1268
1268
  String resultString = toString(result);
1269
1269
  promise.resolve(resultString);
1270
1270
  } catch (Exception e) {
1271
- Log.d("get", e.getMessage());
1271
+ Log.d("ReactNative", e.getMessage());
1272
1272
  promise.reject(e);
1273
1273
  }
1274
1274
  });
@@ -1283,7 +1283,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
1283
1283
  String resultString = toString(result);
1284
1284
  promise.resolve(resultString);
1285
1285
  } catch (Exception e) {
1286
- Log.d("get", e.getMessage());
1286
+ Log.d("ReactNative", e.getMessage());
1287
1287
  promise.reject(e);
1288
1288
  }
1289
1289
  });
@@ -31,12 +31,12 @@ const disconnectApi = async api => {
31
31
  await api.disconnect();
32
32
  };
33
33
 
34
- /*function addDoubleSlashToSeed(seed: string): string {
35
- return seed.startsWith('//') ? seed : '//' + seed;
34
+ /*function addDoubleSlashToSeed(seed: string): string {
35
+ return seed.startsWith('//') ? seed : '//' + seed;
36
36
  }*/
37
37
 
38
- /*
39
- createManifest: This function batch uploads manifests
38
+ /*
39
+ createManifest: This function batch uploads manifests
40
40
  */
41
41
  exports.disconnectApi = disconnectApi;
42
42
  function serialize(obj) {
@@ -133,8 +133,8 @@ const batchUploadManifest = async function (api, seed, cids_i, poolId_i) {
133
133
  }
134
134
  };
135
135
 
136
- /*
137
- 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
136
+ /*
137
+ 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
138
138
  */
139
139
  exports.batchUploadManifest = batchUploadManifest;
140
140
  const listPools = async function (api) {
@@ -174,8 +174,8 @@ const listPools = async function (api) {
174
174
  }
175
175
  };
176
176
 
177
- /*
178
- checkJoinRequest: This function takes poolId and AccontId and returns a promise of an object that contains request to the pools.
177
+ /*
178
+ checkJoinRequest: This function takes poolId and AccontId and returns a promise of an object that contains request to the pools.
179
179
  */
180
180
  exports.listPools = listPools;
181
181
  const checkJoinRequest = async (api, poolId, accountId) => {
@@ -225,8 +225,8 @@ exports.getUserPool = getUserPool;
225
225
  function isAccountInfo(obj) {
226
226
  return 'data' in obj && 'free' in obj.data;
227
227
  }
228
- /*
229
- checkAccountExsists: This function takes accountId and checks if the account exists
228
+ /*
229
+ checkAccountExsists: This function takes accountId and checks if the account exists
230
230
  */
231
231
  const checkAccountBalance = async (api, accountId) => {
232
232
  console.log('checkAcocuntExsists in react-native started');
@@ -23,12 +23,12 @@ export const disconnectApi = async api => {
23
23
  await api.disconnect();
24
24
  };
25
25
 
26
- /*function addDoubleSlashToSeed(seed: string): string {
27
- return seed.startsWith('//') ? seed : '//' + seed;
26
+ /*function addDoubleSlashToSeed(seed: string): string {
27
+ return seed.startsWith('//') ? seed : '//' + seed;
28
28
  }*/
29
29
 
30
- /*
31
- createManifest: This function batch uploads manifests
30
+ /*
31
+ createManifest: This function batch uploads manifests
32
32
  */
33
33
  function serialize(obj) {
34
34
  return JSON.stringify(obj);
@@ -124,8 +124,8 @@ export const batchUploadManifest = async function (api, seed, cids_i, poolId_i)
124
124
  }
125
125
  };
126
126
 
127
- /*
128
- 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
127
+ /*
128
+ 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
129
129
  */
130
130
  export const listPools = async function (api) {
131
131
  let start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
@@ -164,8 +164,8 @@ export const listPools = async function (api) {
164
164
  }
165
165
  };
166
166
 
167
- /*
168
- checkJoinRequest: This function takes poolId and AccontId and returns a promise of an object that contains request to the pools.
167
+ /*
168
+ checkJoinRequest: This function takes poolId and AccontId and returns a promise of an object that contains request to the pools.
169
169
  */
170
170
  export const checkJoinRequest = async (api, poolId, accountId) => {
171
171
  console.log('checkJoinRequest in react-native started');
@@ -212,8 +212,8 @@ export const getUserPool = async (api, accountId) => {
212
212
  function isAccountInfo(obj) {
213
213
  return 'data' in obj && 'free' in obj.data;
214
214
  }
215
- /*
216
- checkAccountExsists: This function takes accountId and checks if the account exists
215
+ /*
216
+ checkAccountExsists: This function takes accountId and checks if the account exists
217
217
  */
218
218
  export const checkAccountBalance = async (api, accountId) => {
219
219
  console.log('checkAcocuntExsists in react-native started');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@functionland/react-native-fula",
3
- "version": "1.29.0",
3
+ "version": "1.32.0",
4
4
  "description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
@@ -1,289 +1,289 @@
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, ({ 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
- pools.pools.push(formattedPoolInfo);
176
- }
177
- }
178
- return Promise.resolve(pools);
179
- } catch (err) {
180
- return Promise.reject(err);
181
- }
182
- };
183
-
184
- /*
185
- checkJoinRequest: This function takes poolId and AccontId and returns a promise of an object that contains request to the pools.
186
- */
187
- export const checkJoinRequest = async (
188
- api: ApiPromise | undefined,
189
- poolId: number,
190
- accountId: string
191
- ): Promise<BType.PoolRequest | null> => {
192
- console.log('checkJoinRequest in react-native started');
193
- try {
194
- if (api === undefined) {
195
- api = await init();
196
- }
197
- // Type guard to assure TypeScript that api is not undefined
198
- if (!api?.query?.pool?.poolRequests) {
199
- throw new Error('Failed to initialize api or api.query.pool');
200
- }
201
-
202
- const poolRequest = await api.query.pool.poolRequests(poolId, accountId);
203
-
204
- if (poolRequest != null) {
205
- let formattedPoolRequest: BType.PoolRequest = JSON.parse(
206
- JSON.stringify(poolRequest.toHuman())
207
- );
208
- return Promise.resolve(formattedPoolRequest);
209
- }
210
- return Promise.resolve(null);
211
- } catch (err) {
212
- return Promise.reject(err);
213
- }
214
- };
215
-
216
- export const getUserPool = async (
217
- api: ApiPromise | undefined,
218
- accountId: string
219
- ): Promise<BType.PoolUsers | null> => {
220
- console.log('GetUserPool in react-native started');
221
- try {
222
- if (api === undefined) {
223
- api = await init();
224
- }
225
- // Type guard to assure TypeScript that api is not undefined
226
- if (!api?.query?.pool?.users) {
227
- throw new Error(
228
- 'Failed to initialize api or api.query.pool or api.query.pool.users'
229
- );
230
- }
231
-
232
- const poolUsers = await api.query.pool.users(accountId);
233
-
234
- if (poolUsers != null) {
235
- let formattedPoolUsers: BType.PoolUsers = JSON.parse(
236
- JSON.stringify(poolUsers.toHuman())
237
- );
238
- return Promise.resolve(formattedPoolUsers);
239
- }
240
- return Promise.resolve(null);
241
- } catch (err) {
242
- return Promise.reject(err);
243
- }
244
- };
245
-
246
- function isAccountInfo(obj: any): obj is { data: { free: any } } {
247
- return 'data' in obj && 'free' in obj.data;
248
- }
249
- /*
250
- checkAccountExsists: This function takes accountId and checks if the account exists
251
- */
252
- export const checkAccountBalance = async (
253
- api: ApiPromise | undefined,
254
- accountId: string
255
- ): Promise<string> => {
256
- console.log('checkAcocuntExsists in react-native started');
257
- try {
258
- if (api === undefined) {
259
- api = await init();
260
- }
261
- // Type guard to assure TypeScript that api is not undefined
262
- if (!api?.query?.system?.account) {
263
- throw new Error('Failed to initialize api or api.query.account');
264
- }
265
- let balance: any;
266
- let accountInfo = await api.query.system.account(accountId);
267
- if (isAccountInfo(accountInfo)) {
268
- balance = accountInfo.data.free;
269
- }
270
-
271
- if (balance && balance !== '0' && balance > 0) {
272
- return Promise.resolve(balance.toHuman());
273
- }
274
- return Promise.resolve('0');
275
- } catch (err) {
276
- return Promise.reject(err);
277
- }
278
- };
279
-
280
- export const getAccountIdFromSeed = async (seed: string): Promise<string> => {
281
- try {
282
- await cryptoWaitReady();
283
- const keyring = new Keyring({ type: 'sr25519' });
284
- const account = keyring.addFromUri(seed, { name: 'account' }, 'sr25519');
285
- return Promise.resolve(account.address);
286
- } catch (err) {
287
- return Promise.reject(err);
288
- }
289
- };
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, ({ 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
+ pools.pools.push(formattedPoolInfo);
176
+ }
177
+ }
178
+ return Promise.resolve(pools);
179
+ } catch (err) {
180
+ return Promise.reject(err);
181
+ }
182
+ };
183
+
184
+ /*
185
+ checkJoinRequest: This function takes poolId and AccontId and returns a promise of an object that contains request to the pools.
186
+ */
187
+ export const checkJoinRequest = async (
188
+ api: ApiPromise | undefined,
189
+ poolId: number,
190
+ accountId: string
191
+ ): Promise<BType.PoolRequest | null> => {
192
+ console.log('checkJoinRequest in react-native started');
193
+ try {
194
+ if (api === undefined) {
195
+ api = await init();
196
+ }
197
+ // Type guard to assure TypeScript that api is not undefined
198
+ if (!api?.query?.pool?.poolRequests) {
199
+ throw new Error('Failed to initialize api or api.query.pool');
200
+ }
201
+
202
+ const poolRequest = await api.query.pool.poolRequests(poolId, accountId);
203
+
204
+ if (poolRequest != null) {
205
+ let formattedPoolRequest: BType.PoolRequest = JSON.parse(
206
+ JSON.stringify(poolRequest.toHuman())
207
+ );
208
+ return Promise.resolve(formattedPoolRequest);
209
+ }
210
+ return Promise.resolve(null);
211
+ } catch (err) {
212
+ return Promise.reject(err);
213
+ }
214
+ };
215
+
216
+ export const getUserPool = async (
217
+ api: ApiPromise | undefined,
218
+ accountId: string
219
+ ): Promise<BType.PoolUsers | null> => {
220
+ console.log('GetUserPool in react-native started');
221
+ try {
222
+ if (api === undefined) {
223
+ api = await init();
224
+ }
225
+ // Type guard to assure TypeScript that api is not undefined
226
+ if (!api?.query?.pool?.users) {
227
+ throw new Error(
228
+ 'Failed to initialize api or api.query.pool or api.query.pool.users'
229
+ );
230
+ }
231
+
232
+ const poolUsers = await api.query.pool.users(accountId);
233
+
234
+ if (poolUsers != null) {
235
+ let formattedPoolUsers: BType.PoolUsers = JSON.parse(
236
+ JSON.stringify(poolUsers.toHuman())
237
+ );
238
+ return Promise.resolve(formattedPoolUsers);
239
+ }
240
+ return Promise.resolve(null);
241
+ } catch (err) {
242
+ return Promise.reject(err);
243
+ }
244
+ };
245
+
246
+ function isAccountInfo(obj: any): obj is { data: { free: any } } {
247
+ return 'data' in obj && 'free' in obj.data;
248
+ }
249
+ /*
250
+ checkAccountExsists: This function takes accountId and checks if the account exists
251
+ */
252
+ export const checkAccountBalance = async (
253
+ api: ApiPromise | undefined,
254
+ accountId: string
255
+ ): Promise<string> => {
256
+ console.log('checkAcocuntExsists in react-native started');
257
+ try {
258
+ if (api === undefined) {
259
+ api = await init();
260
+ }
261
+ // Type guard to assure TypeScript that api is not undefined
262
+ if (!api?.query?.system?.account) {
263
+ throw new Error('Failed to initialize api or api.query.account');
264
+ }
265
+ let balance: any;
266
+ let accountInfo = await api.query.system.account(accountId);
267
+ if (isAccountInfo(accountInfo)) {
268
+ balance = accountInfo.data.free;
269
+ }
270
+
271
+ if (balance && balance !== '0' && balance > 0) {
272
+ return Promise.resolve(balance.toHuman());
273
+ }
274
+ return Promise.resolve('0');
275
+ } catch (err) {
276
+ return Promise.reject(err);
277
+ }
278
+ };
279
+
280
+ export const getAccountIdFromSeed = async (seed: string): Promise<string> => {
281
+ try {
282
+ await cryptoWaitReady();
283
+ const keyring = new Keyring({ type: 'sr25519' });
284
+ const account = keyring.addFromUri(seed, { name: 'account' }, 'sr25519');
285
+ return Promise.resolve(account.address);
286
+ } catch (err) {
287
+ return Promise.reject(err);
288
+ }
289
+ };
@@ -1,96 +1,96 @@
1
- export interface SeededResponse {
2
- seed: string;
3
- account: string;
4
- }
5
-
6
- export interface AccountExistsResponse {
7
- account: string;
8
- exists: boolean;
9
- }
10
-
11
- export interface PoolCreateResponse {
12
- owner: string;
13
- poolID: number;
14
- }
15
-
16
- export interface GetAccountResponse {
17
- account: string;
18
- }
19
-
20
- export interface AssetsBalanceResponse {
21
- amount: string;
22
- }
23
-
24
- export interface PoolJoinResponse {
25
- account: string;
26
- poolID: number;
27
- }
28
-
29
- export interface PoolCancelJoinResponse {
30
- account: string;
31
- poolID: number;
32
- }
33
-
34
- export interface PoolRequestsResponse {
35
- poolRequests: PoolRequest[];
36
- }
37
-
38
- export interface PoolListResponse {
39
- pools: Pool[];
40
- }
41
-
42
- export interface PoolVoteResponse {
43
- account: string;
44
- poolID: number;
45
- }
46
-
47
- export interface PoolLeaveResponse {
48
- account: string;
49
- poolID: number;
50
- }
51
-
52
- export interface ManifestUploadResponse {
53
- uploader: string;
54
- storage: string[];
55
- manifestMetadata: ManifestMetadata;
56
- poolID: number;
57
- }
58
-
59
- export interface PoolRequest {
60
- poolID: number;
61
- account: string;
62
- voted: string[];
63
- positiveVotes: number;
64
- peerID: string;
65
- }
66
-
67
- export interface PoolUsers {
68
- poolID: number;
69
- account: string;
70
- requestPoolId: string;
71
- peerID: string;
72
- }
73
-
74
- export interface Pool {
75
- poolID: number;
76
- owner: string;
77
- poolName: string;
78
- parent: string;
79
- participants: string[];
80
- }
81
-
82
- export interface ManifestMetadata {
83
- job: ManifestJob;
84
- }
85
-
86
- export interface ManifestJob {
87
- work: string;
88
- engine: string;
89
- uri: string;
90
- }
91
- export interface BloxFreeSpaceResponse {
92
- size: number;
93
- avail: number;
94
- used: number;
95
- used_percentage: number;
96
- }
1
+ export interface SeededResponse {
2
+ seed: string;
3
+ account: string;
4
+ }
5
+
6
+ export interface AccountExistsResponse {
7
+ account: string;
8
+ exists: boolean;
9
+ }
10
+
11
+ export interface PoolCreateResponse {
12
+ owner: string;
13
+ poolID: number;
14
+ }
15
+
16
+ export interface GetAccountResponse {
17
+ account: string;
18
+ }
19
+
20
+ export interface AssetsBalanceResponse {
21
+ amount: string;
22
+ }
23
+
24
+ export interface PoolJoinResponse {
25
+ account: string;
26
+ poolID: number;
27
+ }
28
+
29
+ export interface PoolCancelJoinResponse {
30
+ account: string;
31
+ poolID: number;
32
+ }
33
+
34
+ export interface PoolRequestsResponse {
35
+ poolRequests: PoolRequest[];
36
+ }
37
+
38
+ export interface PoolListResponse {
39
+ pools: Pool[];
40
+ }
41
+
42
+ export interface PoolVoteResponse {
43
+ account: string;
44
+ poolID: number;
45
+ }
46
+
47
+ export interface PoolLeaveResponse {
48
+ account: string;
49
+ poolID: number;
50
+ }
51
+
52
+ export interface ManifestUploadResponse {
53
+ uploader: string;
54
+ storage: string[];
55
+ manifestMetadata: ManifestMetadata;
56
+ poolID: number;
57
+ }
58
+
59
+ export interface PoolRequest {
60
+ poolID: number;
61
+ account: string;
62
+ voted: string[];
63
+ positiveVotes: number;
64
+ peerID: string;
65
+ }
66
+
67
+ export interface PoolUsers {
68
+ poolID: number;
69
+ account: string;
70
+ requestPoolId: string;
71
+ peerID: string;
72
+ }
73
+
74
+ export interface Pool {
75
+ poolID: number;
76
+ owner: string;
77
+ poolName: string;
78
+ parent: string;
79
+ participants: string[];
80
+ }
81
+
82
+ export interface ManifestMetadata {
83
+ job: ManifestJob;
84
+ }
85
+
86
+ export interface ManifestJob {
87
+ work: string;
88
+ engine: string;
89
+ uri: string;
90
+ }
91
+ export interface BloxFreeSpaceResponse {
92
+ size: number;
93
+ avail: number;
94
+ used: number;
95
+ used_percentage: number;
96
+ }