# Gas Metering


Make sure you have all you need before proceeding:

Players can start playing checkers with your Cosmos blockchain. Transaction fees are paid by players themselves, at least the fee related to transporting the serialized bytes and the other gas-metered parts like bank.

Next add your own gas metering to reflect the costs that different transactions impose or you can add costs to discourage spam.

# New information

These values are an inspiration but you can set your own. Save them as new constants:

Copy const ( CreateGameGas = 10 PlayMoveGas = 10 RejectGameGas = 0 )

# Add handling

Add a line that consumes the designated amount of gas in each relevant handler:

  1. When handling a game creation:

    Copy ctx.GasMeter().ConsumeGas(types.CreateGameGas, "Create game") x checkers keeper msg_server_create_game.go View source
  2. When handling a move:

    Copy ctx.GasMeter().ConsumeGas(types.PlayMoveGas, "Play a move") x checkers keeper msg_server_play_move.go View source
  3. When handling a game rejection:

    Copy ctx.GasMeter().ConsumeGas(types.RejectGameGas, "Reject game") x checkers keeper msg_server_reject_game.go View source

You don't meter gas in your EndBlock handler because it is called by a player sending a transaction. It is instead a service rendered by the network. If you want to account for the gas cost of an expiration, you have to devise a way to pre-collect it as part of the other messages.

Avoid calling ConsumeGas from within a loop. If you know the number of times your code loops, consume all the gas ahead of the loop. With this trick the transaction may run out of gas as if you had run the steps if the signer had not sent enough gas. This strategy saves computations on your node.

# Next up

Make your checkers blockchain more user-friendly by helping players avoid bad transactions via a query that tests a move. Just follow the exercise in the next section.