-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
able to compile and fix decoder emitter test
- Loading branch information
Showing
20 changed files
with
673 additions
and
563 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,113 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gin-gonic/gin/binding" | ||
|
||
band "github.com/bandprotocol/chain/v3/app" | ||
) | ||
|
||
type Request struct { | ||
Address string `json:"address" binding:"required"` | ||
} | ||
|
||
type Response struct { | ||
TxHash string `json:"txHash"` | ||
} | ||
|
||
func handleRequest(gc *gin.Context, c *Context) { | ||
key := <-c.keys | ||
defer func() { | ||
c.keys <- key | ||
}() | ||
|
||
var req Request | ||
if err := gc.ShouldBindBodyWith(&req, binding.JSON); err != nil { | ||
gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
to, err := sdk.AccAddressFromBech32(req.Address) | ||
if err != nil { | ||
gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
address, err := key.GetAddress() | ||
if err != nil { | ||
gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
msg := banktypes.NewMsgSend(address, to, c.amount) | ||
|
||
clientCtx := client.Context{ | ||
Client: c.client, | ||
Codec: cdc, | ||
TxConfig: band.MakeEncodingConfig().TxConfig, | ||
BroadcastMode: "async", | ||
InterfaceRegistry: band.MakeEncodingConfig().InterfaceRegistry, | ||
} | ||
accountRetriever := authtypes.AccountRetriever{} | ||
acc, err := accountRetriever.GetAccount(clientCtx, address) | ||
if err != nil { | ||
gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
txf := tx.Factory{}. | ||
WithAccountNumber(acc.GetAccountNumber()). | ||
WithSequence(acc.GetSequence()). | ||
WithTxConfig(band.MakeEncodingConfig().TxConfig). | ||
WithGas(200000).WithGasAdjustment(1). | ||
WithChainID(cfg.ChainID). | ||
WithMemo(""). | ||
WithGasPrices(c.gasPrices.String()). | ||
WithKeybase(keybase). | ||
WithAccountRetriever(clientCtx.AccountRetriever) | ||
|
||
txb, err := txf.BuildUnsignedTx(msg) | ||
if err != nil { | ||
gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
err = tx.Sign(txf, key.Name, txb, true) | ||
if err != nil { | ||
gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
txBytes, err := clientCtx.TxConfig.TxEncoder()(txb.GetTx()) | ||
if err != nil { | ||
gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
// broadcast to a Tendermint node | ||
res, err := clientCtx.BroadcastTxSync(txBytes) | ||
if err != nil { | ||
gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
if res.Code != 0 { | ||
gc.JSON(http.StatusInternalServerError, gin.H{ | ||
"error": fmt.Sprintf(":exploding_head: Tx returned nonzero code %d with log %s, tx hash: %s", | ||
res.Code, res.RawLog, res.TxHash, | ||
), | ||
}) | ||
return | ||
} | ||
gc.JSON(200, Response{ | ||
TxHash: res.TxHash, | ||
}) | ||
} | ||
// import ( | ||
// "context" | ||
// "fmt" | ||
// "net/http" | ||
|
||
// "github.com/cosmos/cosmos-sdk/client" | ||
// "github.com/cosmos/cosmos-sdk/client/tx" | ||
// sdk "github.com/cosmos/cosmos-sdk/types" | ||
// authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
// banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
// "github.com/gin-gonic/gin" | ||
// "github.com/gin-gonic/gin/binding" | ||
|
||
// band "github.com/bandprotocol/chain/v3/app" | ||
// ) | ||
|
||
// type Request struct { | ||
// Address string `json:"address" binding:"required"` | ||
// } | ||
|
||
// type Response struct { | ||
// TxHash string `json:"txHash"` | ||
// } | ||
|
||
// func handleRequest(gc *gin.Context, c *Context) { | ||
// key := <-c.keys | ||
// defer func() { | ||
// c.keys <- key | ||
// }() | ||
|
||
// var req Request | ||
// if err := gc.ShouldBindBodyWith(&req, binding.JSON); err != nil { | ||
// gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
// return | ||
// } | ||
// to, err := sdk.AccAddressFromBech32(req.Address) | ||
// if err != nil { | ||
// gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
// return | ||
// } | ||
|
||
// address, err := key.GetAddress() | ||
// if err != nil { | ||
// gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
// return | ||
// } | ||
|
||
// msg := banktypes.NewMsgSend(address, to, c.amount) | ||
|
||
// clientCtx := client.Context{ | ||
// Client: c.client, | ||
// Codec: cdc, | ||
// TxConfig: band.MakeEncodingConfig().TxConfig, | ||
// BroadcastMode: "async", | ||
// InterfaceRegistry: band.MakeEncodingConfig().InterfaceRegistry, | ||
// } | ||
// accountRetriever := authtypes.AccountRetriever{} | ||
// acc, err := accountRetriever.GetAccount(clientCtx, address) | ||
// if err != nil { | ||
// gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
// return | ||
// } | ||
|
||
// txf := tx.Factory{}. | ||
// WithAccountNumber(acc.GetAccountNumber()). | ||
// WithSequence(acc.GetSequence()). | ||
// WithTxConfig(band.MakeEncodingConfig().TxConfig). | ||
// WithGas(200000).WithGasAdjustment(1). | ||
// WithChainID(cfg.ChainID). | ||
// WithMemo(""). | ||
// WithGasPrices(c.gasPrices.String()). | ||
// WithKeybase(keybase). | ||
// WithAccountRetriever(clientCtx.AccountRetriever) | ||
|
||
// txb, err := txf.BuildUnsignedTx(msg) | ||
// if err != nil { | ||
// gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
// return | ||
// } | ||
|
||
// err = tx.Sign(context.Background(), txf, key.Name, txb, true) | ||
// if err != nil { | ||
// gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
// return | ||
// } | ||
|
||
// txBytes, err := clientCtx.TxConfig.TxEncoder()(txb.GetTx()) | ||
// if err != nil { | ||
// gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
// return | ||
// } | ||
|
||
// // broadcast to a Tendermint node | ||
// res, err := clientCtx.BroadcastTxSync(txBytes) | ||
// if err != nil { | ||
// gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
// return | ||
// } | ||
|
||
// if res.Code != 0 { | ||
// gc.JSON(http.StatusInternalServerError, gin.H{ | ||
// "error": fmt.Sprintf(":exploding_head: Tx returned nonzero code %d with log %s, tx hash: %s", | ||
// res.Code, res.RawLog, res.TxHash, | ||
// ), | ||
// }) | ||
// return | ||
// } | ||
// gc.JSON(200, Response{ | ||
// TxHash: res.TxHash, | ||
// }) | ||
// } |
Oops, something went wrong.