/
connect.ts
174 lines (156 loc) · 5.91 KB
/
connect.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
/*
Fadroma Cross-Chain Connector
Copyright (C) 2022 Hack.bg
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
import {
Console, Error, Chain, ChainMode, ChainId, Mocknet, bold, randomChainId
} from '@fadroma/agent'
import type { Agent, ChainRegistry } from '@fadroma/agent'
import * as Scrt from '@fadroma/scrt'
import * as CW from '@fadroma/cw'
export * from '@fadroma/cw'
import { Config } from '@hackbg/conf'
import type { Environment } from '@hackbg/conf'
// Populate `Chain.variants` with catalog of possible connections:
Object.assign(Chain.variants as ChainRegistry, {
// Support for Secret Network
ScrtMainnet: Scrt.Chain.mainnet,
ScrtTestnet: Scrt.Chain.testnet,
// Devnet is injected here by @hackbg/fadroma
})
export * from '@hackbg/conf'
export * from '@fadroma/agent'
export { Scrt, Mocknet }
export default function connect <A extends Agent> (
config: Partial<ConnectConfig> = new ConnectConfig()
): A {
return new ConnectConfig(config).getAgent()
}
/** Connection configuration. Factory for `Chain` and `Agent` objects. */
export class ConnectConfig extends Config {
constructor (
options: Partial<ConnectConfig> = {},
environment?: Environment
) {
super(environment)
this.override(options)
Object.defineProperty(this, 'mnemonic', { enumerable: false, writable: true })
this.scrt = new Scrt.Config(options?.scrt, environment)
this.chainId = this.getString('FADROMA_CHAIN_ID', ()=>this.getChainId())
}
protected getChainId () {
const chainIds = {
Mocknet: 'mocknet',
ScrtDevnet: 'fadroma-devnet',
ScrtTestnet: this.scrt.testnetChainId,
ScrtMainnet: this.scrt.mainnetChainId,
}
return chainIds[this.chain as keyof typeof chainIds]
}
protected getChainMode () {
const chainModes = {
Mocknet: ChainMode.Mocknet,
ScrtDevnet: ChainMode.Devnet,
ScrtTestnet: ChainMode.Testnet,
ScrtMainnet: ChainMode.Mainnet,
}
if (!this.chain) throw new ConnectError.NoChainSelected(chainModes)
const result = chainModes[this.chain as keyof typeof chainModes]
if (!result) throw new ConnectError.UnknownChainSelected(this.chain, chainModes)
return result
}
/** Logger handle. */
log = new ConnectConsole('@fadroma/connect')
/** Secret Network configuration. */
scrt: Scrt.Config
/** Name of stored mnemonic to use for authentication (currently devnet only) */
agentName: string = this.getString('FADROMA_AGENT', ()=>'Admin')
/** Name of chain to use. */
chain?: keyof ChainRegistry = this.getString('FADROMA_CHAIN', ()=>'Mocknet')
/** Override chain id. */
chainId?: ChainId
/** Override chain mode. */
chainMode: ChainMode = this.getString('FADROMA_CHAIN_MODE', () => this.getChainMode())
/** Mnemonic to use for authentication to testnet. */
testnetMnemonic?: string
= this.getString('FADROMA_TESTNET_MNEMONIC', ()=>undefined)
/** Mnemonic to use for authentication. Hidden from logs by default. */
mnemonic?: string
= this.getString('FADROMA_MNEMONIC', ()=>undefined)
/** Create the Chain instance specified by the configuration. */
getChain <C extends Chain> (
chainToGet: keyof ChainRegistry|ChainRegistry[keyof ChainRegistry]|undefined = this.chain
): C {
if (!chainToGet) {
chainToGet = this.chain
if (!chainToGet) throw new Error.Missing.Chain()
}
if (typeof chainToGet === 'string') { // allow name to be passed
chainToGet = Chain.variants[chainToGet]
}
if (!chainToGet) { // if still unspecified, throw
throw new ConnectError.UnknownChainSelected(this.chain!, Chain.variants)
}
return chainToGet({ config: this }) as C // create Chain object
}
/** Create the Agent instance identified by the configuration. */
getAgent <A extends Agent> (options: Partial<A> = {}): A {
options.chain ??= this.getChain()
options.name ??= this.agentName
if (this.chainMode === ChainMode.Testnet) {
options.mnemonic ??= this.testnetMnemonic
} else {
options.mnemonic ??= this.mnemonic
}
return options.chain.getAgent(options) as A
}
/** List all known chains. */
listChains () {
this.log.supportedChains()
this.log.selectedChain(this.chain as string)
}
}
export class ConnectConsole extends Console {
label = 'Fadroma Connect'
supportedChains (supportedChains: Record<string, unknown> = Chain.variants) {
this.br()
this.info('Known chain names:')
for (const chain of Object.keys(supportedChains).sort()) {
this.info(` ${bold(chain)}`)
}
}
selectedChain (chain?: string) {
this.br()
if (chain) {
this.info('Selected chain:')
this.info(` ${bold(chain)}`)
} else {
this.info('No selected chain. Set FADROMA_CHAIN in .env or shell environment.')
}
this.br()
}
}
export class ConnectError extends Error {
static SelectChainHint =
`Try setting the FADROMA_CHAIN env var to one of the supported values.`
static UnknownChainSelected = this.define('UnknownChainSelected',
(name: string, chains?: Record<string, unknown>)=>{
//chains && log.supportedChains(chains)
return `Unknown chain "${name}". ${this.SelectChainHint}`
})
static NoChainSelected = this.define('NoChainSelected',
(chains?: Record<string, unknown>)=>{
//chains && log.supportedChains(chains)
return `No chain selected. ${this.SelectChainHint}`
})
}