Skip to content

Commit

Permalink
Merge pull request #33 from oracleNetworkProtocol/worry-free
Browse files Browse the repository at this point in the history
 x/token symbol to min-unit
  • Loading branch information
seedlings-calm authored Mar 29, 2022
2 parents eb50069 + a256cc0 commit 8899dbf
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 19 deletions.
32 changes: 23 additions & 9 deletions docs/endpoints/grpc-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ Before generating a transaction, a new instance of a `TxBuilder` needs to be cre
```go
import (
"github.com/oracleNetworkProtocol/plugchain/app"
sdk "github.com/cosmos/cosmos-sdk/types"
plugchainapp "github.com/oracleNetworkProtocol/plugchain/app"
)

func sendTx() error {
// Choose your codec: Amino or Protobuf. Here, we use Protobuf, given by the following function.
encCfg := app.MakeEncodingConfig()
encCfg := ethencoding.MakeConfig(plugchainapp.ModuleBasics)

config := sdk.GetConfig()
plugchainapp.SetBech32Prefixes(config)


// Create a new TxBuilder.
txBuilder := encCfg.TxConfig.NewTxBuilder()
Expand Down Expand Up @@ -74,18 +80,23 @@ According to the different functions you operate, you need to prepare different
import (
"encoding/hex"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
_ "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
)
chainID := "plugchain"
chainID := "plugchain_520-1"
addr1, _ := sdk.AccAddressFromBech32("gx1s65azh0yj7n8yn4u0q449wt50eqr4qtyjzmhed")
addr2, _ := sdk.AccAddressFromBech32("gx1d0ug2e7ehy6prw6msrtqwt55mydmxdsx4em9ds")
addr3, _ := sdk.AccAddressFromBech32("gx1pq9yjvqwpmd5r6gpjs8cathhcljmymvp66sjjp")
//Initiator private key
priv := "55e2413b83e590944c6a4bcb443374c60bba847fc079788bd97ea455cb555bf0"
privB, _ := hex.DecodeString(priv)
priv1 := secp256k1.PrivKey{Key: privB}
accountSeq := uint64(1)
acountNumber := uint64(0)
// To query the address information as follows, use account_number, sequence, you need to lock the private key type according to the `@type` type, the EthAcount type is `eth_secp256k1`, and the BaseAccount is `secp256k1`
//curl -X GET "http://8.210.180.240:1317/cosmos/auth/v1beta1/accounts/gx13udxpqpmq6herxqk9yqa3agln8a0va9whjuqe7" -H "accept: application/json"
accountSeq := uint64(0)
acountNumber := uint64(8)
//EthAccount type, using package "github.com/tharsis/ethermint/crypto/ethsecp256k1"
//BaseAccount type using package "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
priv1 := ethsecp256k1.PrivKey{Key: priva}
```


