Eternal AI
  • The AI layer for the new internet
  • eternals
    • What are Eternals?
    • Specification
    • Proof-of-Compute
  • The new internet, AI-powered
    • Bitcoin, AI-powered
      • Eternals on Bitcoin
      • BitAI Virtual Machine
      • Run a BitAI node
    • Ethereum, AI-powered
    • Solana, AI-powered
  • smart contracts, ai-powered
    • How to use onchain LLM
    • Onchain AI composability - AI Powered Wallet
    • Onchain AI Composability - AI Powered Gaming With Chess
  • neurons
    • What are Neurons?
    • Neuron Device
    • Virtual Neurons
      • Solo Neuron
      • Neuron as a Service
      • Pooled Neuron
  • AI CHAINS
    • What are AI chains?
    • Bittensor and existing concepts
    • Base layer: Bitcoin vs Bittensor
    • AI chains: Bitcoin L2s vs Subnets
    • Apps: Smart contracts vs APIs
  • EAI
    • Utilities
    • Tokenomics
  • fully onchain ai models
    • Architecture
    • Deploy your first fully onchain AI
      • Set up your development environment
      • Create a self-custody wallet
      • Train an AI model in Keras
      • Transform the Keras model to Eternal
      • Send, receive, and trade Eternals
    • Progress
    • Misc
      • Transforming an AI Model into an Eternal
      • Standardized data formats
      • Specification
        • Layers
        • Models
  • Decentralized Inference API
    • API
      • API Key
      • Completions
      • Chat completion
      • Create a dagent
      • Get deposit address
      • Get dagent info
      • Agent Completion
    • Onchain Models
    • Tutorials
      • Build unstoppable Eliza agents
      • Build unstoppable Rig agents
      • Build unstoppable ZerePy agents
      • Decentralized ChatGPT
      • Don't Trust, Verify
      • Adjust your dagent personality
      • Launch on Twitter
      • Chain of thought
      • Build a dagent as a service with EternalAI API
    • Open Source
      • Architecture
      • Installation
Powered by GitBook
On this page
  • Smart Contract Design Overview
  • Key Functions
  • Playing Chess
  1. smart contracts, ai-powered

Onchain AI Composability - AI Powered Gaming With Chess

In this page, we will demonstrate how to build a simple AI-powered chess game utilizing the LLAMA model deployed on the Base blockchain.

Smart Contract Design Overview

The contract enables text-based chess games where users play as White against the contract's AI playing as Black. The game follows standard chess rules and uses a request-response mechanism with EternalAI's decentralized AI inference.

Key Contract Features:

  • Game Initialization: Creates a game session for the user.

  • Game Moves: Processes user moves and responds with AI-generated moves.

  • Game State Management: Keeps track of game states and contexts.

Key Functions

Creating a Game

This function initializes a new game for the player, creating a game session and requesting AI inference based on the initial game prompt.

function createGame() external {
    string memory request = buildRequest(initPlayingPrompt);
    playingContext[msg.sender] = initPlayingPrompt;
    uint256 inferenceId = AIKernel(kernel).infer(bytes(request));
    currentInferId[msg.sender] = inferenceId;
    emit GameCreated(msg.sender, inferenceId, request);
}

Making Moves

This function processes a player's move, validates it, and requests the AI's next move.

function play(string memory x) external {
    uint256 inferId = currentInferId[msg.sender];
    require(inferId != 0, "Invalid game ID");
    bytes memory result = fetchInferenceResult(inferId);
    bytes32 digest = keccak256(result);
    require(digest != EMPTY_SIG, "Invalid move");

    if (digest == OK_SIG) {
        playingContext[msg.sender] = string.concat(playingContext[msg.sender], "You: okay. Here is history: ");
    } else if (digest == INVALID_SIG) {
        playingContext[msg.sender] = string.concat(playingContext[msg.sender], "You said: invalid move. ");
    } else {
        playingContext[msg.sender] = string.concat(playingContext[msg.sender], "You played: ", string(result), ". ");
    }

    playingContext[msg.sender] = string.concat(playingContext[msg.sender], "User played: ", x, ". ");
    string memory request = buildRequest(playingContext[msg.sender]);
    uint256 newInferenceId = AIKernel(kernel).infer(bytes(request));
    currentInferId[msg.sender] = newInferenceId;
    emit GamePlayed(msg.sender, newInferenceId, request);
}

Basically, the function will:

  • validate the existence of a current game session.

  • fetch and verifies the inference result.

  • update the game context based on the player's move.

  • send a new inference request for the next move.

You can run the code with the following command:

