-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.ts
144 lines (131 loc) · 4.55 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
import {
ChainMappings,
Chains,
CrossChainContractsLoader,
L2Chain,
TBTCContracts,
} from "../contracts"
import { providers, Signer } from "ethers"
import { EthereumBridge } from "./bridge"
import { EthereumWalletRegistry } from "./wallet-registry"
import { EthereumTBTCToken } from "./tbtc-token"
import { EthereumTBTCVault } from "./tbtc-vault"
import { EthereumAddress } from "./address"
import { EthereumL1BitcoinDepositor } from "./l1-bitcoin-depositor"
export * from "./address"
export * from "./bridge"
export * from "./depositor-proxy"
export * from "./l1-bitcoin-depositor"
export * from "./tbtc-token"
export * from "./tbtc-vault"
export * from "./wallet-registry"
// The `adapter` module should not be re-exported directly as it
// contains low-level contract integration code. Re-export only components
// that are relevant for `lib/ethereum` clients.
export { EthersContractConfig as EthereumContractConfig } from "./adapter"
/**
* Represents an Ethereum signer. This type is a wrapper for Ethers-specific
* types and can be either a Signer that can make write transactions
* or a Provider that works only in the read-only mode.
*/
export type EthereumSigner = Signer | providers.Provider
/**
* Resolves the chain ID from the given signer.
* @param signer The signer whose chain ID should be resolved.
* @returns Chain ID as a string.
*/
export async function chainIdFromSigner(
signer: EthereumSigner
): Promise<string> {
let chainId: number
if (Signer.isSigner(signer)) {
chainId = await signer.getChainId()
} else {
const network = await signer.getNetwork()
chainId = network.chainId
}
return chainId.toString()
}
/**
* Resolves the Ethereum address tied to the given signer. The address
* cannot be resolved for signers that works in the read-only mode
* @param signer The signer whose address should be resolved.
* @returns Ethereum address or undefined for read-only signers.
* @throws Throws an error if the address of the signer is not a proper
* Ethereum address.
*/
export async function ethereumAddressFromSigner(
signer: EthereumSigner
): Promise<EthereumAddress | undefined> {
if (Signer.isSigner(signer)) {
return EthereumAddress.from(await signer.getAddress())
} else {
return undefined
}
}
/**
* Loads Ethereum implementation of tBTC core contracts for the given Ethereum
* chain ID and attaches the given signer there.
* @param signer Signer that should be attached to tBTC contracts.
* @param chainId Ethereum chain ID.
* @returns Handle to tBTC core contracts.
* @throws Throws an error if the signer's Ethereum chain ID is other than
* the one used to load tBTC contracts.
*/
export async function loadEthereumCoreContracts(
signer: EthereumSigner,
chainId: Chains.Ethereum
): Promise<TBTCContracts> {
const signerChainId = await chainIdFromSigner(signer)
if (signerChainId !== chainId) {
throw new Error("Signer uses different chain than Ethereum core contracts")
}
const bridge = new EthereumBridge({ signerOrProvider: signer }, chainId)
const tbtcToken = new EthereumTBTCToken({ signerOrProvider: signer }, chainId)
const tbtcVault = new EthereumTBTCVault({ signerOrProvider: signer }, chainId)
const walletRegistry = new EthereumWalletRegistry(
{ signerOrProvider: signer },
chainId
)
return {
bridge,
tbtcToken,
tbtcVault,
walletRegistry,
}
}
/**
* Creates the Ethereum implementation of tBTC cross-chain contracts loader.
* The provided signer is attached to loaded L1 contracts. The given
* Ethereum chain ID is used to load the L1 contracts and resolve the chain
* mapping that provides corresponding L2 chains IDs.
* @param signer Ethereum L1 signer.
* @param chainId Ethereum L1 chain ID.
* @returns Loader for tBTC cross-chain contracts.
* @throws Throws an error if the signer's Ethereum chain ID is other than
* the one used to construct the loader.
*/
export async function ethereumCrossChainContractsLoader(
signer: EthereumSigner,
chainId: Chains.Ethereum
): Promise<CrossChainContractsLoader> {
const signerChainId = await chainIdFromSigner(signer)
if (signerChainId !== chainId) {
throw new Error(
"Signer uses different chain than Ethereum cross-chain contracts"
)
}
const loadChainMapping = () =>
ChainMappings.find((ecm) => ecm.ethereum === chainId)
const loadL1Contracts = async (l2ChainName: L2Chain) => ({
l1BitcoinDepositor: new EthereumL1BitcoinDepositor(
{ signerOrProvider: signer },
chainId,
l2ChainName
),
})
return {
loadChainMapping,
loadL1Contracts,
}
}