1- package blockchain
1+ package base
22
33import (
44 "context"
55 "fmt"
6- "math "
6+ "strings "
77 "time"
88
99 txtypes "cosmossdk.io/api/cosmos/tx/v1beta1"
@@ -13,11 +13,11 @@ import (
1313 "google.golang.org/grpc/codes"
1414 "google.golang.org/grpc/status"
1515
16- sdkcrypto "github.com/LumeraProtocol/sdk-go/pkg/crypto"
1716 waittx "github.com/LumeraProtocol/sdk-go/internal/wait-tx"
17+ sdkcrypto "github.com/LumeraProtocol/sdk-go/pkg/crypto"
1818)
1919
20- // Simulate runs a gas simulation for a provided tx bytes
20+ // Simulate runs a gas simulation for a provided tx bytes.
2121func (c * Client ) Simulate (ctx context.Context , txBytes []byte ) (uint64 , error ) {
2222 svc := txtypes .NewServiceClient (c .conn )
2323 resp , err := svc .Simulate (ctx , & txtypes.SimulateRequest {
@@ -32,7 +32,7 @@ func (c *Client) Simulate(ctx context.Context, txBytes []byte) (uint64, error) {
3232 return resp .GasInfo .GasUsed , nil
3333}
3434
35- // Broadcast broadcasts a signed transaction with a chosen broadcast mode
35+ // Broadcast broadcasts a signed transaction with a chosen broadcast mode.
3636func (c * Client ) Broadcast (ctx context.Context , txBytes []byte , mode txtypes.BroadcastMode ) (string , error ) {
3737 svc := txtypes .NewServiceClient (c .conn )
3838 resp , err := svc .BroadcastTx (ctx , & txtypes.BroadcastTxRequest {
@@ -69,6 +69,22 @@ func (c *Client) BuildAndSignTxWithGasAdjustment(ctx context.Context, msg sdk.Ms
6969}
7070
7171func (c * Client ) buildAndSignTx (ctx context.Context , msg sdk.Msg , memo string , gasAdjustment float64 ) ([]byte , error ) {
72+ if c .keyring == nil {
73+ return nil , fmt .Errorf ("keyring is required" )
74+ }
75+ if strings .TrimSpace (c .keyName ) == "" {
76+ return nil , fmt .Errorf ("key name is required" )
77+ }
78+ if strings .TrimSpace (c .config .AccountHRP ) == "" {
79+ return nil , fmt .Errorf ("account HRP is required" )
80+ }
81+ if strings .TrimSpace (c .config .FeeDenom ) == "" {
82+ return nil , fmt .Errorf ("fee denom is required" )
83+ }
84+ if c .config .GasPrice .IsNil () || c .config .GasPrice .IsZero () {
85+ return nil , fmt .Errorf ("gas price is required" )
86+ }
87+
7288 // 1) Tx config and builder
7389 txCfg := sdkcrypto .NewDefaultTxConfig ()
7490 builder := txCfg .NewTxBuilder ()
@@ -84,14 +100,14 @@ func (c *Client) buildAndSignTx(ctx context.Context, msg sdk.Msg, memo string, g
84100 if err != nil {
85101 return nil , fmt .Errorf ("load key %q: %w" , c .keyName , err )
86102 }
87- accAddr , err := rec . GetAddress ( )
103+ accAddr , err := sdkcrypto . AddressFromKey ( c . keyring , c . keyName , c . config . AccountHRP )
88104 if err != nil {
89- return nil , fmt .Errorf ("get address for %q: %w" , c .keyName , err )
105+ return nil , fmt .Errorf ("derive address for %q: %w" , c .keyName , err )
90106 }
91107
92108 authq := authtypes .NewQueryClient (c .conn )
93109 acctResp , err := authq .AccountInfo (ctx , & authtypes.QueryAccountInfoRequest {
94- Address : accAddr . String () ,
110+ Address : accAddr ,
95111 })
96112 if err != nil {
97113 return nil , fmt .Errorf ("query account info: %w" , err )
@@ -145,8 +161,8 @@ func (c *Client) buildAndSignTx(ctx context.Context, msg sdk.Msg, memo string, g
145161 }
146162
147163 // Ensure a minimum fee to satisfy chain requirements
148- fee := int64 ( math . Ceil ( float64 (gas ) / 40.0 )) //the gas price is 0.025
149- minFee := sdk .NewCoins (sdk .NewInt64Coin ( "ulume" , fee ))
164+ feeDec := c . config . GasPrice . MulInt64 ( int64 (gas )). Ceil (). TruncateInt ()
165+ minFee := sdk .NewCoins (sdk .NewCoin ( c . config . FeeDenom , feeDec ))
150166 builder .SetFeeAmount (minFee )
151167
152168 // 5) Sign with real credentials, overwriting placeholder
@@ -179,6 +195,43 @@ func (c *Client) GetTx(ctx context.Context, hash string) (*txtypes.GetTxResponse
179195 return resp , nil
180196}
181197
198+ // GetTxsByEvents searches for transactions matching event filters.
199+ func (c * Client ) GetTxsByEvents (ctx context.Context , events []string , page , limit uint64 ) (* txtypes.GetTxsEventResponse , error ) {
200+ if len (events ) == 0 {
201+ return nil , fmt .Errorf ("events are required" )
202+ }
203+ filtered := make ([]string , 0 , len (events ))
204+ for _ , evt := range events {
205+ evt = strings .TrimSpace (evt )
206+ if evt != "" {
207+ filtered = append (filtered , evt )
208+ }
209+ }
210+ if len (filtered ) == 0 {
211+ return nil , fmt .Errorf ("events are required" )
212+ }
213+ if page == 0 {
214+ page = 1
215+ }
216+ query := strings .Join (filtered , " AND " )
217+ req := & txtypes.GetTxsEventRequest {
218+ Events : append ([]string (nil ), filtered ... ),
219+ Query : query ,
220+ OrderBy : txtypes .OrderBy_ORDER_BY_DESC ,
221+ }
222+ req .Page = page
223+ req .Limit = limit
224+ svc := txtypes .NewServiceClient (c .conn )
225+ resp , err := svc .GetTxsEvent (ctx , req )
226+ if err != nil {
227+ return nil , fmt .Errorf ("get txs by events: %w" , err )
228+ }
229+ if resp == nil {
230+ return nil , fmt .Errorf ("empty get txs by events response" )
231+ }
232+ return resp , nil
233+ }
234+
182235// WaitForTxInclusion waits for a transaction to reach a final state using a
183236// websocket subscriber when possible and falling back to periodic gRPC polling.
184237// A new waiter (and therefore a new websocket subscription) is created for each
@@ -260,7 +313,7 @@ func (c *Client) ExtractEventAttribute(tx *txtypes.GetTxResponse, eventType, att
260313 if ev == nil {
261314 continue
262315 }
263- // Note: abci.Event uses GetType_() since 'type' is a reserved field name
316+ // Note: abci.Event uses GetType_() since 'type' is a reserved field name.
264317 if ev .GetType_ () == eventType {
265318 for _ , attr := range ev .GetAttributes () {
266319 if attr == nil {
0 commit comments