Skip to content

Commit

Permalink
Fix: position in transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Oct 27, 2024
1 parent 0addd5e commit f2da3fb
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 49 deletions.
50 changes: 24 additions & 26 deletions pkg/indexer/decode/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/shopspring/decimal"
)

func parseActions(height types.Level, blockTime time.Time, from, hash string, tx *DecodedTx, ctx *Context) ([]storage.Action, error) {
func parseActions(height types.Level, blockTime time.Time, from string, tx *DecodedTx, ctx *Context) ([]storage.Action, error) {
var (
rawActions = tx.UnsignedTx.GetActions()
actions = make([]storage.Action, len(rawActions))
Expand Down Expand Up @@ -109,32 +109,30 @@ func parseActions(height types.Level, blockTime time.Time, from, hash string, tx
return nil, err
}

if txFees, ok := ctx.Fees[hash]; ok {
if actionFee, ok := txFees[int64(i)]; ok {
actionFee.Height = height
actionFee.Time = blockTime
actionFee.Payer = &storage.Address{
Hash: from,
}
actions[i].Fee = actionFee

fromAmount := actionFee.Amount.Neg()
addr := ctx.Addresses.Set(from, height, fromAmount, actionFee.Asset, 0, 0)
actions[i].BalanceUpdates = append(actions[i].BalanceUpdates, storage.BalanceUpdate{
Address: addr,
Height: actions[i].Height,
Currency: actionFee.Asset,
Update: fromAmount,
})

to := ctx.Addresses.Set(ctx.Proposer, height, actionFee.Amount, actionFee.Asset, 0, 0)
actions[i].BalanceUpdates = append(actions[i].BalanceUpdates, storage.BalanceUpdate{
Address: to,
Height: actions[i].Height,
Currency: actionFee.Asset,
Update: actionFee.Amount,
})
if actionFee, ok := ctx.Fees[int64(i)]; ok {
actionFee.Height = height
actionFee.Time = blockTime
actionFee.Payer = &storage.Address{
Hash: from,
}
actions[i].Fee = actionFee

fromAmount := actionFee.Amount.Neg()
addr := ctx.Addresses.Set(from, height, fromAmount, actionFee.Asset, 0, 0)
actions[i].BalanceUpdates = append(actions[i].BalanceUpdates, storage.BalanceUpdate{
Address: addr,
Height: actions[i].Height,
Currency: actionFee.Asset,
Update: fromAmount,
})

to := ctx.Addresses.Set(ctx.Proposer, height, actionFee.Amount, actionFee.Asset, 0, 0)
actions[i].BalanceUpdates = append(actions[i].BalanceUpdates, storage.BalanceUpdate{
Address: to,
Height: actions[i].Height,
Currency: actionFee.Asset,
Update: actionFee.Amount,
})
}

if deposit, ok := ctx.Deposits[int64(i)]; ok {
Expand Down
14 changes: 4 additions & 10 deletions pkg/indexer/decode/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Context struct {
ActionTypes storageTypes.Bits
Constants map[string]*storage.Constant
Bridges map[string]*storage.Bridge
Fees map[string]map[int64]*storage.Fee
Fees map[int64]*storage.Fee
Transfers []*storage.Transfer
Deposits map[int64]*storage.Deposit
Proposer string
Expand All @@ -42,7 +42,7 @@ func NewContext(bridgeAssets map[string]string) Context {
Validators: NewValidators(),
Constants: make(map[string]*storage.Constant),
Bridges: make(map[string]*storage.Bridge),
Fees: make(map[string]map[int64]*storage.Fee),
Fees: make(map[int64]*storage.Fee),
Transfers: make([]*storage.Transfer, 0),
Deposits: make(map[int64]*storage.Deposit),

Expand Down Expand Up @@ -79,14 +79,8 @@ func (ctx *Context) BridgesArray() []*storage.Bridge {
return arr
}

func (ctx *Context) AddFee(hash string, idx int64, fee *storage.Fee) {
if txFees, ok := ctx.Fees[hash]; ok {
txFees[idx] = fee
} else {
ctx.Fees[hash] = map[int64]*storage.Fee{
idx: fee,
}
}
func (ctx *Context) AddFee(idx int64, fee *storage.Fee) {
ctx.Fees[idx] = fee
}

func (ctx *Context) AddBridgeAsset(bridge, asset string) {
Expand Down
5 changes: 1 addition & 4 deletions pkg/indexer/decode/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package decode

import (
"encoding/hex"

astria "buf.build/gen/go/astria/protocol-apis/protocolbuffers/go/astria/protocol/transaction/v1alpha1"
"github.com/celenium-io/astria-indexer/internal/storage"
storageTypes "github.com/celenium-io/astria-indexer/internal/storage/types"
Expand Down Expand Up @@ -50,8 +48,7 @@ func Tx(b types.BlockData, index int, ctx *Context) (d DecodedTx, err error) {
d.Signer = ctx.Addresses.Set(address, b.Height, decimal.Zero, "", 0, 1)
ctx.Addresses.UpdateNonce(address, d.UnsignedTx.GetParams().GetNonce())

hash := hex.EncodeToString(b.Block.Txs[index].Hash())
d.Actions, err = parseActions(b.Height, b.Block.Time, address, hash, &d, ctx)
d.Actions, err = parseActions(b.Height, b.Block.Time, address, &d, ctx)
if err != nil {
return d, errors.Wrap(err, "parsing actions")
}
Expand Down
15 changes: 6 additions & 9 deletions pkg/indexer/parser/parseEvents.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ func getAsset(ctx context.Context, api node.Api, val string) (string, error) {

func parseTxFees(ctx context.Context, attrs []types.EventAttribute, decodeCtx *decode.Context, api node.Api) error {
var (
fee = new(storage.Fee)
err error
idx int64
hash string
fee = new(storage.Fee)
err error
idx int64
)
for i := range attrs {
switch attrs[i].Key {
Expand All @@ -92,11 +91,9 @@ func parseTxFees(ctx context.Context, attrs []types.EventAttribute, decodeCtx *d
if err != nil {
return err
}
case "actionType":
case "actionName":
fee.ActionType = attrs[i].Value
case "sourceTransactionId":
hash = attrs[i].Value
case "sourceActionIndex":
case "positionInTransaction":
actionIndex, err := strconv.ParseInt(attrs[i].Value, 10, 64)
if err != nil {
return err
Expand All @@ -106,7 +103,7 @@ func parseTxFees(ctx context.Context, attrs []types.EventAttribute, decodeCtx *d
}
}

decodeCtx.AddFee(hash, idx, fee)
decodeCtx.AddFee(idx, fee)
return nil
}

Expand Down
41 changes: 41 additions & 0 deletions pkg/indexer/parser/parseEvents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
package parser

import (
"context"
"testing"

"github.com/celenium-io/astria-indexer/pkg/indexer/decode"
"github.com/celenium-io/astria-indexer/pkg/node/mock"
"github.com/celenium-io/astria-indexer/pkg/types"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func Test_parseTxDeposit(t *testing.T) {
Expand Down Expand Up @@ -51,3 +54,41 @@ func Test_parseTxDeposit(t *testing.T) {
require.EqualValues(t, "0x9d0CEC7BEB948Ab046e8b64E9aa6Cc9b731A9613", deposit.DestinationChainAddress)
})
}

func Test_parseTxFee(t *testing.T) {
t.Run("test fee event", func(t *testing.T) {
attrs := []types.EventAttribute{
{
Key: "actionName",
Value: "astria.protocol.transaction.v1.RollupDataSubmission",
}, {
Key: "asset",
Value: "ibc/704031c868fd3d3c84a1cfa8cb45deba4ea746b44697f7f4a6ed1b8f6c239b82",
}, {
Key: "feeAmount",
Value: "321",
}, {
Key: "positionInTransaction",
Value: "0",
},
}

ctrl := gomock.NewController(t)
defer ctrl.Finish()
api := mock.NewMockApi(ctrl)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

decodeCtx := decode.NewContext(map[string]string{})
err := parseTxFees(ctx, attrs, &decodeCtx, api)
require.NoError(t, err)
require.Len(t, decodeCtx.Fees, 1)

fee, ok := decodeCtx.Fees[0]
require.True(t, ok)
require.EqualValues(t, "321", fee.Amount.String())
require.EqualValues(t, "nria", fee.Asset)
require.EqualValues(t, "astria.protocol.transaction.v1.RollupDataSubmission", fee.ActionType)
})
}

0 comments on commit f2da3fb

Please sign in to comment.