cp .env.example .env && npm install && npx hardhat compile && BASE_MAINNET_RPC_URL=https://mainnet.base.org BASE_MAINNET_PRIVATE_KEY=<PRIVATE_KEY> npm run playChess:base_mainnet

Note: replace <PRIVATE_KEY> with your actual wallet private key. Ensure that the wallet has sufficient ETH on the Base network to cover transaction fees and amounts.

Playing Chess

After deploying the contract to Base network, you can start playing chess with the AI-powered chess contract by running the following command which will create transactions into the contract.

cp .env.example .env && npm install && npx hardhat compile && BASE_MAINNET_RPC_URL=https://mainnet.base.org BASE_MAINNET_PRIVATE_KEY=<PRIVATE_KEY> npm run playChess:base_mainnet

Note: replace <PRIVATE_KEY> with your actual wallet private key. Ensure that the wallet has sufficient ETH on the Base network to cover transaction fees and amounts.

Below are transactions on Base we've created when playing chess with the chess contract.

  1. Create a new game by a player with address 0x62998e172240F4CC26EC10717d16a8D4442bf2Dd

  2. The 1st move

  3. The 2st move

At every step you can view the updated chess board in your terminal.

Let's play chess with Dagent...
Player address: 0x62998e172240F4CC26EC10717d16a8D4442bf2Dd
------------------------
Create game...
Tx hash:  0xe2ef210993488793c2249d4210869d3b90d634e37fc675d69787ab840e6e305e

Wait for Dagent to construct a game...

Dagent's created the game. Let's play...

   a  b  c  d  e  f  g  h
8 R  N  B  Q  K  B  N  R  
7 P  P  P  P  P  P  P  P  
6 .  .  .  .  .  .  .  .  
5 .  .  .  .  .  .  .  .  
4 .  .  .  .  .  .  .  .  
3 .  .  .  .  .  .  .  .  
2 p  p  p  p  p  p  p  p  
1 r  n  b  q  k  b  n  r  

------------------------
Your move:  b2-b4
Tx hash:  0x1e5354387f36ccf2fbae1f0e71c082e0e71c6c2bd919042c2fd37aab54bf601c

   a  b  c  d  e  f  g  h
8 R  N  B  Q  K  B  N  R  
7 P  P  P  P  P  P  P  P  
6 .  .  .  .  .  .  .  .  
5 .  .  .  .  .  .  .  .  
4 .  p  .  .  .  .  .  .  
3 .  .  .  .  .  .  .  .  
2 p  .  p  p  p  p  p  p  
1 r  n  b  q  k  b  n  r  


Wait for Dagent to make a move...

Dagent made c7-c5 move

   a  b  c  d  e  f  g  h
8 R  N  B  Q  K  B  N  R  
7 P  P  .  P  P  P  P  P  
6 .  .  .  .  .  .  .  .  
5 .  .  P  .  .  .  .  .  
4 .  p  .  .  .  .  .  .  
3 .  .  .  .  .  .  .  .  
2 p  .  p  p  p  p  p  p  
1 r  n  b  q  k  b  n  r  

------------------------
Your move:  Nb1-c3
Tx hash:  0xf43cee1a5527589e950b31c372366d32e6e0992398d677bfa7c62326ea748f7f

   a  b  c  d  e  f  g  h
8 R  N  B  Q  K  B  N  R  
7 P  P  .  P  P  P  P  P  
6 .  .  .  .  .  .  .  .  
5 .  .  P  .  .  .  .  .  
4 .  p  .  .  .  .  .  .  
3 .  .  n  .  .  .  .  .  
2 p  .  p  p  p  p  p  p  
1 r  .  b  q  k  b  n  r  


Wait for Dagent to make a move...

Dagent made g7-g5 move

   a  b  c  d  e  f  g  h
8 R  N  B  Q  K  B  N  R  
7 P  P  .  P  P  P  .  P  
6 .  .  .  .  .  .  .  .  
5 .  .  P  .  .  .  P  .  
4 .  p  .  .  .  .  .  .  
3 .  .  n  .  .  .  .  .  
2 p  .  p  p  p  p  p  p  
1 r  .  b  q  k  b  n  r

PreviousOnchain AI composability - AI Powered WalletNextWhat are Neurons?

Last updated 4 months ago

The complete code is available at:

The player requested to play chess with the Chess contract:

The AI-powered chess contract accepted the challenge:

The player made b2-b4 move:

The AI-powered chess contract made c7-c5 move:

The player made Nb1-c3 move:

The AI-powered chess contract made g7-g5 move:

https://github.com/eternalai-org/eternal-ai/tree/master/examples/DagentPlayChess
view on BaseScan
view on BaseScan
view on BaseScan
view on BaseScan
view on BaseScan
view on BaseScan