github.com/Finschia/finschia-sdk@v0.48.1/x/bank/atlas/atlas-v0.39.1.md (about)

     1  # x/bank
     2  
     3  The `x/bank` module is responsible for handling multi-asset coin transfers between
     4  accounts and tracking special-case pseudo-transfers which must work differently
     5  with particular kinds of accounts.
     6  
     7  ## Usage
     8  
     9  1. Import the module.
    10  
    11     ```go
    12     import (
    13         "github.com/Finschia/finschia-sdk/x/bank"
    14     )
    15     ```
    16  
    17  2. Add `AppModuleBasic` to your `ModuleBasics`.
    18  
    19      ```go
    20      var (
    21        ModuleBasics = module.NewBasicManager(
    22          // ...
    23          bank.AppModuleBasic{},
    24        }
    25      )
    26      ```
    27  
    28  3. Create the module's parameter subspace in your application constructor.
    29  
    30     ```go
    31     func NewApp(...) *App {
    32       // ...
    33       app.subspaces[bank.ModuleName] = app.ParamsKeeper.Subspace(bank.DefaultParamspace)
    34     }
    35     ```
    36  
    37  4. Create the keeper. Note, the `x/bank` module depends on the `x/auth` module
    38     and a list of blocklisted account addresses which funds are not allowed to be
    39     sent to. Your application will need to define this method based your needs.
    40  
    41     ```go
    42     func NewApp(...) *App {
    43       // ...
    44       app.BankKeeper = bank.NewBaseKeeper(
    45         app.AccountKeeper, app.subspaces[bank.ModuleName], app.BlocklistedAccAddrs(),
    46       )
    47     }
    48     ```
    49  
    50  5. Add the `x/bank` module to the app's `ModuleManager`.
    51  
    52     ```go
    53     func NewApp(...) *App {
    54       // ...
    55       app.mm = module.NewManager(
    56         // ...
    57         bank.NewAppModule(app.BankKeeper, app.AccountKeeper),
    58         // ...
    59       )
    60     }
    61     ```
    62  
    63  6. Set the `x/bank` module genesis order.
    64  
    65     ```go
    66     func NewApp(...) *App {
    67       // ...
    68       app.mm.SetOrderInitGenesis(..., bank.ModuleName, ...)
    69     }
    70     ```
    71  
    72  7. Add the `x/bank` module to the simulation manager (if you have one set).
    73  
    74     ```go
    75     func NewApp(...) *App {
    76       // ...
    77       app.sm = module.NewSimulationManager(
    78         // ...
    79         bank.NewAppModule(app.BankKeeper, app.AccountKeeper),
    80         // ...
    81       )
    82     }
    83  
    84  ## Genesis
    85  
    86  The `x/bank` module defines its genesis state as follows:
    87  
    88  ```go
    89  type GenesisState struct {
    90    SendEnabled bool `json:"send_enabled" yaml:"send_enabled"`
    91  }
    92  ```
    93  
    94  The `SendEnabled` parameter determines if transfers are enabled or disabled
    95  entirely on the chain. This can be used to start a network without enabling
    96  transfers while ensuring critical network functionality is operating as expected.
    97  
    98  ## Messages
    99  
   100  ### `MsgSend`
   101  
   102  The `x/bank` module allows for transfer of funds from a source account to a
   103  destination account.
   104  
   105  ```go
   106  type MsgSend struct {
   107    FromAddress sdk.AccAddress `json:"from_address" yaml:"from_address"`
   108    ToAddress   sdk.AccAddress `json:"to_address" yaml:"to_address"`
   109    Amount      sdk.Coins      `json:"amount" yaml:"amount"`
   110  }
   111  ```
   112  
   113  ### `MsgMultiSend`
   114  
   115  The `x/bank` module also allows for multiple inputs and outputs. The sum of all
   116  inputs must be equivalent to the sum of all outputs.
   117  
   118  ```go
   119  type Input struct {
   120    Address sdk.AccAddress `json:"address" yaml:"address"`
   121    Coins   sdk.Coins      `json:"coins" yaml:"coins"`
   122  }
   123  
   124  type Output struct {
   125    Address sdk.AccAddress `json:"address" yaml:"address"`
   126    Coins   sdk.Coins      `json:"coins" yaml:"coins"`
   127  }
   128  
   129  type MsgMultiSend struct {
   130    Inputs  []Input  `json:"inputs" yaml:"inputs"`
   131    Outputs []Output `json:"outputs" yaml:"outputs"`
   132  }
   133  ```
   134  
   135  ## Client
   136  
   137  ### CLI
   138  
   139  The `x/bank` supports the following transactional commands.
   140  
   141  1. Send tokens via a `MsgSend` message.
   142  
   143     ```shell
   144     app tx send [from_key_or_address] [to_address] [amount] [...flags]
   145     ```
   146  
   147  Note, the `x/bank` module does not natively support constructing a `MsgMultiSend`
   148  message. This type of message must be constructed manually, but it may be signed
   149  and broadcasted via the CLI.
   150  
   151  ### REST
   152  
   153  The `x/bank` supports various query API endpoints and a `MsgSend` construction
   154  endpoint.
   155  
   156  1. Construct an unsigned `MsgSend` transaction.
   157  
   158     | Method | Path                     |
   159     | :----- | :----------------------- |
   160     | `POST` | `/bank/accounts/{address}/transfers` |
   161  
   162     Sample payload:
   163  
   164     ```json
   165     {
   166         "base_req": {
   167             "chain_id": "chain-foo",
   168             "from": "cosmos1u3fneykx9carelvurc6av22vpjvptytj9wklk0",
   169             "memo": "memo",
   170             "fees": [
   171                 {
   172                     "denom": "stake",
   173                     "amount": "25000"
   174                 }
   175             ]
   176         },
   177         "amount": [
   178             {
   179                 "denom": "stake",
   180                 "amount": "400000000"
   181             }
   182         ]
   183     }
   184     ```
   185  
   186  2. Query for an account's balance.
   187  
   188     | Method | Path                     |
   189     | :----- | :----------------------- |
   190     | `GET` | `/bank/balances/{address}` |
   191  
   192     Sample response:
   193  
   194     ```json
   195     {
   196         "height": "0",
   197         "result": [
   198             {
   199                 "denom": "node0token",
   200                 "amount": "1000000000"
   201             },
   202             {
   203                 "denom": "stake",
   204                 "amount": "400000000"
   205             }
   206         ]
   207     }
   208     ```