-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathstake.ts
217 lines (197 loc) · 5.43 KB
/
stake.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import BN = require('bn.js')
import gql from 'graphql-tag'
import { Observable } from 'rxjs'
import { first } from 'rxjs/operators'
import { Arc, IApolloQueryOptions } from './arc'
import { IProposalOutcome} from './proposal'
import { Address, ICommonQueryOptions, IStateful } from './types'
import { createGraphQlQuery, isAddress } from './utils'
export interface IStakeStaticState {
id?: string
staker: Address
createdAt: Date | undefined
outcome: IProposalOutcome
amount: BN // amount staked
proposal: string
}
export interface IStakeState extends IStakeStaticState {
id: string
}
export interface IStakeQueryOptions extends ICommonQueryOptions {
where?: {
id?: string
staker?: Address
dao?: Address
proposal?: string
createdAt?: number
[key: string]: any
}
}
export class Stake implements IStateful<IStakeState> {
public static fragments = {
StakeFields: gql`fragment StakeFields on ProposalStake {
id
createdAt
dao {
id
}
staker
proposal {
id
}
outcome
amount
}`
}
/**
* Stake.search(context, options) searches for stake entities
* @param context an Arc instance that provides connection information
* @param options the query options, cf. IStakeQueryOptions
* @return an observable of Stake objects
*/
public static search(
context: Arc,
options: IStakeQueryOptions = {},
apolloQueryOptions: IApolloQueryOptions = {}
): Observable <Stake[]> {
if (!options.where) { options.where = {}}
let where = ''
const proposalId = options.where.proposal
// if we are searching for stakes on a specific proposal (a common case), we
// will structure the query so that stakes are stored in the cache together wit the proposal
if (proposalId) {
delete options.where.proposal
}
for (const key of Object.keys(options.where)) {
if (options.where[key] === undefined) {
continue
}
if (key === 'staker' || key === 'dao') {
const option = options.where[key] as string
isAddress(option)
options.where[key] = option.toLowerCase()
}
where += `${key}: "${options.where[key] as string}"\n`
}
let query
const itemMap = (r: any) => {
let outcome: IProposalOutcome = IProposalOutcome.Pass
if (r.outcome === 'Pass') {
outcome = IProposalOutcome.Pass
} else if (r.outcome === 'Fail') {
outcome = IProposalOutcome.Fail
} else {
throw new Error(`Unexpected value for proposalStakes.outcome: ${r.outcome}`)
}
return new Stake({
amount: new BN(r.amount || 0),
createdAt: r.createdAt,
id: r.id,
outcome,
proposal: r.proposal.id,
staker: r.staker
}, context)
}
if (proposalId) {
query = gql`query ProposalStakesSearchFromProposal
{
proposal (id: "${proposalId}") {
id
stakes ${createGraphQlQuery(options, where)} {
...StakeFields
}
}
}
${Stake.fragments.StakeFields}
`
return context.getObservableObject(
query,
(r: any) => {
if (r === null) { // no such proposal was found
return []
}
const stakes = r.stakes
return stakes.map(itemMap)
},
apolloQueryOptions
) as Observable<Stake[]>
} else {
query = gql`query ProposalStakesSearch
{
proposalStakes ${createGraphQlQuery(options, where)} {
...StakeFields
}
}
${Stake.fragments.StakeFields}
`
return context.getObservableList(
query,
itemMap,
apolloQueryOptions
) as Observable<Stake[]>
}
}
public id: string|undefined
public staticState: IStakeStaticState|undefined
constructor(
idOrOpts: string|IStakeStaticState,
public context: Arc
) {
if (typeof idOrOpts === 'string') {
this.id = idOrOpts
} else {
this.id = idOrOpts.id
this.setStaticState(idOrOpts as IStakeStaticState)
}
}
public state(apolloQueryOptions: IApolloQueryOptions = {}): Observable<IStakeState> {
const query = gql`query StakeState
{
proposalStake (id: "${this.id}") {
id
createdAt
staker
proposal {
id
}
outcome
amount
}
}
`
const itemMap = (item: any): IStakeState => {
if (item === null) {
throw Error(`Could not find a Stake with id ${this.id}`)
}
this.setStaticState({
amount: item.reputation,
createdAt: item.createdAt,
id: item.id,
outcome: item.outcome,
proposal: item.proposal.id,
staker: item.staker
})
return {
amount: item.reputation,
createdAt: item.createdAt,
id: item.id,
outcome: item.outcome,
proposal: item.proposal.id,
staker: item.staker
}
}
return this.context.getObservableObject(query, itemMap, apolloQueryOptions)
}
public setStaticState(opts: IStakeStaticState) {
this.staticState = opts
}
public async fetchStaticState(): Promise<IStakeStaticState> {
if (!!this.staticState) {
return this.staticState
} else {
const state = await this.state({subscribe: false}).pipe(first()).toPromise()
this.setStaticState(state)
return state
}
}
}