forked from AstarNetwork/dapp-staking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
55 lines (46 loc) · 1.55 KB
/
index.ts
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
import { ApiPromise, WsProvider } from "@polkadot/api";
import { Keyring } from "@polkadot/keyring";
import {
initApi,
getStakeCall,
subscribeToProtocolStateChanges,
} from "@astar-network/dapp-staking-v3";
// async function protocolState() {
// const wsProvider = new WsProvider("ws://127.0.0.1:9944");
// const api = await ApiPromise.create({ provider: wsProvider });
// initApi(api);
// const unsub = await subscribeToProtocolStateChanges((state) => {
// console.log("Protocol state:", state);
// // unsub();
// });
// }
// protocolState();
/**
* Staking demo
*/
async function stakeTest(): Promise<void> {
// Initialize Polkadot api
const wsProvider = new WsProvider("wss://rpc.shibuya.astar.network");
const api = await ApiPromise.create({ provider: wsProvider });
// Initialize keyring and create a signer account.
const keyring = new Keyring({ type: "sr25519", ss58Format: 5 });
const alice = keyring.addFromUri("//Alice");
// Initialize dApp staking api
initApi(api);
// Stake on a dApp
// Call dApp staking api to get a batch call to lock and stake
const stakeAmount = 5_000_000_000_000_000_000n;
const stakeBatch = await getStakeCall(alice.address, stakeAmount, [
{
address: "0xb196bac674bc0e02e78db10e3c015ed4ec802658",
amount: stakeAmount,
},
]);
// Sign and send the batch call.
await stakeBatch.signAndSend(alice, (result) => {
console.log("Stake result:", result.status.toHuman());
});
}
stakeTest()
.catch((err) => console.error(err.message))
.finally(() => process.exit());