Skip to main content

How to make cross-shard contract calls

Cross-shard contract calls can be executed via the Calimero bridge, allowing contracts on Calimero and NEAR to interact with each other through callbacks.

Example: Tic-Tac-Toe Game

An example DApp deployed on the NEAR testnet demonstrates cross-shard calls between Calimero and NEAR. The Tic-Tac-Toe game contracts can be found here.

Game Registration

To initiate a game on Calimero, both players need to register on the NEAR testnet contract. The following commands register two players for the game:

near call tictactoe.igi.testnet register_player --accountId igi.testnet
near call tictactoe.igi.testnet register_player --accountId mikimaus.testnet

Players registered for the game: playerA and playerB.

Starting the Game

Once the second player registers, a CALIMERO_EVENT_CROSS_CALL event is emitted, transmitting the arguments required to call the Calimero shard and start the game. The bridge service receives this event and attempts to prove on the cross-shard connector on Calimero that a Tic-Tac-Toe game needs to start. If the proof is successful, the start_game method is called on the Tic-Tac-Toe contract on Calimero. Subsequently, a CALIMERO_EVENT_CROSS_RESPONSE event is emitted from the cross-shard connector on Calimero. Upon successful proof on NEAR, the callback method game_started is executed. You can view the transaction showing the executed callback.

Game Completion

Similarly, you can observe how the game_ended method was called from Calimero to NEAR testnet here.

Viewing the Final Game Result

To view the final game result on NEAR testnet, use the following command:

near view tictactoe.igi.testnet get_finished_game --args '{"game_id":0}'

This command retrieves the details of the finished game, including the game board, the players, the game status, and whose turn it is.

Example Result:

board: [ [ 'O', 'X', 'X' ], [ 'O', 'U', 'U' ], [ 'O', 'U', 'U' ] ],
player_a: 'igi.testnet',
player_b: 'igi.testnet',
status: 'PlayerAWon',
player_a_turn: false

By following these steps, you can see how cross-shard contract calls are made and the game progresses between Calimero and NEAR in the Tic-Tac-Toe example.