-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
226 lines (190 loc) · 6.15 KB
/
index.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
import {
CosmWasmClient,
SigningCosmWasmClient,
} from '@cosmjs/cosmwasm-stargate'
import { WalletData } from './wallet'
import { Chain } from '@chain-registry/types'
import {
FactoryClient,
FactoryQueryClient,
FactoryMessageComposer,
MemoryClient,
MemoryQueryClient,
VersionControlClient,
VersionControlQueryClient,
} from '@abstract/abstract.js'
import { ABSTRACT_DEPLOYMENTS } from 'config/addresses'
import connectCosmWasmClient from './cosmwasm/connect'
import Vaults from './vaults'
import Assets from './assets'
export interface EquilibriumClientConstructor {
wallet: WalletData | null
chainInfo: Chain
signingCosmWasmClient: SigningCosmWasmClient | null
}
export class EquilibriumClient {
private _cosmWasmClient: CosmWasmClient | null = null
public signingCosmWasmClient: SigningCosmWasmClient | null = null
private _factoryClient: FactoryQueryClient | null = null
public signingFactoryClient: FactoryClient | null = null
public factoryMessageComposer: FactoryMessageComposer | null = null
private _memoryClient: MemoryQueryClient | null = null
public signingMemoryClient: MemoryClient | null = null
private _versionControlClient: VersionControlQueryClient | null = null
public signingVersionControlClient: VersionControlClient | null = null
public factoryContract: string | null = null
public memoryContract: string | null = null
public versionControlContract: string | null = null
public chainInfo: Chain
private _wallet: WalletData | null = null
private _vaults: Vaults | null = null
private _assets: Assets | null = null
constructor({
wallet,
chainInfo,
signingCosmWasmClient,
}: EquilibriumClientConstructor) {
this._wallet = wallet
this.chainInfo = chainInfo
this.signingCosmWasmClient = signingCosmWasmClient
}
public async connect() {
// CosmWasm client already exists!
if (this._cosmWasmClient) {
return
}
// Create CosmWasm client
this._cosmWasmClient = await connectCosmWasmClient(
process.env.NEXT_PUBLIC_RPC!,
)
// Create query clients
await this.createFactoryClient()
await this.createMemoryClient()
await this.createVersionControlClient()
// Remove factoryMessageComposer if we are not connected anymore
await this.createFactoryMessageComposer()
}
public get cosmWasmClient(): CosmWasmClient {
return this._cosmWasmClient as CosmWasmClient
}
public get factoryClient(): FactoryQueryClient {
return (
this.signingFactoryClient || (this._factoryClient as FactoryQueryClient)
)
}
public get memoryClient(): MemoryQueryClient {
return this.signingMemoryClient || (this._memoryClient as MemoryQueryClient)
}
public get versionControlClient(): VersionControlQueryClient {
return (
this.signingVersionControlClient ||
(this._versionControlClient as VersionControlQueryClient)
)
}
public get wallet(): WalletData {
return this._wallet as WalletData
}
public get vaults(): Vaults {
if (!this.cosmWasmClient) {
throw new Error('Client not connected')
}
if (this._vaults) {
return this._vaults
}
this._vaults = new Vaults(this)
return this._vaults
}
public get assets(): Assets {
if (this._assets) {
return this._assets
}
this._assets = new Assets()
return this._assets
}
private async createFactoryClient() {
this.factoryContract =
ABSTRACT_DEPLOYMENTS[this.chainInfo.chain_id]['OS_FACTORY']
if (this._wallet?.address && this.signingCosmWasmClient) {
this.signingFactoryClient = new FactoryClient(
this.signingCosmWasmClient,
this._wallet.address,
this.factoryContract,
)
} else if (this.cosmWasmClient) {
this._factoryClient = new FactoryQueryClient(
this.cosmWasmClient,
this.factoryContract,
)
}
return this.signingFactoryClient ?? this._factoryClient
}
private async createFactoryMessageComposer() {
this.factoryContract =
ABSTRACT_DEPLOYMENTS[this.chainInfo.chain_id]['OS_FACTORY']
if (this._wallet?.address && this.signingCosmWasmClient) {
this.factoryMessageComposer = new FactoryMessageComposer(
this._wallet.address,
this.factoryContract,
)
} else {
this.factoryMessageComposer = null
}
return this.factoryMessageComposer
}
private async createMemoryClient() {
this.memoryContract =
ABSTRACT_DEPLOYMENTS[this.chainInfo.chain_id]['MEMORY']
if (this._wallet?.address && this.signingCosmWasmClient) {
this.signingMemoryClient = new MemoryClient(
this.signingCosmWasmClient,
this._wallet.address,
this.memoryContract,
)
} else if (this.cosmWasmClient) {
this._memoryClient = new MemoryQueryClient(
this.cosmWasmClient,
this.memoryContract,
)
}
return this.signingMemoryClient ?? this._memoryClient
}
private async createVersionControlClient() {
this.versionControlContract =
ABSTRACT_DEPLOYMENTS[this.chainInfo.chain_id]['VERSION_CONTROL']
if (this._wallet?.address && this.signingCosmWasmClient) {
this.signingVersionControlClient = new VersionControlClient(
this.signingCosmWasmClient,
this._wallet.address,
this.versionControlContract!,
)
} else if (this.cosmWasmClient) {
this._versionControlClient = new VersionControlQueryClient(
this.cosmWasmClient,
this.versionControlContract!,
)
}
return this.signingVersionControlClient ?? this._versionControlClient
}
public async connectSigning() {
try {
if (!this.cosmWasmClient) {
throw new Error('cosmWasmClient could not connect')
}
if (!this.signingCosmWasmClient) {
throw new Error('signingCosmWasmClient could not connect')
}
// Create signing clients
await this.createFactoryClient()
await this.createMemoryClient()
await this.createVersionControlClient()
await this.createFactoryMessageComposer()
return this._wallet
} catch (e) {
console.error(e)
}
}
public async disconnectSigning() {
this.signingCosmWasmClient?.disconnect()
this._wallet = null
}
}