Skip to content
This repository was archived by the owner on Sep 13, 2022. It is now read-only.

Commit 8be656e

Browse files
committed
complete construction parse
1 parent 8ec3a1d commit 8be656e

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

server/services/construction_service.go

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,72 @@ func (s *ConstructionAPIService) ConstructionHash(
121121

122122
// ConstructionParse implements the /construction/parse endpoint.
123123
func (s *ConstructionAPIService) ConstructionParse(
124-
context.Context,
125-
*types.ConstructionParseRequest,
124+
ctx context.Context,
125+
request *types.ConstructionParseRequest,
126126
) (*types.ConstructionParseResponse, *types.Error) {
127-
panic("implement me")
127+
tx, err := ckbRpc.TransactionFromString(request.Transaction)
128+
if err != nil {
129+
return nil, &types.Error{
130+
Code: 11,
131+
Message: fmt.Sprintf("can not decode transaction string: %s", request.Transaction),
132+
Retriable: false,
133+
}
134+
}
135+
136+
signers := make(map[string]bool)
137+
index := int64(0)
138+
operations := make([]*types.Operation, 0)
139+
for _, input := range tx.Inputs {
140+
ptx, err := s.client.GetTransaction(ctx, input.PreviousOutput.TxHash)
141+
if err != nil {
142+
return nil, ServerError
143+
}
144+
145+
addr := GenerateAddress(s.network, ptx.Transaction.Outputs[input.PreviousOutput.Index].Lock)
146+
signers[addr] = true
147+
operations = append(operations, &types.Operation{
148+
OperationIdentifier: &types.OperationIdentifier{
149+
Index: index,
150+
},
151+
Type: "Transfer",
152+
Status: "Success",
153+
Account: &types.AccountIdentifier{
154+
Address: addr,
155+
},
156+
Amount: &types.Amount{
157+
Value: fmt.Sprintf("-%d", ptx.Transaction.Outputs[input.PreviousOutput.Index].Capacity),
158+
Currency: CkbCurrency,
159+
},
160+
})
161+
index++
162+
}
163+
for _, output := range tx.Outputs {
164+
operations = append(operations, &types.Operation{
165+
OperationIdentifier: &types.OperationIdentifier{
166+
Index: index,
167+
},
168+
Type: "Transfer",
169+
Status: "Success",
170+
Account: &types.AccountIdentifier{
171+
Address: GenerateAddress(s.network, output.Lock),
172+
},
173+
Amount: &types.Amount{
174+
Value: fmt.Sprintf("%d", output.Capacity),
175+
Currency: CkbCurrency,
176+
},
177+
})
178+
index++
179+
}
180+
181+
addresses := make([]string, 0, len(signers))
182+
for addr := range signers {
183+
addresses = append(addresses, addr)
184+
}
185+
186+
return &types.ConstructionParseResponse{
187+
Operations: operations,
188+
Signers: addresses,
189+
}, nil
128190
}
129191

130192
// ConstructionPayloads implements the /construction/payloads endpoint.

0 commit comments

Comments
 (0)