State channels and smart contracts


#1

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?


#2

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.


#3

Thanks for your answer Dimitar!!