forked from zksync-sdk/zksync2-go
/
wallet_l2.go
273 lines (245 loc) · 8.47 KB
/
wallet_l2.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
package accounts
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/zksync-sdk/zksync2-go/clients"
"github.com/zksync-sdk/zksync2-go/contracts/erc20"
"github.com/zksync-sdk/zksync2-go/contracts/ethtoken"
"github.com/zksync-sdk/zksync2-go/contracts/l2bridge"
zkTypes "github.com/zksync-sdk/zksync2-go/types"
"github.com/zksync-sdk/zksync2-go/utils"
"math/big"
)
// WalletL2 implements the AdapterL2 interface.
type WalletL2 struct {
client *clients.Client
signer *Signer
auth *bind.TransactOpts
defaultL2BridgeAddress common.Address
defaultL2Bridge *l2bridge.IL2Bridge
}
// NewWalletL2 creates an instance of WalletL2 associated with the account provided by the raw private key.
func NewWalletL2(rawPrivateKey []byte, client *clients.Client) (*WalletL2, error) {
chainID, err := (*client).ChainID(context.Background())
if err != nil {
return nil, err
}
signer, err := NewBaseSignerFromRawPrivateKey(rawPrivateKey, chainID.Int64())
if err != nil {
return nil, err
}
s := Signer(signer)
return NewWalletL2FromSigner(&s, client)
}
// NewWalletL2FromSigner creates an instance of WalletL2.
// The client can be optional; if it is not provided, only AdapterL2.SignTransaction,
// AdapterL2.Address, AdapterL2.Signer can be performed, as the rest of the
// functionalities require communication to the network.
func NewWalletL2FromSigner(signer *Signer, client *clients.Client) (*WalletL2, error) {
if client == nil {
return &WalletL2{signer: signer}, nil
}
bridgeContracts, err := (*client).BridgeContracts(context.Background())
if err != nil {
return nil, err
}
defaultL2Bridge, err := l2bridge.NewIL2Bridge(bridgeContracts.L2Erc20DefaultBridge, *client)
if err != nil {
return nil, fmt.Errorf("failed to load IL1Bridge: %w", err)
}
chainId, err := (*client).ChainID(context.Background())
auth, err := newTransactorWithSigner(signer, chainId)
if err != nil {
return nil, fmt.Errorf("failed to init TransactOpts: %w", err)
}
return &WalletL2{
client: client,
signer: signer,
auth: auth,
defaultL2BridgeAddress: bridgeContracts.L2Erc20DefaultBridge,
defaultL2Bridge: defaultL2Bridge,
}, nil
}
func (a *WalletL2) Address() common.Address {
return a.auth.From
}
func (a *WalletL2) Signer() Signer {
return *a.signer
}
func (a *WalletL2) Balance(ctx context.Context, token common.Address, at *big.Int) (*big.Int, error) {
if token == utils.EthAddress {
return (*a.client).BalanceAt(ensureContext(ctx), a.Address(), at)
}
erc20Token, err := erc20.NewIERC20(token, *a.client)
if err != nil {
return nil, err
}
return erc20Token.BalanceOf(&bind.CallOpts{
From: a.Address(),
BlockNumber: at,
Context: ensureContext(ctx),
}, a.Address())
}
func (a *WalletL2) AllBalances(ctx context.Context) (map[common.Address]*big.Int, error) {
return (*a.client).AllAccountBalances(ensureContext(ctx), a.Address())
}
func (a *WalletL2) L2BridgeContracts(_ context.Context) (*zkTypes.L2BridgeContracts, error) {
return &zkTypes.L2BridgeContracts{Erc20: a.defaultL2Bridge}, nil
}
func (a *WalletL2) Withdraw(auth *TransactOpts, tx WithdrawalTransaction) (*types.Transaction, error) {
opts := ensureTransactOpts(auth)
if tx.Token == utils.EthAddress {
eth, err := ethtoken.NewIEthToken(utils.L2EthTokenAddress, *a.client)
if err != nil {
return nil, err
}
withdrawTx, err := eth.Withdraw(opts.ToTransactOpts(a.Address(), a.auth.Signer), tx.To)
if err != nil {
return nil, err
}
return withdrawTx, nil
} else {
if tx.BridgeAddress == nil {
tx.BridgeAddress = &a.defaultL2BridgeAddress
}
bridge, err := l2bridge.NewIL2Bridge(*tx.BridgeAddress, *a.client)
if err != nil {
return nil, err
}
withdrawTx, err := bridge.Withdraw(opts.ToTransactOpts(a.Address(), a.auth.Signer), tx.To, tx.Token, tx.Amount)
if err != nil {
return nil, err
}
return withdrawTx, nil
}
}
func (a *WalletL2) EstimateGasWithdraw(ctx context.Context, msg WithdrawalCallMsg) (uint64, error) {
return (*a.client).EstimateGasWithdraw(ensureContext(ctx), msg.ToWithdrawalCallMsg(a.Address()))
}
func (a *WalletL2) Transfer(auth *TransactOpts, tx TransferTransaction) (*types.Transaction, error) {
opts := ensureTransactOpts(auth)
if opts.GasLimit == 0 {
gas, err := (*a.client).EstimateGasTransfer(opts.Context, tx.ToTransferCallMsg(a.Address(), opts))
if err != nil {
return nil, err
}
opts.GasLimit = gas
}
if tx.Token == utils.EthAddress {
return a.transferETH(opts, tx)
}
token, err := erc20.NewIERC20(tx.Token, *a.client)
if err != nil {
return nil, fmt.Errorf("failed to load erc20 contract: %w", err)
}
opts.Value = big.NewInt(0)
return token.Transfer(opts.ToTransactOpts(a.Address(), a.auth.Signer), tx.To, tx.Amount)
}
func (a *WalletL2) EstimateGasTransfer(ctx context.Context, msg TransferCallMsg) (uint64, error) {
return (*a.client).EstimateGasTransfer(ensureContext(ctx), msg.ToTransferCallMsg(a.Address()))
}
func (a *WalletL2) CallContract(ctx context.Context, msg CallMsg, blockNumber *big.Int) ([]byte, error) {
return (*a.client).CallContractL2(ensureContext(ctx), msg.ToCallMsg(a.Address()), blockNumber)
}
func (a *WalletL2) PopulateTransaction(ctx context.Context, tx Transaction) (*zkTypes.Transaction712, error) {
if tx.ChainID == nil {
tx.ChainID = (*a.signer).Domain().ChainId
}
if tx.Nonce == nil {
nonce, err := (*a.client).NonceAt(ensureContext(ctx), a.Address(), nil)
if err != nil {
return nil, fmt.Errorf("failed to get nonce: %w", err)
}
tx.Nonce = new(big.Int).SetUint64(nonce)
}
if tx.GasFeeCap == nil {
gasFeeCap, err := (*a.client).SuggestGasPrice(ensureContext(ctx))
if err != nil {
return nil, fmt.Errorf("failed to SuggestGasPrice: %w", err)
}
tx.GasFeeCap = gasFeeCap
}
if tx.GasTipCap == nil {
tx.GasTipCap = big.NewInt(100_000_000) // TODO: Estimate correct one
}
if tx.Meta == nil {
tx.Meta = &zkTypes.Eip712Meta{GasPerPubdata: utils.NewBig(utils.DefaultGasPerPubdataLimit.Int64())}
} else if tx.Meta.GasPerPubdata == nil {
tx.Meta.GasPerPubdata = utils.NewBig(utils.DefaultGasPerPubdataLimit.Int64())
}
if tx.Gas == 0 {
gas, err := (*a.client).EstimateGasL2(ensureContext(ctx), tx.ToCallMsg(a.Address()))
if err != nil {
return nil, fmt.Errorf("failed to EstimateGasL2: %w", err)
}
tx.Gas = gas
}
return tx.ToTransaction712(a.auth.From), nil
}
func (a *WalletL2) SignTransaction(tx *zkTypes.Transaction712) ([]byte, error) {
signature, err := (*a.signer).SignTypedData((*a.signer).Domain(), tx)
if err != nil {
return nil, err
}
return tx.RLPValues(signature)
}
func (a *WalletL2) SendTransaction(ctx context.Context, tx *Transaction) (common.Hash, error) {
preparedTx, err := a.PopulateTransaction(ensureContext(ctx), *tx)
if err != nil {
return common.Hash{}, err
}
rawTx, err := a.SignTransaction(preparedTx)
if err != nil {
return common.Hash{}, err
}
return (*a.client).SendRawTransaction(context.Background(), rawTx)
}
func (a *WalletL2) transferETH(auth *TransactOpts, tx TransferTransaction) (*types.Transaction, error) {
if auth.GasPrice != nil {
if auth.Nonce == nil {
nonce, err := (*a.client).NonceAt(auth.Context, a.Address(), nil)
if err != nil {
return nil, fmt.Errorf("failed to get nonce: %w", err)
}
auth.Nonce = new(big.Int).SetUint64(nonce)
}
transaction := types.NewTx(
&types.LegacyTx{
Nonce: auth.Nonce.Uint64(),
GasPrice: auth.GasPrice,
Gas: auth.GasLimit,
To: &tx.To,
Value: tx.Amount,
})
signedTx, _ := types.SignTx(transaction, types.NewLondonSigner((*a.signer).Domain().ChainId), (*a.signer).PrivateKey())
err := (*a.client).SendTransaction(auth.Context, signedTx)
if err != nil {
return nil, err
}
return signedTx, nil
} else {
preparedTx, err := a.PopulateTransaction(auth.Context, *tx.ToTransaction(auth))
if err != nil {
return nil, err
}
transaction := types.NewTx(
&types.DynamicFeeTx{
ChainID: (*a.signer).Domain().ChainId,
Nonce: preparedTx.Nonce.Uint64(),
GasTipCap: preparedTx.GasTipCap,
GasFeeCap: preparedTx.GasFeeCap,
Gas: preparedTx.Gas.Uint64(),
To: preparedTx.To,
Value: preparedTx.Value,
})
signedTx, _ := types.SignTx(transaction, types.NewLondonSigner((*a.signer).Domain().ChainId), (*a.signer).PrivateKey())
err = (*a.client).SendTransaction(auth.Context, signedTx)
if err != nil {
return nil, err
}
return signedTx, nil
}
}