Expand Down Expand Up @@ -214,10 +225,13 @@ func sendTx(ctx context.Context) error {
// --snip--

// Create a connection to the gRPC server.
grpcConn := grpc.Dial(
grpcConn,ger := grpc.Dial(
"127.0.0.1:9090", // Or your gRPC server address.
grpc.WithInsecure(), // The SDK doesn't support any transport security mechanism.
)
if ger != nil {
panic(ger)
}
defer grpcConn.Close()

// Broadcast the tx via gRPC. We create a new client for the Protobuf Tx
Expand All @@ -227,15 +241,15 @@ func sendTx(ctx context.Context) error {
grpcRes, err := txClient.BroadcastTx(
ctx,
&tx.BroadcastTxRequest{
Mode: tx.BroadcastMode_BROADCAST_MODE_SYNC,
Mode: tx.BroadcastMode_BROADCAST_MODE_ASYNC,
TxBytes: txBytes, // Proto-binary of the signed transaction, see previous step.
},
)
if err != nil {
return err
}

fmt.Println(grpcRes.TxResponse.Code) // Should be `0` if the tx is successful
fmt.Println(grpcRes.GetTxResponse())

return nil
}
Expand Down
162 changes: 153 additions & 9 deletions docs/zh/endpoints/grpc-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ Plug Chain Hub 附带的所有可用 gRPC 端点的概述见[Protobuf 文档](./
```go
import (
"github.com/oracleNetworkProtocol/plugchain/app"
sdk "github.com/cosmos/cosmos-sdk/types"
plugchainapp "github.com/oracleNetworkProtocol/plugchain/app"
)

func sendTx() error {
// 选择您的编解码器:Amino 或 Protobuf
encCfg := app.MakeEncodingConfig()
encCfg := ethencoding.MakeConfig(plugchainapp.ModuleBasics)

config := sdk.GetConfig()
plugchainapp.SetBech32Prefixes(config)

// 创建一个新的 TxBuilder。
txBuilder := encCfg.TxConfig.NewTxBuilder()
Expand Down Expand Up @@ -74,18 +79,23 @@ TxBuilder interface {
import (
"encoding/hex"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
_ "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
)
chainID := "plugchain"
chainID := "plugchain_520-1"
addr1, _ := sdk.AccAddressFromBech32("gx1s65azh0yj7n8yn4u0q449wt50eqr4qtyjzmhed")
addr2, _ := sdk.AccAddressFromBech32("gx1d0ug2e7ehy6prw6msrtqwt55mydmxdsx4em9ds")
addr3, _ := sdk.AccAddressFromBech32("gx1pq9yjvqwpmd5r6gpjs8cathhcljmymvp66sjjp")
//发起者私钥
priv := "55e2413b83e590944c6a4bcb443374c60bba847fc079788bd97ea455cb555bf0"
privB, _ := hex.DecodeString(priv)
priv1 := secp256k1.PrivKey{Key: privB}
accountSeq := uint64(1)
acountNumber := uint64(0)
// 如下查询地址的信息,使用account_number,sequence,需要根据 `@type` 类型 来锁定私钥类型,EthAcount 类型为`eth_secp256k1`,BaseAccount为`secp256k1`
//curl -X GET "http://8.210.180.240:1317/cosmos/auth/v1beta1/accounts/gx13udxpqpmq6herxqk9yqa3agln8a0va9whjuqe7" -H "accept: application/json"
accountSeq := uint64(0)
acountNumber := uint64(8)
//EthAccount 类型, 使用包 "github.com/tharsis/ethermint/crypto/ethsecp256k1"
//BaseAccount 类型 ,使用包 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
priv1 := ethsecp256k1.PrivKey{Key: priva}
```


Expand Down Expand Up @@ -215,10 +225,14 @@ func sendTx() error {
// --剪断--

// 创建一个grpc服务
grpcConn := grpc.Dial(
grpcConn,ger := grpc.Dial(
"127.0.0.1:9090", // 你的 gRPC 服务器地址。
grpc.WithInsecure(), // SDK 不支持任何传输安全机制。
)
if ger != nil {
panic(ger)
}

defer grpcConn.Close()

// 通过 gRPC 广播 tx。 我们为 Protobuf Tx 服务创建了一个新客户端。
Expand All @@ -227,16 +241,146 @@ func sendTx() error {
grpcRes, err := txClient.BroadcastTx(
context.Background(),
&tx.BroadcastTxRequest{
Mode: tx.BroadcastMode_BROADCAST_MODE_SYNC,
Mode: tx.BroadcastMode_BROADCAST_MODE_ASYNC,
TxBytes: txBytes,
},
)
if err != nil {
return err
}

fmt.Println(grpcRes.TxResponse.Code) // 如果 tx 成功,则应为 0
fmt.Println(grpcRes.GetTxResponse())

return nil
}
```


## 完整代码
```go
package main

import (
"context"
"encoding/base64"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"net/http"

cliTx "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

plugchainapp "github.com/oracleNetworkProtocol/plugchain/app"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
ethencoding "github.com/tharsis/ethermint/encoding"
"google.golang.org/grpc"
)

func main() {
var chainID = "plugchain_520-1"

encCfg := ethencoding.MakeConfig(plugchainapp.ModuleBasics)
config := sdk.GetConfig()
plugchainapp.SetBech32Prefixes(config)

txBuilder := encCfg.TxConfig.NewTxBuilder()

addr1, _ := sdk.AccAddressFromBech32("gx13udxpqpmq6herxqk9yqa3agln8a0va9whjuqe7")
addr2, _ := sdk.AccAddressFromBech32("gx1r4ffuq72pmny390lkk4l4x8wzamkcnru24wxaq")
priv := "30116C43525488477CA78BB9C5653FB421334C7117070DFDD80240C895234505"
priva, _ := hex.DecodeString(priv)
// 如下查询地址的信息,使用account_number,sequence,需要根据 `@type` 类型 来锁定私钥类型,EthAcount 类型为`eth_secp256k1`,BaseAccount为`secp256k1`
//curl -X GET "http://8.210.180.240:1317/cosmos/auth/v1beta1/accounts/gx13udxpqpmq6herxqk9yqa3agln8a0va9whjuqe7" -H "accept: application/json"
accountSeq := uint64(0)
acountNumber := uint64(8)
//EthAccount 类型, 使用包 "github.com/tharsis/ethermint/crypto/ethsecp256k1"
//BaseAccount 类型 ,使用包 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
priv1 := ethsecp256k1.PrivKey{Key: priva}

msg1 := banktypes.NewMsgSend(addr1, addr2, sdk.NewCoins(sdk.NewInt64Coin("uplugcn", 111)))
// msg2 := banktypes.NewMsgSend(addr2, addr1, sdk.NewCoins(sdk.NewInt64Coin("uplugcn", 222)))
err := txBuilder.SetMsgs(msg1)
if err != nil {
panic(err)
}
txBuilder.SetGasLimit(200000)
txBuilder.SetFeeAmount(sdk.NewCoins(sdk.NewInt64Coin("uplugcn", 20)))
txBuilder.SetMemo("test send")
// txBuilder.SetTimeoutHeight(61111800)

sign := signing.SignatureV2{
PubKey: priv1.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: encCfg.TxConfig.SignModeHandler().DefaultMode(),
Signature: nil,
},

Sequence: accountSeq,
}
err = txBuilder.SetSignatures(sign)
if err != nil {
panic(err)
}

sign = signing.SignatureV2{}
signerD := xauthsigning.SignerData{
ChainID: chainID,
AccountNumber: acountNumber,
Sequence: accountSeq,
}
sign, err = cliTx.SignWithPrivKey(
encCfg.TxConfig.SignModeHandler().DefaultMode(), signerD,
txBuilder, cryptotypes.PrivKey(&priv1), encCfg.TxConfig, accountSeq)
if err != nil {
panic(err)
}
err = txBuilder.SetSignatures(sign)
if err != nil {
panic(err)

}

txBytes, er := encCfg.TxConfig.TxEncoder()(txBuilder.GetTx())

if er != nil {
panic(err)

}

grpcConn, ger := grpc.Dial(
"localhost:9090",
grpc.WithInsecure(),
)

if ger != nil {
panic(ger)

}
defer grpcConn.Close()
// base64 encode the encoded tx bytes
// txBytes1 := base64.StdEncoding.EncodeToString(txBytes)
txClient := tx.NewServiceClient(grpcConn)

grpcRes, gerr := txClient.BroadcastTx(
context.Background(),
&tx.BroadcastTxRequest{
Mode: tx.BroadcastMode_BROADCAST_MODE_ASYNC,
TxBytes: txBytes,
},
)
log.Println("broadcast-tx::::", gerr, grpcRes)
if gerr != nil {
panic(err)
}
fmt.Println(grpcRes.GetTxResponse())

}
```
2 changes: 1 addition & 1 deletion x/token/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (k Keeper) BurnToken(ctx sdk.Context, symbol string, amount uint64, owner s
burnCoin := sdk.NewCoin(token.MinUnit, sdk.NewIntFromUint64(amount).Mul(precision))
burnCoins := sdk.NewCoins(burnCoin)

addrTotal := k.bankKeeper.GetBalance(ctx, owner, symbol)
addrTotal := k.bankKeeper.GetBalance(ctx, owner, token.MinUnit)

if !addrTotal.Amount.GT(burnCoin.Amount) {
return sdkerrors.Wrapf(types.ErrInvalidAmount, "Insufficient account balance")
Expand Down

0 comments on commit 8899dbf

Please sign in to comment.