Skip to content

Commit

Permalink
zeto: pass ReadStates during assembly for lock
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Richardson <[email protected]>
  • Loading branch information
awrichar committed Jan 8, 2025
1 parent 6269b27 commit 43f14d7
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions domains/zeto/internal/zeto/handler_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,28 @@ func (h *lockHandler) decodeTransferCall(ctx context.Context, config *types.Doma
return &params, err
}

func (h *lockHandler) loadCoins(ctx context.Context, ids []any, stateQueryContext string) ([]*types.ZetoCoin, error) {
func (h *lockHandler) loadCoins(ctx context.Context, ids []any, stateQueryContext string) ([]*types.ZetoCoin, []*prototk.StateRef, error) {
inputIDs := make([]any, 0, len(ids))
stateRefs := make([]*prototk.StateRef, 0, len(ids))
for _, input := range ids {
parsed, err := tktypes.ParseHexUint256(ctx, input.(string))
if err != nil {
return nil, err
return nil, nil, err
}
if !parsed.NilOrZero() {
inputIDs = append(inputIDs, parsed)
stateRefs = append(stateRefs, &prototk.StateRef{
Id: parsed.String(),
SchemaId: h.zeto.coinSchema.Id,
})
}
}

// TODO: this should probably query all states and not just available ones
queryBuilder := query.NewQueryBuilder().In(".id", inputIDs)
inputStates, err := h.zeto.findAvailableStates(ctx, false, stateQueryContext, queryBuilder.Query().String())
if err != nil {
return nil, err
return nil, nil, err
}
if len(inputStates) != len(inputIDs) {
missingStates := make([]*tktypes.HexUint256, 0, len(inputIDs))
Expand All @@ -128,17 +133,17 @@ func (h *lockHandler) loadCoins(ctx context.Context, ids []any, stateQueryContex
missingStates = append(missingStates, idInt)
}
}
return nil, i18n.NewError(ctx, msgs.MsgStatesNotFound, missingStates)
return nil, nil, i18n.NewError(ctx, msgs.MsgStatesNotFound, missingStates)
}

inputCoins := make([]*types.ZetoCoin, len(inputStates))
for i, state := range inputStates {
err := json.Unmarshal([]byte(state.DataJson), &inputCoins[i])
if err != nil {
return nil, err
return nil, nil, err
}
}
return inputCoins, nil
return inputCoins, stateRefs, nil
}

func (h *lockHandler) Assemble(ctx context.Context, tx *types.ParsedTransaction, req *prototk.AssembleTransactionRequest) (*prototk.AssembleTransactionResponse, error) {
Expand All @@ -147,7 +152,7 @@ func (h *lockHandler) Assemble(ctx context.Context, tx *types.ParsedTransaction,
if err != nil {
return nil, i18n.NewError(ctx, msgs.MsgErrorDecodeTransferCall, err)
}
inputCoins, err := h.loadCoins(ctx, decodedTransfer.Inputs, req.StateQueryContext)
inputCoins, inputStates, err := h.loadCoins(ctx, decodedTransfer.Inputs, req.StateQueryContext)
if err != nil {
return nil, err
}
Expand All @@ -169,8 +174,7 @@ func (h *lockHandler) Assemble(ctx context.Context, tx *types.ParsedTransaction,
return &prototk.AssembleTransactionResponse{
AssemblyResult: prototk.AssembleTransactionResponse_OK,
AssembledTransaction: &prototk.AssembledTransaction{
InputStates: []*prototk.StateRef{},
OutputStates: []*prototk.NewState{},
ReadStates: inputStates,
},
AttestationPlan: []*prototk.AttestationRequest{
{
Expand Down

0 comments on commit 43f14d7

Please sign in to comment.