-
Notifications
You must be signed in to change notification settings - Fork 21
/
Vote.ts
191 lines (166 loc) 路 4.2 KB
/
Vote.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { JSONSerializable } from '../../../util/json';
import { AccAddress } from '../../bech32';
import {
Vote as Vote_pb,
VoteOption,
WeightedVoteOption as WeightedVoteOption_pb,
} from '@terra-money/terra.proto/cosmos/gov/v1/gov';
import { Dec, Numeric } from '../../numeric';
import Long from 'long';
/**
* Stores vote information for a proposal
*/
export class Vote extends JSONSerializable<Vote.Amino, Vote.Data, Vote.Proto> {
public Option = VoteOption;
/**
* @param proposal_id ID of proposal to vote on
* @param voter voter's account address
* @param options voting options
*/
constructor(
public proposal_id: number,
public voter: AccAddress,
public options: WeightedVoteOption[]
) {
super();
}
public static fromAmino(data: Vote.Amino, _?: boolean): Vote {
_;
const { proposal_id, voter, options } = data;
return new Vote(
Number.parseInt(proposal_id),
voter,
options.map(v => WeightedVoteOption.fromAmino(v))
);
}
public toAmino(_?: boolean): Vote.Amino {
_;
const { proposal_id, voter, options } = this;
const res: Vote.Amino = {
proposal_id: proposal_id.toFixed(),
voter,
options: options.map(v => v.toAmino()),
};
return res;
}
public static fromData(data: Vote.Data, _?: boolean): Vote {
_;
const { proposal_id, voter, options } = data;
return new Vote(
Number.parseInt(proposal_id),
voter,
options.map(v => WeightedVoteOption.fromData(v))
);
}
public toData(_?: boolean): Vote.Data {
_;
const { proposal_id, voter, options } = this;
const res: Vote.Data = {
proposal_id: proposal_id.toFixed(),
voter,
options: options.map(v => v.toData()),
};
return res;
}
public static fromProto(proto: Vote.Proto, _?: boolean): Vote {
_;
return new Vote(
proto.proposalId.toNumber(),
proto.voter,
proto.options.map(o => WeightedVoteOption.fromProto(o))
);
}
public toProto(_?: boolean): Vote.Proto {
_;
const { proposal_id, voter, options } = this;
return Vote_pb.fromPartial({
options: options.map(o => o.toProto()),
proposalId: Long.fromNumber(proposal_id),
voter,
});
}
}
export namespace Vote {
export const Option = VoteOption;
export type Option = VoteOption;
export interface Amino {
proposal_id: string;
voter: AccAddress;
options: WeightedVoteOption.Amino[];
}
export interface Data {
proposal_id: string;
voter: AccAddress;
options: WeightedVoteOption.Data[];
}
export type Proto = Vote_pb;
}
export class WeightedVoteOption extends JSONSerializable<
WeightedVoteOption.Amino,
WeightedVoteOption.Data,
WeightedVoteOption.Proto
> {
public weight: Dec;
constructor(public option: VoteOption, weight: Numeric.Input) {
super();
this.weight = new Dec(weight);
}
public static fromAmino(
data: WeightedVoteOption.Amino,
_?: boolean
): WeightedVoteOption {
_;
const { option, weight } = data;
return new WeightedVoteOption(option, weight);
}
public toAmino(_?: boolean): WeightedVoteOption.Amino {
_;
const { option, weight } = this;
return {
option,
weight: weight.toString(),
};
}
public static fromData(
data: WeightedVoteOption.Data,
_?: boolean
): WeightedVoteOption {
_;
const { option, weight } = data;
return new WeightedVoteOption(option, weight);
}
public toData(_?: boolean): WeightedVoteOption.Data {
_;
const { option, weight } = this;
return {
option,
weight: weight.toString(),
};
}
public static fromProto(
proto: WeightedVoteOption.Proto,
_?: boolean
): WeightedVoteOption {
_;
return new WeightedVoteOption(proto.option, proto.weight);
}
public toProto(_?: boolean): WeightedVoteOption.Proto {
_;
const { option, weight } = this;
return WeightedVoteOption_pb.fromPartial({
option,
weight: weight.toString(),
});
}
}
export namespace WeightedVoteOption {
export interface Amino {
option: VoteOption;
weight: string;
}
export interface Data {
option: VoteOption;
weight: string;
}
export type Proto = WeightedVoteOption_pb;
}