-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathreputation.ts
142 lines (128 loc) · 4.16 KB
/
reputation.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
import { ApolloQueryResult } from 'apollo-client'
import BN = require('bn.js')
import gql from 'graphql-tag'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { Arc, IApolloQueryOptions } from './arc'
import { ABI_DIR, REPUTATION_CONTRACT_VERSION } from './settings'
import { Address, ICommonQueryOptions, IStateful, Web3Receipt } from './types'
import { createGraphQlQuery, isAddress } from './utils'
export interface IReputationState {
address: Address
totalSupply: BN
dao: Address
}
export interface IReputationQueryOptions extends ICommonQueryOptions {
id?: string
dao?: Address
[key: string]: any
}
export class Reputation implements IStateful<IReputationState> {
/**
* Reputation.search(context, options) searches for reputation entities
* @param context an Arc instance that provides connection information
* @param options the query options, cf. IReputationQueryOptions
* @return an observable of Reputation objects
*/
public static search(
context: Arc,
options: IReputationQueryOptions = {},
apolloQueryOptions: IApolloQueryOptions = {}
): Observable<Reputation[]> {
let where = ''
if (!options.where) { options.where = {}}
for (const key of Object.keys(options.where)) {
if (options[key] === undefined) {
continue
}
if (key === 'dao') {
const option = options[key] as string
isAddress(option)
options[key] = option.toLowerCase()
}
where += `${key}: "${options[key] as string}"\n`
}
const query = gql`query ReputationSearch {
reps
${createGraphQlQuery(options, where)}
{
id
}
}`
return context.getObservableList(
query,
(r: any) => new Reputation(r.id, context),
apolloQueryOptions
)
}
public address: Address
constructor(public id: Address, public context: Arc) {
isAddress(id)
this.address = id
}
public state(apolloQueryOptions: IApolloQueryOptions = {}): Observable<IReputationState> {
const query = gql`query ReputationState
{
rep (id: "${this.address.toLowerCase()}") {
id
totalSupply
dao {
id
}
}
}`
const itemMap = (item: any): IReputationState => {
if (item === null) {
throw Error(`Could not find a reputation contract with address ${this.address.toLowerCase()}`)
}
return {
address: item.id,
dao: item.dao.id,
totalSupply: new BN(item.totalSupply)
}
}
return this.context.getObservableObject(query, itemMap, apolloQueryOptions) as Observable<IReputationState>
}
public reputationOf(address: Address): Observable<BN> {
isAddress(address)
const query = gql`query ReputationHolderReputation {
reputationHolders (
where: { address:"${address}",
contract: "${this.address}"}
)
{
id, address, balance,contract
}
}`
return this.context.getObservable(query).pipe(
map((r: ApolloQueryResult<any>) => r.data.reputationHolders),
map((items: any[]) => {
const item = items.length > 0 && items[0]
return item.balance !== undefined ? new BN(item.balance) : new BN(0)
})
)
}
/*
* get a web3 contract instance for this token
*/
public contract() {
const abi = require(`${ABI_DIR}/${REPUTATION_CONTRACT_VERSION}/Reputation.json`)
return this.context.getContract(this.address, abi)
}
public mint(beneficiary: Address, amount: BN) {
const contract = this.contract()
const transaction = contract.methods.mint(beneficiary, amount.toString())
const mapReceipt = (receipt: Web3Receipt) => receipt
const sender = this.context.web3.eth.accounts.wallet[0].address
const errHandler = async (err: Error) => {
const owner = await contract.methods.owner().call()
if (owner.toLowerCase() !== sender.toLowerCase()) {
return Error(`Minting failed: sender ${sender} is not the owner of the contract at ${contract._address}` +
`(which is ${owner})`)
}
await transaction.call()
return err
}
return this.context.sendTransaction(transaction, mapReceipt, errHandler)
}
}