-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmember.ts
281 lines (257 loc) · 8.65 KB
/
member.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import BN = require('bn.js')
import gql from 'graphql-tag'
import { Observable } from 'rxjs'
import { first, map } from 'rxjs/operators'
import { Arc, IApolloQueryOptions } from './arc'
import { DAO } from './dao'
import { toIOperationObservable } from './operation'
import { IProposalQueryOptions, Proposal } from './proposal'
import { Reward } from './reward'
import { IStakeQueryOptions, Stake } from './stake'
import { Address, ICommonQueryOptions, IStateful } from './types'
import { concat, createGraphQlQuery, hexStringToUint8Array,
isAddress
// stringToUint8Array
} from './utils'
import { IVoteQueryOptions, Vote } from './vote'
export interface IMemberStaticState {
id?: string,
address: Address,
contract?: Address,
dao?: Address
}
export interface IMemberState extends IMemberStaticState {
contract: Address
id: string
reputation: BN
}
export interface IMemberQueryOptions extends ICommonQueryOptions {
where?: {
id?: string
address?: Address,
dao?: Address
}
}
/**
* Represents an account that holds reputaion in a specific DAO
*/
export class Member implements IStateful<IMemberState> {
public static fragments = {
ReputationHolderFields: gql`
fragment ReputationHolderFields on ReputationHolder {
id
address
contract
dao {
id
}
balance
}
`
}
/**
* Member.search(context, options) searches for member entities
* @param context an Arc instance that provides connection information
* @param options the query options, cf. IMemberQueryOptions
* @return an observable of IRewardState objects
*/
public static search(
context: Arc,
options: IMemberQueryOptions = {},
apolloQueryOptions: IApolloQueryOptions = {}
): Observable<Member[]> {
if (!options.where) { options.where = {}}
if (options.where.id) {
return new Member(options.where.id, context).state().pipe(map((r: any) => [r]))
} else {
let where = ''
for (const key of Object.keys(options.where)) {
if (options.where[key] === undefined) {
continue
}
if (key === 'address' || key === 'dao') {
const option = options.where[key] as string
isAddress(option)
options.where[key] = option.toLowerCase()
}
where += `${key}: "${options.where[key] as string}"\n`
}
where += ' dao_not: null\n'
const query = gql`
query ReputationHolderSearch {
reputationHolders ${createGraphQlQuery(options, where)} {
...ReputationHolderFields
}
}
${Member.fragments.ReputationHolderFields}
`
return context.getObservableList(
query,
(r: any) => new Member({ id: r.id, address: r.address, dao: r.dao.id, contract: r.contract}, context),
apolloQueryOptions
)
}
}
public id: string|undefined
public staticState: IMemberStaticState|undefined
/**
* @param address addresssof the member
* @param daoAdress addresssof the DAO this member is a member of
* @param context an instance of Arc
*/
constructor(idOrOpts: string|IMemberStaticState, public context: Arc) {
if (typeof idOrOpts === 'string') {
this.id = idOrOpts as string
} else {
const opts: IMemberStaticState = idOrOpts as IMemberStaticState
this.setStaticState(opts)
}
}
public async fetchStaticState(): Promise<IMemberStaticState> {
if (!!this.staticState) {
return this.staticState
} else {
const state = await this.state().pipe(first()).toPromise()
return this.setStaticState({
address: state.address,
contract: state.contract,
dao: state.dao,
id: state.id
})
}
}
public calculateId(opts: { contract: Address, address: Address}): string {
const seed = concat(
hexStringToUint8Array(opts.contract.toLowerCase()),
hexStringToUint8Array(opts.address.toLowerCase())
)
return this.context.web3.utils.keccak256(seed)
}
public setStaticState(opts: IMemberStaticState) {
isAddress(opts.address)
if (!opts.id && opts.contract && opts.address) {
opts.id = this.calculateId({ contract: opts.contract, address: opts.address})
}
this.id = opts.id
this.staticState = {
address: opts.address.toLowerCase(),
contract: opts.contract && opts.contract.toLowerCase(),
dao: opts.dao && opts.dao.toLowerCase(),
id: opts.id
}
return this.staticState
}
public state(apolloQueryOptions: IApolloQueryOptions = {}): Observable<IMemberState> {
let query: any
if (this.id) {
query = gql`query ReputionHolderStateFromId {
# contract: ${this.staticState && this.staticState.contract}
# address: ${this.staticState && this.staticState.address}
reputationHolder (
id: "${this.id}"
) {
...ReputationHolderFields
}
}
${Member.fragments.ReputationHolderFields}
`
return this.context.getObservableObject(
query,
(r: any) => {
if (r === null || r === undefined || r.id === undefined) {
// we return a dummy object with 0 reputation
const staticState = this.staticState as IMemberStaticState
if (staticState) {
return {
address: staticState.address,
dao: staticState.dao,
reputation: new BN(0)
}
} else {
throw Error(`No member with id ${this.id} was found`)
}
}
return { id: r.id, address: r.address, dao: r.dao.id, contract: r.contract, reputation: new BN(r.balance)}
},
apolloQueryOptions
)
} else {
const staticState = this.staticState as IMemberStaticState
query = gql`query ReputationHolderStateFromDAOAndAddress {
reputationHolders (
where: {
address: "${staticState.address}"
dao: "${staticState.dao}"
}
) {
...ReputationHolderFields
}
}
${Member.fragments.ReputationHolderFields}
`
}
const itemMap = (items: any) => {
if (items.length === 0) {
const staticState = this.staticState as IMemberStaticState
return {
address: staticState.address,
dao: staticState.dao,
reputation: new BN(0)
}
} else {
const item = items[0]
return {
address: item.address,
contract: item.contract,
dao: item.dao.id,
id: item.id,
reputation: new BN(item.balance)
}
}
}
return this.context.getObservableObject(query, itemMap, apolloQueryOptions) as Observable<IMemberState>
}
public async dao(): Promise < DAO > {
const staticState = await this.fetchStaticState()
return new DAO(staticState.dao as Address, this.context)
}
public rewards(): Observable < Reward[] > {
throw new Error('not implemented')
}
public proposals(
options: IProposalQueryOptions = {},
apolloQueryOptions: IApolloQueryOptions = {}
): Observable < Proposal[] > {
const observable = Observable.create(async (observer: any) => {
const state = await this.fetchStaticState()
if (!options.where) { options.where = {} }
options.where.proposer = state.address
options.where.dao = state.dao
const sub = Proposal.search(this.context, options, apolloQueryOptions).subscribe(observer)
return () => sub.unsubscribe()
})
return toIOperationObservable(observable)
}
public stakes(options: IStakeQueryOptions = {}, apolloQueryOptions: IApolloQueryOptions = {}): Observable<Stake[]> {
const observable = Observable.create(async (observer: any) => {
const state = await this.fetchStaticState()
if (!options.where) { options.where = {} }
options.where.staker = state.address
options.where.dao = state.dao
const sub = Stake.search(this.context, options, apolloQueryOptions) .subscribe(observer)
return () => sub.unsubscribe()
})
return toIOperationObservable(observable)
}
public votes(options: IVoteQueryOptions = {}, apolloQueryOptions: IApolloQueryOptions = {}): Observable < Vote[] > {
const observable = Observable.create(async (observer: any) => {
const state = await this.fetchStaticState()
if (!options.where) { options.where = {} }
options.where.voter = state.address
options.where.dao = state.dao
const sub = Vote.search(this.context, options, apolloQueryOptions) .subscribe(observer)
return () => sub.unsubscribe()
})
return toIOperationObservable(observable)
}
}