x/bankAbstractThisdocumentspecifiesthebankmoduleoftheCosmosSDK.Thebankmoduleisresponsibleforhandlingmulti-assetcointransfersbetw
x/bank
本文档对Cosmos SDK的bank模块进行说明。
bank模块负责账户间的各种代币资产转账以及追踪特殊情况下必须以不同方式工作的特殊账号的伪转账(特别是vesting账户的代理/取消代理)。此模块暴露了几个不同权限的接口用于安全地与其他模块进行那些必须改变用户余额的交互。
另外,银行模块追踪并可以查询所有资产的总供应量。
此模块也被应用于Cosmos Hub中。
代币供应supply
功能:
网络的总供应量等于账户的所有代币总和。总供应量会随着代币Coin
的铸造(比如使用铸造从技术上实现通胀)和燃烧(由于被惩罚或者治理模块被废弃时造成的燃烧)实时更新。
代币供应功能性地引入了一个新的类型auth.Account
,被模块用来分配代币以及特殊应用场景下铸造或燃烧代币。这些模块账户能够彼此发送和接收代币。这个设计取代了之前的设计——模块需要接收代币时,需要将发送者的代币燃烧掉,需要发送代币时,再在目标账户内铸造出新的代币。这个新设计移除了模块间的重复逻辑。
模块账户ModuleAccount
接口定义如下:
type ModuleAccount interface {
auth.Account // same methods as the Account interface
GetName() string // name of the module; used to obtain the address
GetPermissions() []string // permissions of module account
HasPermission(string) bool
}
注意! 任何允许直接或间接发送资金的模块或者消息处理函数必须确保这些资金不能发送到模块账户(除非是有意为之)
为了以下目的,代币供应的Keeper
也为auth模块的Keeper
和bank模块的Keeper
引入了和模块账户ModuleAccount
有关的新的包装函数:
Name
获取或设置模块账户ModuleAccount
.Name
就可以接收和发送代币到别的模块账户ModuleAccount
或者标准账户Account
(BaseAccount
或者VestingAccount
)ModuleAccount
铸造Mint
或烧毁Burn
代币(受权限限制)。每个模块账户ModuleAccount
都有一套不同的权限来提供不同的能力去执行特定的动作。权限需要在创建代币供应Keeper
时注册以便每次ModuleAccount
调用授权函数时查询,Keeper
能查够查询具体账户的权限来决定是否执行该动作。
可用的权限有以下三个:
Minter
:允许模块铸造具体数量的代币。Burner
: 允许模块烧毁具体数量的代币。Staking
: 允许模块代理或取消代理具体数量的代币。x/bank
模块主要拥有以下几个对象的状态:
另外,x/bank
模块拥有下列索引用来管理上述状态:
0x0 | byte(denom) -> byte(amount)
0x1 | byte(denom) -> ProtocolBuffer(Metadata)
0x2 | byte(address length) | []byte(address) | []byte(balance.Denom) -> ProtocolBuffer(balance)
0x03 | byte(denom) | 0x00 | []byte(address) -> 0
银行模块以0x05
为前缀把自己的参数存储在状态中,通过治理模块或者权威账户地址可以更改这些参数。
0x05 | ProtocolBuffer(Params)
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/bank/v1beta1/bank.proto#L12-L23
银行模块提供暴露了有一些keeper接口,可以传递给其他模块以便读取或者更改账户的余额。 其他模块应当按需引入这些接口,避免滥用。
在实际生产中需要仔细阅读bank
模块的代码以确保权限限制规则是符合你预期的。
x/bank
模块可以接受一个账户地址的字典作为黑名单,可以禁止这些账户地址通过MsgSend
,MsgMultiSend
以及像SendCoinsFromModuleToAccount
直接调用的api来接收代币。
典型的,这些地址为模块账户地址。如果这些地址以状态机规则以外的方式接接收资金,不变量可能会受到破坏,这将可能导致网络宕机。
x/bank
提供了一个地址的黑名单,如果向黑名单内的地址发送资金会发生错误,例如,使用IBC.
多方转账交易的输入。
// Input models transaction input.
message Input {
string address = 1;
repeated cosmos.base.v1beta1.Coin coins = 2;
}
多方转账交易的输出。
// Output models transaction outputs.
message Output {
string address = 1;
repeated cosmos.base.v1beta1.Coin coins = 2;
}
基础keeper提供了所有的权限,可以任意更改账户的余额,铸造代币或是销毁代币。
要限制每个模块铸造代币的权限,可以通过baseKeeper的WithMintCoinsRestriction
来给予具体的铸造限制(例如:只能铸造特定的代币)。
// Keeper defines a module interface that facilitates the transfer of coins
// between accounts.
type Keeper interface {
SendKeeper
WithMintCoinsRestriction(MintingRestrictionFn) BaseKeeper
InitGenesis(context.Context, *types.GenesisState)
ExportGenesis(context.Context) *types.GenesisState
GetSupply(ctx context.Context, denom string) sdk.Coin
HasSupply(ctx context.Context, denom string) bool
GetPaginatedTotalSupply(ctx context.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error)
IterateTotalSupply(ctx context.Context, cb func(sdk.Coin) bool)
GetDenomMetaData(ctx context.Context, denom string) (types.Metadata, bool)
HasDenomMetaData(ctx context.Context, denom string) bool
SetDenomMetaData(ctx context.Context, denomMetaData types.Metadata)
IterateAllDenomMetaData(ctx context.Context, cb func(types.Metadata) bool)
SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromModuleToModule(ctx context.Context, senderModule, recipientModule string, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
DelegateCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
UndelegateCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
MintCoins(ctx context.Context, moduleName string, amt sdk.Coins) error
BurnCoins(ctx context.Context, moduleName string, amt sdk.Coins) error
DelegateCoins(ctx context.Context, delegatorAddr, moduleAccAddr sdk.AccAddress, amt sdk.Coins) error
UndelegateCoins(ctx context.Context, moduleAccAddr, delegatorAddr sdk.AccAddress, amt sdk.Coins) error
// GetAuthority gets the address capable of executing governance proposal messages. Usually the gov module account.
GetAuthority() string
types.QueryServer
}
send keeper提供了访问账户并在账户间转账的权限。send keeper不会改变代币的总供应量(不涉及铸造或销毁代币)。
// SendKeeper defines a module interface that facilitates the transfer of coins
// between accounts without the possibility of creating coins.
type SendKeeper interface {
ViewKeeper
AppendSendRestriction(restriction SendRestrictionFn)
PrependSendRestriction(restriction SendRestrictionFn)
ClearSendRestriction()
InputOutputCoins(ctx context.Context, input types.Input, outputs []types.Output) error
SendCoins(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error
GetParams(ctx context.Context) types.Params
SetParams(ctx context.Context, params types.Params) error
IsSendEnabledDenom(ctx context.Context, denom string) bool
SetSendEnabled(ctx context.Context, denom string, value bool)
SetAllSendEnabled(ctx context.Context, sendEnableds []*types.SendEnabled)
DeleteSendEnabled(ctx context.Context, denom string)
IterateSendEnabledEntries(ctx context.Context, cb func(denom string, sendEnabled bool) (stop bool))
GetAllSendEnabledEntries(ctx context.Context) []types.SendEnabled
IsSendEnabledCoin(ctx context.Context, coin sdk.Coin) bool
IsSendEnabledCoins(ctx context.Context, coins ...sdk.Coin) error
BlockedAddr(addr sdk.AccAddress) bool
}
SendKeeper
在转账前会执行SendRestrictionFn
函数。
// 一个SendRestrictionFn可以限制和/或提供一个新的接收地址。
type SendRestrictionFn func(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) (newToAddr sdk.AccAddress, err error)
在SendKeeper
(或BaseKeeper
)创建之后,可以通过AppendSendRestriction
或者PrependSendRestriction
函数来添加转账限制。
两个函数和之前已提供的限制共同组成了转账限制。AppendSendRestriction
添加的限制条件会在之前的条件执行后再执行。
PrependSendRestriction
添加的条件会在之前的条件执行前先执行。这些限制条件在发生错误时会依照短路逻辑处理,例如如果第一个条件返回了一个错误,则第二个条件不会执行。
在SendCoins
期间,限制条件会在代币从转账账户扣除以及添加到被转账账户之前执行。
在InputOutputCoins
期间,限制条件会在代币从转账账户扣除以及添加到被转账账户之后执行。
A send restriction function should make use of a custom value in the context to allow bypassing that specific restriction. 一个转账限制函数
Send Restrictions are not placed on ModuleToAccount
or ModuleToModule
transfers. This is done due to modules needing to move funds to user accounts and other module accounts. This is a design decision to allow for more flexibility in the state machine. The state machine should be able to move funds between module accounts and user accounts without restrictions.
Secondly this limitation would limit the usage of the state machine even for itself. users would not be able to receive rewards, not be able to move funds between module accounts. In the case that a user sends funds from a user account to the community pool and then a governance proposal is used to get those tokens into the users account this would fall under the discretion of the app chain developer to what they would like to do here. We can not make strong assumptions here. Thirdly, this issue could lead into a chain halt if a token is disabled and the token is moved in the begin/endblock. This is the last reason we see the current change and more damaging then beneficial for users.
For example, in your module's keeper package, you'd define the send restriction function:
var _ banktypes.SendRestrictionFn = Keeper{}.SendRestrictionFn
func (k Keeper) SendRestrictionFn(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) (sdk.AccAddress, error) {
// Bypass if the context says to.
if mymodule.HasBypass(ctx) {
return toAddr, nil
}
// Your custom send restriction logic goes here.
return nil, errors.New("not implemented")
}
The bank keeper should be provided to your keeper's constructor so the send restriction can be added to it:
func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, bankKeeper mymodule.BankKeeper) Keeper {
rv := Keeper{/*...*/}
bankKeeper.AppendSendRestriction(rv.SendRestrictionFn)
return rv
}
Then, in the mymodule
package, define the context helpers:
const bypassKey = "bypass-mymodule-restriction"
// WithBypass returns a new context that will cause the mymodule bank send restriction to be skipped.
func WithBypass(ctx context.Context) context.Context {
return sdk.UnwrapSDKContext(ctx).WithValue(bypassKey, true)
}
// WithoutBypass returns a new context that will cause the mymodule bank send restriction to not be skipped.
func WithoutBypass(ctx context.Context) context.Context {
return sdk.UnwrapSDKContext(ctx).WithValue(bypassKey, false)
}
// HasBypass checks the context to see if the mymodule bank send restriction should be skipped.
func HasBypass(ctx context.Context) bool {
bypassValue := ctx.Value(bypassKey)
if bypassValue == nil {
return false
}
bypass, isBool := bypassValue.(bool)
return isBool && bypass
}
Now, anywhere where you want to use SendCoins
or InputOutputCoins
, but you don't want your send restriction applied:
func (k Keeper) DoThing(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) error {
return k.bankKeeper.SendCoins(mymodule.WithBypass(ctx), fromAddr, toAddr, amt)
}
The view keeper provides read-only access to account balances. The view keeper does not have balance alteration functionality. All balance lookups are O(1)
.
// ViewKeeper defines a module interface that facilitates read only access to
// account balances.
type ViewKeeper interface {
ValidateBalance(ctx context.Context, addr sdk.AccAddress) error
HasBalance(ctx context.Context, addr sdk.AccAddress, amt sdk.Coin) bool
GetAllBalances(ctx context.Context, addr sdk.AccAddress) sdk.Coins
GetAccountsBalances(ctx context.Context) []types.Balance
GetBalance(ctx context.Context, addr sdk.AccAddress, denom string) sdk.Coin
LockedCoins(ctx context.Context, addr sdk.AccAddress) sdk.Coins
SpendableCoins(ctx context.Context, addr sdk.AccAddress) sdk.Coins
SpendableCoin(ctx context.Context, addr sdk.AccAddress, denom string) sdk.Coin
IterateAccountBalances(ctx context.Context, addr sdk.AccAddress, cb func(coin sdk.Coin) (stop bool))
IterateAllBalances(ctx context.Context, cb func(address sdk.AccAddress, coin sdk.Coin) (stop bool))
}
Send coins from one address to another.
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/bank/v1beta1/tx.proto#L38-L53
The message will fail under the following conditions:
to
address is restrictedSend coins from one sender and to a series of different address. If any of the receiving addresses do not correspond to an existing account, a new account is created.
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/bank/v1beta1/tx.proto#L58-L69
The message will fail under the following conditions:
to
addresses are restrictedThe bank
module params can be updated through MsgUpdateParams
, which can be done using governance proposal. The signer will always be the gov
module account address.
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/bank/v1beta1/tx.proto#L74-L88
The message handling can fail if:
Used with the x/gov module to set create/edit SendEnabled entries.
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/bank/v1beta1/tx.proto#L96-L117
The message will fail under the following conditions:
Used to burn coins from an account. The coins are removed from the account and the total supply is reduced.
https://github.com/cosmos/cosmos-sdk/blob/1af000b3ef6296f9928caf494fe5bb812990f22d/proto/cosmos/bank/v1beta1/tx.proto#L131-L148
This message will fail under the following conditions:
The bank module emits the following events:
Type | Attribute Key | Attribute Value |
---|---|---|
transfer | recipient | {recipientAddress} |
transfer | amount | {amount} |
message | module | bank |
message | action | send |
message | sender | {senderAddress} |
Type | Attribute Key | Attribute Value |
---|---|---|
transfer | recipient | {recipientAddress} |
transfer | amount | {amount} |
message | module | bank |
message | action | multisend |
message | sender | {senderAddress} |
In addition to message events, the bank keeper will produce events when the following methods are called (or any method which ends up calling them)
{
"type": "coinbase",
"attributes": [
{
"key": "minter",
"value": "{{sdk.AccAddress of the module minting coins}}",
"index": true
},
{
"key": "amount",
"value": "{{sdk.Coins being minted}}",
"index": true
}
]
}
{
"type": "coin_received",
"attributes": [
{
"key": "receiver",
"value": "{{sdk.AccAddress of the module minting coins}}",
"index": true
},
{
"key": "amount",
"value": "{{sdk.Coins being received}}",
"index": true
}
]
}
{
"type": "burn",
"attributes": [
{
"key": "burner",
"value": "{{sdk.AccAddress of the module burning coins}}",
"index": true
},
{
"key": "amount",
"value": "{{sdk.Coins being burned}}",
"index": true
}
]
}
{
"type": "coin_spent",
"attributes": [
{
"key": "spender",
"value": "{{sdk.AccAddress of the module burning coins}}",
"index": true
},
{
"key": "amount",
"value": "{{sdk.Coins being burned}}",
"index": true
}
]
}
{
"type": "coin_received",
"attributes": [
{
"key": "receiver",
"value": "{{sdk.AccAddress of the address beneficiary of the coins}}",
"index": true
},
{
"key": "amount",
"value": "{{sdk.Coins being received}}",
"index": true
}
]
}
{
"type": "coin_spent",
"attributes": [
{
"key": "spender",
"value": "{{sdk.AccAddress of the address which is spending coins}}",
"index": true
},
{
"key": "amount",
"value": "{{sdk.Coins being spent}}",
"index": true
}
]
}
The bank module contains the following parameters
The SendEnabled parameter is now deprecated and not to be use. It is replaced with state store records.
The default send enabled value controls send transfer capability for all
coin denominations unless specifically included in the array of SendEnabled
parameters.
A user can query and interact with the bank
module using the CLI.
The query
commands allow users to query bank
state.
simd query bank --help
The balances
command allows users to query account balances by address.
simd query bank balances [address] [flags]
Example:
simd query bank balances cosmos1..
Example Output:
balances:
- amount: "1000000000"
denom: stake
pagination:
next_key: null
total: "0"
The denom-metadata
command allows users to query metadata for coin denominations. A user can query metadata for a single denomination using the --denom
flag or all denominations without it.
simd query bank denom-metadata [flags]
Example:
simd query bank denom-metadata --denom stake
Example Output:
metadata:
base: stake
denom_units:
- aliases:
- STAKE
denom: stake
description: native staking token of simulation app
display: stake
name: SimApp Token
symbol: STK
The total
command allows users to query the total supply of coins. A user can query the total supply for a single coin using the --denom
flag or all coins without it.
simd query bank total [flags]
Example:
simd query bank total --denom stake
Example Output:
amount: "10000000000"
denom: stake
The send-enabled
command allows users to query for all or some SendEnabled entries.
simd query bank send-enabled [denom1 ...] [flags]
Example:
simd query bank send-enabled
Example output:
send_enabled:
- denom: foocoin
enabled: true
- denom: barcoin
pagination:
next-key: null
total: 2
The tx
commands allow users to interact with the bank
module.
simd tx bank --help
The send
command allows users to send funds from one account to another.
simd tx bank send [from_key_or_address] [to_address] [amount] [flags]
Example:
simd tx bank send cosmos1.. cosmos1.. 100stake
A user can query the bank
module using gRPC endpoints.
The Balance
endpoint allows users to query account balance by address for a given denomination.
cosmos.bank.v1beta1.Query/Balance
Example:
grpcurl -plaintext \
-d '{"address":"cosmos1..","denom":"stake"}' \
localhost:9090 \
cosmos.bank.v1beta1.Query/Balance
Example Output:
{
"balance": {
"denom": "stake",
"amount": "1000000000"
}
}
The AllBalances
endpoint allows users to query account balance by address for all denominations.
cosmos.bank.v1beta1.Query/AllBalances
Example:
grpcurl -plaintext \
-d '{"address":"cosmos1.."}' \
localhost:9090 \
cosmos.bank.v1beta1.Query/AllBalances
Example Output:
{
"balances": [
{
"denom": "stake",
"amount": "1000000000"
}
],
"pagination": {
"total": "1"
}
}
The DenomMetadata
endpoint allows users to query metadata for a single coin denomination.
cosmos.bank.v1beta1.Query/DenomMetadata
Example:
grpcurl -plaintext \
-d '{"denom":"stake"}' \
localhost:9090 \
cosmos.bank.v1beta1.Query/DenomMetadata
Example Output:
{
"metadata": {
"description": "native staking token of simulation app",
"denomUnits": [
{
"denom": "stake",
"aliases": [
"STAKE"
]
}
],
"base": "stake",
"display": "stake",
"name": "SimApp Token",
"symbol": "STK"
}
}
The DenomsMetadata
endpoint allows users to query metadata for all coin denominations.
cosmos.bank.v1beta1.Query/DenomsMetadata
Example:
grpcurl -plaintext \
localhost:9090 \
cosmos.bank.v1beta1.Query/DenomsMetadata
Example Output:
{
"metadatas": [
{
"description": "native staking token of simulation app",
"denomUnits": [
{
"denom": "stake",
"aliases": [
"STAKE"
]
}
],
"base": "stake",
"display": "stake",
"name": "SimApp Token",
"symbol": "STK"
}
],
"pagination": {
"total": "1"
}
}
The DenomOwners
endpoint allows users to query metadata for a single coin denomination.
cosmos.bank.v1beta1.Query/DenomOwners
Example:
grpcurl -plaintext \
-d '{"denom":"stake"}' \
localhost:9090 \
cosmos.bank.v1beta1.Query/DenomOwners
Example Output:
{
"denomOwners": [
{
"address": "cosmos1..",
"balance": {
"denom": "stake",
"amount": "5000000000"
}
},
{
"address": "cosmos1..",
"balance": {
"denom": "stake",
"amount": "5000000000"
}
},
],
"pagination": {
"total": "2"
}
}
The TotalSupply
endpoint allows users to query the total supply of all coins.
cosmos.bank.v1beta1.Query/TotalSupply
Example:
grpcurl -plaintext \
localhost:9090 \
cosmos.bank.v1beta1.Query/TotalSupply
Example Output:
{
"supply": [
{
"denom": "stake",
"amount": "10000000000"
}
],
"pagination": {
"total": "1"
}
}
The SupplyOf
endpoint allows users to query the total supply of a single coin.
cosmos.bank.v1beta1.Query/SupplyOf
Example:
grpcurl -plaintext \
-d '{"denom":"stake"}' \
localhost:9090 \
cosmos.bank.v1beta1.Query/SupplyOf
Example Output:
{
"amount": {
"denom": "stake",
"amount": "10000000000"
}
}
The Params
endpoint allows users to query the parameters of the bank
module.
cosmos.bank.v1beta1.Query/Params
Example:
grpcurl -plaintext \
localhost:9090 \
cosmos.bank.v1beta1.Query/Params
Example Output:
{
"params": {
"defaultSendEnabled": true
}
}
The SendEnabled
endpoints allows users to query the SendEnabled entries of the bank
module.
Any denominations NOT returned, use the Params.DefaultSendEnabled
value.
cosmos.bank.v1beta1.Query/SendEnabled
Example:
grpcurl -plaintext \
localhost:9090 \
cosmos.bank.v1beta1.Query/SendEnabled
Example Output:
{
"send_enabled": [
{
"denom": "foocoin",
"enabled": true
},
{
"denom": "barcoin"
}
],
"pagination": {
"next-key": null,
"total": 2
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!