Skip to content

Commit

Permalink
chore: update for changes in protobuf schema (#6)
Browse files Browse the repository at this point in the history
This updates the go sequencer client for recent changes to the protobuf
schema.
  • Loading branch information
mycodecrafting authored Mar 7, 2024
1 parent 0df599d commit b17543b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 39 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ package main

import (
"context"
"crypto/sha256"
"fmt"

sqproto "buf.build/gen/go/astria/astria/protocolbuffers/go/astria/sequencer/v1alpha1"
client "github.com/astriaorg/go-sequencer-client/client"
sqproto "github.com/astriaorg/go-sequencer-client/proto"
)

func main() {
Expand All @@ -24,14 +25,15 @@ func main() {
panic(err)
}

rollupId := sha256.Sum256([]byte("test-chain"))
tx := &sqproto.UnsignedTransaction{
Nonce: 1,
Actions: []*sqproto.Action{
{
Value: &sqproto.Action_SequenceAction{
SequenceAction: &sqproto.SequenceAction{
ChainId: []byte("test-chain"),
Data: []byte("test-data"),
RollupId: rollupId[:],
Data: []byte("test-data"),
},
},
},
Expand Down
25 changes: 21 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
sqproto "buf.build/gen/go/astria/astria/protocolbuffers/go/astria/sequencer/v1alpha1"
)

// Should this live here?
type BalanceResponse struct {
Denom string `json:"denom,omitempty"`
Balance *big.Int `json:"balance,omitempty"`
}

// Client is an HTTP tendermint client.
type Client struct {
client *http.HTTP
Expand All @@ -40,7 +46,7 @@ func (c *Client) BroadcastTxSync(ctx context.Context, tx *sqproto.SignedTransact
return c.client.BroadcastTxSync(ctx, bytes)
}

func (c *Client) GetBalance(ctx context.Context, addr [20]byte) (*big.Int, error) {
func (c *Client) GetBalances(ctx context.Context, addr [20]byte) ([]*BalanceResponse, error) {
resp, err := c.client.ABCIQueryWithOptions(ctx, fmt.Sprintf("accounts/balance/%x", addr), []byte{}, client.ABCIQueryOptions{
Height: 0,
Prove: false,
Expand All @@ -53,13 +59,13 @@ func (c *Client) GetBalance(ctx context.Context, addr [20]byte) (*big.Int, error
return nil, errors.New(resp.Response.Log)
}

balanceResp := &sqproto.BalanceResponse{}
err = proto.Unmarshal(resp.Response.Value, balanceResp)
protoBalanceResp := &sqproto.BalanceResponse{}
err = proto.Unmarshal(resp.Response.Value, protoBalanceResp)
if err != nil {
return nil, err
}

return protoU128ToBigInt(balanceResp.Balance), nil
return balanceResponseFromProto(protoBalanceResp), nil
}

func (c *Client) GetNonce(ctx context.Context, addr [20]byte) (uint32, error) {
Expand Down Expand Up @@ -90,3 +96,14 @@ func protoU128ToBigInt(u128 *primproto.Uint128) *big.Int {
hi.Lsh(hi, 64)
return lo.Add(lo, hi)
}

func balanceResponseFromProto(resp *sqproto.BalanceResponse) []*BalanceResponse {
var balanceResponses []*BalanceResponse
for _, balance := range resp.Balances {
balanceResponses = append(balanceResponses, &BalanceResponse{
Balance: protoU128ToBigInt(balance.Balance),
Denom: balance.Denom,
})
}
return balanceResponses
}
9 changes: 5 additions & 4 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package client

import (
"context"
"math/big"
"crypto/sha256"
"testing"

sqproto "buf.build/gen/go/astria/astria/protocolbuffers/go/astria/sequencer/v1alpha1"
Expand All @@ -17,13 +17,14 @@ func TestSignAndBroadcastTx(t *testing.T) {
client, err := NewClient("http://localhost:26657")
require.NoError(t, err)

rollupId := sha256.Sum256([]byte("test-chain"))
tx := &sqproto.UnsignedTransaction{
Nonce: 1,
Actions: []*sqproto.Action{
{
Value: &sqproto.Action_SequenceAction{
SequenceAction: &sqproto.SequenceAction{
RollupId: []byte("test-chain"),
RollupId: rollupId[:],
Data: []byte("test-data"),
},
},
Expand All @@ -43,9 +44,9 @@ func TestGetBalance(t *testing.T) {
client, err := NewClient("http://localhost:26657")
require.NoError(t, err)

balance, err := client.GetBalance(context.Background(), [20]byte{})
balance, err := client.GetBalances(context.Background(), [20]byte{})
require.NoError(t, err)
require.Equal(t, balance, big.NewInt(0))
require.Empty(t, balance)
}

func TestGetNonce(t *testing.T) {
Expand Down
14 changes: 12 additions & 2 deletions client/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"crypto/ed25519"
"crypto/sha256"

proto "google.golang.org/protobuf/proto"

sqproto "buf.build/gen/go/astria/astria/protocolbuffers/go/astria/sequencer/v1alpha1"
Expand Down Expand Up @@ -36,8 +37,17 @@ func GenerateSigner() (*Signer, error) {
}

func (s *Signer) SignTransaction(tx *sqproto.UnsignedTransaction) (*sqproto.SignedTransaction, error) {
if len(tx.FeeAssetId) == 0 {
tx.FeeAssetId = DefaultAstriaAssetID[:]
for _, action := range tx.Actions {
switch v := action.Value.(type) {
case *sqproto.Action_TransferAction:
if len(v.TransferAction.FeeAssetId) == 0 {
v.TransferAction.FeeAssetId = DefaultAstriaAssetID[:]
}
case *sqproto.Action_SequenceAction:
if len(v.SequenceAction.FeeAssetId) == 0 {
v.SequenceAction.FeeAssetId = DefaultAstriaAssetID[:]
}
}
}

bytes, err := proto.Marshal(tx)
Expand Down
8 changes: 6 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,16 @@ func handleGetBalance() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

balance, err := client.GetBalance(ctx, address20)
balances, err := client.GetBalances(ctx, address20)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

fmt.Println("Balance:", balance)
fmt.Println("Balances:")
for _, bal := range balances {
fmt.Printf(" %s: %s\n", bal.Denom, bal.Balance.String())
}

os.Exit(0)
}
9 changes: 2 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ module github.com/astriaorg/go-sequencer-client
go 1.20

require (
buf.build/gen/go/astria/astria/protocolbuffers/go v1.31.0-20231130012811-2fd7e6d46ebd.2
buf.build/gen/go/astria/astria/protocolbuffers/go v1.33.0-20240305174753-59e2afe18f90.1
github.com/cometbft/cometbft v0.37.0
github.com/stretchr/testify v1.8.1
google.golang.org/protobuf v1.31.0
google.golang.org/protobuf v1.33.0
)

require (
buf.build/gen/go/cosmos/cosmos-proto/protocolbuffers/go v1.31.0-20211202220400-1935555c206d.2 // indirect
buf.build/gen/go/cosmos/cosmos-sdk/protocolbuffers/go v1.31.0-20230719110346-aa25660f4ff7.2 // indirect
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.31.0-20230509103710-5e5b9fdd0180.2 // indirect
buf.build/gen/go/cosmos/ibc/protocolbuffers/go v1.31.0-20230913112312-7ab44ae956a0.2 // indirect
buf.build/gen/go/penumbra-zone/penumbra/protocolbuffers/go v1.31.0-20231120132728-bc443669626d.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
Expand Down
41 changes: 24 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
buf.build/gen/go/astria/astria/protocolbuffers/go v1.31.0-20231130012811-2fd7e6d46ebd.2 h1:gq4dRHG9GJEh8tkTsFwgrHA+tPiZead6YKRgsaidQJg=
buf.build/gen/go/astria/astria/protocolbuffers/go v1.31.0-20231130012811-2fd7e6d46ebd.2/go.mod h1:OPKBvxqrsHTBen+bbkQT/beHK/aSznSulzR+JeDSUKw=
buf.build/gen/go/cosmos/cosmos-proto/protocolbuffers/go v1.31.0-20211202220400-1935555c206d.2 h1:O+V9Yq7KkauH1cMnUAHxsl5n+b7ZD/RZnHo9ZiJICGg=
buf.build/gen/go/cosmos/cosmos-proto/protocolbuffers/go v1.31.0-20211202220400-1935555c206d.2/go.mod h1:yHKMbegvhfMbDGsQp/q7tECJTfZf0wrgTRcH1glgEQk=
buf.build/gen/go/cosmos/cosmos-sdk/protocolbuffers/go v1.31.0-20230522115704-e7a85cef453e.2/go.mod h1:d16Ahcn3H831Oe+NnNOWuXkV3aTiPOBZvUAZaHYECkU=
buf.build/gen/go/cosmos/cosmos-sdk/protocolbuffers/go v1.31.0-20230719110346-aa25660f4ff7.2 h1:mDJdIdbHw10arzuVIpFoQr6h8cxKPcdT2r91Q2izQu0=
buf.build/gen/go/cosmos/cosmos-sdk/protocolbuffers/go v1.31.0-20230719110346-aa25660f4ff7.2/go.mod h1:vg3BLbBJ3Vl9Wu7GE6BmxYq1+VMU4Yg8cv9YG7qF2AQ=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.31.0-20221020125208-34d970b699f8.2/go.mod h1:rTzewyj2LANV2IIuGZnKMsvbIEVAguDwBvMdxD1pa3k=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.31.0-20230509103710-5e5b9fdd0180.2 h1:4JCzFq6Gwr5hx3BpGky420OsrZjdMI8rAB3T7zcsDrs=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.31.0-20230509103710-5e5b9fdd0180.2/go.mod h1:rTzewyj2LANV2IIuGZnKMsvbIEVAguDwBvMdxD1pa3k=
buf.build/gen/go/cosmos/ibc/protocolbuffers/go v1.31.0-20230913112312-7ab44ae956a0.2 h1:9LwQA/UwYerRClC/fNzeyynz+AfDenAmyZj5G8HPLmU=
buf.build/gen/go/cosmos/ibc/protocolbuffers/go v1.31.0-20230913112312-7ab44ae956a0.2/go.mod h1:piKWSxT4f990Ium90hWQnNwuqxhsoYxhPtZYcOz2vNs=
buf.build/gen/go/cosmos/ics23/protocolbuffers/go v1.31.0-20221207100654-55085f7c710a.2/go.mod h1:1V7tFOxP2P2x8G0oasO+VqSHN0Bhly5I/e+2vENGomQ=
buf.build/gen/go/penumbra-zone/penumbra/protocolbuffers/go v1.31.0-20231120132728-bc443669626d.2 h1:IOCmnR+xxYcOUdG2fRR6KRAkATFKMJYE1Z5zVV1Pphg=
buf.build/gen/go/penumbra-zone/penumbra/protocolbuffers/go v1.31.0-20231120132728-bc443669626d.2/go.mod h1:h82VMeit0Ae3VmGvS6QH6yJx9GPHM3y4y0x1JU3Vyyc=
buf.build/gen/go/astria/astria/protocolbuffers/go v1.32.0-20240215175137-9570e0c3082f.1 h1:J2y85BwNSx4IWCpYIokBnqWMRidiNyefiCdZRWsf7/4=
buf.build/gen/go/astria/astria/protocolbuffers/go v1.32.0-20240215175137-9570e0c3082f.1/go.mod h1:HTIae3hIWhV69v7f8BJQzr2tP6yd0/gxwLrMiG306RY=
buf.build/gen/go/astria/astria/protocolbuffers/go v1.33.0-20240305174753-59e2afe18f90.1 h1:+FTM2JjaQOlR6t1aLCZFAAmmg44EF5WKRPxizrIu6Yg=
buf.build/gen/go/astria/astria/protocolbuffers/go v1.33.0-20240305174753-59e2afe18f90.1/go.mod h1:0U4DDtI0mlXCn+E7ibF+xYiU0GQPo/350iGBFz24poI=
buf.build/gen/go/cosmos/cosmos-proto/protocolbuffers/go v1.32.0-20211202220400-1935555c206d.1/go.mod h1:GpU2rx3tDDSvCER8/rvvgu6s6LeMU73TKjfBZ89OZKg=
buf.build/gen/go/cosmos/cosmos-proto/protocolbuffers/go v1.33.0-20211202220400-1935555c206d.1/go.mod h1:KYMBU6zc1GB+YvVZRLr9KE6/caPzA9t6A+Bgw4rFlck=
buf.build/gen/go/cosmos/cosmos-sdk/protocolbuffers/go v1.32.0-20230522115704-e7a85cef453e.1/go.mod h1:J8VpbpzO6pccEOWdLbYylDP8lRPifk2EA8tmJNkdZZo=
buf.build/gen/go/cosmos/cosmos-sdk/protocolbuffers/go v1.32.0-20230719110346-aa25660f4ff7.1/go.mod h1:Tl9HTTTqDT4kfcsEwfEyUeFdJmbcgSOXL2q50rG5XBw=
buf.build/gen/go/cosmos/cosmos-sdk/protocolbuffers/go v1.33.0-20230522115704-e7a85cef453e.1/go.mod h1:DjOw/ckisd5uGkr0EU1zfD/2sdKMDtDzZLH5vp7oDEI=
buf.build/gen/go/cosmos/cosmos-sdk/protocolbuffers/go v1.33.0-20230719110346-aa25660f4ff7.1/go.mod h1:wbYl/Us9Yce2yJ15DvqU7a/xzLinQ4BYeaC48jnk9wU=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.32.0-20221020125208-34d970b699f8.1/go.mod h1:5GqIYthcy/ASmnKcaT26APpxMhZirnIHXHKki69zjWI=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.32.0-20230509103710-5e5b9fdd0180.1/go.mod h1:5GqIYthcy/ASmnKcaT26APpxMhZirnIHXHKki69zjWI=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.33.0-20221020125208-34d970b699f8.1/go.mod h1:xtjtwWsC8cy8MRjqWXbwsqCPUORN/TTtapeqtiXZmuc=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.33.0-20230509103710-5e5b9fdd0180.1/go.mod h1:xtjtwWsC8cy8MRjqWXbwsqCPUORN/TTtapeqtiXZmuc=
buf.build/gen/go/cosmos/ibc/protocolbuffers/go v1.32.0-20230913112312-7ab44ae956a0.1/go.mod h1:IPgm3IicGPCXFRfFMLOgHFp3kexNEULuIzEJshSNPcY=
buf.build/gen/go/cosmos/ibc/protocolbuffers/go v1.33.0-20230913112312-7ab44ae956a0.1/go.mod h1:sa+77A4yAnxDBT7tBlQNwo34jnKBYhwq0OoSl0jCz2I=
buf.build/gen/go/cosmos/ics23/protocolbuffers/go v1.32.0-20221207100654-55085f7c710a.1/go.mod h1:pjXPxEgmuc0apOM+/10DC/vWa3di1I0Z+CxwKqRXWYQ=
buf.build/gen/go/cosmos/ics23/protocolbuffers/go v1.33.0-20221207100654-55085f7c710a.1/go.mod h1:coKPoLDU4gj19wVXznM5nFqfWQUNeg6aOsqE44HOyDY=
buf.build/gen/go/penumbra-zone/penumbra/protocolbuffers/go v1.32.0-20231120132728-bc443669626d.1/go.mod h1:HOI8VDE0aOk+R3EtXPICCUm0pO8D0PSnS7k2fFq2soE=
buf.build/gen/go/penumbra-zone/penumbra/protocolbuffers/go v1.33.0-20231120132728-bc443669626d.1/go.mod h1:SJGYzIfiP2r2P5wyGmxvLQ8W2HbgNhip2onaprJU27c=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down Expand Up @@ -545,8 +550,10 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit b17543b

Please sign in to comment.