-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathordinals.ts
46 lines (38 loc) · 1.23 KB
/
ordinals.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
import { getBlock, getBlockHashByHeight } from './src/utils';
function subsidy(height: number) {
return (
(BigInt(50) * BigInt(100_000_000)) >> (BigInt(height) / BigInt(210_000n))
);
}
function firstOrdinal(height: number) {
let start = BigInt(0);
for (let i = 0; i < height; i++) {
start += subsidy(Number(i));
}
return start;
}
function assignOrdinals(height: number) {
const first = firstOrdinal(height);
const last = first + subsidy(height);
let coinbaseOrdinals: bigint[] = Array.from(
{ length: Number(last - first) },
(_, i) => first + BigInt(i)
);
const blockHash = await getBlockHashByHeight(height);
const block = await getBlock(blockHash);
for (const tx of block.tx) {
const ordinals: bigint[] = [];
for (const input of transaction.inputs) {
ordinals.push(...input.ordinals);
}
for (const output of transaction.outputs) {
output.ordinals = ordinals.slice(0, Number(output.value));
ordinals.splice(0, Number(output.value));
}
coinbaseOrdinals.push(...ordinals);
}
for (const output of block.transactions[0].outputs) {
output.ordinals = coinbaseOrdinals.slice(0, Number(output.value));
coinbaseOrdinals.splice(0, Number(output.value));
}
}