-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
176 lines (160 loc) · 4.69 KB
/
App.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, {Fragment} from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import { ApiPromise, WsProvider } from '@polkadot/api';
import { Keyring } from '@polkadot/keyring';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
blockNumber: 0,
chainInfo: '',
aliceBalance: 0,
transactionHash: ''
}
// Initialise the provider to connect to the local node
const providerUrl = Platform.select({
ios: "ws://localhost:9944",
android: "ws://10.0.2.2:9944"
});
this.provider = new WsProvider(providerUrl);
}
async componentDidMount() {
const api = await ApiPromise.create({
provider: this.provider
});
// Retrieve the chain & node information information via rpc calls
const [chain, nodeName, nodeVersion] = await Promise.all([
api.rpc.system.chain(),
api.rpc.system.name(),
api.rpc.system.version()
]);
this.setState({
chainInfo: `You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`
});
// get latest block number
const unsubscribe = await api.rpc.chain.subscribeNewHead(
header => {
this.setState({
blockNumber: `${header.blockNumber}`
});
}
);
// get alice balance
const Alice = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
let aliceBalance = await api.query.balances.freeBalance(Alice);
const Gavin = "5D18bgh1CtovjnMgJ6eEe8c7ZmKgenR8onjYwD5qeKNbp5QU";
let gavinBalance = await api.query.balances.freeBalance(Gavin);
this.setState({
aliceBalance: aliceBalance.toString(),
gavinBalance: gavinBalance.toString()
})
// transfer 500 to Gavin
const keyring = new Keyring({ type: "sr25519" });
const alice = keyring.addFromUri("//Alice");
// const keyring = new Keyring({ type: "ed25519" });
// const gavin = keyring.addFromUri("//Gavin");
// gavin.address equals to "5D18bgh1CtovjnMgJ6eEe8c7ZmKgenR8onjYwD5qeKNbp5QU"
// alice.address = 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
const extrinsic = api.tx.balances.transfer(Gavin, 500);
// Sign and send the transaction using our account
const hash = await extrinsic.signAndSend(alice);
this.setState({
transactionHash: hash.toString(16)
})
}
render() {
let {
blockNumber,
chainInfo,
aliceBalance,
gavinBalance,
transactionHash
} = this.state;
return (
<Fragment>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}
>
<Header />
<View style={styles.body}>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Chain Information</Text>
<Text style={styles.sectionDescription}>{chainInfo}</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Block Number</Text>
<Text style={styles.sectionDescription}>
Current Block Number: {blockNumber}
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Read Chain State</Text>
<Text style={styles.sectionDescription}>
Alice balance: {aliceBalance} Unit {"\n"}
Gavin balance: {gavinBalance} Unit
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Transfer from Alice to Gavin</Text>
<Text style={styles.sectionDescription}>
TX: {transactionHash}
</Text>
</View>
</View>
</ScrollView>
</SafeAreaView>
</Fragment>
);
}
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
color: Colors.dark,
},
highlight: {
fontWeight: '700',
},
});
export default App;