Skip to content

Commit

Permalink
Fix: unlock assets
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Sep 24, 2024
1 parent e8671b5 commit 3c4a11c
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 52 deletions.
1 change: 1 addition & 0 deletions internal/storage/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type IBridge interface {
ByAddress(ctx context.Context, addressId uint64) (Bridge, error)
ByRollup(ctx context.Context, rollupId uint64, limit, offset int) ([]Bridge, error)
ByRoles(ctx context.Context, addressId uint64, limit, offset int) ([]Bridge, error)
ListWithAddress(ctx context.Context, limit, offset int) ([]Bridge, error)
}

type Bridge struct {
Expand Down
39 changes: 39 additions & 0 deletions internal/storage/mock/bridge.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/storage/postgres/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/celenium-io/astria-indexer/internal/storage"
"github.com/dipdup-net/go-lib/database"
sdk "github.com/dipdup-net/indexer-sdk/pkg/storage"
"github.com/dipdup-net/indexer-sdk/pkg/storage/postgres"
)

Expand Down Expand Up @@ -89,3 +90,20 @@ func (b *Bridge) ByRoles(ctx context.Context, addressId uint64, limit, offset in
Scan(ctx, &result)
return
}

func (b *Bridge) ListWithAddress(ctx context.Context, limit, offset int) (result []storage.Bridge, err error) {
query := b.DB().NewSelect().
Model((*storage.Bridge)(nil)).
Offset(offset)

query = limitScope(query, limit)
query = sortScope(query, "id", sdk.SortOrderAsc)

err = b.DB().NewSelect().
TableExpr("(?) as bridge", query).
ColumnExpr("bridge.*").
ColumnExpr("address.hash as address__hash").
Join("left join address as address on address.id = bridge.address_id").
Scan(ctx, &result)
return
}
25 changes: 25 additions & 0 deletions internal/storage/postgres/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,28 @@ func (s *StorageTestSuite) TestBridgeByRoles() {
hash, _ := hex.DecodeString("19ba8abb3e4b56a309df6756c47b97e298e3a72d88449d36a0fadb1ca7366539")
s.Require().Equal(hash, bridge.Rollup.AstriaId)
}

func (s *StorageTestSuite) TestBridgeListWithAddress() {
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()

bridges, err := s.storage.Bridges.ListWithAddress(ctx, 1, 0)
s.Require().NoError(err)
s.Require().Len(bridges, 1)

bridge := bridges[0]
s.Require().EqualValues(7316, bridge.InitHeight)
s.Require().EqualValues(1, bridge.AddressId)
s.Require().EqualValues(1, bridge.SudoId)
s.Require().EqualValues(1, bridge.WithdrawerId)
s.Require().EqualValues(1, bridge.RollupId)
s.Require().EqualValues("nria", bridge.Asset)
s.Require().EqualValues("nria", bridge.FeeAsset)

s.Require().NotNil(bridge.Address)
s.Require().Equal("astria1lm45urgugesyhaymn68xww0m6g49zreqa32w7p", bridge.Address.Hash)

s.Require().Nil(bridge.Sudo)
s.Require().Nil(bridge.Withdrawer)
s.Require().Nil(bridge.Rollup)
}
27 changes: 19 additions & 8 deletions pkg/indexer/decode/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

astria "buf.build/gen/go/astria/protocol-apis/protocolbuffers/go/astria/protocol/transactions/v1alpha1"
"github.com/celenium-io/astria-indexer/internal/currency"
"github.com/celenium-io/astria-indexer/internal/storage"
storageTypes "github.com/celenium-io/astria-indexer/internal/storage/types"
"github.com/celenium-io/astria-indexer/pkg/types"
Expand Down Expand Up @@ -513,6 +514,7 @@ func parseInitBridgeAccount(body *astria.Action_InitBridgeAccountAction, from st
ActionType: action.Type,
}
ctx.AddBridge(&bridge)
ctx.AddBridgeAsset(from, bridge.Asset)
}
return nil
}
Expand Down Expand Up @@ -651,7 +653,6 @@ func parseBridgeLock(body *astria.Action_BridgeLockAction, from string, height t
Update: decAmount.Neg(),
},
)

}
}
return nil
Expand All @@ -676,16 +677,26 @@ func parseBridgeUnlock(body *astria.Action_BridgeUnlockAction, from string, heig
action.Data["bridge"] = bridge
}

decAmount := decimal.RequireFromString(amount)
toAddr := ctx.Addresses.Set(toAddress, height, decAmount, feeAsset, 1, 0)
var (
decAmount = decimal.RequireFromString(amount)
fromAddr *storage.Address
unlockAsset string
)

var fromAddr *storage.Address
if bridge == "" {
fromAddr = ctx.Addresses.Set(from, height, decAmount.Neg(), feeAsset, 1, 0)
fromAddr = ctx.Addresses.Set(from, height, decAmount.Neg(), "", 1, 0)
unlockAsset = currency.DefaultCurrency
} else {
fromAddr = ctx.Addresses.Set(bridge, height, decAmount.Neg(), feeAsset, 1, 0)
asset, ok := ctx.bridgeAssets[bridge]
if !ok {
return errors.Errorf("unknown bridge asset: %s", bridge)
}
fromAddr = ctx.Addresses.Set(bridge, height, decAmount.Neg(), asset, 1, 0)
unlockAsset = asset
}

toAddr := ctx.Addresses.Set(toAddress, height, decAmount, unlockAsset, 1, 0)

action.Addresses = append(action.Addresses,
&storage.AddressAction{
Address: toAddr,
Expand All @@ -707,13 +718,13 @@ func parseBridgeUnlock(body *astria.Action_BridgeUnlockAction, from string, heig
storage.BalanceUpdate{
Address: toAddr,
Height: action.Height,
Currency: feeAsset,
Currency: unlockAsset,
Update: decAmount,
},
storage.BalanceUpdate{
Address: fromAddr,
Height: action.Height,
Currency: feeAsset,
Currency: unlockAsset,
Update: decAmount.Neg(),
},
)
Expand Down
Loading

0 comments on commit 3c4a11c

Please sign in to comment.