-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathautomaticCircleBridge.ts
182 lines (158 loc) · 4.75 KB
/
automaticCircleBridge.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
import type {
AccountAddress,
AutomaticCircleBridge,
Chain,
ChainAddress,
ChainsConfig,
Contracts,
Network,
Platform,
} from '@wormhole-foundation/sdk-connect';
import {
chainToChainId,
circle,
nativeChainIds,
toChainId,
} from '@wormhole-foundation/sdk-connect';
import type { Provider, TransactionRequest } from 'ethers';
import { ethers_contracts } from './index.js';
import type { CircleRelayer } from './ethers-contracts/index.js';
import type { EvmChains } from '@wormhole-foundation/sdk-evm';
import {
EvmAddress,
EvmPlatform,
EvmUnsignedTransaction,
addChainId,
addFrom,
} from '@wormhole-foundation/sdk-evm';
import '@wormhole-foundation/sdk-evm-tokenbridge';
import { EvmWormholeCore } from '@wormhole-foundation/sdk-evm-core';
export class EvmAutomaticCircleBridge<N extends Network, C extends EvmChains>
implements AutomaticCircleBridge<N, C>
{
readonly circleRelayer: CircleRelayer;
readonly chainId: bigint;
readonly tokenAddr: string;
readonly core: EvmWormholeCore<N, C>;
// https://github.com/wormhole-foundation/wormhole-connect/blob/development/sdk/src/contexts/eth/context.ts#L379
constructor(
readonly network: N,
readonly chain: C,
readonly provider: Provider,
readonly contracts: Contracts,
) {
if (network === 'Devnet')
throw new Error('AutomaticCircleBridge not supported on Devnet');
this.chainId = nativeChainIds.networkChainToNativeChainId.get(
network,
chain,
) as bigint;
const relayerAddress = this.contracts.cctp?.wormholeRelayer;
if (!relayerAddress)
throw new Error(
`Wormhole Circle relayer contract for domain ${chain} not found`,
);
this.circleRelayer = ethers_contracts.CircleRelayer__factory.connect(
relayerAddress,
provider,
);
const tokenAddr = circle.usdcContract.get(
this.network as circle.CircleNetwork,
this.chain as circle.CircleChain,
);
if (!tokenAddr)
throw new Error(
`USDC contract not found for ${this.network} ${this.chain}`,
);
this.tokenAddr = tokenAddr;
this.core = new EvmWormholeCore(network, chain, provider, contracts);
}
static async fromRpc<N extends Network>(
provider: Provider,
config: ChainsConfig<N, Platform>,
): Promise<EvmAutomaticCircleBridge<N, EvmChains>> {
const [network, chain] = await EvmPlatform.chainFromRpc(provider);
const conf = config[chain]!;
if (conf.network !== network)
throw new Error(`Network mismatch: ${conf.network} != ${network}`);
return new EvmAutomaticCircleBridge(
network as N,
chain,
provider,
conf.contracts,
);
}
async getRelayerFee(destination: Chain): Promise<bigint> {
return this.circleRelayer.relayerFee(
toChainId(destination),
this.tokenAddr,
);
}
async *transfer(
sender: AccountAddress<C>,
recipient: ChainAddress,
amount: bigint,
nativeGas?: bigint,
): AsyncGenerator<EvmUnsignedTransaction<N, C>> {
const senderAddr = new EvmAddress(sender).toString();
const recipientChainId = chainToChainId(recipient.chain);
const recipientAddress = recipient.address
.toUniversalAddress()
.toUint8Array();
const nativeTokenGas = nativeGas ? nativeGas : 0n;
const tokenContract = EvmPlatform.getTokenImplementation(
this.provider,
this.tokenAddr,
);
const allowance = await tokenContract.allowance(
senderAddr,
this.circleRelayer.target,
);
if (allowance < amount) {
const txReq = await tokenContract.approve.populateTransaction(
this.circleRelayer.target,
amount,
);
yield this.createUnsignedTx(
addFrom(txReq, senderAddr),
'ERC20.approve of CircleRelayer',
);
}
const messageFee = await this.core.getMessageFee();
const txReq =
await this.circleRelayer.transferTokensWithRelay.populateTransaction(
this.tokenAddr,
amount,
nativeTokenGas,
recipientChainId,
recipientAddress,
{ value: messageFee },
);
yield this.createUnsignedTx(
addFrom(txReq, senderAddr),
'CircleRelayer.transfer',
);
}
private createUnsignedTx(
txReq: TransactionRequest,
description: string,
parallelizable: boolean = false,
): EvmUnsignedTransaction<N, C> {
return new EvmUnsignedTransaction(
addChainId(txReq, this.chainId),
this.network,
this.chain,
description,
parallelizable,
);
}
async nativeTokenAmount(amount: bigint): Promise<bigint> {
return await this.circleRelayer.calculateNativeSwapAmountOut(
this.tokenAddr,
amount,
);
}
async maxSwapAmount(): Promise<bigint> {
return await this.circleRelayer.calculateMaxSwapAmountIn(this.tokenAddr);
}
}