@functionland/react-native-fula 1.39.2 → 1.41.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.
- package/android/build.gradle +1 -1
- package/android/src/main/java/land/fx/fula/FulaModule.java +8 -6
- package/lib/commonjs/protocols/blockchain.js +6 -2
- package/lib/commonjs/protocols/blockchain.js.map +1 -1
- package/lib/commonjs/protocols/chain-api.js +25 -17
- package/lib/commonjs/protocols/chain-api.js.map +1 -1
- package/lib/commonjs/protocols/fula.js +22 -8
- package/lib/commonjs/protocols/fula.js.map +1 -1
- package/lib/module/protocols/blockchain.js +6 -2
- package/lib/module/protocols/blockchain.js.map +1 -1
- package/lib/module/protocols/chain-api.js +25 -17
- package/lib/module/protocols/chain-api.js.map +1 -1
- package/lib/module/protocols/fula.js +22 -8
- package/lib/module/protocols/fula.js.map +1 -1
- package/lib/typescript/interfaces/fulaNativeModule.d.ts +2 -2
- package/package.json +1 -1
- package/src/interfaces/fulaNativeModule.ts +2 -2
- package/src/protocols/blockchain.ts +2 -2
- package/src/protocols/chain-api.ts +290 -290
|
@@ -1,290 +1,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 } = 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 } = 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
|
+
};
|