forked from hashgraph/hedera-sdk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
116 lines (99 loc) · 3.2 KB
/
App.tsx
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
import { useEffect, useState } from "react";
import { StatusBar } from "expo-status-bar";
import { Alert, StyleSheet, Text, View } from "react-native";
import "@ethersproject/shims";
import {
Client,
AccountId,
TransferTransaction,
AccountBalanceQuery,
AccountInfoQuery,
PrivateKey,
Mnemonic,
TransactionResponse,
AccountInfo,
AccountBalance,
} from "@hashgraph/sdk";
import { OPERATOR_ID, OPERATOR_KEY } from "@env";
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
alignContent: "center",
padding: 20,
},
});
const App = () => {
const operatorId = AccountId.fromString(OPERATOR_ID);
const operatorKey = PrivateKey.fromString(OPERATOR_KEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
const [transaction, setTransaction] = useState<TransactionResponse | null>(
null
);
const [info, setInfo] = useState<AccountInfo | null>(null);
const [balance, setBalance] = useState<AccountBalance | null>(null);
const [mnemonic, setMnemonic] = useState<Mnemonic | null>(null);
useEffect(() => {
const init = async () => {
try {
const response = await new TransferTransaction()
.addHbarTransfer(operatorId, -1)
.addHbarTransfer("0.0.3", 1)
.execute(client);
setTransaction(response);
} catch (err: any) {
Alert.alert(err.toString());
}
try {
const info = await new AccountInfoQuery()
.setAccountId(operatorId)
.execute(client);
setInfo(info);
} catch (err: any) {
Alert.alert(err.toString());
}
try {
const balance = await new AccountBalanceQuery()
.setAccountId(operatorId)
.execute(client);
setBalance(balance);
} catch (err: any) {
Alert.alert(err.toString());
}
try {
const mnemonic = await Mnemonic.generate12();
setMnemonic(mnemonic);
} catch (err: any) {
Alert.alert(err.toString());
}
};
init();
}, []);
return (
<View style={styles.container}>
<StatusBar style="auto" />
{transaction && (
<Text testID="transactionId">
TransactionId: {transaction.transactionId.toString()}
</Text>
)}
{info && (
<Text testID="info">Info: {info.accountId.toString()}</Text>
)}
{balance && (
<Text testID="balance">
Balance: {balance.hbars.toString()}
</Text>
)}
{mnemonic && (
<Text testID="mnemonic">
Mnemonic: {mnemonic._mnemonic.toString()}
</Text>
)}
<StatusBar style="auto" />
</View>
);
};
export default App;