The tools will enable you to perform:
- Project Setup With Webpack: Enable a project structure with WebGL to be compiled by Webpack
- Integrate Web SDK: Allow all EOAs and Sequence Wallet to authenticate the user
- Deploy a Collectibles Contract: Create your own collectible contract
- Deploy a Remote Minter and Mint In-game Tokens: Perform gasless relayed transactions with Cloudflare workers
- Leverage Items In-game: Integrate collectibles in the game using the Sequence Indexer
- Burn In-game Achievement tokens: Burn game achievements with wagmi
- (Optional) Integrate Embedded Wallet Into Web SDK: Enable smooth UX without the use of signer signed messages
1. Project setup with webpack
Clone Repo
We’ll first start by cloning down a template project, which has a few WebGL based components created usingthree, all compiled using webpack
Template WebGL JS Web SDK Starter
Clone the above repo down, cd into the repo with cd template-webgl-js-sequence-kit-starter
Update .env
Create a .env file (using the .env.example) with the environment variables
2. Integrate Web SDK
Now that we have a project structure set up, we can integrate Web SDKSetup App.jsx Component
Create a folder within the src folder called react and create 2 files: App.jsx and Login.jsx
In App.jsx include the following code
Login.jsx file add the following code in order to create a button at the top of the screen to login to the application
Render Component In Javascript index.js
Finally, add in the index.js import the App.jsx component, and render it to be appended to the root id element in index.html
Create a Click Handler To Call The Login Modal
Add the following code toLogin.jsx component
index.js
index.html
3. Deploy a Collectibles Contract
You’ll need to create a collectible from the Sequence Builder which can be accomplished with the following guide We should create 2 collections: 1 for achievement tokens and the other for the planes4. Deploy a Remote Minter & Mint In-game Achievement Tokens
Then, in order to send transactions to the blockchain in a seamless fashion that are gasless, implement a Cloudflare Worker to mint items from a contract deployed in the previous step, ensuring that the transactions API contract address is inputed as aMinter Role
We will allow there to be multiple paths to mint collectibles: a plane collectible and an achievement collectible.
This is accomplished in the code by adding a key/value of isPlane to the normal cloudflare request body, and creating an additional if/else in the cloudflare worker.
You can view the code for this in this github repository
For this guide, we will be running all cloudflare code in a local dev envivronment, which can be accomplished by starting the cloudflare worker in the named project folder, with:
5. Leverage Items In-game
This section will be broken into 2 implementations of updating UI with in-game asset ownership changes:- Displaying Plane changes based on Wallet assets
- Displaying UI changes based on Wallet assets
Displaying Plane changes based on Wallet assets
To implement changes to the game based on what the wallet asset owns, you can implement a button that mints a token, then on the response, checks for indexer changes In theindex.js we include a button attached to the onclick attribute of the element in index.html
callContract takes care of the minting by calling a fetch that is wrapped in a mutex to ensure 1 mint happens at a time, to prevent click mashers, added to the SequenceController class in /API/SequenceController.js
fetchPlaneTokens will poll on the result until there’s an asset in your wallet, updating the plane color to represent a different plane.
fetchPlaneTokens is implemented with the following code, where the balance conditional check is greater than 1, and the tokenID is equal to the searched for id.
This UI conditional logic would change based on your application
Displaying UI Changes Based on Wallet Assets
Next, we implement a UI change where we add aburn achievement button, based on if the user has an achievement or not
First, implement the similiar html/js click handler logic like before
where this time, the isPlane value of callContract is set to false
fetchTokensFromAchievementMint which is added to the SequenceController
display attribute make the button appear
6. Burn In-Game Achievement Tokens
Finally, to burn the achievement token, we can no longer use a cloduflare worker for actions sent to the blockchain, because when the minting was performed ‘on behalf of’ the address using the transactions API (making themsg.sender in the contract one of the relayer addresses) for this, we want to make sure the msg.sender in the contract proves ownership of the token, and is sent directly from the user. We’ll use wagmi frontend functions as well as some class composition to accomplish this.
burnToken is a passed in function from our react component that uses the similiar pattern of using mutexes, and we send the transaction using sendTransaction from the wagmi package, and wait for a transaction hash update to return the callback
SequenceController, call the sendBurnToken function wrapped in burnToken to make the react function accessible to the rest of the application
SequenceController
7. (Optional) Integrate Embedded Wallet Into Web SDK
If you’d like to smooth the user journey to allow no user transaction signing across the board, you can enable an Embedded Wallet by updating your configuration of your Web SDK react component. By accomplishing this, we reduce a pop-up when burning tokens withwagmi, since rewarding achievement tokens and minting collectibles are completed using a cloudflare worker for gasless transactions.
This can be accomplished by adding a few environment variables, and switching the type of connector we use.
First update your .env file with the following environment secrets
App.jsx
To learn more about In-Game Wallets, see
here