-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtransfer.go
More file actions
493 lines (426 loc) · 17.7 KB
/
Copy pathtransfer.go
File metadata and controls
493 lines (426 loc) · 17.7 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright (C) 2026 Circle Internet Group, Inc.
// This file is part of the cctp-go project.
// The cctp-go project is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The cctp-go project 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the cctp-go project. If not, see <http://www.gnu.org/licenses/>.
package cctp
import (
"context"
"fmt"
"math/big"
"time"
"github.com/charmbracelet/log"
"github.com/circlefin/cctp-go/internal/util"
"github.com/circlefin/cctp-go/internal/wallet"
"github.com/circlefin/cctp-go/messagetransmitter"
"github.com/circlefin/cctp-go/tokenmessenger"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind/v2"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
// TransferStep represents a step in the transfer process
type TransferStep string
const (
StepCheckBalance TransferStep = "checking_balance"
StepCheckAllowance TransferStep = "checking_allowance"
StepApproving TransferStep = "approving"
StepApprovingWait TransferStep = "approving_wait"
StepBurning TransferStep = "burning"
StepBurningWait TransferStep = "burning_wait"
StepPollingAttestation TransferStep = "polling_attestation"
StepMinting TransferStep = "minting"
StepMintingWait TransferStep = "minting_wait"
StepComplete TransferStep = "complete"
StepError TransferStep = "error"
)
// TransferType represents the type of CCTP transfer
type TransferType string
const (
TransferTypeFast TransferType = "fast" // Fast Transfer (~8-20 seconds, with fee)
TransferTypeStandard TransferType = "standard" // Standard Transfer (~13-19 minutes, no fee)
TransferTypeAuto TransferType = "auto" // Auto-select based on chain capabilities
)
// TransferUpdate represents an update in the transfer process
type TransferUpdate struct {
Step TransferStep
Message string
TxHash string
Error error
SourceChain *Chain
DestChain *Chain
}
// TransferParams holds parameters for a transfer
type TransferParams struct {
SourceChain *Chain
DestChain *Chain
Amount *big.Int
RecipientAddress common.Address
Testnet bool
TransferType TransferType // Fast, Standard, or Auto (default: Auto)
CachedBalance *big.Int // Optional: pre-fetched balance to skip redundant check
}
// TransferOrchestrator orchestrates the entire transfer workflow
type TransferOrchestrator struct {
wallet *wallet.Wallet
irisClient *IrisClient
sourceClient *ethclient.Client
destClient *ethclient.Client
params *TransferParams
}
// NewTransferOrchestrator creates a new transfer orchestrator
func NewTransferOrchestrator(
w *wallet.Wallet,
params *TransferParams,
irisBaseURL string,
) (*TransferOrchestrator, error) {
// Create Iris client
irisClient := NewIrisClient(irisBaseURL)
// Create RPC clients
sourceClient, err := ethclient.Dial(params.SourceChain.RPC)
if err != nil {
return nil, fmt.Errorf("failed to connect to source chain: %w", err)
}
destClient, err := ethclient.Dial(params.DestChain.RPC)
if err != nil {
return nil, fmt.Errorf("failed to connect to destination chain: %w", err)
}
return &TransferOrchestrator{
wallet: w,
irisClient: irisClient,
sourceClient: sourceClient,
destClient: destClient,
params: params,
}, nil
}
// Execute executes the complete transfer workflow
func (t *TransferOrchestrator) Execute(ctx context.Context, updates chan<- TransferUpdate) error {
defer close(updates)
// Step 1: Check USDC balance (use cached if available)
var balance *big.Int
if t.params.CachedBalance != nil {
// Use cached balance from UI to avoid redundant RPC call
balance = t.params.CachedBalance
updates <- TransferUpdate{Step: StepCheckBalance, Message: fmt.Sprintf("Balance: %s USDC (cached)", util.FormatUSDCBalance(balance))}
} else {
// Fetch balance if not cached
updates <- TransferUpdate{Step: StepCheckBalance, Message: "Checking USDC balance..."}
usdcAddress := common.HexToAddress(t.params.SourceChain.USDC)
var err error
balance, err = util.GetUSDCBalance(ctx, t.sourceClient, usdcAddress, t.wallet.Address)
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: err}
return err
}
updates <- TransferUpdate{Step: StepCheckBalance, Message: fmt.Sprintf("Balance: %s USDC", util.FormatUSDCBalance(balance))}
}
// Validate balance regardless of whether it was cached or fetched
if balance.Cmp(t.params.Amount) < 0 {
err := fmt.Errorf("insufficient USDC balance: have %s, need %s", util.FormatUSDCBalance(balance), util.FormatUSDCBalance(t.params.Amount))
updates <- TransferUpdate{Step: StepError, Error: err}
return err
}
// Step 2: Check allowance
updates <- TransferUpdate{Step: StepCheckAllowance, Message: "Checking USDC allowance..."}
tokenMessengerAddr := common.HexToAddress(t.params.SourceChain.TokenMessengerV2)
// Create USDC contract instance for allowance and approval operations
ierc20 := tokenmessenger.NewIERC20()
usdcAddress := common.HexToAddress(t.params.SourceChain.USDC)
usdcInstance := ierc20.Instance(t.sourceClient, usdcAddress)
// Use V2 bindings to check allowance
allowance, err := bind.Call(usdcInstance, &bind.CallOpts{Context: ctx},
ierc20.PackAllowance(t.wallet.Address, tokenMessengerAddr), ierc20.UnpackAllowance)
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("failed to check allowance: %w", err)}
return err
}
// Step 3: Approve if needed
if allowance.Cmp(t.params.Amount) < 0 {
updates <- TransferUpdate{Step: StepApproving, Message: "Approving USDC..."}
// Create transaction options
auth, err := t.wallet.CreateTransactOpts(ctx, t.sourceClient, t.params.SourceChain.ChainID)
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("failed to create auth: %w", err)}
return err
}
// Use V2 bindings to send approve transaction
approveTx, err := bind.Transact(usdcInstance, auth, ierc20.PackApprove(tokenMessengerAddr, t.params.Amount))
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("failed to send approve tx: %w", err)}
return err
}
updates <- TransferUpdate{
Step: StepApprovingWait,
Message: "Waiting for approval confirmation...",
TxHash: approveTx.Hash().Hex(),
}
// Wait for approval confirmation using V2 helper
_, err = bind.WaitMined(ctx, t.sourceClient, approveTx.Hash())
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("approval transaction failed: %w", err)}
return err
}
updates <- TransferUpdate{
Step: StepApproving,
Message: "USDC approved successfully",
TxHash: approveTx.Hash().Hex(),
}
// Verify the allowance was updated
newAllowance, err := bind.Call(usdcInstance, &bind.CallOpts{Context: ctx},
ierc20.PackAllowance(t.wallet.Address, tokenMessengerAddr), ierc20.UnpackAllowance)
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("failed to verify allowance: %w", err)}
return err
}
if newAllowance.Cmp(t.params.Amount) < 0 {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("allowance not updated correctly: have %s, need %s", newAllowance.String(), t.params.Amount.String())}
return fmt.Errorf("allowance verification failed")
}
}
// Step 4: Determine transfer type and fetch fees
transferType := t.params.TransferType
if transferType == "" || transferType == TransferTypeAuto {
// Auto-select: use Fast if available, otherwise Standard
if t.params.SourceChain.InstantFinality {
transferType = TransferTypeStandard
log.Info("Auto-selected Standard Transfer (instant finality chain)")
} else {
transferType = TransferTypeFast
log.Info("Auto-selected Fast Transfer")
}
}
// Determine finality threshold based on transfer type
var minFinalityThreshold uint32
var transferTypeLabel string
if transferType == TransferTypeFast {
minFinalityThreshold = 1000
transferTypeLabel = "Fast Transfer"
} else {
minFinalityThreshold = 2000
transferTypeLabel = "Standard Transfer"
}
updates <- TransferUpdate{
Step: StepBurning,
Message: fmt.Sprintf("Querying fees for %s...", transferTypeLabel),
}
// Fetch fee information from API
feesResp, err := t.irisClient.GetTransferFees(ctx, t.params.SourceChain.Domain, t.params.DestChain.Domain)
if err != nil {
log.Warn("Failed to fetch fees from API, using fallback", "error", err)
// Fallback to conservative maxFee if API fails
feesResp = &FeesResponse{
Data: []FeeInfo{
{FinalityThreshold: 1000, MinimumFee: 14}, // Conservative: highest known fee (Linea)
{FinalityThreshold: 2000, MinimumFee: 0},
},
}
}
// Find the appropriate fee for the selected finality threshold
var feeBps uint32 = 0
for _, feeInfo := range feesResp.Data {
if feeInfo.FinalityThreshold == minFinalityThreshold {
feeBps = feeInfo.MinimumFee
break
}
}
// Calculate maxFee based on fee rate (with 1 bps safety buffer)
// maxFee = (amount * (feeBps + 1)) / 10000
var maxFee *big.Int
if feeBps > 0 {
// Add 1 bps buffer for fee fluctuations
feeWithBuffer := big.NewInt(int64(feeBps + 1))
maxFee = new(big.Int).Mul(t.params.Amount, feeWithBuffer)
maxFee = maxFee.Div(maxFee, big.NewInt(10000))
// Ensure minimum of 1 unit
if maxFee.Cmp(big.NewInt(0)) == 0 {
maxFee = big.NewInt(1)
}
} else {
// Standard Transfer with no fee - still need non-zero maxFee for contract
maxFee = big.NewInt(1)
}
// Step 5: Burn USDC
updates <- TransferUpdate{
Step: StepBurning,
Message: fmt.Sprintf("Burning USDC on source chain (%s)...", transferTypeLabel),
}
log.Info("Using transfer mode", "mode", transferTypeLabel, "threshold", minFinalityThreshold, "fee_bps", feeBps)
// Convert recipient address to bytes32
recipientBytes32 := util.AddressToBytes32(t.params.RecipientAddress)
log.Debug("=== BURN PARAMETERS ===")
log.Debug("Transfer parameters", "type", transferTypeLabel, "amount", t.params.Amount.String(), "dest_domain", t.params.DestChain.Domain)
log.Debug("Recipient info", "address", t.params.RecipientAddress.Hex(), "bytes32", fmt.Sprintf("%x", recipientBytes32))
log.Debug("Token and fees", "burn_token", t.params.SourceChain.USDC, "max_fee", maxFee.String(), "fee_bps", feeBps)
log.Debug("Finality", "min_finality_threshold", minFinalityThreshold)
log.Debug("======================")
// Create V2 token messenger instance
tokenMessengerV2 := tokenmessenger.NewTokenMessengerV2()
tokenMessengerInstance := tokenMessengerV2.Instance(t.sourceClient, tokenMessengerAddr)
// Pack the depositForBurn call
burnData := tokenMessengerV2.PackDepositForBurn(
t.params.Amount,
t.params.DestChain.Domain,
recipientBytes32,
common.HexToAddress(t.params.SourceChain.USDC),
[32]byte{}, // empty destinationCaller (bytes32(0) = any address can call)
maxFee, // maxFee calculated based on fee API response
minFinalityThreshold, // 1000 = Fast Transfer, 2000 = Standard Transfer
)
// Try to call the contract first to get detailed error
callMsg := ethereum.CallMsg{
From: t.wallet.Address,
To: &tokenMessengerAddr,
Data: burnData,
Value: big.NewInt(0),
}
_, callErr := t.sourceClient.CallContract(ctx, callMsg, nil)
if callErr != nil {
// Log detailed debug info
log.Error("=== BURN TRANSACTION FAILED ===")
log.Error("Burn transaction would fail", "error", callErr)
log.Error("Chain info", "source", t.params.SourceChain.Name, "source_domain", t.params.SourceChain.Domain, "dest", t.params.DestChain.Name, "dest_domain", t.params.DestChain.Domain)
log.Error("Transaction details", "amount", t.params.Amount.String(), "recipient", t.params.RecipientAddress.Hex())
log.Error("Contract addresses", "usdc", t.params.SourceChain.USDC, "token_messenger", tokenMessengerAddr.Hex(), "wallet", t.wallet.Address.Hex())
log.Error("Fee parameters", "max_fee", maxFee.String(), "min_finality_threshold", minFinalityThreshold)
log.Error("Call data", "data", fmt.Sprintf("%x", burnData))
log.Error("================================")
updates <- TransferUpdate{
Step: StepError,
Error: fmt.Errorf("burn call would fail: %w", callErr),
}
return callErr
}
// Create transaction options for burn
auth, err := t.wallet.CreateTransactOpts(ctx, t.sourceClient, t.params.SourceChain.ChainID)
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("failed to create auth: %w", err)}
return err
}
// Use V2 bindings to send depositForBurn transaction
burnTx, err := bind.Transact(tokenMessengerInstance, auth, burnData)
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("failed to send burn tx: %w", err)}
return err
}
updates <- TransferUpdate{
Step: StepBurningWait,
Message: "Waiting for burn confirmation...",
TxHash: burnTx.Hash().Hex(),
}
// Wait for burn confirmation using V2 helper
_, err = bind.WaitMined(ctx, t.sourceClient, burnTx.Hash())
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("burn transaction failed: %w", err)}
return err
}
updates <- TransferUpdate{
Step: StepBurning,
Message: fmt.Sprintf("USDC burned successfully (%s)", transferTypeLabel),
TxHash: burnTx.Hash().Hex(),
}
// Step 6: Poll for attestation
var estimatedTime string
if transferType == TransferTypeFast {
estimatedTime = "~8-20 seconds"
} else {
estimatedTime = "~13-19 minutes"
}
updates <- TransferUpdate{
Step: StepPollingAttestation,
Message: fmt.Sprintf("Waiting for attestation from Circle (%s expected)...", estimatedTime),
}
msg, err := t.irisClient.PollForAttestation(
ctx,
t.params.SourceChain.Domain,
burnTx.Hash().Hex(),
func(attempt int, elapsed time.Duration) {
updates <- TransferUpdate{
Step: StepPollingAttestation,
Message: fmt.Sprintf("Polling for attestation (attempt %d, %s elapsed)...", attempt, elapsed.Round(time.Second)),
}
},
)
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("failed to get attestation: %w", err)}
return err
}
updates <- TransferUpdate{
Step: StepPollingAttestation,
Message: "Attestation received!",
}
// Step 7: Mint on destination chain
updates <- TransferUpdate{Step: StepMinting, Message: "Minting USDC on destination chain..."}
// Decode hex strings (remove 0x prefix if present)
messageBytes := common.FromHex(msg.Message)
attestationBytes := common.FromHex(msg.Attestation)
log.Debug("=== MINT TRANSACTION ===")
log.Debug("Destination chain info", "chain", t.params.DestChain.Name, "domain", t.params.DestChain.Domain, "message_transmitter", t.params.DestChain.MessageTransmitterV2)
log.Debug("Message sizes", "message_len", len(messageBytes), "attestation_len", len(attestationBytes))
log.Debug("Message hex", "message", fmt.Sprintf("%x", messageBytes), "attestation", fmt.Sprintf("%x", attestationBytes))
log.Debug("Message raw", "message", msg.Message, "attestation", msg.Attestation)
log.Debug("=======================")
// Create V2 message transmitter instance
messageTransmitterV2 := messagetransmitter.NewMessageTransmitterV2()
messageTransmitterAddr := common.HexToAddress(t.params.DestChain.MessageTransmitterV2)
messageTransmitterInstance := messageTransmitterV2.Instance(t.destClient, messageTransmitterAddr)
// Pack the receiveMessage call
mintData := messageTransmitterV2.PackReceiveMessage(messageBytes, attestationBytes)
// Create transaction options for mint (on destination chain)
authDest, err := t.wallet.CreateTransactOpts(ctx, t.destClient, t.params.DestChain.ChainID)
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("failed to create auth: %w", err)}
return err
}
// Use V2 bindings to send receiveMessage transaction
mintTx, err := bind.Transact(messageTransmitterInstance, authDest, mintData)
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("failed to send mint tx: %w", err)}
return err
}
updates <- TransferUpdate{
Step: StepMintingWait,
Message: "Waiting for mint confirmation...",
TxHash: mintTx.Hash().Hex(),
}
// Wait for mint confirmation using V2 helper
receipt, err := bind.WaitMined(ctx, t.destClient, mintTx.Hash())
if err != nil {
updates <- TransferUpdate{Step: StepError, Error: fmt.Errorf("mint transaction failed: %w", err)}
return err
}
log.Info("Mint transaction confirmed", "tx_hash", mintTx.Hash().Hex())
log.Debug("Receipt details", "status", receipt.Status, "gas_used", receipt.GasUsed, "num_logs", len(receipt.Logs))
log.Info("USDC minted to recipient", "recipient", t.params.RecipientAddress.Hex(), "chain", t.params.DestChain.Name)
if len(receipt.Logs) == 0 {
log.Warn("No events emitted in mint transaction!")
updates <- TransferUpdate{
Step: StepMinting,
Message: "WARNING: Mint tx succeeded but no events emitted. Message may have been already received.",
}
}
// Step 8: Complete
updates <- TransferUpdate{
Step: StepComplete,
Message: fmt.Sprintf("Transfer complete! USDC minted to %s on %s (%s)", t.params.RecipientAddress.Hex(), t.params.DestChain.Name, transferTypeLabel),
TxHash: mintTx.Hash().Hex(),
}
return nil
}
// Close closes the RPC clients
func (t *TransferOrchestrator) Close() {
if t.sourceClient != nil {
t.sourceClient.Close()
}
if t.destClient != nil {
t.destClient.Close()
}
}