Skip to content

Commit

Permalink
Merge pull request #78 from functionland/added-GetUserPool
Browse files Browse the repository at this point in the history
Added get user pool
  • Loading branch information
ehsan6sha authored Dec 21, 2023
2 parents ae586de + 2b6c088 commit 43f336b
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 10 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ dependencies {
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation 'com.github.functionland:fula-build-aar:v1.20.0' // From jitpack.io
implementation 'com.github.functionland:fula-build-aar:v1.29.0' // From jitpack.io
implementation 'com.github.functionland:wnfs-android:v1.8.1' // From jitpack.io
implementation 'commons-io:commons-io:20030203.000550'
implementation 'commons-codec:commons-codec:1.15'
Expand Down
3 changes: 3 additions & 0 deletions android/src/main/java/land/fx/fula/FulaModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,9 @@ private String[] initInternal(byte[] identity, String storePath, String bloxAddr
if(rootCid != null && !rootCid.isEmpty()){
Log.d("ReactNative", "Re-setting cid from input: "+rootCid);
cid = rootCid;
this.rootConfig = new land.fx.wnfslib.Config(cid);
this.reloadFS(this.client, identity, cid);
this.encrypt_and_store_config();
}
if(cid == null || cid.isEmpty()) {
Log.d("ReactNative", "Tried to recover cid but was not successful. Creating new ones");
Expand Down
72 changes: 64 additions & 8 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const App = () => {
const [value, setValue] = React.useState<string>('');
const [inprogress, setInprogress] = React.useState<boolean>(false);
const [newRootCid, setNewRootCid] = React.useState<string>('');
const seed = '0xmd93c00b5v99f99ti871r8r17r2rt66ee277777ge1be6fb47709b691efb0e777';
const root_cid = 'bafyr4ibz4okarrsr7awv6wrwbnpnwi73rk72cnwgi5unvad5l75lr2zctu';
const seed =
'0xmd93c00b5v99f99ti871r8r17r2rt66ee277777ge1be6fb47709b691efb0e777';

const [initComplete, setInitComplete] = React.useState<
{ peerId: string; rootCid: string; private_ref: string } | {}
Expand Down Expand Up @@ -53,17 +55,22 @@ const App = () => {
153, 106, 217, 201, 106, 9, 66, 33, 214, 195, 255, 234, 178, 244, 203, 112,
62, 91, 140, 55, 179, 10, 208, 210, 177, 111, 61, 46, 73, 148, 14, 62,
];
const bloxPeerId_tower =
'12D3KooWACVcVsQh18jM9UudRQzeYEjxCJQJgFUaAgs41tayjxC4';
const bloxPeerId_laptop =
'12D3KooWLGatFxDzMrKd4S6UC4GAtuM4zcFJW8RPuMR9SH7j46A8';
const bloxPeerId_tower = '12D3KooWACVcVsQh18jM9UudRQzeYEjxCJQJgFUaAgs41tayjxC4';
const bloxPeerId_laptop = '12D3KooWRTzN7HfmjoUBHokyRZuKdyohVVSGqKBMF24ZC3tGK78Q';

const bloxAddr = '/dns/relay.dev.fx.land/tcp/4001/p2p/12D3KooWDRrBaAfPwsGJivBoUw5fE7ZpDiyfUjqgiURq2DEcL835/p2p-circuit/p2p/' + bloxPeerId_laptop;
//const bloxAddr = '/ip4/192.168.2.14/tcp/40001/p2p/' + bloxPeerId_laptop;

const initFula = async () => {
try {
return fula.init(privateKey_tower.toString(), '', bloxAddr, '');
return fula.init(
privateKey_tower.toString(),
'',
bloxAddr,
'',
true,
root_cid
);
} catch (e) {
console.log(e);
return Promise.reject(e);
Expand Down Expand Up @@ -460,6 +467,56 @@ const App = () => {
color={inprogress ? 'green' : 'blue'}
/>

<Button
title={
inprogress ? 'Putting & Getting...' : 'Check Join Request Status'
}
onPress={async () => {
try {
chainApi.init().then(async (api: any) => {
console.log('api created');
let accountId = await chainApi.getAccountIdFromSeed(seed);
console.log('account ID is ' + accountId);
chainApi
.checkJoinRequest(api, 1, "5DD9qAHKNUqcYaKf5qgYra9y8s9BtbfLanJrTr3hQsK5XGGP")
.then((res: any) => {
console.log('join request status created');
console.log(res);
})
.catch((e: any) => {
console.log('join request status failed');
console.log(e);
});
});
} catch (e) {}
}}
color={inprogress ? 'green' : 'blue'}
/>

<Button
title={inprogress ? 'Putting & Getting...' : 'Get User Pool'}
onPress={async () => {
try {
chainApi.init().then(async (api: any) => {
console.log('api created');
let accountId = await chainApi.getAccountIdFromSeed(seed);
console.log('account ID is ' + accountId);
chainApi
.getUserPool(api, "5CcHZucP2u1FXQW9wuyC11vAVxB3c48pUhc5cc9b3oxbKPL2")
.then((res: any) => {
console.log('GetUserPool created');
console.log(res);
})
.catch((e: any) => {
console.log('GetUserPool failed');
console.log(e);
});
});
} catch (e) {}
}}
color={inprogress ? 'green' : 'blue'}
/>

<Button
title={inprogress ? 'Getting...' : 'Get Blox Free Space'}
onPress={async () => {
Expand Down Expand Up @@ -643,7 +700,6 @@ const App = () => {
color={inprogress ? 'green' : 'blue'}
/>


<Button
title={inprogress ? 'Putting & Getting...' : 'Test Replicate'}
onPress={async () => {
Expand Down Expand Up @@ -690,7 +746,7 @@ const App = () => {
.then((r2: any) => {
console.log('amount received');
console.log(r2);
});
});
}
})
.catch((e: any) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@functionland/react-native-fula",
"version": "1.20.0",
"version": "1.29.0",
"description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
30 changes: 30 additions & 0 deletions src/protocols/chain-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,36 @@ export const checkJoinRequest = async (
}
};

export const getUserPool = async (
api: ApiPromise | undefined,
accountId: string
): Promise<BType.PoolUsers | null> => {
console.log('GetUserPool in react-native started');
try {
if (api === undefined) {
api = await init();
}
// Type guard to assure TypeScript that api is not undefined
if (!api?.query?.pool?.users) {
throw new Error(
'Failed to initialize api or api.query.pool or api.query.pool.users'
);
}

const poolUsers = await api.query.pool.users(accountId);

if (poolUsers != null) {
let formattedPoolUsers: BType.PoolUsers = JSON.parse(
JSON.stringify(poolUsers.toHuman())
);
return Promise.resolve(formattedPoolUsers);
}
return Promise.resolve(null);
} catch (err) {
return Promise.reject(err);
}
};

function isAccountInfo(obj: any): obj is { data: { free: any } } {
return 'data' in obj && 'free' in obj.data;
}
Expand Down
7 changes: 7 additions & 0 deletions src/types/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ export interface PoolRequest {
peerID: string;
}

export interface PoolUsers {
poolID: number;
account: string;
requestPoolId: string;
peerID: string;
}

export interface Pool {
poolID: number;
owner: string;
Expand Down

0 comments on commit 43f336b

Please sign in to comment.