-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchain-api.ts
More file actions
131 lines (121 loc) · 4.03 KB
/
chain-api.ts
File metadata and controls
131 lines (121 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { default as EventTypes } from '../interfaces/api-lookup';
import { ApiPromise, WsProvider } from '@polkadot/api';
// SBP-M1 review: remove commented out code
//import { Keyring } from '@polkadot/keyring';
import type * as BType from '../types/blockchain';
const types = {
FulaPoolPool: EventTypes.FulaPoolPool,
};
export const init = async (
// SBP-M1 review: consider making configurable
wsAddress: string = 'wss://node3.functionyard.fx.land'
): Promise<ApiPromise> => {
const provider = new WsProvider(wsAddress);
const api = await ApiPromise.create({ types, provider }).catch((err) => {
console.log(err);
return Promise.reject(err);
});
return api;
};
export const disconnectApi = async (api: ApiPromise): Promise<void> => {
await api.disconnect();
};
// SBP-M1 review: remove commented out code
/*
createAccount: This function takes a seed and returns am account
*/
/*export const uploadManifest = async (
api: ApiPromise | undefined,
seed: string,
manifest: typeof EventTypes.FunctionlandFulaCall._enum.upload_manifest
): Promise<BType.ManifestUploadResponse> => {
console.log('uploadManifest in react-native started');
try {
if (api === undefined) {
api = await init();
}
// Simple transaction
const keyring = new Keyring({ type: 'sr25519' });
const userKey = keyring.addFromUri(seed, { name: 'account' });
console.log(
`${userKey.meta.name}: has address ${userKey.address} with publicKey [${userKey.publicKey}]`
);
const submitExtrinsic = await api.tx.manifest.upload(manifest);
const unsub = await submitExtrinsic
.signAndSend(userKey, ({ status, events }) => {
if (status.isInBlock || status.isFinalized) {
console.log(events);
unsub();
return Promise.resolve({ success: true });
}
})
.catch((error) => {
console.log(':( transaction failed', error);
return Promise.reject(error);
});
} catch (err) {
return Promise.reject(err);
}
};*/
/*
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
*/
export const listPools = async (
api: ApiPromise | undefined,
start: number = 1,
length: number = 10
): Promise<BType.PoolListResponse> => {
console.log('listPools in react-native started');
try {
if (api === undefined) {
api = await init();
}
const pools: BType.PoolListResponse = { pools: [] };
const lastPoolId = await api.query.pool.lastPoolId();
let finalReturnedId: number = Number(lastPoolId.toHuman());
if (Number(lastPoolId.toHuman()) > start + length) {
finalReturnedId = start + length;
}
for (let i = start; i <= finalReturnedId; i++) {
const poolInfo = await api.query.pool.pools(i).catch((err) => {
console.log(err);
return Promise.reject(err);
});
if (poolInfo != null) {
let formattedPoolInfo: BType.Pool = JSON.parse(
JSON.stringify(poolInfo.toHuman())
);
pools.pools.push(formattedPoolInfo);
}
}
return Promise.resolve(pools);
} catch (err) {
return Promise.reject(err);
}
};
// SBP-M1 review: typo
/*
checkJoinRequest: This function takes poolId and AccontId and returns a promise of an object that contains request to the pools.
*/
export const checkJoinRequest = async (
api: ApiPromise | undefined,
poolId: number,
accountId: string
): Promise<BType.PoolRequest | null> => {
console.log('checkJoinRequest in react-native started');
try {
if (api === undefined) {
api = await init();
}
const poolRequest = await api.query.pool.poolRequests(poolId, accountId);
if (poolRequest != null) {
let formattedPoolRequest: BType.PoolRequest = JSON.parse(
JSON.stringify(poolRequest.toHuman())
);
return Promise.resolve(formattedPoolRequest);
}
return Promise.resolve(null);
} catch (err) {
return Promise.reject(err);
}
};