This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
submit_batch.go
121 lines (107 loc) · 3.48 KB
/
submit_batch.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
package eth
import (
"context"
"github.com/Worldcoin/hubble-commander/contracts/rollup"
"github.com/Worldcoin/hubble-commander/encoder"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/utils/ref"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/attribute"
)
type SubmitBatchFunc func() (*types.Transaction, error)
func (c *Client) SubmitTransfersBatch(
ctx context.Context,
batchID *models.Uint256,
commitments []models.CommitmentWithTxs,
) (*types.Transaction, error) {
tx, err := c.rollup().
WithValue(c.config.StakeAmount).
WithContext(ctx).
WithGasLimit(*c.config.TransferBatchSubmissionGasLimit).
SubmitTransfer(encoder.CommitmentsToTransferAndC2TSubmitBatchFields(batchID, commitments))
if err != nil {
return nil, errors.WithStack(err)
}
return tx, nil
}
func (c *Client) SubmitCreate2TransfersBatch(
ctx context.Context,
batchID *models.Uint256,
commitments []models.CommitmentWithTxs,
) (*types.Transaction, error) {
tx, err := c.rollup().
WithValue(c.config.StakeAmount).
WithGasLimit(*c.config.C2TBatchSubmissionGasLimit).
WithContext(ctx).
WithAttribute(attribute.String("batchID", batchID.String())).
SubmitCreate2Transfer(encoder.CommitmentsToTransferAndC2TSubmitBatchFields(batchID, commitments))
if err != nil {
return nil, errors.WithStack(err)
}
return tx, nil
}
func (c *Client) SubmitMassMigrationsBatch(
batchID *models.Uint256,
commitments []models.CommitmentWithTxs,
) (*types.Transaction, error) {
tx, err := c.rollup().
WithValue(c.config.StakeAmount).
WithGasLimit(*c.config.MMBatchSubmissionGasLimit).
SubmitMassMigration(encoder.CommitmentsToSubmitMMBatchFields(batchID, commitments))
if err != nil {
return nil, errors.WithStack(err)
}
return tx, nil
}
func (c *Client) SubmitTransfersBatchAndWait(batchID *models.Uint256, commitments []models.CommitmentWithTxs) (*models.Batch, error) {
return c.submitBatchAndWait(func() (*types.Transaction, error) {
return c.SubmitTransfersBatch(context.TODO(), batchID, commitments)
})
}
func (c *Client) SubmitCreate2TransfersBatchAndWait(
batchID *models.Uint256,
commitments []models.CommitmentWithTxs,
) (*models.Batch, error) {
return c.submitBatchAndWait(func() (*types.Transaction, error) {
return c.SubmitCreate2TransfersBatch(context.TODO(), batchID, commitments)
})
}
func (c *Client) SubmitMassMigrationsBatchAndWait(
batchID *models.Uint256,
commitments []models.CommitmentWithTxs,
) (*models.Batch, error) {
return c.submitBatchAndWait(func() (*types.Transaction, error) {
return c.SubmitMassMigrationsBatch(batchID, commitments)
})
}
func (c *Client) submitBatchAndWait(submit SubmitBatchFunc) (batch *models.Batch, err error) {
tx, err := submit()
if err != nil {
return
}
receipt, err := c.WaitToBeMined(tx)
if err != nil {
return nil, err
}
log, err := retrieveLog(receipt, NewBatchEvent)
if err != nil {
return nil, err
}
event := new(rollup.RollupNewBatch)
err = c.Rollup.BoundContract.UnpackLog(event, NewBatchEvent, *log)
if err != nil {
return nil, err
}
return c.handleNewBatchEvent(event)
}
func (c *Client) handleNewBatchEvent(event *rollup.RollupNewBatch) (*models.Batch, error) {
contractBatch, err := c.GetContractBatch(models.NewUint256FromBig(*event.BatchID))
if err != nil {
return nil, err
}
batch := contractBatch.ToModelBatch()
batch.AccountTreeRoot = ref.Hash(common.BytesToHash(event.AccountRoot[:]))
return batch, nil
}