-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from zhiqiang-bianjie/feature/refactor
add coin_type spec
- Loading branch information
Showing
8 changed files
with
209 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/wire" | ||
"github.com/cosmos/cosmos-sdk/x/auth" | ||
"github.com/irisnet/irishub/app" | ||
"github.com/spf13/cobra" | ||
"github.com/tendermint/tendermint/crypto" | ||
"github.com/irisnet/irishub/types" | ||
) | ||
|
||
// GetAccountCmd returns a query account that will display the state of the | ||
// account at a given address. | ||
func GetAccountCmd(storeName string, cdc *wire.Codec, decoder auth.AccountDecoder) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "account [address]", | ||
Short: "Query account balance", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// find the key to look up the account | ||
addr := args[0] | ||
|
||
key, err := sdk.AccAddressFromBech32(addr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := app.NewContext() | ||
cliCtx := ctx.WithCodec(cdc). | ||
WithAccountDecoder(decoder) | ||
if err := cliCtx.EnsureAccountExistsFromAddr(key); err != nil { | ||
return err | ||
} | ||
|
||
acc, err := cliCtx.GetAccount(key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
coins := acc.GetCoins() | ||
var coins_str []string | ||
for _, coin := range coins { | ||
coinName ,_ := types.GetCoinName(coin.String()) | ||
ct, err := ctx.GetCoinType(coinName) | ||
if err != nil { | ||
return err | ||
} | ||
mainCoin, err := ct.Convert(coin.String(), ct.Name) | ||
if err != nil { | ||
return err | ||
} | ||
coins_str = append(coins_str, mainCoin) | ||
} | ||
acct := account{ | ||
Address: acc.GetAddress(), | ||
Coins: coins_str, | ||
PubKey: acc.GetPubKey(), | ||
AccountNumber: acc.GetAccountNumber(), | ||
Sequence: acc.GetSequence(), | ||
} | ||
|
||
output, err := wire.MarshalJSONIndent(cdc, acct) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(string(output)) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
type account struct { | ||
Address sdk.AccAddress `json:"address"` | ||
Coins []string `json:"coins"` | ||
PubKey crypto.PubKey `json:"public_key"` | ||
AccountNumber int64 `json:"account_number"` | ||
Sequence int64 `json:"sequence"` | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package coin | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/wire" | ||
"github.com/spf13/cobra" | ||
"fmt" | ||
"github.com/irisnet/irishub/app" | ||
) | ||
|
||
func GetCmdQueryCoinType(cdc *wire.Codec) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "types [coin_name]", | ||
Short: "query coin_type", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := app.NewContext().WithCodeC(cdc) | ||
res , err := ctx.GetCoinType(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
output, err := wire.MarshalJSONIndent(cdc, res) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(string(output)) | ||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Coin_Type的定义 | ||
coin_type 定义了iris-hub系统中代币的可用单位,只要是系统中已注册的coin_type类型,都可以使用该单位来进行交易。iris-hub中系统默认的代币为iris,iris存在以下几种可用单位:iris-milli,iris-micro,iris-nano,iris-pico,iris-femto,iris-atto。他们之间存在以下换算关系 | ||
``` | ||
1 iris = 10^3 iris-milli | ||
1 iris = 10^6 iris-micro | ||
1 iris = 10^9 iris-nano | ||
1 iris = 10^12 iris-pico | ||
1 iris = 10^15 iris-femto | ||
1 iris = 10^18 iris-atto | ||
``` | ||
coin_type的数据模型如下: | ||
```golang | ||
type CoinType struct { | ||
Name string `json:"name"` | ||
MinUnit Unit `json:"min_unit"` | ||
Units Units `json:"units"` | ||
Origin Origin `json:"origin"` | ||
Desc string `json:"desc"` | ||
} | ||
``` | ||
* Name : 代币名称,也是coin的主单位,例如iris | ||
* MinUnit: coin_type的最小单位,系统中存在的代币都是以最小单位的形式存在,例如iris代币,在iris-hub中存储的单位是iris-atto。当用户发送交易到iris-hub中,使用的必须是该代币的最小单位。但是如果你使用的是iris-hub提供的命令行工具,你可以使用任何系统识别的单位,系统将自动转化为该代币对应的最小单位形式。比如如果你使用send命令转移1iris,命令行将在后端处理为10^18 iris-atto,使用交易hash查询到的交易详情,你也只会看到10^18 iris-atto。Unit结构定义如下: | ||
```golang | ||
type Unit struct { | ||
Denom string `json:"denom"` | ||
Decimal int `json:"decimal"` | ||
} | ||
``` | ||
其中Denom定义为该单位的名称,Decimal定义为该单位支持的最大精度,例如iris-atto支持的最大精度为18 | ||
* Units:定义了coin_type下可用的一组单位 | ||
* Origin:定义了该coin_type的来源,取值:Native(系统内部,iris),External(系统外部,例如eth等),UserIssued(用户自定义) | ||
* Desc:对该代币coin_type的描述 | ||
|
||
如果想查询某种代币的coin_type配置,可以使用如下命令 | ||
```golang | ||
iriscli coin types [coin_name] | ||
``` |
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