Skip to content
On this page

1. Open a channel โ€‹

In order to use the game platform to interact with another player, you first need to open a state channel.

What is a channel? โ€‹

A channel is the central notion on the game platform. It is formed between two players, each of which post bonds (tokens) into the channel. The bonds are transferred via games that can be executed off-chain.

Several channels between the same two players can be open at the same time. This may be desirable in order to have different parameters, e.g. a different withdraw_delay (see below). Otherwise it is not necessary, as the same channel can be used for several games.

Opening a channel โ€‹

Everyone can open a channel by calling the new_channel entrypoint:

python
@sp.entrypoint
def new_channel(self, params):
    sp.set_type(
        params,
        sp.TRecord(
            # One time usage string.
            # Anything as long as it is not reused it for another channel.
            nonce=sp.TString,
            # Each party associated with its public key.
            players=sp.TMap(sp.TAddress, sp.TKey),
            # The number of seconds the parties have to challenge a player
            # from withdrawing.
            withdraw_delay=sp.TInt
        )
    )
python
import smartpy as sp
gp = sp.io.import_template("state_channel_games/game_platform.py")

@sp.add_test(name="New channel")
def test():
    sc = sp.test_scenario()
    player1 = sp.test_account("player1")
    player2 = sp.test_account("player2")
    players = {player1.address: player1.public_key, player2.address: player2.public_key}
    platform_address = sp.address('KT1_ADDRESS_OF_THE_PLATFORM') # Can be None
    ledger_address = sp.address('KT1_ADDRESS_OF_THE_LEDGER')
    platform = gp.GamePlatform(
        admins = sp.set([player1.address]),
        ledger = ledger_address,
        self_addr = platform_address
    )
    sc += platform

    # New channel
    platform.new_channel(
        players = players,
        nonce = "Channel 1",
        withdraw_delay = 3600 * 24
    ).run(sender = player1)

Build the channel_id โ€‹

The channel id will be used for any operation that is executed under the channel.

The channel id is the BLAKE2B hash of the PACK of a triple of:

  • address of the platform
  • players map (same as above)
  • nonce (same as above)

INFO

The channel_id includes the platform address. That's why the SmartPy template has an optionnal self_addr parameter to replicate an on-chain platform.

python
import smartpy as sp

channel_id = sp.blake2b(
    sp.pack(
        (
            sp.address('KT1_ADDRESS_OF_THE_PLATFORM'),
            players,
            nonce
        )
    )
)