-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
95 lines (82 loc) · 3.18 KB
/
main.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
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
import { ApiPromise, WsProvider } from "@polkadot/api";
import { SignerOptions, SubmittableExtrinsic } from "@polkadot/api/types";
import { Keyring } from '@polkadot/keyring';
import { KeyringPair } from "@polkadot/keyring/types";
import { cryptoWaitReady } from "@polkadot/util-crypto";
import { Region, RegionId } from "coretime-utils";
const cleanup = async () => {
console.log("Beginning the cleanup 🧹🗑️");
const keyring = new Keyring({ type: 'sr25519', ss58Format: 42 });
await cryptoWaitReady();
const cleaner = keyring.addFromMnemonic('PASTE MNEMONIC HERE');
const wsCoretimeProvider = new WsProvider(
"wss://rococo-coretime-rpc.polkadot.io/",
);
const wsRelayProvider = new WsProvider("wss://rococo-rpc.polkadot.io/");
const coretimeApi = await ApiPromise.create({ provider: wsCoretimeProvider });
const relayApi = await ApiPromise.create({ provider: wsRelayProvider });
const regions = (await coretimeApi.query.broker.regions.entries()).map(
(e) => {
const key = (e[0].toHuman() as any)[0];
const regionId = {begin: parseHNString(key.begin), core: parseHNString(key.core), mask: key.mask}
return new Region(regionId, e[1].toJSON() as any);
},
);
const relayHeight = Number((await relayApi.query.system.number()).toJSON());
let expiredRegions: Array<RegionId> = [];
for(const region of regions) {
const consumed = region.consumed({ relayBlockNumber: relayHeight, timeslicePeriod: 80 });
const expired = consumed >= 1;
if(expired) {
expiredRegions.push(region.getRegionId());
}
};
console.log('Number of expired regions: ' + expiredRegions.length);
for(let i = 0; i < expiredRegions.length; i += 20) {
// Chunks of 20:
let calls: any = [];
for(let j = i; j < i + 20; j++) {
if(!expiredRegions[j]) break;
calls.push(coretimeApi.tx.broker.dropRegion(expiredRegions[j]));
}
await submitExtrinsic(cleaner, coretimeApi.tx.utility.batch(calls), {});
}
console.log("Cleaned up ✨✨✨");
};
cleanup();
export async function submitExtrinsic(
signer: KeyringPair,
call: SubmittableExtrinsic<"promise">,
options: Partial<SignerOptions>
): Promise<void> {
try {
return new Promise((resolve, _reject) => {
const unsub = call.signAndSend(signer, options, (result) => {
console.log(`Current status is ${result.status}`);
if (result.status.isInBlock) {
console.log(`Transaction included at blockHash ${result.status.asInBlock}`);
// don't wait for finalization.
unsub.then();
return resolve();
} else if (result.status.isFinalized) {
console.log(`Transaction finalized at blockHash ${result.status.asFinalized}`);
unsub.then();
return resolve();
} else if (result.isError) {
console.log("Transaction error");
unsub.then();
return resolve();
}
});
});
} catch (e) {
console.log(e);
}
}
// parse human readable number string
export const parseHNString = (str: string): number => {
return parseInt(parseHNStringToString(str));
};
export const parseHNStringToString = (str: string): string => {
return str.replace(/,/g, '');
};