-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (49 loc) · 1.69 KB
/
index.js
File metadata and controls
58 lines (49 loc) · 1.69 KB
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
const { ApiPromise, WsProvider } = require('@polkadot/api');
import {
isWeb3Injected,
web3Accounts,
web3Enable,
web3FromAddress
} from "@polkadot/extension-dapp";
web3Enable('polkadot-js/apps');
// Samples
class PolkadotWeb3JSSample {
/***
* login
* @return accounts [{"address":"5D2JMakX2CgtPPkiqUzdsK3Y41vD6HyNy8ZETUjhjRrZFTfG","meta":{"name":"cc1","source":"polkadot-js"}}]
*/
async login() {
if (!isWeb3Injected) {
throw new Error("Please install/unlock the MathWallet first");
}
// meta.source contains the name of the extension that provides this account
const allAccounts = await web3Accounts();
return allAccounts;
}
/***
* Transfer
* @param from from
* @param to to
* @param amount amount
* @return hash
*/
async transfer(from, to, amount) {
//////////////////////////// alexander ////////////////////////////
// Initialise the provider to connect to the local node
const provider = new WsProvider('wss://rpc.polkadot.io');
// Create the API and wait until ready
const api = await ApiPromise.create({ provider });
// finds an injector for an address
const injector = await web3FromAddress(from);
// sets the signer for the address on the @polkadot/api
api.setSigner(injector.signer);
// sign and send out transaction - notice here that the address of the account (as retrieved injected)
// is passed through as the param to the `signAndSend`, the API then calls the extension to present
// to the user and get it signed. Once completex, the api sends the tx + signature via the normal process
const h = api.tx.balances
.transfer(to, amount)
.signAndSend(from);
return h;
}
}
window.PolkadotWeb3JSSample = new PolkadotWeb3JSSample();