This repository was archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget_commitment.go
98 lines (82 loc) · 2.91 KB
/
get_commitment.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
package api
import (
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/dto"
"github.com/Worldcoin/hubble-commander/models/enums/batchstatus"
"github.com/Worldcoin/hubble-commander/models/enums/batchtype"
"github.com/Worldcoin/hubble-commander/storage"
)
var getCommitmentAPIErrors = map[error]*APIError{
storage.AnyNotFoundError: NewAPIError(20000, "commitment not found"),
}
func (a *API) GetCommitment(id models.CommitmentID) (interface{}, error) {
commitment, err := a.unsafeGetCommitment(id)
if err != nil {
return nil, sanitizeError(err, getCommitmentAPIErrors)
}
return commitment, nil
}
func (a *API) unsafeGetCommitment(id models.CommitmentID) (interface{}, error) {
commitment, err := a.storage.GetCommitment(&id)
if err != nil {
return nil, err
}
batch, err := a.storage.GetBatch(commitment.GetCommitmentBase().ID.BatchID)
if err != nil {
return nil, err
}
return a.createCommitmentDTO(commitment, batch)
}
func (a *API) createCommitmentDTO(commitment models.Commitment, batch *models.Batch) (interface{}, error) {
transactions, err := a.getTransactionsForCommitment(commitment)
if err != nil {
return nil, err
}
status := calculateBatchStatus(a.storage.GetLatestBlockNumber(), batch)
switch batch.Type {
case batchtype.Transfer, batchtype.Create2Transfer:
return a.createTxCommitmentDTO(commitment, batch, transactions, status)
case batchtype.MassMigration:
return dto.NewMMCommitment(commitment.ToMMCommitment(), status, batch.MinedTime, transactions), nil
case batchtype.Deposit:
return dto.NewDepositCommitment(commitment.ToDepositCommitment(), status, batch.MinedTime), nil
default:
panic("invalid commitment type")
}
}
func (a *API) getTransactionsForCommitment(commitment models.Commitment) (interface{}, error) {
commitmentBase := commitment.GetCommitmentBase()
switch commitmentBase.Type {
case batchtype.Transfer, batchtype.Create2Transfer, batchtype.MassMigration:
return a.getDTOsForCommitment(commitmentBase.ID)
case batchtype.Deposit:
return nil, nil
case batchtype.Genesis:
return nil, dto.ErrNotImplemented
}
return nil, dto.ErrNotImplemented
}
func (a *API) getDTOsForCommitment(id models.CommitmentID) (interface{}, error) {
txns, err := a.storage.GetTransactionsByCommitmentID(id)
if err != nil {
return nil, err
}
txs := make([]interface{}, 0, txns.Len())
for i := 0; i < txns.Len(); i++ {
txs = append(txs, dto.MakeTransactionForCommitment(txns.At(i)))
}
return txs, nil
}
func (a *API) createTxCommitmentDTO(
commitment models.Commitment,
batch *models.Batch,
transactions interface{},
status *batchstatus.BatchStatus,
) (interface{}, error) {
stateLeaf, err := a.storage.StateTree.Leaf(commitment.ToTxCommitment().FeeReceiver)
if err != nil {
return nil, err
}
commitmentDTO := dto.NewTxCommitment(commitment.ToTxCommitment(), stateLeaf.TokenID, status, batch.MinedTime, transactions)
return commitmentDTO, nil
}