State channels and smart contracts

How can we interact with smart contracts in a state channel? or we can’t do it?

I’m working in a project. We need to develop an app using state channels. We are going to develop a simple game, like tic tac toe. The game contains some logic, so every time a player makes a movement, we need to check if that movement is valid, and in that case, we need to change the state of the game (like punctuations, positions, etc.)

We can implement the app using messages, like TicTacToe example, https://github.com/aeternity/aepp-sophia-examples/tree/master/examples/TicTacToe, but I think we can use the logic coded on a smart contract. Am I wrong?

1 Like

Hi @fedecaccia
You can definitely use contracts inside state channels, this is what makes them state channels and not payment channels :smiley:

AE State Channels are generic state channels where you can hold as many contracts as you want to, call them and modify their state. You can also have your channels read some data on-chain - accounts’ on-chain balances, oracle responses and etc. You can “drag” on-chain contracts off-chain (basically copying their bytecode from the on-chain state trees in the off-chain channel state). You can not modify on-chain entities from an off-chain contract call (and it doesn’t make any sense doing so). As from the other question - off-chain updates resemble closely on-chain interactions with the chain, it is just that those intentions are not wrapped in transactions but rather in updates. They also don’t reach miners but are rather co-signed by both participants (thus making channels in something like a proof-of-stake side chain :smiley: ).

To follow on-chain contract’s transactions, off-chain contract updates are contract create and contract call. Off-chain contracts are not semantically different by on-chain ones. They share the exact same primitives. What is different is the environment they’re run in: in channels you don’t wait for miners to include your transaction but rather wait for the other participant to co-sign it. This allows significantly faster calls. If both participants are cooperating, you can achieve a soft-real time system. In off-chain evnironment you also don’t pay gas - since both participants execute the contract locally, it is considered they both share the costs for it. This allows more complex contracts to be ran (bare in mind disputes, though).

So assuming you mean off-chain contracts - you create contracts in channels and call them. You can see the steps in the examples’ section of channels. We have a ton of tests for channels and we use them to generate examples based on real-life scenarios. This one is for contracts, this one in particular uses on-chain oracle’s response in an off-chain contract call.

4 Likes

Thanks for your answer Dimitar!!

2 Likes

Let’s consider the following scenario:

  1. we create a state channel
  2. inside we create an off-chain contract based on information from the chain
  3. when the channel is closed we want to securely update the on-chain state

How we can assure that the update will be trustless (encoded in a smart-contract off chain)? In other words - that the update will really depend on the final state of the off-chain contract, not some other state?

Example: Let’s have a leaderboard on-chain. The game (and representing contract) is off-chain. We want to update the leaderbord (on-chain contract) based on the result in off-chain contract.

because both parties co-sign state constantly off-chain, the longest update history is considered valid

This means that the logic will have to be coded upfront in the smart contract (on chain), right?

1 Like

yes. just be careful if the game includes any kind of randomness

1 Like