-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.ts
103 lines (86 loc) · 2.17 KB
/
main_test.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
96
97
98
99
100
101
102
103
import { doMigrateBalances } from "./main.ts";
import {
AccountStorageKey,
BalanceData,
LocalAccountFetcher,
} from "./account-fetch.ts";
import { blake2AsHex, encodeAddress } from "@polkadot/util-crypto/mod.ts";
import { Ss58AccountId } from "./util.ts";
import { assertEquals } from "std/assert/mod.ts";
function makeSpec(accounts: [string, bigint][]) {
return {
genesis: {
runtime: {
balances: {
balances: accounts,
},
},
},
};
}
let seed = 0;
function randomSs58() {
const account = blake2AsHex(`seed${seed++}, 32`);
return encodeAddress(account, 42);
}
function randomAccountKey(index: number) {
const address = randomSs58();
return {
accountId: () => new Ss58AccountId(address),
key: index,
};
}
class AccountsBuilder {
private accounts: [AccountStorageKey, BalanceData][] = [];
private index = 0;
add(free: bigint, reserved = 0n, miscFrozen = 0n, feeFrozen = 0n) {
const account = randomAccountKey(this.index++);
this.accounts.push([
account,
{
free,
reserved,
miscFrozen,
feeFrozen,
},
]);
return this;
}
addMany(count: number, free: bigint, reserved = 0n) {
for (let i = 0; i < count; i++) {
this.add(free, reserved);
}
return this;
}
build() {
return this.accounts;
}
}
function expected(
accounts: [AccountStorageKey, BalanceData][],
): [string, bigint][] {
return accounts.map(
([key, { free, reserved }]) =>
[key.accountId().value, free + reserved] as const,
);
}
// deno-lint-ignore no-explicit-any
function assertSpecEquals(actual: any, expected: any) {
actual.genesis.runtime.balances.balances.sort();
expected.genesis.runtime.balances.balances.sort();
assertEquals(actual, expected);
}
Deno.test("basic", async () => {
const accounts = new AccountsBuilder()
.add(100n, 5n)
.add(200n)
.add(300n)
.add(400n)
.add(500n)
.build();
const input = makeSpec([]);
const fetcher = new LocalAccountFetcher(accounts, 2);
const result = await doMigrateBalances(fetcher, input, {});
const expect = makeSpec(expected(accounts));
assertSpecEquals(result, expect);
});