/
community.go
479 lines (389 loc) · 10.9 KB
/
community.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
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
package community
import (
"crypto/ecdsa"
"math/big"
"github.com/daobrussels/cw/pkg/common/ethrequest"
"github.com/daobrussels/cw/pkg/cw"
"github.com/daobrussels/smartcontracts/pkg/contracts/accfactory"
"github.com/daobrussels/smartcontracts/pkg/contracts/account"
"github.com/daobrussels/smartcontracts/pkg/contracts/gateway"
"github.com/daobrussels/smartcontracts/pkg/contracts/grfactory"
"github.com/daobrussels/smartcontracts/pkg/contracts/paymaster"
"github.com/daobrussels/smartcontracts/pkg/contracts/profactory"
"github.com/daobrussels/smartcontracts/pkg/contracts/profile"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)
type CommunityAddress struct {
Gateway common.Address `json:"gateway"`
Paymaster common.Address `json:"paymaster"`
AccountFactory common.Address `json:"accountFactory"`
GratitudeFactory common.Address `json:"gratitudeFactory"`
ProfileFactory common.Address `json:"profileFactory"`
Chain cw.ChainConfig `json:"chain"`
}
type Community struct {
es *ethrequest.EthService
key *ecdsa.PrivateKey
address common.Address
Chain cw.ChainConfig
EntryPoint common.Address
Gateway *gateway.Gateway
paddr common.Address
Paymaster *paymaster.Paymaster
afaddr common.Address
AccountFactory *accfactory.Accfactory
grfaddr common.Address
GratitudeFactory *grfactory.Grfactory
prfaddr common.Address
ProfileFactory *profactory.Profactory
}
func (c *Community) ExportAddress() CommunityAddress {
return CommunityAddress{
Gateway: c.EntryPoint,
Paymaster: c.paddr,
AccountFactory: c.afaddr,
GratitudeFactory: c.grfaddr,
ProfileFactory: c.prfaddr,
Chain: c.Chain,
}
}
// New instantiates a community struct using the provided addresses for the contracts
func New(es *ethrequest.EthService, key *ecdsa.PrivateKey, address common.Address, addr CommunityAddress) (*Community, error) {
// instantiate gateway contract
g, err := gateway.NewGateway(addr.Gateway, es.Client())
if err != nil {
return nil, err
}
// instantiate paymaster contract
p, err := paymaster.NewPaymaster(addr.Paymaster, es.Client())
if err != nil {
return nil, err
}
// instantiate account factory contract
acc, err := accfactory.NewAccfactory(addr.AccountFactory, es.Client())
if err != nil {
return nil, err
}
// instantiate gratitude factory contract
gr, err := grfactory.NewGrfactory(addr.GratitudeFactory, es.Client())
if err != nil {
return nil, err
}
// instantiate profile factory contract
pro, err := profactory.NewProfactory(addr.ProfileFactory, es.Client())
if err != nil {
return nil, err
}
return &Community{
es: es,
key: key,
address: address,
Chain: addr.Chain,
EntryPoint: addr.Gateway,
Gateway: g,
paddr: addr.Paymaster,
Paymaster: p,
afaddr: addr.AccountFactory,
AccountFactory: acc,
grfaddr: addr.GratitudeFactory,
GratitudeFactory: gr,
prfaddr: addr.ProfileFactory,
ProfileFactory: pro,
}, nil
}
// Deploy instantiates a community struct and deploys the contracts
func Deploy(es *ethrequest.EthService, key *ecdsa.PrivateKey, address common.Address, chain cw.ChainConfig) (*Community, error) {
c := &Community{
es: es,
key: key,
address: address,
Chain: chain,
}
// instantiate gateway contract
err := c.DeployGateway()
if err != nil {
return nil, err
}
// deploy paymaster contract
err = c.DeployPaymaster()
if err != nil {
return nil, err
}
// deploy account factory contract
err = c.DeployAccountFactory()
if err != nil {
return nil, err
}
// deploy gratitude factory contract
err = c.DeployGratitudeFactory()
if err != nil {
return nil, err
}
// deploy profile factory contract
err = c.DeployProfileFactory()
if err != nil {
return nil, err
}
return c, nil
}
// NewTransactor returns a new transactor for the community
func (c *Community) NewTransactor() (*bind.TransactOpts, error) {
return bind.NewKeyedTransactorWithChainID(c.key, big.NewInt(int64(c.Chain.ChainID)))
}
// NextNonce returns the next nonce for the community
func (c *Community) NextNonce() (uint64, error) {
return c.es.NextNonce(c.address.Hex())
}
// DeployGateway deploys the gateway contract
func (c *Community) DeployGateway() error {
auth, err := c.NewTransactor()
if err != nil {
return err
}
// get the next nonce for the main wallet
nonce, err := c.NextNonce()
if err != nil {
return err
}
// set default parameters
setDefaultParameters(auth, nonce)
// deploy the gateway contract
addr, _, g, err := gateway.DeployGateway(auth, c.es.Client())
if err != nil {
return err
}
c.EntryPoint = addr
c.Gateway = g
return nil
}
// DeployPaymaster deploys the paymaster contract
func (c *Community) DeployPaymaster() error {
auth, err := c.NewTransactor()
if err != nil {
return err
}
// get the next nonce for the main wallet
nonce, err := c.NextNonce()
if err != nil {
return err
}
// set default parameters
setDefaultParameters(auth, nonce)
// deploy the paymaster contract
addr, _, p, err := paymaster.DeployPaymaster(auth, c.es.Client(), c.EntryPoint)
if err != nil {
return err
}
c.paddr = addr
c.Paymaster = p
return nil
}
// FundPaymaster funds the paymaster contract
func (c *Community) FundPaymaster(amount *big.Int) error {
auth, err := c.NewTransactor()
if err != nil {
return err
}
// get the next nonce for the main wallet
nonce, err := c.NextNonce()
if err != nil {
return err
}
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = amount
_, err = c.Paymaster.Deposit(auth)
if err != nil {
return err
}
return nil
}
// DeployAccountFactory deploys the account factory contract
func (c *Community) DeployAccountFactory() error {
auth, err := c.NewTransactor()
if err != nil {
return err
}
// get the next nonce for the main wallet
nonce, err := c.NextNonce()
if err != nil {
return err
}
// set default parameters
setDefaultParameters(auth, nonce)
// deploy the account factory contract
addr, _, acc, err := accfactory.DeployAccfactory(auth, c.es.Client(), c.EntryPoint)
if err != nil {
return err
}
c.afaddr = addr
c.AccountFactory = acc
return nil
}
// DeployGratitudeFactory deploys the gratitude factory contract
func (c *Community) DeployGratitudeFactory() error {
auth, err := c.NewTransactor()
if err != nil {
return err
}
// get the next nonce for the main wallet
nonce, err := c.NextNonce()
if err != nil {
return err
}
// set default parameters
setDefaultParameters(auth, nonce)
// deploy the gratitude factory contract
addr, _, gr, err := grfactory.DeployGrfactory(auth, c.es.Client(), c.EntryPoint)
if err != nil {
return err
}
c.grfaddr = addr
c.GratitudeFactory = gr
return nil
}
// CreateGratitudeApp creates a gratitude app for the provided owner
func (c *Community) CreateGratitudeApp(owner common.Address) (*common.Address, error) {
auth, err := c.NewTransactor()
if err != nil {
return nil, err
}
// get the next nonce for the main wallet
nonce, err := c.NextNonce()
if err != nil {
return nil, err
}
// set default parameters
setDefaultParameters(auth, nonce)
// create the gratitude app
_, err = c.GratitudeFactory.CreateGratitudeToken(auth, owner, big.NewInt(int64(nonce)))
if err != nil {
return nil, err
}
addr, err := c.GratitudeFactory.GetGratitudeTokenAddress(&bind.CallOpts{}, owner, big.NewInt(int64(nonce)))
if err != nil {
return nil, err
}
return &addr, nil
}
// CreateAccount creates an account for the provided owner
func (c *Community) CreateAccount(owner common.Address) (*common.Address, error) {
auth, err := c.NewTransactor()
if err != nil {
return nil, err
}
// get the next nonce for the main wallet
nonce, err := c.NextNonce()
if err != nil {
return nil, err
}
// set default parameters
setDefaultParameters(auth, nonce)
_, err = c.AccountFactory.CreateAccount(auth, owner, big.NewInt(int64(nonce)))
if err != nil {
return nil, err
}
addr, err := c.AccountFactory.GetAddress(&bind.CallOpts{}, owner, big.NewInt(int64(nonce)))
if err != nil {
return nil, err
}
return &addr, nil
}
// DeployProfileFactory deploys the profile factory contract
func (c *Community) DeployProfileFactory() error {
auth, err := c.NewTransactor()
if err != nil {
return err
}
// get the next nonce for the main wallet
nonce, err := c.NextNonce()
if err != nil {
return err
}
// set default parameters
setDefaultParameters(auth, nonce)
// deploy profile factory contract
addr, _, pr, err := profactory.DeployProfactory(auth, c.es.Client(), c.EntryPoint)
if err != nil {
return err
}
c.prfaddr = addr
c.ProfileFactory = pr
return nil
}
// CreateProfile creates a profile for the provided owner
func (c *Community) CreateProfile(owner common.Address) (*common.Address, error) {
auth, err := c.NewTransactor()
if err != nil {
return nil, err
}
// get the next nonce for the main wallet
nonce, err := c.NextNonce()
if err != nil {
return nil, err
}
// set default parameters
setDefaultParameters(auth, nonce)
_, err = c.ProfileFactory.CreateProfile(auth, owner, big.NewInt(int64(nonce)))
if err != nil {
return nil, err
}
addr, err := c.ProfileFactory.GetProfileAddress(&bind.CallOpts{}, owner, big.NewInt(int64(nonce)))
if err != nil {
return nil, err
}
return &addr, nil
}
// GetProfile returns the profile for the provided owner
func (c *Community) GetProfile(owner common.Address) (*profile.Profile, error) {
p, err := profile.NewProfile(owner, c.es.Client())
if err != nil {
return nil, err
}
return p, nil
}
// GetAccount returns the account for the provided owner
func (c *Community) GetAccount(owner common.Address) (*account.Account, error) {
a, err := account.NewAccount(owner, c.es.Client())
if err != nil {
return nil, err
}
return a, nil
}
// SubmitOp submits an operation to the gateway for processing
func (c *Community) SubmitOp(sender common.Address, data []byte) error {
auth, err := c.NewTransactor()
if err != nil {
return err
}
// get the next nonce for the main wallet
nonce, err := c.NextNonce()
if err != nil {
return err
}
// set default parameters
setDefaultParameters(auth, nonce)
senderNonce, err := c.es.NextNonce(sender.Hex())
if err != nil {
return err
}
// TODO: test and check UserOperation signature and required data
// This is still not tested and may not work
op := &gateway.UserOperation{
Sender: sender,
Nonce: big.NewInt(int64(senderNonce)),
CallData: data,
PaymasterAndData: c.paddr.Bytes(),
}
_, err = c.Gateway.HandleOps(auth, []gateway.UserOperation{*op}, c.address)
if err != nil {
return err
}
return nil
}
// setDefaultParameters sets the nonce, value and gas limit for a default contract transaction
func setDefaultParameters(auth *bind.TransactOpts, nonce uint64) {
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0)
auth.GasLimit = uint64(0)
}