Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
226 lines (191 loc) · 6.78 KB
/
Copy pathclient.go
File metadata and controls
226 lines (191 loc) · 6.78 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
package zerodev
import (
"crypto/ecdsa"
"github.com/DIMO-Network/go-zerodev/account"
"github.com/DIMO-Network/go-zerodev/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
"github.com/friendsofgo/errors"
"math/big"
"net/url"
)
type ClientConfig struct {
AccountAddress common.Address
AccountPK *ecdsa.PrivateKey
EntryPointVersion string
RpcURL *url.URL
PaymasterURL *url.URL
BundlerURL *url.URL
ChainID *big.Int
ReceiptPollingDelaySeconds int
ReceiptPollingRetries int
}
type UserOperationResult struct {
UserOperationHash []byte `json:"userOperationHash"`
Receipt *UserOperationReceipt `json:"receipt,omitempty"`
}
type Client struct {
Signer types.AccountSigner
EntryPoint Entrypoint
PaymasterClient *PaymasterClient
BundlerClient *BundlerClient
ChainID *big.Int
RpcClients struct {
Network *rpc.Client
Paymaster *rpc.Client
Bundler *rpc.Client
}
ReceiptPollingDelay int
ReceiptPollingRetries int
}
func NewClient(config *ClientConfig) (*Client, error) {
if config.AccountPK == nil || config.PaymasterURL == nil || config.BundlerURL == nil || config.EntryPointVersion != EntryPointVersion07 || config.ChainID == nil {
return nil, errors.New("accountPK, paymasterURL, bundlerURL, entryPointVersion and chainID are required")
}
networkRpc, err := rpc.Dial(config.RpcURL.String())
if err != nil {
return nil, errors.Wrap(err, "failed to connect to RPC")
}
paymasterRpc, err := rpc.Dial(config.PaymasterURL.String())
if err != nil {
networkRpc.Close()
return nil, errors.Wrap(err, "failed to connect to Paymaster")
}
bundleRpc, err := rpc.Dial(config.BundlerURL.String())
if err != nil {
paymasterRpc.Close()
networkRpc.Close()
return nil, errors.Wrap(err, "failed to connect to Bundler")
}
entrypoint, err := NewEntrypoint07(networkRpc, config.ChainID)
if err != nil {
networkRpc.Close()
paymasterRpc.Close()
networkRpc.Close()
return nil, errors.Wrap(err, "failed to initialize entrypoint")
}
paymasterClient, err := NewPaymasterClient(paymasterRpc, entrypoint, config.ChainID)
if err != nil {
networkRpc.Close()
paymasterRpc.Close()
networkRpc.Close()
return nil, errors.Wrap(err, "failed to initialize paymasterClient")
}
bundlerClient, err := NewBundlerClient(bundleRpc, entrypoint, config.ChainID)
if err != nil {
networkRpc.Close()
paymasterRpc.Close()
networkRpc.Close()
return nil, errors.Wrap(err, "failed to initialize bundlerClient")
}
signer, err := account.NewSmartAccountPrivateKeySigner(networkRpc, config.AccountAddress, config.AccountPK)
if err != nil {
networkRpc.Close()
paymasterRpc.Close()
networkRpc.Close()
return nil, errors.Wrap(err, "failed to initialize signer")
}
pollingDelaySeconds := 10
if config.ReceiptPollingDelaySeconds > 0 {
pollingDelaySeconds = config.ReceiptPollingDelaySeconds
}
pollingRetries := 24
if config.ReceiptPollingRetries > 0 {
pollingRetries = config.ReceiptPollingRetries
}
return &Client{
Signer: signer,
PaymasterClient: paymasterClient,
BundlerClient: bundlerClient,
EntryPoint: entrypoint,
ChainID: config.ChainID,
RpcClients: struct {
Network *rpc.Client
Paymaster *rpc.Client
Bundler *rpc.Client
}{
Network: networkRpc,
Paymaster: paymasterRpc,
Bundler: bundleRpc,
},
ReceiptPollingDelay: pollingDelaySeconds,
ReceiptPollingRetries: pollingRetries,
}, nil
}
func (c *Client) Close() {
c.RpcClients.Network.Close()
c.RpcClients.Paymaster.Close()
c.RpcClients.Bundler.Close()
}
// GetUserOperationAndHashToSign creates a UserOperation based on the sender and callData, computes its hash and returns both.
// Allows to create UserOperation with custom sender and then customize the signing process.
// After adding signature to the returned UserOperation, it can be sent by SendSignedUserOperation
func (c *Client) GetUserOperationAndHashToSign(sender common.Address, callData *[]byte) (*UserOperation, *common.Hash, error) {
var err error
var op UserOperation
nonce, err := c.EntryPoint.GetNonce(sender)
if err != nil {
return nil, nil, err
}
op.Sender = sender
op.Nonce = nonce
op.CallData = *callData
gasPrice, err := c.BundlerClient.GetUserOperationGasPrice()
if err != nil {
return nil, nil, err
}
op.MaxFeePerGas = gasPrice.Standard.MaxFeePerGas
op.MaxPriorityFeePerGas = gasPrice.Standard.MaxPriorityFeePerGas
sponsorResponse, err := c.PaymasterClient.SponsorUserOperation(&op)
if err != nil {
return nil, nil, err
}
op.Paymaster = sponsorResponse.Paymaster
op.PaymasterData = sponsorResponse.PaymasterData
op.PreVerificationGas = sponsorResponse.PreVerificationGas
op.VerificationGasLimit = sponsorResponse.VerificationGasLimit
op.PaymasterVerificationGasLimit = sponsorResponse.PaymasterVerificationGasLimit
op.PaymasterPostOpGasLimit = sponsorResponse.PaymasterPostOpGasLimit
op.CallGasLimit = sponsorResponse.CallGasLimit
opHash, err := c.EntryPoint.GetUserOperationHash(&op)
if err != nil {
return nil, nil, err
}
return &op, opHash, nil
}
// SendSignedUserOperation sends a pre-signed user operation to the bundler.
// Allows to create UserOperation with different sender and this sender's signature
func (c *Client) SendSignedUserOperation(signedOp *UserOperation, waitForReceipt bool) (*UserOperationResult, error) {
response, err := c.BundlerClient.SendUserOperation(signedOp)
if err != nil {
return nil, err
}
var receipt *UserOperationReceipt
if waitForReceipt {
receipt, _ = c.BundlerClient.GetUserOperationReceipt(response, c.ReceiptPollingDelay, c.ReceiptPollingRetries)
}
return &UserOperationResult{
UserOperationHash: response,
Receipt: receipt,
}, nil
}
// SendUserOperation creates and sends a signed user operation using the provided call data.
// Sender of the user operation is the client's Sender and the signer is SenderSigner
func (c *Client) SendUserOperation(callData *[]byte, waitForReceipt bool) (*UserOperationResult, error) {
op, opHash, err := c.GetUserOperationAndHashToSign(c.Signer.GetAddress(), callData)
if err != nil {
return nil, err
}
signature, err := c.Signer.SignUserOperationHash(*opHash)
if err != nil {
return nil, err
}
op.Signature = signature
return c.SendSignedUserOperation(op, waitForReceipt)
}
func (c *Client) GetUserOperationReceipt(result *UserOperationResult) (*UserOperationReceipt, error) {
return c.BundlerClient.GetUserOperationReceipt(result.UserOperationHash, c.ReceiptPollingDelay, c.ReceiptPollingRetries)
}
func (c *Client) GetSmartAccountSigner(address common.Address, pk *ecdsa.PrivateKey) (types.AccountSigner, error) {
return account.NewSmartAccountPrivateKeySigner(c.RpcClients.Network, address, pk)
}