Skip to content

Commit

Permalink
refactor: modular design - Tx and Query message
Browse files Browse the repository at this point in the history
  • Loading branch information
Moonyongjung committed Sep 8, 2023
1 parent 5e7e0f0 commit 61b42ab
Show file tree
Hide file tree
Showing 119 changed files with 6,317 additions and 11,408 deletions.
12 changes: 7 additions & 5 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ addr, err := key.Bech32AddrString(priKey)
xplac := client.NewXplaClient("chain-id")

// Set private key
xplac = xplac.WithOptions(client.Options{PrivateKey: priKey})
xplac = xplac.WithPrivateKey(priKey)
```
### Set URLs for xpla client
```go
// Need LCD URL when broadcast transactions
xplac := client.NewXplaClient(
"chain-id",
).WithOptions(
client.Options{
provider.Options{
LcdURL: "http://localhost:1317",
}
)
Expand All @@ -38,7 +38,7 @@ xplac := client.NewXplaClient(
xplac := client.NewXplaClient(
"chain-id",
).WithOptions(
client.Options{
provider.Options{
GrpcURL: "http://localhost:9090",
}
)
Expand All @@ -48,7 +48,7 @@ xplac := client.NewXplaClient(
xplac := client.NewXplaClient(
"chain-id",
).WithOptions(
client.Options{
provider.Options{
RpcURL: "http://localhost:26657",
}
)
Expand All @@ -57,14 +57,16 @@ xplac := client.NewXplaClient(
xplac := client.NewXplaClient(
"chain-id",
).WithOptions(
client.Options{
provider.Options{
EvmRpcURL: "http://localhost:8545",
}
)
```

### Optional parameters of xpla client
```go
// github.com/xpladev/xpla.go/provider.go

type Options struct {
// Set private key
PrivateKey key.PrivateKey
Expand Down
8 changes: 4 additions & 4 deletions client/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var xplaTxRes types.TxRes
// Default broadcast mode is "sync" if not xpla client has broadcast mode option.
// The broadcast method is determined according to the broadcast mode option of the xpla client.
// For evm transaction broadcast, use a separate method in this function.
func (xplac *XplaClient) Broadcast(txBytes []byte) (*types.TxRes, error) {
func (xplac *xplaClient) Broadcast(txBytes []byte) (*types.TxRes, error) {

if xplac.GetModule() == mevm.EvmModule {
return xplac.broadcastEvm(txBytes)
Expand All @@ -37,7 +37,7 @@ func (xplac *XplaClient) Broadcast(txBytes []byte) (*types.TxRes, error) {

// Broadcast the transaction with mode "block".
// It takes precedence over the option of the xpla client.
func (xplac *XplaClient) BroadcastBlock(txBytes []byte) (*types.TxRes, error) {
func (xplac *xplaClient) BroadcastBlock(txBytes []byte) (*types.TxRes, error) {
if xplac.GetModule() == mevm.EvmModule {
return xplac.broadcastEvm(txBytes)
}
Expand All @@ -46,15 +46,15 @@ func (xplac *XplaClient) BroadcastBlock(txBytes []byte) (*types.TxRes, error) {

// Broadcast the transaction with mode "Async".
// It takes precedence over the option of the xpla client.
func (xplac *XplaClient) BroadcastAsync(txBytes []byte) (*types.TxRes, error) {
func (xplac *xplaClient) BroadcastAsync(txBytes []byte) (*types.TxRes, error) {
if xplac.GetModule() == mevm.EvmModule {
return xplac.broadcastEvm(txBytes)
}
return broadcastTx(xplac, txBytes, txtypes.BroadcastMode_BROADCAST_MODE_ASYNC)
}

// Broadcast the transaction which is evm transaction by using ethclient of go-ethereum.
func (xplac *XplaClient) broadcastEvm(txBytes []byte) (*types.TxRes, error) {
func (xplac *xplaClient) broadcastEvm(txBytes []byte) (*types.TxRes, error) {
if xplac.GetEvmRpc() == "" {
return nil, util.LogErr(errors.ErrNotSatisfiedOptions, "evm JSON-RPC URL must exist")
}
Expand Down
4 changes: 2 additions & 2 deletions client/broadcast_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// Broadcast generated transactions.
// Broadcast responses, excluding evm, are delivered as "TxResponse" of the entire response structure of the xpla client.
// Support broadcast by using LCD and gRPC at the same time. Default method is gRPC.
func broadcastTx(xplac *XplaClient, txBytes []byte, mode txtypes.BroadcastMode) (*types.TxRes, error) {
func broadcastTx(xplac *xplaClient, txBytes []byte, mode txtypes.BroadcastMode) (*types.TxRes, error) {
broadcastReq := txtypes.BroadcastTxRequest{
TxBytes: txBytes,
Mode: mode,
Expand Down Expand Up @@ -61,7 +61,7 @@ func broadcastTx(xplac *XplaClient, txBytes []byte, mode txtypes.BroadcastMode)

// Broadcast generated transactions of ethereum type.
// Broadcast responses, including evm, are delivered as "TxResponse".
func broadcastTxEvm(xplac *XplaClient, txBytes []byte, broadcastMode string, evmClient *util.EvmClient) (*types.TxRes, error) {
func broadcastTxEvm(xplac *xplaClient, txBytes []byte, broadcastMode string, evmClient *util.EvmClient) (*types.TxRes, error) {
switch {
case xplac.GetMsgType() == mevm.EvmSendCoinMsgType ||
xplac.GetMsgType() == mevm.EvmInvokeSolContractMsgType:
Expand Down
152 changes: 148 additions & 4 deletions client/broadcast_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package client_test

import (
"path/filepath"
"strings"

"github.com/xpladev/xpla.go/client"
"github.com/evmos/ethermint/crypto/hd"
"github.com/xpladev/xpla.go/key"
"github.com/xpladev/xpla.go/provider"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/util"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/gogo/protobuf/jsonpb"
)
Expand Down Expand Up @@ -64,7 +70,7 @@ func (s *ClientTestSuite) TestBroadcast() {
afterQueryAllBalancesResponse.Balances[0].Amount.Sub(beforeQueryAllBalancesResponse.Balances[0].Amount).String(),
)
}
s.xplac = client.ResetXplac(s.xplac)
s.xplac = provider.ResetXplac(s.xplac)
}

func (s *ClientTestSuite) TestBroadcastMode() {
Expand Down Expand Up @@ -99,7 +105,7 @@ func (s *ClientTestSuite) TestBroadcastMode() {
s.Require().NoError(err)
s.xplac.WithSequence(util.FromIntToString(nowSeq + 1))
}
s.xplac = client.ResetXplac(s.xplac)
s.xplac = provider.ResetXplac(s.xplac)
}

func (s *ClientTestSuite) TestBroadcastEVM() {
Expand Down Expand Up @@ -146,5 +152,143 @@ func (s *ClientTestSuite) TestBroadcastEVM() {
testSendAmount,
afterQueryAllBalancesResponse.Balances[0].Amount.Sub(beforeQueryAllBalancesResponse.Balances[0].Amount).String(),
)
s.xplac = client.ResetXplac(s.xplac)
s.xplac = provider.ResetXplac(s.xplac)
}

func (s *ClientTestSuite) TestMultiSignature() {
s.xplac.WithURL(s.apis[0])
rootDir := s.network.Validators[0].Dir
key1 := s.accounts[0]
key2 := s.accounts[1]

key1Name := "key1"
key2Name := "key2"
multiKeyName := "multiKey"

// gen multisig account
kb, err := keyring.New(types.XplaToolDefaultName, keyring.BackendTest, rootDir, nil, hd.EthSecp256k1Option())
s.Require().NoError(err)

err = kb.ImportPrivKey(
key1Name,
key.EncryptArmorPrivKey(key1.PrivKey, key.DefaultEncryptPassphrase),
key.DefaultEncryptPassphrase,
)
s.Require().NoError(err)

err = kb.ImportPrivKey(
key2Name,
key.EncryptArmorPrivKey(key2.PrivKey, key.DefaultEncryptPassphrase),
key.DefaultEncryptPassphrase,
)
s.Require().NoError(err)

var pks []cryptotypes.PubKey
multisigThreshold := 2

k1, err := kb.Key(key1Name)
s.Require().NoError(err)
pks = append(pks, k1.GetPubKey())

k2, err := kb.Key(key2Name)
s.Require().NoError(err)
pks = append(pks, k2.GetPubKey())

multiKeyInfo, err := kb.SaveMultisig(multiKeyName, multisig.NewLegacyAminoPubKey(multisigThreshold, pks))
s.Require().NoError(err)

// send coin to multisig account
s.xplac.WithPrivateKey(key2.PrivKey)

bankSendMsg := types.BankSendMsg{
FromAddress: key2.Address.String(),
ToAddress: multiKeyInfo.GetAddress().String(),
Amount: "10000000000axpla",
}
bankSendTxbytes, err := s.xplac.BankSend(bankSendMsg).CreateAndSignTx()
s.Require().NoError(err)

_, err = s.xplac.Broadcast(bankSendTxbytes)
s.Require().NoError(err)
s.Require().NoError(s.network.WaitForNextBlock())

// create unsigned tx
unsignedTxPath := filepath.Join(rootDir, "unsignedTx.json")
s.xplac.WithOutputDocument(unsignedTxPath)

bankSendMsg2 := types.BankSendMsg{
FromAddress: multiKeyInfo.GetAddress().String(),
ToAddress: key1.Address.String(),
Amount: "10",
}

_, err = s.xplac.BankSend(bankSendMsg2).CreateUnsignedTx()
s.Require().NoError(err)

// create signature of key1
s.xplac.WithPrivateKey(key1.PrivKey)
signature1Path := filepath.Join(rootDir, "signature1.json")
s.xplac.WithOutputDocument(signature1Path)

signTxMsg1 := types.SignTxMsg{
UnsignedFileName: unsignedTxPath,
SignatureOnly: true,
MultisigAddress: multiKeyInfo.GetAddress().String(),
Overwrite: false,
Amino: false,
Offline: false,
}
_, err = s.xplac.SignTx(signTxMsg1)
s.Require().NoError(err)

// create signature of key2
s.xplac.WithPrivateKey(key2.PrivKey)
signature2Path := filepath.Join(rootDir, "signature2.json")
s.xplac.WithOutputDocument(signature2Path)

signTxMsg2 := types.SignTxMsg{
UnsignedFileName: unsignedTxPath,
SignatureOnly: true,
MultisigAddress: multiKeyInfo.GetAddress().String(),
Overwrite: false,
Amino: false,
Offline: false,
}
_, err = s.xplac.SignTx(signTxMsg2)
s.Require().NoError(err)

// create multisigned transaction
s.xplac.WithOutputDocument("")
txMultiSignMsg := types.TxMultiSignMsg{
FileName: unsignedTxPath,
GenerateOnly: true,
FromName: multiKeyInfo.GetName(),
Offline: false,
SignatureFiles: []string{
signature1Path,
signature2Path,
},
KeyringBackend: "test",
KeyringPath: rootDir,
}
multiSignTx, err := s.xplac.MultiSign(txMultiSignMsg)
s.Require().NoError(err)

// broadcast test
sdkTx, err := s.xplac.GetEncoding().TxConfig.TxJSONDecoder()(multiSignTx)
s.Require().NoError(err)
txBytes, err := s.xplac.GetEncoding().TxConfig.TxEncoder()(sdkTx)
s.Require().NoError(err)

_, err = s.xplac.Broadcast(txBytes)

// generate error insufficient funds
// multisig tx is normal
s.Require().Error(err)
s.Require().Equal(
`code 8 : tx failed - [with code 5 : 10000000000axpla is smaller than 133715200000000000axpla: insufficient funds: insufficient funds]`,
err.Error(),
)

s.xplac = provider.ResetXplac(s.xplac)
}
34 changes: 0 additions & 34 deletions client/helper.go

This file was deleted.

4 changes: 2 additions & 2 deletions client/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (

// LoadAccount gets the account info by AccAddress
// If xpla client has gRPC client, query account information by using gRPC
func (xplac *XplaClient) LoadAccount(address sdk.AccAddress) (res authtypes.AccountI, err error) {
func (xplac *xplaClient) LoadAccount(address sdk.AccAddress) (res authtypes.AccountI, err error) {

if xplac.GetGrpcUrl() == "" {

Expand Down Expand Up @@ -57,7 +57,7 @@ func (xplac *XplaClient) LoadAccount(address sdk.AccAddress) (res authtypes.Acco

// Simulate tx and get response
// If xpla client has gRPC client, query simulation by using gRPC
func (xplac *XplaClient) Simulate(txbuilder cmclient.TxBuilder) (*sdktx.SimulateResponse, error) {
func (xplac *xplaClient) Simulate(txbuilder cmclient.TxBuilder) (*sdktx.SimulateResponse, error) {
seq, err := util.FromStringToUint64(xplac.GetSequence())
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions client/info_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client_test

import (
"github.com/xpladev/xpla.go/client"
"github.com/xpladev/xpla.go/provider"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/util"

Expand All @@ -23,7 +23,7 @@ func (s *ClientTestSuite) TestLoadAccount() {
s.Require().NoError(err)
s.Require().Equal(val.String(), res.GetAddress().String())
}
s.xplac = client.ResetXplac(s.xplac)
s.xplac = provider.ResetXplac(s.xplac)
}

func (s *ClientTestSuite) TestSimulate() {
Expand Down Expand Up @@ -64,5 +64,5 @@ func (s *ClientTestSuite) TestSimulate() {
s.Require().NoError(err)

}
s.xplac = client.ResetXplac(s.xplac)
s.xplac = provider.ResetXplac(s.xplac)
}
Loading

0 comments on commit 61b42ab

Please sign in to comment.