Skip to content

Commit

Permalink
return amount and recipient when listing Txs
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Nov 22, 2024
1 parent 9d04f52 commit 7d06a0c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
95 changes: 59 additions & 36 deletions api/grpcserver/v2alpha1/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,30 @@ func convertTxState(tx *types.MeshTransaction) *spacemeshv2alpha1.TransactionSta
}
}

func decodeTxArgs(decoder *scale.Decoder) (*athcon.MethodSelector, *core.Address, *signing.PublicKey, error) {
type spawnTx struct {
Pubkey [32]byte
}

type spendTx struct {
To types.Address
Amount uint64
}

var spawnSelector, spendSelector athcon.MethodSelector

func init() {
var err error
spawnSelector, err = athcon.FromString("athexp_spawn")
if err != nil {
panic(err.Error())

Check warning on line 395 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L395

Added line #L395 was not covered by tests
}
spendSelector, err = athcon.FromString("athexp_spend")
if err != nil {
panic(err.Error())

Check warning on line 399 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L399

Added line #L399 was not covered by tests
}
}

func decodeTxArgs(decoder *scale.Decoder) (any, *core.Address, error) {
reg := registry.New()
wallet.Register(reg)
// multisig.Register(reg)
Expand All @@ -386,45 +409,47 @@ func decodeTxArgs(decoder *scale.Decoder) (*athcon.MethodSelector, *core.Address

_, _, err := scale.DecodeCompact8(decoder)
if err != nil {
return nil, nil, nil, fmt.Errorf("%w: failed to decode version %w", core.ErrMalformed, err)
return nil, nil, fmt.Errorf("%w: failed to decode version %w", core.ErrMalformed, err)

Check warning on line 412 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L412

Added line #L412 was not covered by tests
}

var principal core.Address
if _, err := principal.DecodeScale(decoder); err != nil {
return nil, nil, nil, fmt.Errorf("%w failed to decode principal: %w", core.ErrMalformed, err)
return nil, nil, fmt.Errorf("%w failed to decode principal: %w", core.ErrMalformed, err)

Check warning on line 417 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L417

Added line #L417 was not covered by tests
}

handler := reg.Get(wallet.TemplateAddress)
if handler == nil {
return nil, nil, nil, fmt.Errorf("%w: wallet template not found", core.ErrMalformed)
return nil, nil, fmt.Errorf("%w: wallet template not found", core.ErrMalformed)

Check warning on line 422 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L422

Added line #L422 was not covered by tests
}
output, err := handler.Parse(decoder)
if err != nil {
return nil, nil, nil, fmt.Errorf("%w: failed to parse transaction %w", core.ErrMalformed, err)
return nil, nil, fmt.Errorf("%w: failed to parse transaction %w", core.ErrMalformed, err)

Check warning on line 426 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L426

Added line #L426 was not covered by tests
}

var unmarshaled struct {
*athcon.MethodSelector
signing.PublicKey
var payload athcon.Payload
err = gossamerScale.Unmarshal(output.Payload, &payload)
if err != nil {
return nil, nil, fmt.Errorf("%w: tx payload", core.ErrMalformed)
}

Check warning on line 433 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L432-L433

Added lines #L432 - L433 were not covered by tests
if payload.Selector == nil {
return nil, nil, fmt.Errorf("%w: nil method selector", core.ErrMalformed)

Check warning on line 435 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L435

Added line #L435 was not covered by tests
}
err = gossamerScale.Unmarshal(output.Payload, &unmarshaled)

var txArgs any
switch *payload.Selector {
case spawnSelector:
txArgs = new(spawnTx)
case spendSelector:
txArgs = new(spendTx)
default:
return nil, nil, fmt.Errorf("%w: unknown method selector %s", core.ErrMalformed, payload.Selector.String())

Check warning on line 445 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L444-L445

Added lines #L444 - L445 were not covered by tests
}
err = gossamerScale.Unmarshal(payload.Input, txArgs)
if err != nil {
return nil, nil, nil, fmt.Errorf("%w: malformed spawn payload", core.ErrMalformed)
}
// var p core.Payload
// if _, err = p.DecodeScale(decoder); err != nil {
// return 0, nil, nil, fmt.Errorf("%w: %w", core.ErrMalformed, err)
// }

// args := handler.Args(method)
// if args == nil {
// return 0, nil, nil, fmt.Errorf("%w: unknown method %s %d", core.ErrMalformed, *templateAddress, method)
// }
// if _, err := args.DecodeScale(decoder); err != nil {
// return 0, nil, nil, fmt.Errorf("%w failed to decode method arguments %w", core.ErrMalformed, err)
// }

return unmarshaled.MethodSelector, &wallet.TemplateAddress, &unmarshaled.PublicKey, nil
return nil, nil, fmt.Errorf("%w: malformed tx arguments payload", core.ErrMalformed)
}

Check warning on line 450 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L449-L450

Added lines #L449 - L450 were not covered by tests

return txArgs, &wallet.TemplateAddress, nil
}

func toTxContents(rawTx []byte) (*spacemeshv2alpha1.TransactionContents,
Expand All @@ -434,31 +459,29 @@ func toTxContents(rawTx []byte) (*spacemeshv2alpha1.TransactionContents,
txType := spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_UNSPECIFIED

r := bytes.NewReader(rawTx)
method, _, pubkey, err := decodeTxArgs(scale.NewDecoder(r))
txArgs, _, err := decodeTxArgs(scale.NewDecoder(r))
if err != nil {
return res, txType, err
}

if spawnSelector, err := athcon.FromString("athexp_spawn"); err != nil {
return res, txType, fmt.Errorf("%w: failed to create spawn selector: %w", core.ErrInternal, err)
} else if spendSelector, err := athcon.FromString("athexp_spend"); err != nil {
return res, txType, fmt.Errorf("%w: failed to create spend selector: %w", core.ErrInternal, err)
} else if *method == spawnSelector {
switch args := txArgs.(type) {
case *spawnTx:
res.Contents = &spacemeshv2alpha1.TransactionContents_SingleSigSpawn{
SingleSigSpawn: &spacemeshv2alpha1.ContentsSingleSigSpawn{
Pubkey: pubkey.String(),
Pubkey: signing.NewPublicKey(args.Pubkey[:]).String(),
},
}
txType = spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_SINGLE_SIG_SPAWN
} else if *method == spendSelector {
case *spendTx:
res.Contents = &spacemeshv2alpha1.TransactionContents_Send{
// TODO(lane): we don't currently attempt to parse these from the payload
Send: &spacemeshv2alpha1.ContentsSend{
// Destination: args.Destination.String(),
// Amount: args.Amount,
Destination: args.To.String(),
Amount: args.Amount,
},
}
txType = spacemeshv2alpha1.Transaction_TRANSACTION_TYPE_SINGLE_SIG_SEND
default:
panic("txArgs is guaranteed to be spawn or spend at this point")

Check warning on line 484 in api/grpcserver/v2alpha1/transaction.go

View check run for this annotation

Codecov / codecov/patch

api/grpcserver/v2alpha1/transaction.go#L483-L484

Added lines #L483 - L484 were not covered by tests
}

return res, txType, nil
Expand Down
7 changes: 2 additions & 5 deletions api/grpcserver/v2alpha1/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,8 @@ func TestTransactionService_ParseTransaction(t *testing.T) {
})
require.NoError(t, err)

// TODO(lane): we don't currently parse tx amount for athena txs
require.Equal(t, uint64(0), resp.Tx.Contents.GetSend().Amount)
require.Equal(t, "", resp.Tx.Contents.GetSend().Destination)
// require.Equal(t, amount, resp.Tx.Contents.GetSend().Amount)
// require.Equal(t, addr.String(), resp.Tx.Contents.GetSend().Destination)
require.Equal(t, amount, resp.Tx.Contents.GetSend().Amount)
require.Equal(t, addr.String(), resp.Tx.Contents.GetSend().Destination)
})

t.Run("transaction contents for spawn tx", func(t *testing.T) {
Expand Down

0 comments on commit 7d06a0c

Please sign in to comment.