Skip to content

Commit a056295

Browse files
authored
Merge pull request #75 from functionland/getAccountBalance
get account id and asset balance
2 parents 95facd8 + 3cac408 commit a056295

File tree

7 files changed

+117
-115
lines changed

7 files changed

+117
-115
lines changed

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ dependencies {
9494
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
9595
//noinspection GradleDynamicVersion
9696
implementation "com.facebook.react:react-native:+"
97-
implementation 'com.github.functionland:fula-build-aar:v1.15.1' // From jitpack.io
97+
implementation 'com.github.functionland:fula-build-aar:v1.19.0' // From jitpack.io
9898
implementation 'com.github.functionland:wnfs-android:v1.8.1' // From jitpack.io
9999
implementation 'commons-io:commons-io:20030203.000550'
100100
implementation 'commons-codec:commons-codec:1.15'

android/src/main/java/land/fx/fula/FulaModule.java

Lines changed: 11 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,21 +1151,13 @@ public void shutdown(Promise promise) {
11511151
//////////////////////ANYTHING BELOW IS FOR BLOCKCHAIN/////
11521152
///////////////////////////////////////////////////////////
11531153
@ReactMethod
1154-
public void createAccount(String seedString, Promise promise) {
1154+
public void GetAccount(Promise promise) {
11551155
ThreadUtils.runOnExecutor(() -> {
1156-
Log.d("ReactNative", "createAccount: seedString = " + seedString);
1156+
Log.d("ReactNative", "getAccount called ");
11571157
try {
1158-
if (this.fula == null || this.fula.id() == null || this.fula.id().isEmpty()) {
1159-
promise.reject(new Error("Fula client is not initialized"));
1160-
} else {
1161-
1162-
if (!seedString.startsWith("/")) {
1163-
promise.reject(new Error("seed should start with /"));
1164-
}
1165-
byte[] result = this.fula.seeded(seedString);
1166-
String resultString = toString(result);
1167-
promise.resolve(resultString);
1168-
}
1158+
byte[] result = this.fula.getAccount();
1159+
String resultString = toString(result);
1160+
promise.resolve(resultString);
11691161
} catch (Exception e) {
11701162
Log.d("get", e.getMessage());
11711163
promise.reject(e);
@@ -1174,11 +1166,11 @@ public void createAccount(String seedString, Promise promise) {
11741166
}
11751167

11761168
@ReactMethod
1177-
public void checkAccountExists(String accountString, Promise promise) {
1169+
public void AssetsBalance(String account, String assetId, String classId, Promise promise) {
11781170
ThreadUtils.runOnExecutor(() -> {
1179-
Log.d("ReactNative", "checkAccountExists: accountString = " + accountString);
1171+
Log.d("ReactNative", "getAccount called ");
11801172
try {
1181-
byte[] result = this.fula.accountExists(accountString);
1173+
byte[] result = this.fula.assetsBalance(account, assetId, classId);
11821174
String resultString = toString(result);
11831175
promise.resolve(resultString);
11841176
} catch (Exception e) {
@@ -1189,11 +1181,11 @@ public void checkAccountExists(String accountString, Promise promise) {
11891181
}
11901182

11911183
@ReactMethod
1192-
public void createPool(String seedString, String poolName, Promise promise) {
1184+
public void checkAccountExists(String accountString, Promise promise) {
11931185
ThreadUtils.runOnExecutor(() -> {
1194-
Log.d("ReactNative", "createPool: seedString = " + seedString + "; poolName = " + poolName);
1186+
Log.d("ReactNative", "checkAccountExists: accountString = " + accountString);
11951187
try {
1196-
byte[] result = this.fula.poolCreate(seedString, poolName);
1188+
byte[] result = this.fula.accountExists(accountString);
11971189
String resultString = toString(result);
11981190
promise.resolve(resultString);
11991191
} catch (Exception e) {
@@ -1264,21 +1256,6 @@ public void listPoolJoinRequests(long poolID, Promise promise) {
12641256
});
12651257
}
12661258

1267-
@ReactMethod
1268-
public void votePoolJoinRequest(String seedString, long poolID, String accountString, boolean accept, Promise promise) {
1269-
ThreadUtils.runOnExecutor(() -> {
1270-
Log.d("ReactNative", "votePoolJoinRequest: seedString = " + seedString + "; poolID = " + poolID + "; accountString = " + accountString + "; accept = " + accept);
1271-
try {
1272-
byte[] result = this.fula.poolVote(seedString, poolID, accountString, accept);
1273-
String resultString = toString(result);
1274-
promise.resolve(resultString);
1275-
} catch (Exception e) {
1276-
Log.d("get", e.getMessage());
1277-
promise.reject(e);
1278-
}
1279-
});
1280-
}
1281-
12821259
@ReactMethod
12831260
public void leavePool(long poolID, Promise promise) {
12841261
ThreadUtils.runOnExecutor(() -> {
@@ -1294,36 +1271,6 @@ public void leavePool(long poolID, Promise promise) {
12941271
});
12951272
}
12961273

1297-
@ReactMethod
1298-
public void newReplicationRequest(String seedString, long poolID, long replicationFactor, String cid, Promise promise) {
1299-
ThreadUtils.runOnExecutor(() -> {
1300-
Log.d("ReactNative", "newReplicationRequest: seedString = " + seedString + "; poolID = " + poolID + "; replicationFactor = " + replicationFactor + "; cid = " + cid);
1301-
try {
1302-
byte[] result = this.fula.manifestUpload(seedString, poolID, replicationFactor, cid);
1303-
String resultString = toString(result);
1304-
promise.resolve(resultString);
1305-
} catch (Exception e) {
1306-
Log.d("get", e.getMessage());
1307-
promise.reject(e);
1308-
}
1309-
});
1310-
}
1311-
1312-
@ReactMethod
1313-
public void newStoreRequest(String seedString, long poolID, String uploader, String cid, Promise promise) {
1314-
ThreadUtils.runOnExecutor(() -> {
1315-
Log.d("ReactNative", "newStoreRequest: seedString = " + seedString + "; poolID = " + poolID + "; uploader = " + uploader + "; cid = " + cid);
1316-
try {
1317-
byte[] result = this.fula.manifestStore(seedString, poolID, uploader, cid);
1318-
String resultString = toString(result);
1319-
promise.resolve(resultString);
1320-
} catch (Exception e) {
1321-
Log.d("get", e.getMessage());
1322-
promise.reject(e);
1323-
}
1324-
});
1325-
}
1326-
13271274
@ReactMethod
13281275
public void listAvailableReplicationRequests(long poolID, Promise promise) {
13291276
ThreadUtils.runOnExecutor(() -> {
@@ -1339,51 +1286,6 @@ public void listAvailableReplicationRequests(long poolID, Promise promise) {
13391286
});
13401287
}
13411288

1342-
@ReactMethod
1343-
public void removeReplicationRequest(String seedString, long poolID, String cid, Promise promise) {
1344-
ThreadUtils.runOnExecutor(() -> {
1345-
Log.d("ReactNative", "newReplicationRequest: seedString = " + seedString + "; poolID = " + poolID + "; cid = " + cid);
1346-
try {
1347-
byte[] result = this.fula.manifestRemove(seedString, poolID, cid);
1348-
String resultString = toString(result);
1349-
promise.resolve(resultString);
1350-
} catch (Exception e) {
1351-
Log.d("get", e.getMessage());
1352-
promise.reject(e);
1353-
}
1354-
});
1355-
}
1356-
1357-
@ReactMethod
1358-
public void removeStorer(String seedString, String storage, long poolID, String cid, Promise promise) {
1359-
ThreadUtils.runOnExecutor(() -> {
1360-
Log.d("ReactNative", "removeStorer: seedString = " + seedString + "; storage = " + storage + "; poolID = " + poolID + "; cid = " + cid);
1361-
try {
1362-
byte[] result = this.fula.manifestRemoveStorer(seedString, storage, poolID, cid);
1363-
String resultString = toString(result);
1364-
promise.resolve(resultString);
1365-
} catch (Exception e) {
1366-
Log.d("get", e.getMessage());
1367-
promise.reject(e);
1368-
}
1369-
});
1370-
}
1371-
1372-
@ReactMethod
1373-
public void removeStoredReplication(String seedString, String uploader, long poolID, String cid, Promise promise) {
1374-
ThreadUtils.runOnExecutor(() -> {
1375-
Log.d("ReactNative", "removeStoredReplication: seedString = " + seedString + "; uploader = " + uploader + "; poolID = " + poolID + "; cid = " + cid);
1376-
try {
1377-
byte[] result = this.fula.manifestRemoveStored(seedString, uploader, poolID, cid);
1378-
String resultString = toString(result);
1379-
promise.resolve(resultString);
1380-
} catch (Exception e) {
1381-
Log.d("get", e.getMessage());
1382-
promise.reject(e);
1383-
}
1384-
});
1385-
}
1386-
13871289
@ReactMethod
13881290
private void listRecentCidsAsString(Promise promise) throws Exception {
13891291
ThreadUtils.runOnExecutor(() -> {

example/src/App.tsx

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ const App = () => {
5858
const bloxPeerId_laptop =
5959
'12D3KooWLGatFxDzMrKd4S6UC4GAtuM4zcFJW8RPuMR9SH7j46A8';
6060

61-
const bloxAddr =
62-
'/dns/relay.dev.fx.land/tcp/4001/p2p/12D3KooWDRrBaAfPwsGJivBoUw5fE7ZpDiyfUjqgiURq2DEcL835/p2p-circuit/p2p/' +
63-
bloxPeerId_tower;
64-
//const bloxAddr = '/ip4/192.168.2.87/tcp/40001/p2p/' + bloxPeerId_tower;
61+
//const bloxAddr = '/dns/relay.dev.fx.land/tcp/4001/p2p/12D3KooWDRrBaAfPwsGJivBoUw5fE7ZpDiyfUjqgiURq2DEcL835/p2p-circuit/p2p/' + bloxPeerId_laptop;
62+
const bloxAddr = '/ip4/192.168.2.14/tcp/40001/p2p/' + bloxPeerId_laptop;
6563

6664
const initFula = async () => {
6765
try {
@@ -670,6 +668,44 @@ const App = () => {
670668
}}
671669
color={inprogress ? 'green' : 'blue'}
672670
/>
671+
672+
<Button
673+
title={inprogress ? 'Getting...' : 'Check Balance'}
674+
onPress={async () => {
675+
try {
676+
if (initComplete) {
677+
fula.checkConnection().then((r: any) => {
678+
console.log('connection check');
679+
console.log(r);
680+
if (r) {
681+
console.log('initialization is completed. get account');
682+
blockchain
683+
.getAccount()
684+
.then((res: any) => {
685+
console.log('account received');
686+
console.log(res);
687+
if (res) {
688+
blockchain
689+
.assetsBalance(res.account, '110', '100')
690+
.then((r2: any) => {
691+
console.log('amount received');
692+
console.log(r2);
693+
});
694+
}
695+
})
696+
.catch((e: any) => {
697+
console.log('bloxFreeSpace fetch failed');
698+
console.log(e);
699+
});
700+
}
701+
});
702+
} else {
703+
console.log('wait for init to complete');
704+
}
705+
} catch (e) {}
706+
}}
707+
color={inprogress ? 'green' : 'blue'}
708+
/>
673709
</View>
674710
</View>
675711
);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@functionland/react-native-fula",
3-
"version": "1.14.8",
3+
"version": "1.19.0",
44
"description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/interfaces/fulaNativeModule.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ interface FulaNativeModule {
102102
bloxFreeSpace: () => Promise<string>;
103103
wifiRemoveall: () => Promise<string>;
104104
reboot: () => Promise<string>;
105+
getAccount: () => Promise<string>;
106+
assetsBalance: (
107+
account: string,
108+
assetId: string,
109+
classId: string
110+
) => Promise<string>;
105111
}
106112

107113
const LINKING_ERROR =

src/protocols/blockchain.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,53 @@ export const bloxFreeSpace = (): Promise<BType.BloxFreeSpaceResponse> => {
497497
});
498498
return res;
499499
};
500+
501+
export const getAccount = (): Promise<BType.GetAccountResponse> => {
502+
console.log('getAccount in react-native started');
503+
let res = Fula.getAccount()
504+
.then((res) => {
505+
try {
506+
let jsonRes: BType.GetAccountResponse = JSON.parse(res);
507+
return jsonRes;
508+
} catch (e) {
509+
try {
510+
return JSON.parse(res);
511+
} catch (e1) {
512+
console.error('Error parsing res in get account:', e1);
513+
throw e1; // Rethrow the error to maintain the rejection state
514+
}
515+
}
516+
})
517+
.catch((err) => {
518+
console.error('Error getting account:', err);
519+
throw err; // Rethrow the error to maintain the rejection state
520+
});
521+
return res;
522+
};
523+
524+
export const assetsBalance = (
525+
account: string,
526+
assetId: string,
527+
classId: string
528+
): Promise<BType.AssetsBalanceResponse> => {
529+
console.log('assetsBalance in react-native started');
530+
let res = Fula.assetsBalance(account, assetId, classId)
531+
.then((res) => {
532+
try {
533+
let jsonRes: BType.AssetsBalanceResponse = JSON.parse(res);
534+
return jsonRes;
535+
} catch (e) {
536+
try {
537+
return JSON.parse(res);
538+
} catch (e1) {
539+
console.error('Error parsing res in get asset balance:', e1);
540+
throw e1; // Rethrow the error to maintain the rejection state
541+
}
542+
}
543+
})
544+
.catch((err) => {
545+
console.error('Error getting asset balance:', err);
546+
throw err; // Rethrow the error to maintain the rejection state
547+
});
548+
return res;
549+
};

src/types/blockchain.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ export interface PoolCreateResponse {
1313
poolID: number;
1414
}
1515

16+
export interface GetAccountResponse {
17+
account: string;
18+
}
19+
20+
export interface AssetsBalanceResponse {
21+
amount: string;
22+
}
23+
1624
export interface PoolJoinResponse {
1725
account: string;
1826
poolID: number;

0 commit comments

Comments
 (0)