-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathfireblocks_wallet.go
361 lines (327 loc) · 11.4 KB
/
fireblocks_wallet.go
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
package wallet
import (
"context"
"errors"
"fmt"
"math/big"
"strconv"
"sync"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/fireblocks"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
)
var _ Wallet = (*fireblocksWallet)(nil)
var (
// ErrNotYetBroadcasted indicates that the transaction has not been broadcasted yet.
// This can happen if the transaction is still being processed by Fireblocks and has not been broadcasted to the
// blockchain yet.
ErrNotYetBroadcasted = errors.New("transaction not yet broadcasted")
// ErrReceiptNotYetAvailable indicates that the transaction has been broadcasted but has not been confirmed onchain
// yet.
ErrReceiptNotYetAvailable = errors.New("transaction receipt not yet available")
ErrTransactionFailed = errors.New("transaction failed")
)
type ethClient interface {
ChainID(ctx context.Context) (*big.Int, error)
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
bind.ContractBackend
}
type fireblocksWallet struct {
// mu protects access to nonceToTxID and txIDToNonce which can be
// accessed concurrently by SendTransaction and GetTransactionReceipt
mu sync.Mutex
fireblocksClient fireblocks.Client
ethClient ethClient
vaultAccountName string
logger logging.Logger
chainID *big.Int
// nonceToTx keeps track of the transaction ID for each nonce
// this is used to retrieve the transaction hash for a given nonce
// when a replacement transaction is submitted.
nonceToTxID map[uint64]TxID
txIDToNonce map[TxID]uint64
// caches
account *fireblocks.VaultAccount
whitelistedContracts map[common.Address]*fireblocks.WhitelistedContract
whitelistedAccounts map[common.Address]*fireblocks.WhitelistedAccount
}
func NewFireblocksWallet(
fireblocksClient fireblocks.Client,
ethClient ethClient,
vaultAccountName string,
logger logging.Logger,
) (Wallet, error) {
chainID, err := ethClient.ChainID(context.Background())
if err != nil {
return nil, fmt.Errorf("error getting chain ID: %w", err)
}
logger.Debug("Creating new Fireblocks wallet for chain", "chainID", chainID)
return &fireblocksWallet{
fireblocksClient: fireblocksClient,
ethClient: ethClient,
vaultAccountName: vaultAccountName,
logger: logger,
chainID: chainID,
nonceToTxID: make(map[uint64]TxID),
txIDToNonce: make(map[TxID]uint64),
// caches
account: nil,
whitelistedContracts: make(map[common.Address]*fireblocks.WhitelistedContract),
whitelistedAccounts: make(map[common.Address]*fireblocks.WhitelistedAccount),
}, nil
}
func (t *fireblocksWallet) getAccount(ctx context.Context) (*fireblocks.VaultAccount, error) {
if t.account == nil {
accounts, err := t.fireblocksClient.ListVaultAccounts(ctx)
if err != nil {
return nil, fmt.Errorf("error listing vault accounts: %w", err)
}
for i, a := range accounts {
if a.Name == t.vaultAccountName {
t.account = &accounts[i]
break
}
}
}
return t.account, nil
}
func (f *fireblocksWallet) getWhitelistedAccount(
ctx context.Context,
address common.Address,
) (*fireblocks.WhitelistedAccount, error) {
assetID, ok := fireblocks.AssetIDByChain[f.chainID.Uint64()]
if !ok {
return nil, fmt.Errorf("unsupported chain %d", f.chainID.Uint64())
}
whitelistedAccount, ok := f.whitelistedAccounts[address]
if !ok {
accounts, err := f.fireblocksClient.ListExternalWallets(ctx)
if err != nil {
return nil, fmt.Errorf("error listing external wallets: %w", err)
}
for i, a := range accounts {
for _, asset := range a.Assets {
if asset.Address == address && asset.Status == "APPROVED" && asset.ID == assetID {
f.whitelistedAccounts[address] = &accounts[i]
whitelistedAccount = &accounts[i]
return whitelistedAccount, nil
}
}
}
}
if whitelistedAccount == nil {
return nil, fmt.Errorf("account %s not found in whitelisted accounts", address.Hex())
}
return whitelistedAccount, nil
}
func (t *fireblocksWallet) getWhitelistedContract(
ctx context.Context,
address common.Address,
) (*fireblocks.WhitelistedContract, error) {
assetID, ok := fireblocks.AssetIDByChain[t.chainID.Uint64()]
if !ok {
return nil, fmt.Errorf("unsupported chain %d", t.chainID.Uint64())
}
contract, ok := t.whitelistedContracts[address]
if !ok {
contracts, err := t.fireblocksClient.ListContracts(ctx)
if err != nil {
return nil, fmt.Errorf("error listing contracts: %w", err)
}
for i_c, c := range contracts {
for _, a := range c.Assets {
if a.Address == address && a.Status == "APPROVED" && a.ID == assetID {
t.whitelistedContracts[address] = &contracts[i_c]
contract = &contracts[i_c]
return contract, nil
}
}
}
}
if contract == nil {
return nil, fmt.Errorf("contract %s not found in whitelisted contracts", address.Hex())
}
return contract, nil
}
func (t *fireblocksWallet) SendTransaction(ctx context.Context, tx *types.Transaction) (TxID, error) {
assetID, ok := fireblocks.AssetIDByChain[t.chainID.Uint64()]
if !ok {
return "", fmt.Errorf("unsupported chain %d", t.chainID.Uint64())
}
account, err := t.getAccount(ctx)
if err != nil {
return "", fmt.Errorf("error getting account: %w", err)
}
foundAsset := false
for _, a := range account.Assets {
if a.ID == assetID {
if a.Available == "0" {
return "", errors.New("insufficient funds")
}
foundAsset = true
break
}
}
if !foundAsset {
return "", fmt.Errorf("asset %s not found in account %s", assetID, t.vaultAccountName)
}
t.mu.Lock()
defer t.mu.Unlock()
// if the nonce is already in the map, it means that the transaction was already submitted
// we need to get the replacement transaction hash and use it as the replaceTxByHash parameter
replaceTxByHash := ""
nonce := tx.Nonce()
if txID, ok := t.nonceToTxID[nonce]; ok {
fireblockTx, err := t.fireblocksClient.GetTransaction(ctx, txID)
if err != nil {
return "", fmt.Errorf("error getting fireblocks transaction %s: %w", txID, err)
}
if fireblockTx.TxHash != "" {
replaceTxByHash = fireblockTx.TxHash
}
}
gasLimit := ""
if tx.Gas() > 0 {
gasLimit = strconv.FormatUint(tx.Gas(), 10)
}
// if the gas fees are specified in the transaction, use them.
// Otherwise, use the default "HIGH" gas price estimated by Fireblocks
maxFee := ""
priorityFee := ""
gasPrice := ""
feeLevel := fireblocks.FeeLevel("")
if tx.GasFeeCap().Cmp(big.NewInt(0)) > 0 && tx.GasTipCap().Cmp(big.NewInt(0)) > 0 {
maxFee = weiToGwei(tx.GasFeeCap()).String()
priorityFee = weiToGwei(tx.GasTipCap()).String()
} else if tx.GasPrice().Cmp(big.NewInt(0)) > 0 {
gasPrice = weiToGwei(tx.GasPrice()).String()
} else {
feeLevel = fireblocks.FeeLevelHigh
}
var res *fireblocks.TransactionResponse
if len(tx.Data()) == 0 && tx.Value().Cmp(big.NewInt(0)) > 0 {
targetAccount, clientErr := t.getWhitelistedAccount(ctx, *tx.To())
if clientErr != nil {
return "", fmt.Errorf("error getting whitelisted account %s: %w", tx.To().Hex(), clientErr)
}
req := fireblocks.NewTransferRequest(
"", // externalTxID
assetID,
account.ID, // source account ID
targetAccount.ID, // destination account ID
weiToEther(tx.Value()).String(), // amount in ETH
replaceTxByHash, // replaceTxByHash
gasPrice,
gasLimit,
maxFee,
priorityFee,
feeLevel,
)
res, err = t.fireblocksClient.Transfer(ctx, req)
} else if len(tx.Data()) > 0 {
contract, clientErr := t.getWhitelistedContract(ctx, *tx.To())
if clientErr != nil {
return "", fmt.Errorf("error getting whitelisted contract %s: %w", tx.To().Hex(), clientErr)
}
req := fireblocks.NewContractCallRequest(
"", // externalTxID
assetID,
account.ID, // source account ID
contract.ID, // destination account ID
weiToEther(tx.Value()).String(), // amount
hexutil.Encode(tx.Data()), // calldata
replaceTxByHash, // replaceTxByHash
gasPrice,
gasLimit,
maxFee,
priorityFee,
feeLevel,
)
res, err = t.fireblocksClient.ContractCall(ctx, req)
} else {
return "", errors.New("transaction has no value and no data")
}
if err != nil {
return "", fmt.Errorf("error sending a transaction %s: %w", tx.To().Hex(), err)
}
t.nonceToTxID[nonce] = res.ID
t.txIDToNonce[res.ID] = nonce
t.logger.Debug("Fireblocks contract call complete", "txID", res.ID, "status", res.Status)
return res.ID, nil
}
func (t *fireblocksWallet) CancelTransactionBroadcast(ctx context.Context, txID TxID) (bool, error) {
return t.fireblocksClient.CancelTransaction(ctx, string(txID))
}
func (t *fireblocksWallet) GetTransactionReceipt(ctx context.Context, txID TxID) (*types.Receipt, error) {
fireblockTx, err := t.fireblocksClient.GetTransaction(ctx, txID)
if err != nil {
return nil, fmt.Errorf("error getting fireblocks transaction %s: %w", txID, err)
}
if fireblockTx.Status == fireblocks.Completed {
txHash := common.HexToHash(fireblockTx.TxHash)
receipt, err := t.ethClient.TransactionReceipt(ctx, txHash)
if err == nil {
t.mu.Lock()
defer t.mu.Unlock()
if nonce, ok := t.txIDToNonce[txID]; ok {
delete(t.nonceToTxID, nonce)
delete(t.txIDToNonce, txID)
}
return receipt, nil
}
if errors.Is(err, ethereum.NotFound) {
return nil, fmt.Errorf("%w: for txID %s", ErrReceiptNotYetAvailable, txID)
} else {
return nil, fmt.Errorf("Transaction receipt retrieval failed: %w", err)
}
} else if fireblockTx.Status == fireblocks.Failed ||
fireblockTx.Status == fireblocks.Rejected ||
fireblockTx.Status == fireblocks.Cancelled ||
fireblockTx.Status == fireblocks.Blocked {
return nil, fmt.Errorf("%w: the Fireblocks transaction %s has been %s", ErrTransactionFailed, txID, fireblockTx.Status)
} else if fireblockTx.Status == fireblocks.Submitted ||
fireblockTx.Status == fireblocks.PendingScreening ||
fireblockTx.Status == fireblocks.PendingAuthorization ||
fireblockTx.Status == fireblocks.Queued ||
fireblockTx.Status == fireblocks.PendingSignature ||
fireblockTx.Status == fireblocks.PendingEmailApproval ||
fireblockTx.Status == fireblocks.Pending3rdParty ||
fireblockTx.Status == fireblocks.Broadcasting {
return nil, fmt.Errorf("%w: the Fireblocks transaction %s is in status %s", ErrNotYetBroadcasted, txID, fireblockTx.Status)
}
return nil, fmt.Errorf(
"%w: the Fireblocks transaction %s is in status %s",
ErrReceiptNotYetAvailable,
txID,
fireblockTx.Status,
)
}
func (f *fireblocksWallet) SenderAddress(ctx context.Context) (common.Address, error) {
account, err := f.getAccount(ctx)
if err != nil {
return common.Address{}, fmt.Errorf("error getting account: %w", err)
}
addresses, err := f.fireblocksClient.GetAssetAddresses(
ctx,
account.ID,
fireblocks.AssetIDByChain[f.chainID.Uint64()],
)
if err != nil {
return common.Address{}, fmt.Errorf("error getting asset addresses: %w", err)
}
if len(addresses) == 0 {
return common.Address{}, errors.New("no addresses found")
}
return common.HexToAddress(addresses[0].Address), nil
}
func weiToGwei(wei *big.Int) *big.Float {
return new(big.Float).Quo(new(big.Float).SetInt(wei), big.NewFloat(params.GWei))
}
func weiToEther(wei *big.Int) *big.Float {
return new(big.Float).Quo(new(big.Float).SetInt(wei), big.NewFloat(params.Ether))
}