Skip to content

Commit

Permalink
working example
Browse files Browse the repository at this point in the history
  • Loading branch information
mankenavenkatesh committed Aug 17, 2022
1 parent 1db5a34 commit 77ebd8e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
13 changes: 12 additions & 1 deletion client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ func (c *chainClient) broadcastTx(
err = errors.Wrap(err, "failed to prepareFactory")
return nil, err
}

fmt.Println("prepare tx factory", "Simulate", clientCtx.Simulate)
ctx := context.Background()
if clientCtx.Simulate {
simTxBytes, err := tx.BuildSimTx(txf, msgs...)
Expand All @@ -484,6 +486,7 @@ func (c *chainClient) broadcastTx(
c.gasWanted = adjustedGas
}

fmt.Println("Build unsigned tx")
txn, err := tx.BuildUnsignedTx(txf, msgs...)

if err != nil {
Expand All @@ -492,6 +495,7 @@ func (c *chainClient) broadcastTx(
}

txn.SetFeeGranter(clientCtx.GetFeeGranterAddress())
fmt.Println("Sign tx", "signer", clientCtx.GetFromName())
err = tx.Sign(txf, clientCtx.GetFromName(), txn, true)
if err != nil {
err = errors.Wrap(err, "failed to Sign Tx")
Expand All @@ -504,6 +508,7 @@ func (c *chainClient) broadcastTx(
return nil, err
}

fmt.Println("Broadcast tx request")
req := txtypes.BroadcastTxRequest{
txBytes,
txtypes.BroadcastMode_BROADCAST_MODE_SYNC,
Expand All @@ -516,26 +521,31 @@ func (c *chainClient) broadcastTx(
return res, err
}

fmt.Println("Broadcasted tx")
awaitCtx, cancelFn := context.WithTimeout(context.Background(), defaultBroadcastTimeout)
defer cancelFn()

txHash, _ := hex.DecodeString(res.TxResponse.TxHash)
t := time.NewTimer(defaultBroadcastStatusPoll)

fmt.Println("Broadcasted tx", "txHash", txHash)

for {
select {
case <-awaitCtx.Done():
err := errors.Wrapf(ErrTimedOut, "%s", res.TxResponse.TxHash)
fmt.Println("Error timedout", err)
t.Stop()
return nil, err
case <-t.C:
resultTx, err := clientCtx.Client.Tx(awaitCtx, txHash, false)
if err != nil {
if errRes := client.CheckTendermintError(err, txBytes); errRes != nil {
fmt.Println("Tendermint error", err)
return &txtypes.BroadcastTxResponse{TxResponse: errRes}, err
}

// log.WithError(err).Warningln("Tx Error for Hash:", res.TxHash)
// log.WithError(err).Warningln("Tx Error for Hash:")

t.Reset(defaultBroadcastStatusPoll)
continue
Expand Down Expand Up @@ -586,6 +596,7 @@ func (c *chainClient) runBatchBroadcast() {
log.Debugln("broadcastTx with nonce", c.accSeq)
res, err := c.broadcastTx(c.ctx, c.txFactory, true, toSubmit...)
if err != nil {
fmt.Println("Error broadcasting tx", err)
if strings.Contains(err.Error(), "account sequence mismatch") {
c.syncNonce()
c.txFactory = c.txFactory.WithSequence(c.accSeq)
Expand Down
2 changes: 1 addition & 1 deletion client/common/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func LoadNetwork(name string, node string) Network {
ApiEndpoint: "https://localhost:1317",
TmEndpoint: "http://localhost:26657",
ChainGrpcEndpoint: "tcp://localhost:9090",
ChainId: "router-chain",
ChainId: "router-1",
Fee_denom: "router",
Name: "local",
}
Expand Down
13 changes: 7 additions & 6 deletions examples/chain/1_MsgSend/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ func main() {
fmt.Println(err)
}

fmt.Println("Network", network)
senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.routerd/keyring-file",
os.Getenv("HOME")+"/.routerd",
"routerd",
"file",
"genesis",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
"", // keyring will be used if pk not provided
false,
)

Expand All @@ -53,14 +54,14 @@ func main() {
FromAddress: senderAddress.String(),
ToAddress: "router1mtp76jwymme78xaf0h73cmky8hdy3thhy0xz9a",
Amount: []sdktypes.Coin{{
Denom: "router", Amount: sdktypes.NewInt(10)}, // 1 router
Denom: "router", Amount: sdktypes.NewInt(1000000)}, // 1 router
},
}

chainClient, err := chainclient.NewChainClient(
clientCtx,
network.ChainGrpcEndpoint,
common.OptionTLSCert(network.ChainTlsCert),
// common.OptionTLSCert(network.ChainTlsCert),
common.OptionGasPrices("100000000000000router"),
)

Expand All @@ -69,7 +70,7 @@ func main() {
}

//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
txResponse, err := chainClient.SyncBroadcastMsg(msg)
err = chainClient.QueueBroadcastMsg(msg)

if err != nil {
fmt.Println(err)
Expand All @@ -84,5 +85,5 @@ func main() {
return
}

fmt.Println("gas fee:", gasFee, "TxResponse", txResponse)
fmt.Println("gas fee:", gasFee)
}

0 comments on commit 77ebd8e

Please sign in to comment.