A dynamic on-chain game that involves interacting with stranded space cowboy NPCs, powered by AI and ERC-6551
In this project we wanted to demonstrate the power of AI, Blockchain, and ERC-6551 when it comes to gaming and NPCs. Cosmic Cowboys lets you enter a world with NPCs that are controlled by AI, where you can chat with them about their journey and influence them to make on chain decisions thanks to ERC-6551.
The backstory: The cosmic gold rush began with a bang. Literally. An asteroid the size of Jupiter slammed into our galaxy causing a ripple that knocked out power on Earth globally for three days. Scientists quickly learned the rock was made of 90% lithium. Earth’s electric-hungry population clamored to mine the space rock so that they could power our electrified future. But to get to the rock, the world would need modern day gold miners willing to risk it all to travel across the galaxy in search of fortune and maybe a little fame.
A legion of space cowboys volunteered and were quickly trained, provided with hastily constructed ships, and sent hurtling into the nothing of space. These cowboys stopped at nothing to be first to claim any of the large pieces of lithium land they could. A demolition derby in space ensued. Ships careening off ships. Hacked and redirected navigation systems sending ships into various astroid fields. Comms units overrun with the taunting and sometimes panicked voices of space cowboys battling for a lawless place across infinity.
If only the cowboys had been paying attention to their course.
The ripple that had impacted Earth reverberated across the galaxy and caused the sun to shift ever so slightly in its throne at the center of the Milky Way. That shift was enough to move planets, debris, asteroids, and meteorites. It shifted the course for the cowboys and they were no longer heading toward the rock of lithium. Instead, they were heading for one of Jupiter’s moons.
As the ships crash-landed on Ganymede and the space cowboys realized there was no lithium to mine, they had to find ways to survive. Using the mining tools each ship carried, a settlement was constructed. Conserving fuel, and using the re-usable rockets they’d set off on their adventure in, quick trips for supplies and food sources from Jupiter’s other moons were made possible.
But our space cowboys were stranded on a moon in a desolate land, forced to build a society and fend for themselves.
Game Dynamics
Each cowboy is an ERC-721 NFT that has a ERC-6551 Tokenbound account, which allows them to own a starting out supply of 5 food, 5 supplies, and 20 credits. They also have a starting location of the "Bar." From there they can visit:
Each NPC has all of their backstory stored in their IPFS metadata linked to the NFT and serves as their AI DNA. That background is fed into OpenAI to help curate decisions. Each of actions available to the NPC are run as callable AI functions which then translate to smart contract interactions through the individual NPC TBA (token bound account). This gives the NPCs a true sense of autonomy.
People on Earth (game players) can visit https://cosmiccowboys.cloud and connect with their own wallet or their web2 login such as google, and engage with some of these NPCs. They can view their backstory, their stats, and click "engage" if they want to chat with them. This chat connects their wallet with the NPC TBA and created an encrypted wallet to wallet messaging thanks to XMTP. Through the chat the end user can help steer the NPC to make certain decisions, such as visiting home, or starting a supply mission. The chat UI will notify when these actions are taking and periodically the on chain statistics are updated.
For the smart contracts we made use of the following:
The Operator contract serves as the brain of the entire game logic, taking in the other elements as constructors. The ownership of the ERC721, ERC20, and ERC1155 are transferred to the Operator contract so that contract can make multiple contract calls in on function, such as burning ERC20 tokens while minting supplies during a trade. We used hardhat to help compile and deploy these contracts to multiple chains.
The AI functionality is managed through OpenAI's chat API along with OpenAI's function calling capabilities. A cron job runs hourly and cycles through all 20 NPCs. Each one is reminded of their backstory and their onchain game state and then asked to make a decision on what they should do next. The decision made by AI results in an onchain transaction.
For speed and performance, all onchain actions and data are replicated to IPFS. The game's frontend uses this index and polls for updates to the index file. The frontend also makes heavy use of XMTP. Each player can chat, wallet-to-wallet, with NPCs via XMTP. However, XMTP is also used as a message bus for "system messages". In keeping the player immersed in the game world, system messages are sent through the command line interface in the game and provide additional information. Finally, XMTP messages inform the activity feed seen for each NPC.