Skip to content

Commit

Permalink
Merge pull request #75 from functionland/getAccountBalance
Browse files Browse the repository at this point in the history
get account id and asset balance
  • Loading branch information
ehsan6sha authored Dec 4, 2023
2 parents 95facd8 + 3cac408 commit a056295
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 115 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.15.1' // From jitpack.io
implementation 'com.github.functionland:fula-build-aar:v1.19.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
120 changes: 11 additions & 109 deletions android/src/main/java/land/fx/fula/FulaModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -1151,21 +1151,13 @@ public void shutdown(Promise promise) {
//////////////////////ANYTHING BELOW IS FOR BLOCKCHAIN/////
///////////////////////////////////////////////////////////
@ReactMethod
public void createAccount(String seedString, Promise promise) {
public void GetAccount(Promise promise) {
ThreadUtils.runOnExecutor(() -> {
Log.d("ReactNative", "createAccount: seedString = " + seedString);
Log.d("ReactNative", "getAccount called ");
try {
if (this.fula == null || this.fula.id() == null || this.fula.id().isEmpty()) {
promise.reject(new Error("Fula client is not initialized"));
} else {

if (!seedString.startsWith("/")) {
promise.reject(new Error("seed should start with /"));
}
byte[] result = this.fula.seeded(seedString);
String resultString = toString(result);
promise.resolve(resultString);
}
byte[] result = this.fula.getAccount();
String resultString = toString(result);
promise.resolve(resultString);
} catch (Exception e) {
Log.d("get", e.getMessage());
promise.reject(e);
Expand All @@ -1174,11 +1166,11 @@ public void createAccount(String seedString, Promise promise) {
}

@ReactMethod
public void checkAccountExists(String accountString, Promise promise) {
public void AssetsBalance(String account, String assetId, String classId, Promise promise) {
ThreadUtils.runOnExecutor(() -> {
Log.d("ReactNative", "checkAccountExists: accountString = " + accountString);
Log.d("ReactNative", "getAccount called ");
try {
byte[] result = this.fula.accountExists(accountString);
byte[] result = this.fula.assetsBalance(account, assetId, classId);
String resultString = toString(result);
promise.resolve(resultString);
} catch (Exception e) {
Expand All @@ -1189,11 +1181,11 @@ public void checkAccountExists(String accountString, Promise promise) {
}

@ReactMethod
public void createPool(String seedString, String poolName, Promise promise) {
public void checkAccountExists(String accountString, Promise promise) {
ThreadUtils.runOnExecutor(() -> {
Log.d("ReactNative", "createPool: seedString = " + seedString + "; poolName = " + poolName);
Log.d("ReactNative", "checkAccountExists: accountString = " + accountString);
try {
byte[] result = this.fula.poolCreate(seedString, poolName);
byte[] result = this.fula.accountExists(accountString);
String resultString = toString(result);
promise.resolve(resultString);
} catch (Exception e) {
Expand Down Expand Up @@ -1264,21 +1256,6 @@ public void listPoolJoinRequests(long poolID, Promise promise) {
});
}

@ReactMethod
public void votePoolJoinRequest(String seedString, long poolID, String accountString, boolean accept, Promise promise) {
ThreadUtils.runOnExecutor(() -> {
Log.d("ReactNative", "votePoolJoinRequest: seedString = " + seedString + "; poolID = " + poolID + "; accountString = " + accountString + "; accept = " + accept);
try {
byte[] result = this.fula.poolVote(seedString, poolID, accountString, accept);
String resultString = toString(result);
promise.resolve(resultString);
} catch (Exception e) {
Log.d("get", e.getMessage());
promise.reject(e);
}
});
}

@ReactMethod
public void leavePool(long poolID, Promise promise) {
ThreadUtils.runOnExecutor(() -> {
Expand All @@ -1294,36 +1271,6 @@ public void leavePool(long poolID, Promise promise) {
});
}

@ReactMethod
public void newReplicationRequest(String seedString, long poolID, long replicationFactor, String cid, Promise promise) {
ThreadUtils.runOnExecutor(() -> {
Log.d("ReactNative", "newReplicationRequest: seedString = " + seedString + "; poolID = " + poolID + "; replicationFactor = " + replicationFactor + "; cid = " + cid);
try {
byte[] result = this.fula.manifestUpload(seedString, poolID, replicationFactor, cid);
String resultString = toString(result);
promise.resolve(resultString);
} catch (Exception e) {
Log.d("get", e.getMessage());
promise.reject(e);
}
});
}

@ReactMethod
public void newStoreRequest(String seedString, long poolID, String uploader, String cid, Promise promise) {
ThreadUtils.runOnExecutor(() -> {
Log.d("ReactNative", "newStoreRequest: seedString = " + seedString + "; poolID = " + poolID + "; uploader = " + uploader + "; cid = " + cid);
try {
byte[] result = this.fula.manifestStore(seedString, poolID, uploader, cid);
String resultString = toString(result);
promise.resolve(resultString);
} catch (Exception e) {
Log.d("get", e.getMessage());
promise.reject(e);
}
});
}

@ReactMethod
public void listAvailableReplicationRequests(long poolID, Promise promise) {
ThreadUtils.runOnExecutor(() -> {
Expand All @@ -1339,51 +1286,6 @@ public void listAvailableReplicationRequests(long poolID, Promise promise) {
});
}

@ReactMethod
public void removeReplicationRequest(String seedString, long poolID, String cid, Promise promise) {
ThreadUtils.runOnExecutor(() -> {
Log.d("ReactNative", "newReplicationRequest: seedString = " + seedString + "; poolID = " + poolID + "; cid = " + cid);
try {
byte[] result = this.fula.manifestRemove(seedString, poolID, cid);
String resultString = toString(result);
promise.resolve(resultString);
} catch (Exception e) {
Log.d("get", e.getMessage());
promise.reject(e);
}
});
}

@ReactMethod
public void removeStorer(String seedString, String storage, long poolID, String cid, Promise promise) {
ThreadUtils.runOnExecutor(() -> {
Log.d("ReactNative", "removeStorer: seedString = " + seedString + "; storage = " + storage + "; poolID = " + poolID + "; cid = " + cid);
try {
byte[] result = this.fula.manifestRemoveStorer(seedString, storage, poolID, cid);
String resultString = toString(result);
promise.resolve(resultString);
} catch (Exception e) {
Log.d("get", e.getMessage());
promise.reject(e);
}
});
}

@ReactMethod
public void removeStoredReplication(String seedString, String uploader, long poolID, String cid, Promise promise) {
ThreadUtils.runOnExecutor(() -> {
Log.d("ReactNative", "removeStoredReplication: seedString = " + seedString + "; uploader = " + uploader + "; poolID = " + poolID + "; cid = " + cid);
try {
byte[] result = this.fula.manifestRemoveStored(seedString, uploader, poolID, cid);
String resultString = toString(result);
promise.resolve(resultString);
} catch (Exception e) {
Log.d("get", e.getMessage());
promise.reject(e);
}
});
}

@ReactMethod
private void listRecentCidsAsString(Promise promise) throws Exception {
ThreadUtils.runOnExecutor(() -> {
Expand Down
44 changes: 40 additions & 4 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ const App = () => {
const bloxPeerId_laptop =
'12D3KooWLGatFxDzMrKd4S6UC4GAtuM4zcFJW8RPuMR9SH7j46A8';

const bloxAddr =
'/dns/relay.dev.fx.land/tcp/4001/p2p/12D3KooWDRrBaAfPwsGJivBoUw5fE7ZpDiyfUjqgiURq2DEcL835/p2p-circuit/p2p/' +
bloxPeerId_tower;
//const bloxAddr = '/ip4/192.168.2.87/tcp/40001/p2p/' + bloxPeerId_tower;
//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 {
Expand Down Expand Up @@ -670,6 +668,44 @@ const App = () => {
}}
color={inprogress ? 'green' : 'blue'}
/>

<Button
title={inprogress ? 'Getting...' : 'Check Balance'}
onPress={async () => {
try {
if (initComplete) {
fula.checkConnection().then((r: any) => {
console.log('connection check');
console.log(r);
if (r) {
console.log('initialization is completed. get account');
blockchain
.getAccount()
.then((res: any) => {
console.log('account received');
console.log(res);
if (res) {
blockchain
.assetsBalance(res.account, '110', '100')
.then((r2: any) => {
console.log('amount received');
console.log(r2);
});
}
})
.catch((e: any) => {
console.log('bloxFreeSpace fetch failed');
console.log(e);
});
}
});
} else {
console.log('wait for init to complete');
}
} catch (e) {}
}}
color={inprogress ? 'green' : 'blue'}
/>
</View>
</View>
);
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.14.8",
"version": "1.19.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
6 changes: 6 additions & 0 deletions src/interfaces/fulaNativeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ interface FulaNativeModule {
bloxFreeSpace: () => Promise<string>;
wifiRemoveall: () => Promise<string>;
reboot: () => Promise<string>;
getAccount: () => Promise<string>;
assetsBalance: (
account: string,
assetId: string,
classId: string
) => Promise<string>;
}

const LINKING_ERROR =
Expand Down
50 changes: 50 additions & 0 deletions src/protocols/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,53 @@ export const bloxFreeSpace = (): Promise<BType.BloxFreeSpaceResponse> => {
});
return res;
};

export const getAccount = (): Promise<BType.GetAccountResponse> => {
console.log('getAccount in react-native started');
let res = Fula.getAccount()
.then((res) => {
try {
let jsonRes: BType.GetAccountResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e1) {
console.error('Error parsing res in get account:', e1);
throw e1; // Rethrow the error to maintain the rejection state
}
}
})
.catch((err) => {
console.error('Error getting account:', err);
throw err; // Rethrow the error to maintain the rejection state
});
return res;
};

export const assetsBalance = (
account: string,
assetId: string,
classId: string
): Promise<BType.AssetsBalanceResponse> => {
console.log('assetsBalance in react-native started');
let res = Fula.assetsBalance(account, assetId, classId)
.then((res) => {
try {
let jsonRes: BType.AssetsBalanceResponse = JSON.parse(res);
return jsonRes;
} catch (e) {
try {
return JSON.parse(res);
} catch (e1) {
console.error('Error parsing res in get asset balance:', e1);
throw e1; // Rethrow the error to maintain the rejection state
}
}
})
.catch((err) => {
console.error('Error getting asset balance:', err);
throw err; // Rethrow the error to maintain the rejection state
});
return res;
};
8 changes: 8 additions & 0 deletions src/types/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ export interface PoolCreateResponse {
poolID: number;
}

export interface GetAccountResponse {
account: string;
}

export interface AssetsBalanceResponse {
amount: string;
}

export interface PoolJoinResponse {
account: string;
poolID: number;
Expand Down

0 comments on commit a056295

Please sign in to comment.