-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathposition.ts
143 lines (134 loc) · 3.94 KB
/
position.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { BorshCoder } from "@coral-xyz/anchor";
import { PublicKey } from "@solana/web3.js";
import { convertBNToBigInt } from "./bn";
import type { Staking } from "../../types/staking";
import {
POSITION_BUFFER_SIZE,
POSITIONS_ACCOUNT_HEADER_SIZE,
} from "../constants";
import type {
Position,
PositionAnchor,
StakeAccountPositions,
TargetWithParameters,
} from "../types";
import { PositionState } from "../types";
export const getPositionState = (
position: Position,
currentEpoch: bigint,
): PositionState => {
if (currentEpoch < position.activationEpoch) {
return PositionState.LOCKING;
}
if (!position.unlockingStart) {
return PositionState.LOCKED;
}
const hasActivated = position.activationEpoch <= currentEpoch;
const unlockStarted = position.unlockingStart <= currentEpoch;
const unlockEnded = position.unlockingStart + 1n <= currentEpoch;
if (hasActivated && !unlockStarted) {
return PositionState.PREUNLOCKING;
} else if (unlockStarted && !unlockEnded) {
return PositionState.UNLOCKING;
} else {
return PositionState.UNLOCKED;
}
};
export const getAmountByTargetAndState = (options: {
stakeAccountPositions: StakeAccountPositions;
targetWithParameters: TargetWithParameters;
positionState: PositionState;
epoch: bigint;
}): bigint => {
const { stakeAccountPositions, targetWithParameters, positionState, epoch } =
options;
return stakeAccountPositions.data.positions
.filter((p) => getPositionState(p, epoch) === positionState)
.filter((p) => {
if (targetWithParameters.voting) {
return p.targetWithParameters.voting !== undefined;
}
return (
p.targetWithParameters.integrityPool?.publisher &&
targetWithParameters.integrityPool.publisher.equals(
p.targetWithParameters.integrityPool.publisher,
)
);
})
.map((p) => p.amount)
.reduce((sum, amount) => sum + amount, 0n);
};
export const deserializeStakeAccountPositions = (
address: PublicKey,
data: Buffer,
idl: Staking,
) => {
const coder = new BorshCoder(idl);
let i = 8; // Skip discriminator
const owner = new PublicKey(data.subarray(i, i + 32));
const numberOfPositions = Math.floor(
(data.length - POSITIONS_ACCOUNT_HEADER_SIZE) / POSITION_BUFFER_SIZE,
);
i += 32;
const positions: PositionAnchor[] = [];
for (let j = 0; j < numberOfPositions; j++) {
if (data[i] === 1) {
positions.push(coder.types.decode("position", data.subarray(i + 1)));
}
i += POSITION_BUFFER_SIZE;
}
return {
address,
data: {
owner,
positions: positions.map((p) => convertBNToBigInt(p)),
},
};
};
export const getVotingTokenAmount = (
stakeAccountPositions: StakeAccountPositions,
epoch: bigint,
) => {
const positions = stakeAccountPositions.data.positions;
const votingPositions = positions
.filter((p) => p.targetWithParameters.voting)
.filter((p) =>
[PositionState.LOCKED, PositionState.PREUNLOCKING].includes(
getPositionState(p, epoch),
),
);
const totalVotingTokenAmount = votingPositions.reduce(
(sum, p) => sum + p.amount,
0n,
);
return totalVotingTokenAmount;
};
export const summarizeAccountPositions = (
positions: StakeAccountPositions,
epoch: bigint,
) => {
const summary = {
voting: {
[PositionState.LOCKED]: 0n,
[PositionState.LOCKING]: 0n,
[PositionState.PREUNLOCKING]: 0n,
[PositionState.UNLOCKED]: 0n,
[PositionState.UNLOCKING]: 0n,
},
integrityPool: {
[PositionState.LOCKED]: 0n,
[PositionState.LOCKING]: 0n,
[PositionState.PREUNLOCKING]: 0n,
[PositionState.UNLOCKED]: 0n,
[PositionState.UNLOCKING]: 0n,
},
};
for (const position of positions.data.positions) {
const category = position.targetWithParameters.voting
? "voting"
: "integrityPool";
const state = getPositionState(position, epoch);
summary[category][state] += position.amount;
}
return summary;
};