Convert any web2 game in play2earn game
npm install play2earn
Play2earn is a package used to convert any Nextjs game into a play2earn game with Solana blockchain integration.
- Integrate with solo games
- Integrate with multiplayer games
- No knowledge of Solana / Rust / Blockchain required
- Possibility to switch between Devnet and Mainnet easily
You can read the documentation here:
An example of a 2048 Game (solo) converted into play2earn (if you finish it under a time period) can be found here:
The code can be found here:
An example of a chess game converted to a chess2earn game (between two players) can be found here:
The code associated is available here:
More examples of integration to come!
Your project needs to use React 17 or later.
For deployment and production use cases, you also need to use Nextjs 12.2 or later to secure the transaction.
In order to make the transaction secure and verify that no one is cheating, play2earn needs to integrate with the backend of Web2To3.
Read the documentation here to see how to create a game and use the API key associated.
Add Play2Earn modal to your project by executing npm install play2earn or yarn add play2earn.
Here's an example of basic usage:
``js`
import { Play2EarnModal, finishGameAndGetMoneyWebThree } from "play2earn";
`js`
playerUID={user?.uid}
handleGameStarting={() => handleGameStarting()}
secondsBeforeCancellation={60} />
You also need to add the function which is triggers when the game is starting (i.e after the one/two players have bet in case of
solo/multiplayer game).
`js`
async function handleGameStarting() {
console.log('Updating when the second player has accepted the bet.');
// WRITE_YOUR_LOGIC_HERE...
}
Then, you need to add the function to end the game when one player has won.
`js`
// Call this function when the game is finished with the winnerID
finishGameAndGetMoneyWebThree(process.env.NEXT_PUBLIC_WEBSITE_HOST, gameID, winnerID, winnerID, false)
.then(resultSignature => {
console.log('The money has been transferred to your account, the signature is: ', resultSignature);
});
}
|Name|Type|Default|Description|
|:--|:--:|:-----:|:----------|
|gameWebsiteHost|string|required|The ID of your website, needs to match the one added in the backend.|string
|gameID||required|A unique ID to identify each game, you can use the Date().|string
|playerUID||required|The ID or username of the player. Better if it is unique.|function
|handleGameStarting ||undefined|Callback when player have bet.|string
|gameType||solo|The game type, solo or multiplayer.|number
|numberMultiplayers||2|The number of players in case of multiplayer game.|string
|blockchainType||solana|The blockchain used for the betting.|string
|network ||devnet|The network used, choice between devnet and mainnet.|number
|amountBet||0.1|The amount bet of blockchain's currency.|number
|secondsBeforeCancellation||60|The number of seconds before cancellation of betting in multiplayer game.|
is the ID to identify your game application. You need to register it in the backend of Web2to3 first and match the one registered there.$3
gameID is the identifier of a game started by a player. It needs to be unique. You can use the timestamp to generate unicity. $3
playerUID is the I### onTick
onTick is a callback and triggered every time a new period is started, based on what the D or username of the player. In case of multiplayer, make sure that it is unique within the game started.$3
handleGameStarting is a callback and triggered after all players have bet and the game is ready to start.$3
gameType specifies the type of game played, i.e solo or multiplayer. $3
numberMultiplayers is the number of players in case the gameType is set to multiplayer. Default is 2 players.$3
blockchainType is the type of Blockchain used for currency and transaction. At the moment only solana option is available.$3
devnet network is used for testing or demonstration while mainnet network is used with real cryptocurrencies. $3
In case the solana` blockchain network is used, the amount bet is in SOL currency. npm login
npm version patch
npm publish
The MIT License.
![]() | Marin Bouthemy |